Skip to content

Commit

Permalink
fix(cli): Fix flaky authentication migration and SQL id schema types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Jun 22, 2022
1 parent 252c872 commit 04ce9a5
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
33 changes: 16 additions & 17 deletions packages/cli/src/authentication/templates/knex.tpl.ts
Expand Up @@ -39,24 +39,23 @@ export async function down(knex: Knex): Promise<void> {
`

export const generate = (ctx: AuthenticationGeneratorContext) =>
generator(ctx)
// We need to wait a second otherwise the migration filenames won't be in the right order
.then(
(ctx) => new Promise<AuthenticationGeneratorContext>((resolve) => setTimeout(() => resolve(ctx), 1100))
)
.then(
when(
(ctx) => getDatabaseAdapter(ctx.feathers.database) === 'knex',
renderSource(
migrationTemplate,
toFile(
toFile<AuthenticationGeneratorContext>('migrations', () => {
// Probably not great but it works to align with the Knex migration file format
const migrationDate = new Date().toISOString().replace(/\D/g, '').substring(0, 14)
generator(ctx).then(
when(
(ctx) => getDatabaseAdapter(ctx.feathers.database) === 'knex',
renderSource(
migrationTemplate,
toFile(
toFile<AuthenticationGeneratorContext>('migrations', () => {
// Probably not great but it works to align with the Knex migration file format
// We add 2 seconds so that the migrations run in the correct order
const migrationDate = new Date(Date.now() + 2000)
.toISOString()
.replace(/\D/g, '')
.substring(0, 14)

return `${migrationDate}_authentication`
})
)
return `${migrationDate}_authentication`
})
)
)
)
)
14 changes: 4 additions & 10 deletions packages/cli/src/authentication/templates/user.schema.tpl.ts
Expand Up @@ -16,15 +16,9 @@ export const ${camelName}DataSchema = schema({
${authStrategies
.map((name) =>
name === 'local'
? `email: {
type: 'string'
},
password: {
type: 'string'
}`
: ` ${name}Id: {
type: 'string'
}`
? ` email: { type: 'string' },
password: { type: 'string' }`
: ` ${name}Id: { type: 'string' }`
)
.join(',\n')}
}
Expand Down Expand Up @@ -55,7 +49,7 @@ export const ${camelName}ResultSchema = schema({
properties: {
...${camelName}DataSchema.properties,
${type === 'mongodb' ? '_id' : 'id'}: {
type: 'string'
type: '${type === 'mongodb' ? 'string' : 'number'}'
}
}
} as const)
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/index.ts
Expand Up @@ -20,8 +20,9 @@ export const command = (yargs: Argv) =>
yarg
.command('app', 'Generate a new app', commandRunner)
.command('service', 'Generate a service', commandRunner)
.command('connection', 'Connect to a different database', commandRunner)
.command('hook', 'Generate a hook', commandRunner)
.command('connection', 'Connect to a different database', commandRunner)
.command('authentication', 'Set up authentication with a custom entity', commandRunner)
)
.usage('Usage: $0 <command> [options]')
.help()
2 changes: 1 addition & 1 deletion packages/cli/src/service/templates/schema.tpl.ts
Expand Up @@ -45,7 +45,7 @@ export const ${camelName}ResultSchema = schema({
properties: {
...${camelName}DataSchema.properties,
${type === 'mongodb' ? '_id' : 'id'}: {
type: 'string'
type: '${type === 'mongodb' ? 'string' : 'number'}'
}
}
} as const)
Expand Down
20 changes: 10 additions & 10 deletions packages/cli/src/service/type/custom.tpl.ts
Expand Up @@ -22,9 +22,9 @@ export class ${className} {
async get (id: Id, _params?: ${upperName}Params): Promise<${upperName}Result> {
return {
id: \`\${id}\`,
id: 0,
text: \`A new message with ID: \${id}!\`
};
}
}
async create (data: ${upperName}Data, params?: ${upperName}Params): Promise<${upperName}Result>
Expand All @@ -35,30 +35,30 @@ export class ${className} {
}
return {
id: 'created',
id: 0,
...data
};
}
}
async update (id: NullableId, data: ${upperName}Data, _params?: ${upperName}Params): Promise<${upperName}Result> {
return {
id: \`\${id}\`,
id: 0,
...data
};
}
}
async patch (id: NullableId, data: ${upperName}Data, _params?: ${upperName}Params): Promise<${upperName}Result> {
return {
id: \`\${id}\`,
id: 0,
...data
};
}
}
async remove (id: NullableId, _params?: ${upperName}Params): Promise<${upperName}Result> {
return {
id: \`\${id}\`,
id: 0,
text: 'removed'
};
}
}
}
`
Expand Down

0 comments on commit 04ce9a5

Please sign in to comment.