@postxl/generator 0.25.0 → 0.25.1
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.
|
@@ -114,6 +114,7 @@ function generateModelBusinessLogic({ model, meta }) {
|
|
|
114
114
|
return item
|
|
115
115
|
}
|
|
116
116
|
`;
|
|
117
|
+
// prettier-ignore
|
|
117
118
|
return `
|
|
118
119
|
import { Inject, Injectable, forwardRef } from '@nestjs/common'
|
|
119
120
|
import { FilterOperator } from '@pxl/common'
|
|
@@ -169,18 +170,14 @@ export class ${meta.businessLogic.serviceClassName} {
|
|
|
169
170
|
/**
|
|
170
171
|
* Creates a new ${meta.userFriendlyName}.
|
|
171
172
|
*/
|
|
172
|
-
public async create(item: Omit<${model.typeName}, '${model.idField.name}'> & Partial<{
|
|
173
|
-
${model.idField.name}: ${model.idField.unbrandedTypeName}
|
|
174
|
-
}>): Promise<${model.typeName}> {
|
|
173
|
+
public async create(item: Omit<${model.typeName}, '${model.idField.name}'> & Partial<{${model.idField.name}: ${model.idField.unbrandedTypeName}}>): Promise<${model.typeName}> {
|
|
175
174
|
return this.${modelRepositoryVariableName}.create(item)
|
|
176
175
|
}
|
|
177
176
|
|
|
178
177
|
/**
|
|
179
178
|
* Updates the given ${meta.userFriendlyName}.
|
|
180
179
|
*/
|
|
181
|
-
public async update(item: Partial<${model.typeName}> & {
|
|
182
|
-
id: ${model.brandedIdType}
|
|
183
|
-
}): Promise<${model.typeName}> {
|
|
180
|
+
public async update(item: Partial<${model.typeName}> & {id: ${model.brandedIdType}}): Promise<${model.typeName}> {
|
|
184
181
|
return this.${modelRepositoryVariableName}.update(item)
|
|
185
182
|
}
|
|
186
183
|
|
|
@@ -210,12 +210,11 @@ function generateMainBuildingBlocks_InMemoryOnly({ model, meta, blocks, }) {
|
|
|
210
210
|
public async deleteAll(): Promise<void> {
|
|
211
211
|
return this.init()
|
|
212
212
|
}`,
|
|
213
|
+
// prettier-ignore
|
|
213
214
|
createCode: `
|
|
214
215
|
// Non-mocked version is async - so we keep type-compatible signatures for create() and createWithId()
|
|
215
216
|
// eslint-disable-next-line @typescript-eslint/require-await, @typescript-eslint/no-unused-vars
|
|
216
|
-
public async create(item: Omit<${model.typeName}, '${idField.name}'> & Partial<{
|
|
217
|
-
id: ${idField.unbrandedTypeName}
|
|
218
|
-
}>): Promise<${model.typeName}> {
|
|
217
|
+
public async create(item: Omit<${model.typeName}, '${idField.name}'> & Partial<{id: ${idField.unbrandedTypeName}}>): Promise<${model.typeName}> {
|
|
219
218
|
const newItem = await Promise.resolve(this.verifyItem(item))
|
|
220
219
|
|
|
221
220
|
this.set(newItem)
|
|
@@ -327,10 +326,9 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, imports, blocks, }
|
|
|
327
326
|
return this.init()
|
|
328
327
|
}
|
|
329
328
|
`,
|
|
329
|
+
// prettier-ignore
|
|
330
330
|
createCode: `
|
|
331
|
-
public async create(item: Omit<${model.typeName}, '${idField.name}'> & Partial<{
|
|
332
|
-
id: ${idField.unbrandedTypeName}
|
|
333
|
-
}>): Promise<${model.typeName}> {
|
|
331
|
+
public async create(item: Omit<${model.typeName}, '${idField.name}'> & Partial<{ id: ${idField.unbrandedTypeName} }>): Promise<${model.typeName}> {
|
|
334
332
|
const newItem = this.${decoderFunctionName}(
|
|
335
333
|
await this.db.${meta.data.repository.getMethodFnName}.create({
|
|
336
334
|
data: this.toCreateItem(this.verifyItem(item)),
|
|
@@ -352,16 +350,14 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, imports, blocks, }
|
|
|
352
350
|
|
|
353
351
|
return newItems
|
|
354
352
|
}`,
|
|
353
|
+
// prettier-ignore
|
|
355
354
|
updateCode: `
|
|
356
|
-
public async update(item: Partial<${model.typeName}> & {
|
|
357
|
-
id: ${model.brandedIdType}
|
|
358
|
-
}): Promise<${model.typeName}> {
|
|
355
|
+
public async update(item: Partial<${model.typeName}> & { id: ${model.brandedIdType}}): Promise<${model.typeName}> {
|
|
359
356
|
const existingItem = this.get(item.id)
|
|
360
357
|
|
|
361
358
|
if (!existingItem) {
|
|
362
359
|
throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
|
|
363
360
|
}
|
|
364
|
-
|
|
365
361
|
|
|
366
362
|
${blocks.maxLength.updateCode.join('\n')}
|
|
367
363
|
|
|
@@ -443,10 +439,10 @@ function generateIdBlocks_NoGeneration({ idField, model, meta, }) {
|
|
|
443
439
|
verifyFunctionComment: `an error is thrown as field has no default setting in schema.`,
|
|
444
440
|
verifyFunctionParameterType: model.typeName,
|
|
445
441
|
verifyCode: `
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
442
|
+
if (item.${idField.name} === undefined) {
|
|
443
|
+
throw new Error('Id field ${idField.name} is required!')
|
|
444
|
+
}
|
|
445
|
+
const ${idField.name} = ${meta.types.toBrandedIdTypeFnName}(item.${idField.name})`,
|
|
450
446
|
setCode: '',
|
|
451
447
|
};
|
|
452
448
|
}
|
|
@@ -458,10 +454,10 @@ function generateIdBlock_Int({ idField, model, meta, imports, }) {
|
|
|
458
454
|
return {
|
|
459
455
|
libraryImports: '',
|
|
460
456
|
generateNextIdFunctionName: `
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
457
|
+
protected currentMaxId = 0
|
|
458
|
+
public generateNextId(): ${model.brandedIdType} {
|
|
459
|
+
return ${meta.types.toBrandedIdTypeFnName}(++this.currentMaxId)
|
|
460
|
+
}`,
|
|
465
461
|
initCode: `this.currentMaxId = (await this.db.${meta.data.repository.getMethodFnName}.aggregate({ _max: { ${idField.sourceName}: true } }))._max.${idField.sourceName} ?? 0`,
|
|
466
462
|
verifyFunctionComment: 'the id is generated by increasing the highest former id and assigned to the item.',
|
|
467
463
|
verifyFunctionParameterType: `(Omit<${model.typeName}, '${idField.name}'> & Partial<{${idField.name}: ${idField.unbrandedTypeName}}>)`,
|
|
@@ -477,9 +473,9 @@ function generateIdBlock_UUID({ idField, model, meta, imports, }) {
|
|
|
477
473
|
return {
|
|
478
474
|
libraryImports: `import { randomUUID } from 'crypto'`,
|
|
479
475
|
generateNextIdFunctionName: `
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
476
|
+
public generateNextId(): ${model.brandedIdType} {
|
|
477
|
+
return ${meta.types.toBrandedIdTypeFnName}(randomUUID())
|
|
478
|
+
}`,
|
|
483
479
|
initCode: '',
|
|
484
480
|
verifyFunctionComment: 'a new UUID is generated and assigned to the item.',
|
|
485
481
|
verifyFunctionParameterType: `(Omit<${model.typeName}, '${idField.name}'> & Partial<{${idField.name}: ${idField.unbrandedTypeName}}>)`,
|
|
@@ -689,24 +685,25 @@ function generateRelationsBlocks({ model, imports, }) {
|
|
|
689
685
|
from: relationModelMeta.types.importPath,
|
|
690
686
|
});
|
|
691
687
|
result.mapDeclarations.push(`protected ${r.name}Map: Map<${relationModelMeta.types.brandedIdType}, Map<${model.brandedIdType}, ${model.typeName}>> = new Map()`);
|
|
688
|
+
// prettier-ignore
|
|
692
689
|
result.getterFunctions.push(`
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
690
|
+
/**
|
|
691
|
+
* Function to retrieve all ${(0, string_1.pluralize)(model.name)} that are related to a ${r.name}
|
|
692
|
+
*/
|
|
693
|
+
public ${fieldMeta.getByForeignKeyMethodFnName}(id: ${relationModelMeta.types.brandedIdType}): Map<${model.brandedIdType}, ${model.typeName}> {
|
|
694
|
+
const result = this.${r.name}Map.get(id)
|
|
695
|
+
if (!result) return new Map()
|
|
696
|
+
return result
|
|
697
|
+
}
|
|
701
698
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
699
|
+
/**
|
|
700
|
+
* Function to retrieve all ${model.brandedIdType}s that are related to a ${r.name}
|
|
701
|
+
*/
|
|
702
|
+
public ${fieldMeta.getByForeignKeyIdsMethodFnName}(id: ${relationModelMeta.types.brandedIdType}): ${model.brandedIdType}[] {
|
|
703
|
+
const s = this.${r.name}Map.get(id)
|
|
704
|
+
if (!s) return []
|
|
705
|
+
return Array.from(s.keys())
|
|
706
|
+
}`);
|
|
710
707
|
result.setCode.push(`
|
|
711
708
|
${!r.isRequired ? `if (item.${r.name}) {` : ''}
|
|
712
709
|
let ${r.name}Map = this.${r.name}Map.get(item.${r.name})
|