@postxl/generator 0.0.20 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/jest.config.js +17 -0
- package/dist/src/generator.js +13 -13
- package/dist/src/generators/enums/react.generator.js +55 -55
- package/dist/src/generators/enums/types.generator.js +8 -8
- package/dist/src/generators/indices/datamockmodule.generator.js +46 -46
- package/dist/src/generators/indices/datamodule.generator.js +76 -76
- package/dist/src/generators/indices/dataservice.generator.js +26 -26
- package/dist/src/generators/indices/repositories.generator.js +3 -3
- package/dist/src/generators/indices/testdataservice.generator.js +23 -22
- package/dist/src/generators/models/react.generator/context.generator.js +47 -47
- package/dist/src/generators/models/react.generator/index.js +8 -8
- package/dist/src/generators/models/react.generator/library.generator.js +66 -66
- package/dist/src/generators/models/react.generator/lookup.generator.js +75 -75
- package/dist/src/generators/models/react.generator/modals.generator.js +261 -261
- package/dist/src/generators/models/repository.generator.js +239 -239
- package/dist/src/generators/models/route.generator.js +45 -45
- package/dist/src/generators/models/seed.generator.js +14 -14
- package/dist/src/generators/models/stub.generator.js +19 -19
- package/dist/src/generators/models/types.generator.js +39 -39
- package/dist/src/lib/vfs.js +2 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -2
- package/changelog.md +0 -115
- package/jest.config.ts +0 -18
- package/tests/attributes.test.ts +0 -91
- package/tests/file.test.ts +0 -32
- package/tests/schemas/la/la.prisma +0 -862
- package/tests/schemas/mca/mca.prisma +0 -528
- package/tests/utils/random.ts +0 -11
- package/tests/vfs.test.ts +0 -92
|
@@ -12,18 +12,18 @@ function generateRepository({ model, meta }) {
|
|
|
12
12
|
const { idField, fields } = model;
|
|
13
13
|
const decoder = meta.data.repository.decoderFnName;
|
|
14
14
|
const uniqueStringFields = fields.filter(fields_1.isUniqueStringField);
|
|
15
|
-
const defaultValueInitFn = `
|
|
16
|
-
if (item.${(_a = model.defaultField) === null || _a === void 0 ? void 0 : _a.name}) {
|
|
17
|
-
if (this.defaultValue) {
|
|
18
|
-
console.warn(\`More than one default ${meta.userFriendlyName} found! \${this.defaultValue.id} and \${item.id}\`)
|
|
19
|
-
}
|
|
20
|
-
this.defaultValue = item
|
|
21
|
-
}
|
|
15
|
+
const defaultValueInitFn = `
|
|
16
|
+
if (item.${(_a = model.defaultField) === null || _a === void 0 ? void 0 : _a.name}) {
|
|
17
|
+
if (this.defaultValue) {
|
|
18
|
+
console.warn(\`More than one default ${meta.userFriendlyName} found! \${this.defaultValue.id} and \${item.id}\`)
|
|
19
|
+
}
|
|
20
|
+
this.defaultValue = item
|
|
21
|
+
}
|
|
22
22
|
`;
|
|
23
|
-
const defaultValueInitCheckFn = `
|
|
24
|
-
if (!this.db.isCLI && !this.defaultValue) {
|
|
25
|
-
throw new Error('No default ${meta.userFriendlyName} found!')
|
|
26
|
-
}
|
|
23
|
+
const defaultValueInitCheckFn = `
|
|
24
|
+
if (!this.db.isCLI && !this.defaultValue) {
|
|
25
|
+
throw new Error('No default ${meta.userFriendlyName} found!')
|
|
26
|
+
}
|
|
27
27
|
`;
|
|
28
28
|
const { isGenerated } = idField;
|
|
29
29
|
const idIntInitFn = `this.currentMaxId = (await this.db.${meta.data.repository.getMethodFnName}.aggregate({ _max: { ${idField.sourceName}: true } }))._max.${idField.sourceName} ?? 0`;
|
|
@@ -33,256 +33,256 @@ function generateRepository({ model, meta }) {
|
|
|
33
33
|
from: meta.types.importPath,
|
|
34
34
|
})
|
|
35
35
|
.addImport({ items: [meta.types.zodDecoderFnName], from: meta.types.importPath });
|
|
36
|
-
return `
|
|
37
|
-
import { Injectable, Logger } from '@nestjs/common'
|
|
38
|
-
import { DbService } from '@${model.schemaConfig.project}/db'
|
|
39
|
-
|
|
40
|
-
import { Repository } from '../repository.type'
|
|
41
|
-
|
|
42
|
-
${imports.generate()}
|
|
43
|
-
|
|
44
|
-
@Injectable()
|
|
45
|
-
export class ${meta.data.repositoryClassName} implements Repository<
|
|
46
|
-
${model.typeName},
|
|
47
|
-
${idField.unbrandedTypeName},
|
|
48
|
-
Omit<${model.typeName}, 'id'>
|
|
49
|
-
> {
|
|
50
|
-
protected data: Map<${model.brandedIdType}, ${model.typeName}> = new Map()
|
|
51
|
-
protected logger = new Logger(${meta.data.repositoryClassName}.name)
|
|
52
|
-
|
|
53
|
-
${model.defaultField ? `public defaultValue!: ${model.typeName}` : ''}
|
|
54
|
-
|
|
55
|
-
${isGenerated ? `protected currentMaxId = 0\n` : ''}
|
|
56
|
-
|
|
57
|
-
protected uniqueIds = {
|
|
58
|
-
${uniqueStringFields.map((f) => `'${f.name}': new Map<string, ${model.typeName}>()`).join(',\n')}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
constructor(protected db: DbService) {}
|
|
62
|
-
|
|
63
|
-
public async init() {
|
|
64
|
-
this.data.clear()
|
|
65
|
-
|
|
66
|
-
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.clear()\n`)}
|
|
67
|
-
|
|
68
|
-
this.logger.log(\`Loading ${model.typeName} instances into repository...\`)
|
|
69
|
-
|
|
70
|
-
const data = await this.db.${meta.data.repository.getMethodFnName}.findMany({})
|
|
71
|
-
|
|
72
|
-
for (const rawItem of data) {
|
|
73
|
-
const item = this.${decoder}(rawItem)
|
|
74
|
-
this.data.set(item.id, item)
|
|
75
|
-
|
|
76
|
-
${model.defaultField ? defaultValueInitFn : ''}
|
|
77
|
-
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(item.${f.name}, item)`).join('\n')}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
${isGenerated ? idIntInitFn : ''}
|
|
81
|
-
|
|
82
|
-
${model.defaultField ? defaultValueInitCheckFn : ''}
|
|
83
|
-
|
|
84
|
-
this.logger.log(\`Loaded \${this.data.size} instances of ${model.typeName} into repository!\`)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public async reInit(data: ${model.typeName}[]): Promise<void> {
|
|
88
|
-
if (!this.db.useE2ETestDB) {
|
|
89
|
-
const errorMsg =
|
|
90
|
-
'ReInit() shall only be called in tests using MockRepositories or in DB configured for E2E tests!'
|
|
91
|
-
this.logger.error(errorMsg)
|
|
92
|
-
throw new Error(errorMsg)
|
|
93
|
-
}
|
|
94
|
-
await this.db.runOnlyOnTestDb(() => this.db.${meta.data.repository.getMethodFnName}.createMany({ data: data.map((i) => this.toCreateItem(i)) }))
|
|
95
|
-
return this.init()
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
public get(id: ${model.brandedIdType} | null): ${model.typeName} | null {
|
|
99
|
-
if (id === null) return null
|
|
100
|
-
return this.data.get(${meta.types.toBrandedIdTypeFnName}(id)) ?? null
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
public getAll(): Map<${model.brandedIdType}, ${model.typeName}> {
|
|
104
|
-
return this.data
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public getAllAsArray(): ${model.typeName}[] {
|
|
108
|
-
return Array.from(this.data.values())
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
public filter(predicate: (item: ${model.typeName}) => boolean): ${model.typeName}[] {
|
|
112
|
-
return this.getAllAsArray().filter(predicate)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public findFirst(predicate: (item: ${model.typeName}) => boolean): ${model.typeName} | null {
|
|
116
|
-
return this.getAllAsArray().find(predicate) ?? null
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public count(): number {
|
|
120
|
-
return this.data.size
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private toCreateItem(item: ${model.typeName}) {
|
|
36
|
+
return `
|
|
37
|
+
import { Injectable, Logger } from '@nestjs/common'
|
|
38
|
+
import { DbService } from '@${model.schemaConfig.project}/db'
|
|
39
|
+
|
|
40
|
+
import { Repository } from '../repository.type'
|
|
41
|
+
|
|
42
|
+
${imports.generate()}
|
|
43
|
+
|
|
44
|
+
@Injectable()
|
|
45
|
+
export class ${meta.data.repositoryClassName} implements Repository<
|
|
46
|
+
${model.typeName},
|
|
47
|
+
${idField.unbrandedTypeName},
|
|
48
|
+
Omit<${model.typeName}, 'id'>
|
|
49
|
+
> {
|
|
50
|
+
protected data: Map<${model.brandedIdType}, ${model.typeName}> = new Map()
|
|
51
|
+
protected logger = new Logger(${meta.data.repositoryClassName}.name)
|
|
52
|
+
|
|
53
|
+
${model.defaultField ? `public defaultValue!: ${model.typeName}` : ''}
|
|
54
|
+
|
|
55
|
+
${isGenerated ? `protected currentMaxId = 0\n` : ''}
|
|
56
|
+
|
|
57
|
+
protected uniqueIds = {
|
|
58
|
+
${uniqueStringFields.map((f) => `'${f.name}': new Map<string, ${model.typeName}>()`).join(',\n')}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
constructor(protected db: DbService) {}
|
|
62
|
+
|
|
63
|
+
public async init() {
|
|
64
|
+
this.data.clear()
|
|
65
|
+
|
|
66
|
+
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.clear()\n`)}
|
|
67
|
+
|
|
68
|
+
this.logger.log(\`Loading ${model.typeName} instances into repository...\`)
|
|
69
|
+
|
|
70
|
+
const data = await this.db.${meta.data.repository.getMethodFnName}.findMany({})
|
|
71
|
+
|
|
72
|
+
for (const rawItem of data) {
|
|
73
|
+
const item = this.${decoder}(rawItem)
|
|
74
|
+
this.data.set(item.id, item)
|
|
75
|
+
|
|
76
|
+
${model.defaultField ? defaultValueInitFn : ''}
|
|
77
|
+
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(item.${f.name}, item)`).join('\n')}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
${isGenerated ? idIntInitFn : ''}
|
|
81
|
+
|
|
82
|
+
${model.defaultField ? defaultValueInitCheckFn : ''}
|
|
83
|
+
|
|
84
|
+
this.logger.log(\`Loaded \${this.data.size} instances of ${model.typeName} into repository!\`)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
public async reInit(data: ${model.typeName}[]): Promise<void> {
|
|
88
|
+
if (!this.db.useE2ETestDB) {
|
|
89
|
+
const errorMsg =
|
|
90
|
+
'ReInit() shall only be called in tests using MockRepositories or in DB configured for E2E tests!'
|
|
91
|
+
this.logger.error(errorMsg)
|
|
92
|
+
throw new Error(errorMsg)
|
|
93
|
+
}
|
|
94
|
+
await this.db.runOnlyOnTestDb(() => this.db.${meta.data.repository.getMethodFnName}.createMany({ data: data.map((i) => this.toCreateItem(i)) }))
|
|
95
|
+
return this.init()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
public get(id: ${model.brandedIdType} | null): ${model.typeName} | null {
|
|
99
|
+
if (id === null) return null
|
|
100
|
+
return this.data.get(${meta.types.toBrandedIdTypeFnName}(id)) ?? null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
public getAll(): Map<${model.brandedIdType}, ${model.typeName}> {
|
|
104
|
+
return this.data
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getAllAsArray(): ${model.typeName}[] {
|
|
108
|
+
return Array.from(this.data.values())
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
public filter(predicate: (item: ${model.typeName}) => boolean): ${model.typeName}[] {
|
|
112
|
+
return this.getAllAsArray().filter(predicate)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public findFirst(predicate: (item: ${model.typeName}) => boolean): ${model.typeName} | null {
|
|
116
|
+
return this.getAllAsArray().find(predicate) ?? null
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
public count(): number {
|
|
120
|
+
return this.data.size
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
private toCreateItem(item: ${model.typeName}) {
|
|
124
124
|
${isGenerated
|
|
125
|
-
? `if (item.${idField.name} > ++this.currentMaxId) {
|
|
126
|
-
this.currentMaxId = item.${idField.name}
|
|
127
|
-
} else {
|
|
128
|
-
item.${idField.name} = this.currentMaxId as ${model.brandedIdType}
|
|
125
|
+
? `if (item.${idField.name} > ++this.currentMaxId) {
|
|
126
|
+
this.currentMaxId = item.${idField.name}
|
|
127
|
+
} else {
|
|
128
|
+
item.${idField.name} = this.currentMaxId as ${model.brandedIdType}
|
|
129
129
|
}`
|
|
130
|
-
: ''}
|
|
131
|
-
|
|
132
|
-
${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
|
|
133
|
-
|
|
134
|
-
return {
|
|
135
|
-
${[...model.fields.values()].map((f) => `${f.sourceName}: item.${f.name}`).join(',\n')}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
public async createWithId(item: Omit<${model.typeName}, '${idField.name}'> & {
|
|
139
|
-
id: ${model.brandedIdType}
|
|
140
|
-
}): Promise<${model.typeName}> {
|
|
141
|
-
const newItem = this.${decoder}(
|
|
142
|
-
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
143
|
-
data: this.toCreateItem(item as ${model.typeName}),
|
|
144
|
-
}),
|
|
145
|
-
)
|
|
146
|
-
|
|
147
|
-
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)`).join('\n')}
|
|
148
|
-
|
|
149
|
-
this.data.set(newItem.id, newItem)
|
|
150
|
-
return newItem
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
public async create(
|
|
154
|
-
item: Omit<${model.typeName}, 'id'>
|
|
155
|
-
): Promise<${model.typeName}> {
|
|
156
|
-
${isGenerated ? `const id = ++this.currentMaxId\n` : ''}
|
|
157
|
-
|
|
158
|
-
${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
|
|
159
|
-
|
|
160
|
-
const newItem = this.${decoder}(
|
|
161
|
-
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
162
|
-
data: {
|
|
163
|
-
${isGenerated ? `${idField.sourceName}: id,` : ''}
|
|
130
|
+
: ''}
|
|
131
|
+
|
|
132
|
+
${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
${[...model.fields.values()].map((f) => `${f.sourceName}: item.${f.name}`).join(',\n')}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
public async createWithId(item: Omit<${model.typeName}, '${idField.name}'> & {
|
|
139
|
+
id: ${model.brandedIdType}
|
|
140
|
+
}): Promise<${model.typeName}> {
|
|
141
|
+
const newItem = this.${decoder}(
|
|
142
|
+
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
143
|
+
data: this.toCreateItem(item as ${model.typeName}),
|
|
144
|
+
}),
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)`).join('\n')}
|
|
148
|
+
|
|
149
|
+
this.data.set(newItem.id, newItem)
|
|
150
|
+
return newItem
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
public async create(
|
|
154
|
+
item: Omit<${model.typeName}, 'id'>
|
|
155
|
+
): Promise<${model.typeName}> {
|
|
156
|
+
${isGenerated ? `const id = ++this.currentMaxId\n` : ''}
|
|
157
|
+
|
|
158
|
+
${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
|
|
159
|
+
|
|
160
|
+
const newItem = this.${decoder}(
|
|
161
|
+
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
162
|
+
data: {
|
|
163
|
+
${isGenerated ? `${idField.sourceName}: id,` : ''}
|
|
164
164
|
${[...model.fields.values()]
|
|
165
165
|
.filter((f) => f.kind !== 'id')
|
|
166
166
|
.map((f) => `${f.sourceName}: item.${f.name}`)
|
|
167
|
-
.join(',\n')}
|
|
168
|
-
},
|
|
169
|
-
}),
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)`).join('\n')}
|
|
173
|
-
|
|
174
|
-
this.data.set(newItem.id, newItem)
|
|
175
|
-
return newItem
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
public async createMany(items: Omit<${model.typeName}, 'id' | 'createdAt' | 'updatedAt'>[]): Promise<${model.typeName}[]> {
|
|
179
|
-
const result: ${model.typeName}[] = []
|
|
180
|
-
for (const item of items) {
|
|
181
|
-
const i = await this.create(item)
|
|
182
|
-
result.push(i)
|
|
183
|
-
}
|
|
184
|
-
return result
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
public async update(item: Partial<${model.typeName}> & {
|
|
188
|
-
id: ${model.brandedIdType}
|
|
189
|
-
}): Promise<${model.typeName}> {
|
|
190
|
-
const existingItem = this.get(item.id)
|
|
191
|
-
|
|
192
|
-
if (!existingItem) {
|
|
193
|
-
throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
|
|
194
|
-
}
|
|
195
|
-
|
|
167
|
+
.join(',\n')}
|
|
168
|
+
},
|
|
169
|
+
}),
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)`).join('\n')}
|
|
173
|
+
|
|
174
|
+
this.data.set(newItem.id, newItem)
|
|
175
|
+
return newItem
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
public async createMany(items: Omit<${model.typeName}, 'id' | 'createdAt' | 'updatedAt'>[]): Promise<${model.typeName}[]> {
|
|
179
|
+
const result: ${model.typeName}[] = []
|
|
180
|
+
for (const item of items) {
|
|
181
|
+
const i = await this.create(item)
|
|
182
|
+
result.push(i)
|
|
183
|
+
}
|
|
184
|
+
return result
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
public async update(item: Partial<${model.typeName}> & {
|
|
188
|
+
id: ${model.brandedIdType}
|
|
189
|
+
}): Promise<${model.typeName}> {
|
|
190
|
+
const existingItem = this.get(item.id)
|
|
191
|
+
|
|
192
|
+
if (!existingItem) {
|
|
193
|
+
throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
196
|
${uniqueStringFields
|
|
197
197
|
.map((f) => {
|
|
198
|
-
return `
|
|
199
|
-
if (item.${f.name} !== undefined && existingItem.${f.name} !== item.${f.name}) {
|
|
200
|
-
this.${getEnsureUniqueFnName(f)}(item)
|
|
201
|
-
}
|
|
198
|
+
return `
|
|
199
|
+
if (item.${f.name} !== undefined && existingItem.${f.name} !== item.${f.name}) {
|
|
200
|
+
this.${getEnsureUniqueFnName(f)}(item)
|
|
201
|
+
}
|
|
202
202
|
`;
|
|
203
203
|
})
|
|
204
|
-
.join('\n')}
|
|
205
|
-
|
|
206
|
-
const newItem = this.${decoder}(
|
|
207
|
-
await this.db.${meta.data.repository.getMethodFnName}.update({
|
|
208
|
-
where: {
|
|
209
|
-
${idField.sourceName}: item.${idField.name},
|
|
210
|
-
},
|
|
211
|
-
data: {
|
|
204
|
+
.join('\n')}
|
|
205
|
+
|
|
206
|
+
const newItem = this.${decoder}(
|
|
207
|
+
await this.db.${meta.data.repository.getMethodFnName}.update({
|
|
208
|
+
where: {
|
|
209
|
+
${idField.sourceName}: item.${idField.name},
|
|
210
|
+
},
|
|
211
|
+
data: {
|
|
212
212
|
${[...model.fields.values()]
|
|
213
213
|
.filter((f) => f.kind !== 'id')
|
|
214
214
|
.map((f) => f.isRequired
|
|
215
215
|
? `${f.sourceName}: item.${f.name} ?? existingItem.${f.name}`
|
|
216
216
|
: `${f.sourceName}: item.${f.name}`)
|
|
217
|
-
.join(',\n')}
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
},
|
|
221
|
-
}),
|
|
222
|
-
)
|
|
223
|
-
|
|
217
|
+
.join(',\n')}
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
},
|
|
221
|
+
}),
|
|
222
|
+
)
|
|
223
|
+
|
|
224
224
|
${uniqueStringFields
|
|
225
225
|
.map((f) => {
|
|
226
|
-
return `
|
|
227
|
-
if (item.${f.name} !== undefined && existingItem.${f.name} !== item.${f.name}) {
|
|
228
|
-
this.uniqueIds.${f.name}.delete(existingItem.${f.name})
|
|
229
|
-
if (item.${f.name} !== null) {
|
|
230
|
-
this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)
|
|
231
|
-
}
|
|
232
|
-
}
|
|
226
|
+
return `
|
|
227
|
+
if (item.${f.name} !== undefined && existingItem.${f.name} !== item.${f.name}) {
|
|
228
|
+
this.uniqueIds.${f.name}.delete(existingItem.${f.name})
|
|
229
|
+
if (item.${f.name} !== null) {
|
|
230
|
+
this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
233
|
`;
|
|
234
234
|
})
|
|
235
|
-
.join('\n')}
|
|
236
|
-
|
|
237
|
-
this.data.set(newItem.id, newItem)
|
|
238
|
-
return newItem
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
public async delete(id: ${model.brandedIdType}): Promise<void> {
|
|
242
|
-
const existingItem = this.get(id)
|
|
243
|
-
if (!existingItem) {
|
|
244
|
-
throw new Error(\`Could not delete ${model.typeName} with id \${id}. Not found!\`)
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
await this.db.${meta.data.repository.getMethodFnName}.delete({ where: { ${idField.sourceName}:id } })
|
|
248
|
-
|
|
249
|
-
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.delete(existingItem.${f.name})`).join('\n')}
|
|
250
|
-
|
|
251
|
-
this.data.delete(${meta.types.toBrandedIdTypeFnName}(id))
|
|
252
|
-
}
|
|
253
|
-
|
|
235
|
+
.join('\n')}
|
|
236
|
+
|
|
237
|
+
this.data.set(newItem.id, newItem)
|
|
238
|
+
return newItem
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
public async delete(id: ${model.brandedIdType}): Promise<void> {
|
|
242
|
+
const existingItem = this.get(id)
|
|
243
|
+
if (!existingItem) {
|
|
244
|
+
throw new Error(\`Could not delete ${model.typeName} with id \${id}. Not found!\`)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
await this.db.${meta.data.repository.getMethodFnName}.delete({ where: { ${idField.sourceName}:id } })
|
|
248
|
+
|
|
249
|
+
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.delete(existingItem.${f.name})`).join('\n')}
|
|
250
|
+
|
|
251
|
+
this.data.delete(${meta.types.toBrandedIdTypeFnName}(id))
|
|
252
|
+
}
|
|
253
|
+
|
|
254
254
|
${uniqueStringFields
|
|
255
255
|
.map((f) => {
|
|
256
|
-
return `
|
|
257
|
-
/**
|
|
258
|
-
* Utility function that ensures that the ${f.name} field is unique
|
|
259
|
-
*/
|
|
260
|
-
private ${getEnsureUniqueFnName(f)}(item: { ${f.name}?: string }) {
|
|
261
|
-
if (!item.${f.name}) return
|
|
262
|
-
if (!this.uniqueIds.${f.name}.has(item.${f.name})) return
|
|
263
|
-
let counter = 1
|
|
264
|
-
|
|
265
|
-
let ${f.name}: string
|
|
266
|
-
const source${f.name} =${!f.attributes.maxLength ? `item.${f.name}` : `item.${f.name}.substring(0, ${f.attributes.maxLength - 5})`}
|
|
267
|
-
|
|
268
|
-
do {
|
|
269
|
-
${f.name} = \`\${source${f.name}} (\${++counter})\`
|
|
270
|
-
} while (this.uniqueIds.${f.name}.has(${f.name}))
|
|
271
|
-
|
|
272
|
-
this.logger.log(\`${model.typeName} ${f.name} "\${item.${f.name}}" already exists. Renaming to "\${${f.name}}")\`)
|
|
273
|
-
item.${f.name} = ${f.name}
|
|
274
|
-
}
|
|
256
|
+
return `
|
|
257
|
+
/**
|
|
258
|
+
* Utility function that ensures that the ${f.name} field is unique
|
|
259
|
+
*/
|
|
260
|
+
private ${getEnsureUniqueFnName(f)}(item: { ${f.name}?: string }) {
|
|
261
|
+
if (!item.${f.name}) return
|
|
262
|
+
if (!this.uniqueIds.${f.name}.has(item.${f.name})) return
|
|
263
|
+
let counter = 1
|
|
264
|
+
|
|
265
|
+
let ${f.name}: string
|
|
266
|
+
const source${f.name} =${!f.attributes.maxLength ? `item.${f.name}` : `item.${f.name}.substring(0, ${f.attributes.maxLength - 5})`}
|
|
267
|
+
|
|
268
|
+
do {
|
|
269
|
+
${f.name} = \`\${source${f.name}} (\${++counter})\`
|
|
270
|
+
} while (this.uniqueIds.${f.name}.has(${f.name}))
|
|
271
|
+
|
|
272
|
+
this.logger.log(\`${model.typeName} ${f.name} "\${item.${f.name}}" already exists. Renaming to "\${${f.name}}")\`)
|
|
273
|
+
item.${f.name} = ${f.name}
|
|
274
|
+
}
|
|
275
275
|
`;
|
|
276
276
|
})
|
|
277
|
-
.join('\n\n')}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Utility function that converts a given Database object to a TypeScript model instance
|
|
281
|
-
*/
|
|
282
|
-
private ${decoder}(item: unknown): ${model.typeName} {
|
|
283
|
-
return ${meta.types.zodDecoderFnName}.parse(item)
|
|
284
|
-
}
|
|
285
|
-
}
|
|
277
|
+
.join('\n\n')}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Utility function that converts a given Database object to a TypeScript model instance
|
|
281
|
+
*/
|
|
282
|
+
private ${decoder}(item: unknown): ${model.typeName} {
|
|
283
|
+
return ${meta.types.zodDecoderFnName}.parse(item)
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
286
|
`;
|
|
287
287
|
}
|
|
288
288
|
exports.generateRepository = generateRepository;
|
|
@@ -10,8 +10,8 @@ const imports_1 = require("../../lib/imports");
|
|
|
10
10
|
*/
|
|
11
11
|
function generateRoute({ model, meta }) {
|
|
12
12
|
const { idField, defaultField } = model;
|
|
13
|
-
const defaultValueMethod = `
|
|
14
|
-
getDefault: procedure.query(({ ctx }) => ctx.dataService.${meta.data.dataServiceName}.defaultValue),
|
|
13
|
+
const defaultValueMethod = `
|
|
14
|
+
getDefault: procedure.query(({ ctx }) => ctx.dataService.${meta.data.dataServiceName}.defaultValue),
|
|
15
15
|
`;
|
|
16
16
|
const createMethod = getCreateMethod({ model, meta });
|
|
17
17
|
const updateMethod = getUpdateMethod({ model, meta });
|
|
@@ -27,24 +27,24 @@ function generateRoute({ model, meta }) {
|
|
|
27
27
|
from: depMeta.types.importPath,
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
|
-
return `
|
|
31
|
-
import { z } from 'zod'
|
|
32
|
-
import { procedure, router
|
|
33
|
-
|
|
34
|
-
${imports.generate()}
|
|
35
|
-
|
|
36
|
-
export const ${meta.trpc.routerName} = router({
|
|
37
|
-
${defaultField ? defaultValueMethod : ''}
|
|
38
|
-
|
|
39
|
-
get: procedure
|
|
40
|
-
.input(z.${idField.unbrandedTypeName}().transform(${meta.types.toBrandedIdTypeFnName}))
|
|
41
|
-
.query(async ({ input, ctx }) => await ctx.dataService.${meta.data.dataServiceName}.get(input)),
|
|
42
|
-
getMap: procedure.query(({ ctx }) => ctx.dataService.${meta.data.dataServiceName}.getAll()),
|
|
43
|
-
|
|
44
|
-
${omit(createMethod, model.attributes.skipCreate)}
|
|
45
|
-
${omit(updateMethod, model.attributes.skipUpdate)}
|
|
46
|
-
${omit(deleteMethod, model.attributes.skipDelete)}
|
|
47
|
-
})
|
|
30
|
+
return `
|
|
31
|
+
import { z } from 'zod'
|
|
32
|
+
import { procedure, router } from '../trpc'
|
|
33
|
+
|
|
34
|
+
${imports.generate()}
|
|
35
|
+
|
|
36
|
+
export const ${meta.trpc.routerName} = router({
|
|
37
|
+
${defaultField ? defaultValueMethod : ''}
|
|
38
|
+
|
|
39
|
+
get: procedure
|
|
40
|
+
.input(z.${idField.unbrandedTypeName}().transform(${meta.types.toBrandedIdTypeFnName}))
|
|
41
|
+
.query(async ({ input, ctx }) => await ctx.dataService.${meta.data.dataServiceName}.get(input)),
|
|
42
|
+
getMap: procedure.query(({ ctx }) => ctx.dataService.${meta.data.dataServiceName}.getAll()),
|
|
43
|
+
|
|
44
|
+
${omit(createMethod, model.attributes.skipCreate)}
|
|
45
|
+
${omit(updateMethod, model.attributes.skipUpdate)}
|
|
46
|
+
${omit(deleteMethod, model.attributes.skipDelete)}
|
|
47
|
+
})
|
|
48
48
|
`;
|
|
49
49
|
}
|
|
50
50
|
exports.generateRoute = generateRoute;
|
|
@@ -62,31 +62,31 @@ function getCreateMethod({ model: { fields }, meta }) {
|
|
|
62
62
|
.filter((f) => f.kind !== 'id')
|
|
63
63
|
.map((field) => `${field.name}: z.${(0, zod_1.getZodDecoderDefinition)({ field })}`)
|
|
64
64
|
.join(',');
|
|
65
|
-
return `
|
|
66
|
-
create: procedure.input(z.object({
|
|
67
|
-
${parameters}
|
|
68
|
-
}))
|
|
69
|
-
.mutation(({ input, ctx }) => ctx.dataService.${meta.data.dataServiceName}.create(input)),
|
|
65
|
+
return `
|
|
66
|
+
create: procedure.input(z.object({
|
|
67
|
+
${parameters}
|
|
68
|
+
}))
|
|
69
|
+
.mutation(({ input, ctx }) => ctx.dataService.${meta.data.dataServiceName}.create(input)),
|
|
70
70
|
`;
|
|
71
71
|
}
|
|
72
72
|
function getUpdateMethod({ model: { fields }, meta }) {
|
|
73
73
|
const parameters = fields
|
|
74
74
|
.map((field) => `${field.name}: z.${(0, zod_1.getZodDecoderDefinition)({ field, allowAnyOptionalField: field.kind !== 'id' })}`)
|
|
75
75
|
.join(',');
|
|
76
|
-
return `update: procedure
|
|
77
|
-
.input(
|
|
78
|
-
z.object({
|
|
79
|
-
${parameters}
|
|
80
|
-
})
|
|
81
|
-
)
|
|
82
|
-
.mutation(({ input, ctx }) => ctx.dataService.${meta.data.dataServiceName}.update(input)),
|
|
76
|
+
return `update: procedure
|
|
77
|
+
.input(
|
|
78
|
+
z.object({
|
|
79
|
+
${parameters}
|
|
80
|
+
})
|
|
81
|
+
)
|
|
82
|
+
.mutation(({ input, ctx }) => ctx.dataService.${meta.data.dataServiceName}.update(input)),
|
|
83
83
|
`;
|
|
84
84
|
}
|
|
85
85
|
function getDeleteMethod({ idField, meta }) {
|
|
86
|
-
return `
|
|
87
|
-
delete: procedure
|
|
88
|
-
.input(z.${idField.unbrandedTypeName}().transform(${meta.types.toBrandedIdTypeFnName}))
|
|
89
|
-
.mutation(({ input, ctx }) => ctx.dataService.${meta.data.dataServiceName}.delete(input)),
|
|
86
|
+
return `
|
|
87
|
+
delete: procedure
|
|
88
|
+
.input(z.${idField.unbrandedTypeName}().transform(${meta.types.toBrandedIdTypeFnName}))
|
|
89
|
+
.mutation(({ input, ctx }) => ctx.dataService.${meta.data.dataServiceName}.delete(input)),
|
|
90
90
|
`;
|
|
91
91
|
}
|
|
92
92
|
/**
|
|
@@ -98,15 +98,15 @@ function generateRoutesIndex({ models, meta }) {
|
|
|
98
98
|
for (const { meta } of mm) {
|
|
99
99
|
imports.addImport({ items: [meta.trpc.routerName], from: meta.trpc.routerFilePath });
|
|
100
100
|
}
|
|
101
|
-
return `
|
|
102
|
-
${imports.generate()}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Object with all generated routes.
|
|
106
|
-
*/
|
|
107
|
-
export const routes = {
|
|
108
|
-
${mm.map(({ meta }) => `${meta.trpc.routerName}`).join(',\n')}
|
|
109
|
-
}
|
|
101
|
+
return `
|
|
102
|
+
${imports.generate()}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Object with all generated routes.
|
|
106
|
+
*/
|
|
107
|
+
export const routes = {
|
|
108
|
+
${mm.map(({ meta }) => `${meta.trpc.routerName}`).join(',\n')}
|
|
109
|
+
}
|
|
110
110
|
`;
|
|
111
111
|
}
|
|
112
112
|
exports.generateRoutesIndex = generateRoutesIndex;
|