@postxl/generator 0.6.10 → 0.7.0
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.
|
@@ -36,6 +36,7 @@ function generateRepository({ model, meta }) {
|
|
|
36
36
|
return `
|
|
37
37
|
import { Injectable, Logger } from '@nestjs/common'
|
|
38
38
|
import { DbService, ${model.sourceName} as DbType } from '@${model.schemaConfig.project}/db'
|
|
39
|
+
import { format, pluralize } from '@${model.schemaConfig.project}/common'
|
|
39
40
|
|
|
40
41
|
import { Repository } from '../repository.type'
|
|
41
42
|
|
|
@@ -65,15 +66,17 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
65
66
|
${uniqueStringFields.map((f) => `'${f.name}': new Map<string, ${model.typeName}>()`).join(',\n')}
|
|
66
67
|
}
|
|
67
68
|
|
|
68
|
-
constructor(protected db: DbService) {}
|
|
69
|
+
constructor(${model.attributes.inMemoryOnly ? '' : 'protected '}db: DbService) {}
|
|
69
70
|
|
|
70
71
|
public async init() {
|
|
71
72
|
this.data.clear()
|
|
72
73
|
|
|
73
74
|
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.clear()\n`)}
|
|
74
75
|
|
|
75
|
-
|
|
76
|
-
|
|
76
|
+
${model.attributes.inMemoryOnly
|
|
77
|
+
? 'return Promise.resolve()'
|
|
78
|
+
: `
|
|
79
|
+
|
|
77
80
|
const data = await this.db.${meta.data.repository.getMethodFnName}.findMany({})
|
|
78
81
|
|
|
79
82
|
for (const rawItem of data) {
|
|
@@ -88,17 +91,21 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
88
91
|
|
|
89
92
|
${model.defaultField ? defaultValueInitCheckFn : ''}
|
|
90
93
|
|
|
91
|
-
this.logger.log(
|
|
94
|
+
this.logger.log(\`\${format(this.data.size)} \${pluralize('${model.typeName}', this.data.size)} loaded\`)
|
|
95
|
+
`}
|
|
92
96
|
}
|
|
93
97
|
|
|
94
98
|
public async reInit(data: ${model.typeName}[]): Promise<void> {
|
|
99
|
+
${model.attributes.inMemoryOnly
|
|
100
|
+
? 'return Promise.resolve()'
|
|
101
|
+
: `
|
|
95
102
|
if (!this.db.useE2ETestDB) {
|
|
96
103
|
const errorMsg =
|
|
97
104
|
'ReInit() shall only be called in tests using MockRepositories or in DB configured for E2E tests!'
|
|
98
105
|
this.logger.error(errorMsg)
|
|
99
106
|
throw new Error(errorMsg)
|
|
100
107
|
}
|
|
101
|
-
await this.db.runOnlyOnTestDb(() => this.db.${meta.data.repository.getMethodFnName}.createMany({ data: data.map((i) => this.toCreateItem(i)) }))
|
|
108
|
+
await this.db.runOnlyOnTestDb(() => this.db.${meta.data.repository.getMethodFnName}.createMany({ data: data.map((i) => this.toCreateItem(i)) }))`}
|
|
102
109
|
return this.init()
|
|
103
110
|
}
|
|
104
111
|
|
|
@@ -147,11 +154,16 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
147
154
|
public async createWithId(item: Omit<${model.typeName}, '${idField.name}'> & {
|
|
148
155
|
id: ${model.brandedIdType}
|
|
149
156
|
}): Promise<${model.typeName}> {
|
|
157
|
+
${model.attributes.inMemoryOnly
|
|
158
|
+
? `
|
|
159
|
+
${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
|
|
160
|
+
const newItem = await Promise.resolve(item)`
|
|
161
|
+
: `
|
|
150
162
|
const newItem = this.${decoder}(
|
|
151
163
|
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
152
164
|
data: this.toCreateItem(item as ${model.typeName}),
|
|
153
165
|
}),
|
|
154
|
-
)
|
|
166
|
+
)`}
|
|
155
167
|
|
|
156
168
|
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)`).join('\n')}
|
|
157
169
|
|
|
@@ -162,26 +174,35 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
162
174
|
public async create(
|
|
163
175
|
item: Omit<${model.typeName}, 'id'>
|
|
164
176
|
): Promise<${model.typeName}> {
|
|
177
|
+
${model.attributes.inMemoryOnly && !isGenerated
|
|
178
|
+
? `throw new Error('Create without Id not possible without autogenerated Id. Please adjust schema if you need this function!')`
|
|
179
|
+
: `
|
|
180
|
+
|
|
165
181
|
${isGenerated ? `const id = ++this.currentMaxId\n` : ''}
|
|
166
182
|
|
|
167
183
|
${uniqueStringFields.map((f) => `this.${getEnsureUniqueFnName(f)}(item)`).join('\n')}
|
|
168
184
|
|
|
185
|
+
${model.attributes.inMemoryOnly
|
|
186
|
+
? `
|
|
187
|
+
const newItem = await Promise.resolve({ item, id })
|
|
188
|
+
`
|
|
189
|
+
: `
|
|
169
190
|
const newItem = this.${decoder}(
|
|
170
191
|
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
171
192
|
data: {
|
|
172
193
|
${isGenerated ? `${idField.sourceName}: id,` : ''}
|
|
173
194
|
${[...model.fields.values()]
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
195
|
+
.filter((f) => f.kind !== 'id')
|
|
196
|
+
.map((f) => `${f.sourceName}: item.${f.name}`)
|
|
197
|
+
.join(',\n')}
|
|
177
198
|
},
|
|
178
199
|
}),
|
|
179
|
-
)
|
|
200
|
+
)`}
|
|
180
201
|
|
|
181
202
|
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.set(newItem.${f.name}, newItem)`).join('\n')}
|
|
182
203
|
|
|
183
204
|
this.data.set(newItem.id, newItem)
|
|
184
|
-
return newItem
|
|
205
|
+
return newItem`}
|
|
185
206
|
}
|
|
186
207
|
|
|
187
208
|
public async createMany(items: ${model.typeName}[]) {
|
|
@@ -192,9 +213,12 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
192
213
|
}`
|
|
193
214
|
: ''}
|
|
194
215
|
|
|
216
|
+
${model.attributes.inMemoryOnly
|
|
217
|
+
? 'await Promise.resolve()'
|
|
218
|
+
: `
|
|
195
219
|
await this.db.${meta.data.repository.getMethodFnName}.createMany({ data: items.map(item => ({
|
|
196
220
|
${[...model.fields.values()].map((f) => `${f.sourceName}: item.${f.name}`).join(',\n')}}))
|
|
197
|
-
})
|
|
221
|
+
})`}
|
|
198
222
|
|
|
199
223
|
for (const item of items) {
|
|
200
224
|
this.data.set(item.id, item)
|
|
@@ -223,7 +247,12 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
223
247
|
`;
|
|
224
248
|
})
|
|
225
249
|
.join('\n')}
|
|
226
|
-
|
|
250
|
+
|
|
251
|
+
${model.attributes.inMemoryOnly
|
|
252
|
+
? `
|
|
253
|
+
const newItem = await Promise.resolve({ ...existingItem, ...item })
|
|
254
|
+
`
|
|
255
|
+
: `
|
|
227
256
|
const newItem = this.${decoder}(
|
|
228
257
|
await this.db.${meta.data.repository.getMethodFnName}.update({
|
|
229
258
|
where: {
|
|
@@ -231,16 +260,14 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
231
260
|
},
|
|
232
261
|
data: {
|
|
233
262
|
${[...model.fields.values()]
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
263
|
+
.filter((f) => f.kind !== 'id')
|
|
264
|
+
.map((f) => f.isRequired
|
|
265
|
+
? `${f.sourceName}: item.${f.name} ?? existingItem.${f.name}`
|
|
266
|
+
: `${f.sourceName}: item.${f.name}`)
|
|
267
|
+
.join(',\n')}
|
|
241
268
|
},
|
|
242
269
|
}),
|
|
243
|
-
)
|
|
270
|
+
)`}
|
|
244
271
|
|
|
245
272
|
${uniqueStringFields
|
|
246
273
|
.map((f) => {
|
|
@@ -265,8 +292,14 @@ export class ${meta.data.repositoryClassName} implements Repository<
|
|
|
265
292
|
throw new Error(\`Could not delete ${model.typeName} with id \${id}. Not found!\`)
|
|
266
293
|
}
|
|
267
294
|
|
|
295
|
+
${model.attributes.inMemoryOnly
|
|
296
|
+
? `
|
|
297
|
+
await Promise.resolve()
|
|
298
|
+
`
|
|
299
|
+
: `
|
|
268
300
|
await this.db.${meta.data.repository.getMethodFnName}.delete({ where: { ${idField.sourceName}:id } })
|
|
269
|
-
|
|
301
|
+
`}
|
|
302
|
+
|
|
270
303
|
${uniqueStringFields.map((f) => `this.uniqueIds.${f.name}.delete(existingItem.${f.name})`).join('\n')}
|
|
271
304
|
|
|
272
305
|
this.data.delete(${meta.types.toBrandedIdTypeFnName}(id))
|
package/dist/lib/attributes.d.ts
CHANGED
|
@@ -28,6 +28,11 @@ export type ModelAttributes = {
|
|
|
28
28
|
* Note: Prisma has it's own schema attribute - but does not expose it in the DMMF.
|
|
29
29
|
*/
|
|
30
30
|
databaseSchema?: string;
|
|
31
|
+
/**
|
|
32
|
+
* Schema tag: ´@@InMemoryOnly()`
|
|
33
|
+
* Whether the model should be stored in the database or only in memory.
|
|
34
|
+
*/
|
|
35
|
+
inMemoryOnly: boolean;
|
|
31
36
|
};
|
|
32
37
|
export type FieldAttributes = {
|
|
33
38
|
/**
|
|
@@ -53,6 +53,7 @@ function getModelAttributes(model) {
|
|
|
53
53
|
skipUpdate: Object.hasOwn(attributes, 'skipUpdate') || Object.hasOwn(attributes, 'customUpdate'),
|
|
54
54
|
skipCreate: Object.hasOwn(attributes, 'skipCreate') || Object.hasOwn(attributes, 'customCreate'),
|
|
55
55
|
skipDelete: Object.hasOwn(attributes, 'skipDelete') || Object.hasOwn(attributes, 'customDelete'),
|
|
56
|
+
inMemoryOnly: Object.hasOwn(attributes, 'inMemoryOnly'),
|
|
56
57
|
description: attributes.description && (0, remeda_1.isString)(attributes.description) ? attributes.description : undefined,
|
|
57
58
|
databaseSchema: attributes.schema && (0, remeda_1.isString)(attributes.schema) ? attributes.schema : undefined,
|
|
58
59
|
};
|