/
knex.tpl.ts
62 lines (57 loc) · 1.77 KB
/
knex.tpl.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { generator, when, toFile } from '@feathershq/pinion'
import { getDatabaseAdapter, renderSource } from '../../commons'
import { AuthenticationGeneratorContext } from '../index'
const migrationTemplate = ({
kebabPath,
authStrategies
}: AuthenticationGeneratorContext) => /* ts */ `import type { Knex } from 'knex'
export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('${kebabPath}', function (table) {
table.dropColumn('text')${authStrategies
.map((name) =>
name === 'local'
? `
table.string('email').unique()
table.string('password')`
: `
table.string('${name}Id')`
)
.join('\n')}
})
}
export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('${kebabPath}', function (table) {
table.string('text')${authStrategies
.map((name) =>
name === 'local'
? `
table.dropColumn('email')
table.dropColumn('password')`
: `
table.dropColumn('${name}Id')
`
)
.join('\n')}
})
}
`
export const generate = (ctx: AuthenticationGeneratorContext) =>
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`
})
)
)
)
)