@postxl/generator 0.36.0 → 0.38.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.
@@ -20,9 +20,9 @@ function generateDataService({ models, meta }) {
20
20
  .join(',\n');
21
21
  const initializer = mm.map(({ meta }) => `await this.${meta.data.dataServiceName}.init()`).join('\n');
22
22
  const excelExports = mm
23
- .map(({ meta }) => `${meta.data.excelExportTableName}: mapValues(this.${meta.data.dataServiceName}.getAll()),`)
23
+ .map(({ meta }) => `${meta.data.excelExportTableName}: mapValues(await this.${meta.data.dataServiceName}.getAll()),`)
24
24
  .join('\n');
25
- const isEmptyChecks = mm.map(({ meta }) => `this.${meta.data.dataServiceName}.count() === 0`).join(' &&');
25
+ const isEmptyChecks = mm.map(({ meta }) => `(await this.${meta.data.dataServiceName}.count()) === 0`).join(' &&');
26
26
  return `
27
27
  import { Injectable } from '@nestjs/common'
28
28
  import { mapValues } from '@pxl/common'
@@ -33,7 +33,7 @@ ${imports.generate()}
33
33
  export class DataService {
34
34
  constructor(${constructor}) {}
35
35
 
36
- public prepareExcelExport() {
36
+ public async prepareExcelExport() {
37
37
  return {
38
38
  ${excelExports}
39
39
  }
@@ -43,7 +43,7 @@ export class DataService {
43
43
  ${initializer}
44
44
  }
45
45
 
46
- public isEmpty(): boolean {
46
+ public async isEmpty(): Promise<boolean> {
47
47
  return (
48
48
  ${isEmptyChecks}
49
49
  )
@@ -41,7 +41,7 @@ function generateModelBusinessLogicView({ model, meta }) {
41
41
  for (const relation of (0, fields_1.getRelationFields)(model)) {
42
42
  const refModel = relation.relationToModel;
43
43
  const refMeta = (0, meta_1.getModelMetadata)({ model: refModel });
44
- const variableGetter = `this.${viewServiceClassName}.${refMeta.businessLogic.view.serviceVariableName}.get(itemRaw.${relation.name})`;
44
+ const variableGetter = `await this.${viewServiceClassName}.${refMeta.businessLogic.view.serviceVariableName}.get(itemRaw.${relation.name})`;
45
45
  const variablePresenceCheck = `
46
46
  if (!${relation.relatedModelBacklinkFieldName}) {
47
47
  throw new Error(\`Could not find ${refMeta.types.typeName} with id \${itemRaw.${relation.name}} for ${model.typeName}.${relation.name}!\`)
@@ -66,8 +66,8 @@ function generateModelBusinessLogicView({ model, meta }) {
66
66
  * Returns the linked ${meta.userFriendlyName} with the given id or null if it does not exist.
67
67
  * Linked: The ${meta.userFriendlyName} contains the linked (raw) items themselves, not only the ids.
68
68
  */
69
- public getLinkedItem(id: ${model.brandedIdType}): ${meta.types.linkedTypeName} | null {
70
- const itemRaw = this.${modelRepositoryVariableName}.get(id)
69
+ public async getLinkedItem(id: ${model.brandedIdType}): Promise<${meta.types.linkedTypeName} | null> {
70
+ const itemRaw = await this.${modelRepositoryVariableName}.get(id)
71
71
  if (!itemRaw) {
72
72
  return null
73
73
  }
@@ -107,7 +107,7 @@ export class ${meta.businessLogic.view.serviceClassName} {
107
107
  * Returns the raw ${meta.userFriendlyName} with the given id or null if it does not exist.
108
108
  * Raw: The ${meta.userFriendlyName} only contains linked Ids, not the linked items themselves.
109
109
  */
110
- public get(id: ${model.brandedIdType}): ${meta.types.typeName} | null {
110
+ public async get(id: ${model.brandedIdType}): Promise<${meta.types.typeName} | null> {
111
111
  return this.${modelRepositoryVariableName}.get(id)
112
112
  }
113
113
 
@@ -116,14 +116,14 @@ export class ${meta.businessLogic.view.serviceClassName} {
116
116
  /**
117
117
  * Returns a map of all ${meta.userFriendlyNamePlural}.
118
118
  */
119
- public getAll(): Map<${meta.types.brandedIdType}, ${model.typeName}> {
119
+ public async getAll(): Promise<Map<${meta.types.brandedIdType}, ${model.typeName}>> {
120
120
  return this.${modelRepositoryVariableName}.getAll()
121
121
  }
122
122
 
123
123
  /**
124
124
  * Returns a list of filtered, sorted and paginated ${meta.userFriendlyNamePlural}.
125
125
  */
126
- public getList({
126
+ public async getList({
127
127
  filter,
128
128
  sort,
129
129
  cursor,
@@ -132,7 +132,7 @@ export class ${meta.businessLogic.view.serviceClassName} {
132
132
  sort?: { field: keyof ${model.typeName}; ascending: boolean }
133
133
  cursor?: { startRow: number; endRow: number }
134
134
  }) {
135
- const items = this.data.getAllAsArray()
135
+ const items = await this.data.getAllAsArray()
136
136
  const filtered = !filter
137
137
  ? items
138
138
  : items.filter((item) => filterFn(item, filter.field, filter.operator, filter.value))
@@ -109,33 +109,33 @@ export class ${meta.data.repositoryClassName} implements Repository<${model.type
109
109
 
110
110
  ${mainBlocks.deleteAllCode}
111
111
 
112
- public get(id: ${model.brandedIdType} | null): ${model.typeName} | null {
112
+ public async get(id: ${model.brandedIdType} | null): Promise<${model.typeName} | null> {
113
113
  if (id === null) {
114
- return null
114
+ return Promise.resolve(null)
115
115
  }
116
- return this.data.get(id) ?? null
116
+ return Promise.resolve(this.data.get(id) ?? null)
117
117
  }
118
118
 
119
- public getAll(): Map<${model.brandedIdType}, ${model.typeName}> {
120
- return new Map(this.data)
119
+ public async getAll(): Promise<Map<${model.brandedIdType}, ${model.typeName}>> {
120
+ return Promise.resolve(new Map(this.data))
121
121
  }
122
122
 
123
- public getAllAsArray(): ${model.typeName}[] {
124
- return Array.from(this.data.values())
123
+ public async getAllAsArray(): Promise<${model.typeName}[]> {
124
+ return Promise.resolve(Array.from(this.data.values()))
125
125
  }
126
126
 
127
127
  ${indexBlocks.getterFunctions.join('\n')}
128
128
 
129
- public filter(predicate: (item: ${model.typeName}) => boolean): ${model.typeName}[] {
130
- return this.getAllAsArray().filter(predicate)
129
+ public async filter(predicate: (item: ${model.typeName}) => boolean): Promise<${model.typeName}[]> {
130
+ return (await this.getAllAsArray()).filter(predicate)
131
131
  }
132
132
 
133
- public findFirst(predicate: (item: ${model.typeName}) => boolean): ${model.typeName} | null {
134
- return this.getAllAsArray().find(predicate) ?? null
133
+ public async findFirst(predicate: (item: ${model.typeName}) => boolean): Promise<${model.typeName} | null> {
134
+ return (await this.getAllAsArray()).find(predicate) ?? null
135
135
  }
136
136
 
137
- public count(): number {
138
- return this.data.size
137
+ public async count(): Promise<number> {
138
+ return Promise.resolve(this.data.size)
139
139
  }
140
140
 
141
141
  ${mainBlocks.createCode}
@@ -313,7 +313,7 @@ function _generateMainBuildingBlocks_InMemoryOnly({ model, meta, blocks, }) {
313
313
  public async update(
314
314
  { item, execution }: ${methodTypeSignatures.update.parameters[0]}
315
315
  ): ${methodTypeSignatures.update.returnType} {
316
- const existingItem = this.get(item.id)
316
+ const existingItem = await this.get(item.id)
317
317
 
318
318
  if (!existingItem) {
319
319
  throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
@@ -363,7 +363,7 @@ function _generateMainBuildingBlocks_InMemoryOnly({ model, meta, blocks, }) {
363
363
  public async upsert(
364
364
  { item, execution }: ${methodTypeSignatures.upsert.parameters[0]}
365
365
  ): ${methodTypeSignatures.upsert.returnType} {
366
- const existingItem = item.${model.idField.name} ? this.get(item.${model.idField.name}) : null
366
+ const existingItem = item.${model.idField.name} ? (await this.get(item.${model.idField.name})) : null
367
367
  if (existingItem) {
368
368
  return this.update({ item: item as ${meta.types.dto.update}, execution })
369
369
  } else {
@@ -391,7 +391,7 @@ function _generateMainBuildingBlocks_InMemoryOnly({ model, meta, blocks, }) {
391
391
  public async delete(
392
392
  { id, execution }: ${methodTypeSignatures.delete.parameters[0]}
393
393
  ): ${methodTypeSignatures.delete.returnType} {
394
- const existingItem = this.get(id)
394
+ const existingItem = await this.get(id)
395
395
  if (!existingItem) {
396
396
  throw new Error(\`Could not delete ${model.typeName} with id \${id}. Not found!\`)
397
397
  }
@@ -599,7 +599,7 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, imports, blocks, }
599
599
  public async update(
600
600
  { item, execution }: ${methodTypeSignatures.update.parameters[0]}
601
601
  ): ${methodTypeSignatures.update.returnType} {
602
- const existingItem = this.get(item.${idField.name})
602
+ const existingItem = await this.get(item.${idField.name})
603
603
  if (!existingItem) {
604
604
  throw new Error(\`Could not update ${meta.userFriendlyName} with id \${item.id}. Not found!\`)
605
605
  }
@@ -662,7 +662,7 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, imports, blocks, }
662
662
  public async upsert(
663
663
  { item, execution }: ${methodTypeSignatures.upsert.parameters[0]}
664
664
  ): ${methodTypeSignatures.upsert.returnType} {
665
- const existingItem = item.${model.idField.name} ? this.get(item.${model.idField.name}) : null
665
+ const existingItem = item.${model.idField.name} ? (await this.get(item.${model.idField.name})) : null
666
666
  if (existingItem) {
667
667
  return this.update({ item: item as ${meta.types.dto.update}, execution })
668
668
  } else {
@@ -690,7 +690,7 @@ function generateMainBuildingBlocks_InDatabase({ model, meta, imports, blocks, }
690
690
  public async delete(
691
691
  { id, execution }: ${methodTypeSignatures.delete.parameters[0]}
692
692
  ): ${methodTypeSignatures.delete.returnType} {
693
- const existingItem = this.get(id)
693
+ const existingItem = await this.get(id)
694
694
  if (!existingItem) {
695
695
  throw new Error(\`Could not delete ${model.typeName} with id \${id}. Not found!\`)
696
696
  }
@@ -1098,28 +1098,28 @@ function generateRelationsBlocks({ model, imports, }) {
1098
1098
  /**
1099
1099
  * Function to retrieve all ${(0, string_1.pluralize)(model.name)} that are related to a ${r.name}
1100
1100
  */
1101
- public ${fieldMeta.getByForeignKeyMethodFnName}(
1101
+ public async ${fieldMeta.getByForeignKeyMethodFnName}(
1102
1102
  id: ${relationModelMeta.types.brandedIdType}
1103
- ): Map<${model.brandedIdType}, ${model.typeName}> {
1103
+ ): Promise<Map<${model.brandedIdType}, ${model.typeName}>> {
1104
1104
  const result = this.${r.name}Map.get(id)
1105
1105
  if (!result) {
1106
- return new Map()
1106
+ return Promise.resolve(new Map<${model.brandedIdType}, ${model.typeName}>())
1107
1107
  }
1108
- return new Map(result)
1108
+ return Promise.resolve(new Map(result))
1109
1109
  }
1110
1110
 
1111
1111
  /**
1112
1112
  * Function to retrieve all ${model.brandedIdType}s that are related to a ${r.name}
1113
1113
  */
1114
- public ${fieldMeta.getByForeignKeyIdsMethodFnName}(
1114
+ public async ${fieldMeta.getByForeignKeyIdsMethodFnName}(
1115
1115
  id: ${relationModelMeta.types.brandedIdType}
1116
- ): ${model.brandedIdType}[] {
1116
+ ): Promise<${model.brandedIdType}[]> {
1117
1117
  const s = this.${r.name}Map.get(id)
1118
1118
  if (!s) {
1119
- return []
1119
+ return Promise.resolve([])
1120
1120
  }
1121
1121
 
1122
- return Array.from(s.keys())
1122
+ return Promise.resolve(Array.from(s.keys()))
1123
1123
  }`);
1124
1124
  result.setCode.push(`
1125
1125
  ${!r.isRequired ? `if (item.${r.name}) {` : ''}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.36.0",
3
+ "version": "0.38.0",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {