-
-
Notifications
You must be signed in to change notification settings - Fork 796
Expand file tree
/
Copy pathknex.tpl.ts
More file actions
92 lines (82 loc) · 2.64 KB
/
knex.tpl.ts
File metadata and controls
92 lines (82 loc) · 2.64 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { generator, toFile } from '@feathershq/pinion'
import { renderSource } from '../../commons'
import { ServiceGeneratorContext } from '../index'
const migrationTemplate = ({
kebabPath
}: ServiceGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/knexfile.html
import type { Knex } from 'knex'
export async function up(knex: Knex): Promise<void> {
await knex.schema.createTable('${kebabPath}', table => {
table.increments('id')
table.string('text')
})
}
export async function down(knex: Knex): Promise<void> {
await knex.schema.dropTable('${kebabPath}')
}
`
export const template = ({
className,
upperName,
feathers,
schema,
fileName,
relative
}: ServiceGeneratorContext) => /* ts */ `// For more information about this file see https://dove.feathersjs.com/guides/cli/service.class.html#database-services
import type { Params } from '@feathersjs/feathers'
import { KnexService } from '@feathersjs/knex'
import type { KnexAdapterParams, KnexAdapterOptions } from '@feathersjs/knex'
import type { Application } from '${relative}/declarations'
${
schema
? `import type {
${upperName},
${upperName}Data,
${upperName}Patch,
${upperName}Query
} from './${fileName}.schema'
`
: `
export type ${upperName} = any
export type ${upperName}Data = any
export type ${upperName}Patch = any
export type ${upperName}Query = any
`
}
export interface ${upperName}Params extends KnexAdapterParams<${upperName}Query> {
}
// By default calls the standard Knex adapter service methods but can be customized with your own functionality.
export class ${className}<ServiceParams extends Params = ${upperName}Params>
extends KnexService<${upperName}, ${upperName}Data, ServiceParams, ${upperName}Patch> {
}
export const getOptions = (app: Application): KnexAdapterOptions => {
return {
paginate: app.get('paginate'),
Model: app.get('${feathers.database}Client'),
name: '${fileName}'
}
}
`
export const generate = (ctx: ServiceGeneratorContext) =>
generator(ctx)
.then(
renderSource(
template,
toFile<ServiceGeneratorContext>(({ lib, folder, fileName }) => [
lib,
'services',
...folder,
`${fileName}.class`
])
)
)
.then(
renderSource(
migrationTemplate,
toFile<ServiceGeneratorContext>('migrations', ({ kebabName }) => {
// 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)
return `${migrationDate}_${kebabName}`
})
)
)