@postxl/generator 0.56.1 → 0.56.3

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.
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.generateBusinessLogicActionTypes = void 0;
4
4
  const imports_1 = require("../../lib/imports");
5
5
  const meta_1 = require("../../lib/meta");
6
- const types_1 = require("../../lib/schema/types");
7
6
  const file_1 = require("../../lib/utils/file");
8
7
  /**
9
8
  * Generates the action types for the BusinessLogicModule.
@@ -11,7 +10,7 @@ const file_1 = require("../../lib/utils/file");
11
10
  function generateBusinessLogicActionTypes({ models, meta }) {
12
11
  const imports = imports_1.ImportsGenerator.from(meta.businessLogic.update.actionTypesFilePath).addImport({
13
12
  from: meta.actions.importPath,
14
- items: [(0, types_1.toTypeName)('ActionDefPayload'), (0, types_1.toTypeName)('ActionDefResult')],
13
+ items: [meta.actions.definition.payload, meta.actions.definition.resultDict],
15
14
  });
16
15
  const updateServiceImports = [];
17
16
  const modelNameTypeElements = [];
@@ -27,8 +26,8 @@ function generateBusinessLogicActionTypes({ models, meta }) {
27
26
  to: modelMeta.businessLogic.update.serviceFilePath,
28
27
  });
29
28
  updateServiceImports.push(`import type * as ${className} from '${relativeImportPath}'`);
30
- actionsTypeElements.push(`ActionDefPayload<${className}.Scope, ${className}.Actions>`);
31
- actionResultTypeElements.push(`${scope}: ActionDefResult<${className}.Actions>`);
29
+ actionsTypeElements.push(`${meta.actions.definition.payload}<${className}.Scope, ${className}.Actions>`);
30
+ actionResultTypeElements.push(`${scope}: ${meta.actions.definition.resultDict}<${className}.Actions>`);
32
31
  }
33
32
  return /* ts */ `
34
33
  ${imports.generate()}
@@ -36,7 +36,7 @@ function generateDataMockModule({ models, meta }) {
36
36
  [meta.data.dataModuleFilePath]: [Types.toVariableName('DataModule')],
37
37
  [meta.data.dataService.filePath]: [meta.data.dataService.class],
38
38
  // we need to import the file directly instead via the normal index file as this would cause a circular dependency else
39
- [meta.actions.importPath]: [meta.actions.actionExecution.mock],
39
+ [meta.actions.importPath]: [meta.actions.execution.mock],
40
40
  });
41
41
  for (const { model, meta } of mm) {
42
42
  imports.addTypeImport({ items: [model.typeName], from: meta.types.importPath });
@@ -29,7 +29,7 @@ function generateDataService({ models, meta }) {
29
29
  [meta.types.importPath]: [(0, types_1.toAnnotatedTypeName)(meta.types.dto.create), (0, types_1.toAnnotatedTypeName)(meta.types.dto.update)],
30
30
  [meta.data.repository.typeFilePath]: [(0, types_1.toAnnotatedTypeName)(meta.data.repository.typeName)],
31
31
  [meta.data.types.filePath]: [(0, types_1.toAnnotatedTypeName)(meta.data.types.bulkMutation)],
32
- [meta.actions.importPath]: [meta.actions.actionExecution.interface],
32
+ [meta.actions.importPath]: [meta.actions.execution.interface],
33
33
  });
34
34
  const creates = [];
35
35
  const updates = [];
@@ -77,7 +77,7 @@ export class ${meta.data.dataService.class} {
77
77
  }
78
78
  public async ${meta.data.dataService.executeBulkMutations}(
79
79
  { steps, execution }:
80
- { steps: ${meta.data.types.bulkMutation}[]; execution: ${meta.actions.actionExecution.interface} }
80
+ { steps: ${meta.data.types.bulkMutation}[]; execution: ${meta.actions.execution.interface} }
81
81
  ) {
82
82
  let index = 0
83
83
  for (const step of steps) {
@@ -88,7 +88,7 @@ export class ${meta.data.dataService.class} {
88
88
 
89
89
  public async ${meta.data.dataService.executeBulkMutation}(
90
90
  { data, execution }:
91
- { data: ${meta.data.types.bulkMutation}, execution: ${meta.actions.actionExecution.interface}}
91
+ { data: ${meta.data.types.bulkMutation}, execution: ${meta.actions.execution.interface}}
92
92
  ) {
93
93
  // NOTE: the order of these calls is important, because of foreign key constraints
94
94
  // The current order is based on the order of the models in the schema
@@ -114,7 +114,7 @@ export class ${meta.data.dataService.class} {
114
114
  name: string
115
115
  data: ${meta.types.dto.create}<T, ID>[] | undefined
116
116
  repo: ${meta.data.repository.typeName}<T, ID>
117
- execution: ${meta.actions.actionExecution.interface}
117
+ execution: ${meta.actions.execution.interface}
118
118
  }): Promise<void> {
119
119
  if (!data) {
120
120
  return
@@ -146,7 +146,7 @@ export class ${meta.data.dataService.class} {
146
146
  name: string
147
147
  data: ${meta.types.dto.update}<T, ID>[] | undefined
148
148
  repo: ${meta.data.repository.typeName}<T, ID>
149
- execution: ${meta.actions.actionExecution.interface}
149
+ execution: ${meta.actions.execution.interface}
150
150
  }): Promise<void> {
151
151
  if (!data) {
152
152
  return
@@ -178,7 +178,7 @@ export class ${meta.data.dataService.class} {
178
178
  name: string
179
179
  data: (${meta.types.dto.create}<T, ID> | ${meta.types.dto.update}<T, ID>)[] | undefined
180
180
  repo: ${meta.data.repository.typeName}<T, ID>
181
- execution: ${meta.actions.actionExecution.interface}
181
+ execution: ${meta.actions.execution.interface}
182
182
  }): Promise<void> {
183
183
  if (!data) {
184
184
  return
@@ -210,7 +210,7 @@ export class ${meta.data.dataService.class} {
210
210
  name: string
211
211
  data: ID[] | undefined
212
212
  repo: ${meta.data.repository.typeName}<T, ID>
213
- execution: ${meta.actions.actionExecution.interface}
213
+ execution: ${meta.actions.execution.interface}
214
214
  }): Promise<void> {
215
215
  if (!data) {
216
216
  return
@@ -22,7 +22,7 @@ function generateImportExportImportService({ models, meta }) {
22
22
  (0, types_1.toAnnotatedTypeName)(dto.idType),
23
23
  (0, types_1.toAnnotatedTypeName)(dto.update),
24
24
  ],
25
- [meta.actions.importPath]: [meta.actions.actionExecution.interface, meta.actions.dispatcher.class],
25
+ [meta.actions.importPath]: [meta.actions.execution.interface, meta.actions.dispatcher.class],
26
26
  [types.filePath]: [
27
27
  (0, types_1.toAnnotatedTypeName)(delta_Fields),
28
28
  (0, types_1.toAnnotatedTypeName)(delta_Model.type),
@@ -78,7 +78,7 @@ export class ${meta.importExport.importService.name} {
78
78
  execution
79
79
  }: {
80
80
  action: Action_Import;
81
- execution: ${meta.actions.actionExecution.interface}
81
+ execution: ${meta.actions.execution.interface}
82
82
  }) {
83
83
  switch (action.type) {
84
84
  case 'detect-delta':
@@ -110,7 +110,7 @@ export class ${meta.importExport.importService.name} {
110
110
  execution
111
111
  }: {
112
112
  delta: Delta;
113
- execution: ${meta.actions.actionExecution.interface}
113
+ execution: ${meta.actions.execution.interface}
114
114
  }) {
115
115
  const bulkMutations = ${converterFunctions.deltaToBulkMutations}(delta)
116
116
  return this.data.${meta.data.dataService.executeBulkMutations}({ steps: bulkMutations, execution })
@@ -121,7 +121,7 @@ export class ${meta.importExport.importService.name} {
121
121
  execution
122
122
  }: {
123
123
  data: ${meta.importExport.decoder.decodedPXLModelDataTypeName};
124
- execution: ${meta.actions.actionExecution.interface}
124
+ execution: ${meta.actions.execution.interface}
125
125
  }) {
126
126
  const { bulkMutations } = await this.detectDelta(data)
127
127
  return this.data.${meta.data.dataService.executeBulkMutations}({ steps: bulkMutations, execution })
@@ -23,14 +23,14 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
23
23
  ],
24
24
  [meta.businessLogic.view.serviceFilePath]: [meta.businessLogic.view.serviceClassName],
25
25
  [schemaMeta.actions.importPath]: [
26
- schemaMeta.actions.actionExecution.interface,
26
+ schemaMeta.actions.execution.interface,
27
27
  schemaMeta.actions.dispatcher.interface,
28
- schemaMeta.actions.dispatcher.actionMethod,
28
+ schemaMeta.actions.definition.payload,
29
+ schemaMeta.actions.definition.result,
29
30
  ],
30
31
  [schemaMeta.businessLogic.update.serviceFilePath]: schemaMeta.businessLogic.update.serviceClassName,
31
32
  [schemaMeta.businessLogic.view.serviceFilePath]: schemaMeta.businessLogic.view.serviceClassName,
32
33
  });
33
- const { dispatcher } = schemaMeta.actions;
34
34
  for (const relation of (0, fields_1.getRelationFields)(model)) {
35
35
  // NOTE: We add branded id type and type name imports only for foreign models.
36
36
  if (relation.relationToModel.typeName === model.typeName) {
@@ -39,7 +39,7 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
39
39
  const refMeta = (0, meta_1.getModelMetadata)({ model: relation.relationToModel });
40
40
  imports.addImport({
41
41
  items: [refMeta.types.toBrandedIdTypeFnName],
42
- from: refMeta.types.filePath,
42
+ from: refMeta.types.importPath,
43
43
  });
44
44
  }
45
45
  /**
@@ -54,9 +54,16 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
54
54
  `@Inject(forwardRef(() => ${schemaMeta.businessLogic.view.serviceClassName})) private readonly viewService: ${schemaMeta.businessLogic.view.serviceClassName}`,
55
55
  ];
56
56
  const { zodCreateObject, zodUpdateObject, zodUpsertObject } = meta.businessLogic.update;
57
+ const { dispatcher, definition } = schemaMeta.actions;
58
+ /**
59
+ * A return value of the dispatcher.
60
+ *
61
+ * NOTE: We need to cast to the return value every time so that the function correctly determines the return type.
62
+ */
63
+ const dispatcherReturnValue = `Promise<${definition.result}<Actions, A>>`;
57
64
  return /* ts */ `
58
65
  import { Inject, Injectable, forwardRef } from '@nestjs/common'
59
- import { ExhaustiveSwitchCheck, UnionOmit } from '@backend/common'
66
+ import { ExhaustiveSwitchCheck } from '@backend/common'
60
67
  import { z } from 'zod'
61
68
 
62
69
  ${imports.generate()}
@@ -154,37 +161,37 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
154
161
  /**
155
162
  * Dispatches an action to the appropriate method of the repository.
156
163
  */
157
- public async dispatch<A extends ${dispatcher.actionMethod}<Scope, Actions>>({
164
+ public async dispatch<A extends ${definition.payload}<Scope, Actions>>({
158
165
  action,
159
166
  execution,
160
167
  }: {
161
- action: UnionOmit<A, 'result'>
162
- execution: ${schemaMeta.actions.actionExecution.interface}
163
- }): Promise<A['result']> {
168
+ action: A
169
+ execution: ${schemaMeta.actions.execution.interface}
170
+ }): ${dispatcherReturnValue} {
164
171
  switch (action.type) {
165
172
  case 'create':
166
- return this.data.create({ item: action.payload, execution })
173
+ return this.data.create({ item: action.payload, execution }) as ${dispatcherReturnValue}
167
174
 
168
175
  case 'createMany':
169
- return this.data.createMany({ items: action.payload, execution })
176
+ return this.data.createMany({ items: action.payload, execution }) as ${dispatcherReturnValue}
170
177
 
171
178
  case 'update':
172
- return this.data.update({ item: action.payload, execution })
179
+ return this.data.update({ item: action.payload, execution }) as ${dispatcherReturnValue}
173
180
 
174
181
  case 'updateMany':
175
- return this.data.updateMany({ items: action.payload, execution })
182
+ return this.data.updateMany({ items: action.payload, execution }) as ${dispatcherReturnValue}
176
183
 
177
184
  case 'upsert':
178
- return this.data.upsert({ item: action.payload, execution })
185
+ return this.data.upsert({ item: action.payload, execution }) as ${dispatcherReturnValue}
179
186
 
180
187
  case 'upsertMany':
181
- return this.data.upsertMany({ items: action.payload, execution })
188
+ return this.data.upsertMany({ items: action.payload, execution }) as ${dispatcherReturnValue}
182
189
 
183
190
  case 'delete':
184
- return this.data.delete({ id: action.payload, execution })
191
+ return this.data.delete({ id: action.payload, execution }) as ${dispatcherReturnValue}
185
192
 
186
193
  case 'deleteMany':
187
- return this.data.deleteMany({ ids: action.payload, execution })
194
+ return this.data.deleteMany({ ids: action.payload, execution }) as ${dispatcherReturnValue}
188
195
 
189
196
  default:
190
197
  throw new ExhaustiveSwitchCheck(action)
@@ -59,8 +59,8 @@ function generateModelBusinessLogicView({ model, meta }) {
59
59
  variableDefinition: `const ${relationVariableName} = ${relationVariableDefinition}`,
60
60
  });
61
61
  if (relation.relationToModel.typeName !== model.typeName) {
62
- imports.addImport({ from: refMeta.types.filePath, items: [refMeta.types.toBrandedIdTypeFnName] });
63
- imports.addTypeImport({ from: refMeta.types.filePath, items: [refModel.brandedIdType, refMeta.types.typeName] });
62
+ imports.addImport({ from: refMeta.types.importPath, items: [refMeta.types.toBrandedIdTypeFnName] });
63
+ imports.addTypeImport({ from: refMeta.types.importPath, items: [refModel.brandedIdType, refMeta.types.typeName] });
64
64
  }
65
65
  }
66
66
  const hasLinkedItems = variables.size > 0;
@@ -68,7 +68,7 @@ function generateModelBusinessLogicView({ model, meta }) {
68
68
  // NOTE: If we need to generate the linked item type, we need to import the enum types.
69
69
  for (const enumField of (0, fields_1.getEnumFields)(model)) {
70
70
  const enumMeta = (0, meta_1.getEnumMetadata)(enumField);
71
- imports.addTypeImport({ from: enumMeta.types.filePath, items: [enumField.typeName] });
71
+ imports.addTypeImport({ from: enumMeta.types.importPath, items: [enumField.typeName] });
72
72
  }
73
73
  }
74
74
  const linkedItemsGetterFn = `
@@ -25,7 +25,7 @@ function generateRepository({ model, meta }) {
25
25
  (0, types_1.toAnnotatedTypeName)(meta.types.dto.update),
26
26
  (0, types_1.toAnnotatedTypeName)(meta.types.dto.upsert),
27
27
  ],
28
- [schemaMeta.actions.importPath]: [schemaMeta.actions.actionExecution.interface],
28
+ [schemaMeta.actions.importPath]: [schemaMeta.actions.execution.interface],
29
29
  });
30
30
  // NOTE: We first generate different parts of the code responsible for a particular task
31
31
  // and then we combine them into the final code block.
@@ -990,42 +990,42 @@ function getRepositoryMethodsTypeSignatures({ model, meta }) {
990
990
  return {
991
991
  create: {
992
992
  jsDoc: [`Creates a new ${meta.userFriendlyName} and returns it.`],
993
- parameters: [`{item: ${meta.types.dto.create}, execution: ${schemaMeta.actions.actionExecution.interface}}`],
993
+ parameters: [`{item: ${meta.types.dto.create}, execution: ${schemaMeta.actions.execution.interface}}`],
994
994
  returnType: `Promise<${model.typeName}>`,
995
995
  },
996
996
  createMany: {
997
997
  jsDoc: [`Creates multiple new ${meta.userFriendlyNamePlural} and returns them.`],
998
- parameters: [`{items: ${meta.types.dto.create}[], execution: ${schemaMeta.actions.actionExecution.interface}}`],
998
+ parameters: [`{items: ${meta.types.dto.create}[], execution: ${schemaMeta.actions.execution.interface}}`],
999
999
  returnType: `Promise<${model.typeName}[]>`,
1000
1000
  },
1001
1001
  update: {
1002
1002
  jsDoc: [`Updates a ${meta.userFriendlyName} and returns it.`],
1003
- parameters: [`{item: ${meta.types.dto.update}, execution: ${schemaMeta.actions.actionExecution.interface}}`],
1003
+ parameters: [`{item: ${meta.types.dto.update}, execution: ${schemaMeta.actions.execution.interface}}`],
1004
1004
  returnType: `Promise<${model.typeName}>`,
1005
1005
  },
1006
1006
  updateMany: {
1007
1007
  jsDoc: [`Updates multiple ${meta.userFriendlyNamePlural} and returns them.`],
1008
- parameters: [`{items: ${meta.types.dto.update}[], execution: ${schemaMeta.actions.actionExecution.interface}}`],
1008
+ parameters: [`{items: ${meta.types.dto.update}[], execution: ${schemaMeta.actions.execution.interface}}`],
1009
1009
  returnType: `Promise<${model.typeName}[]>`,
1010
1010
  },
1011
1011
  upsert: {
1012
1012
  jsDoc: [`Creates or updates a ${meta.userFriendlyName} and returns it.`],
1013
- parameters: [`{item: ${meta.types.dto.upsert}, execution: ${schemaMeta.actions.actionExecution.interface}}`],
1013
+ parameters: [`{item: ${meta.types.dto.upsert}, execution: ${schemaMeta.actions.execution.interface}}`],
1014
1014
  returnType: `Promise<${model.typeName}>`,
1015
1015
  },
1016
1016
  upsertMany: {
1017
1017
  jsDoc: [`Creates or updates multiple ${meta.userFriendlyNamePlural} and returns them.`],
1018
- parameters: [`{items: ${meta.types.dto.upsert}[], execution: ${schemaMeta.actions.actionExecution.interface}}`],
1018
+ parameters: [`{items: ${meta.types.dto.upsert}[], execution: ${schemaMeta.actions.execution.interface}}`],
1019
1019
  returnType: `Promise<${model.typeName}[]>`,
1020
1020
  },
1021
1021
  delete: {
1022
1022
  jsDoc: [`Deletes a ${meta.userFriendlyName} and returns its id.`],
1023
- parameters: [`{id: ${model.brandedIdType}, execution: ${schemaMeta.actions.actionExecution.interface}}`],
1023
+ parameters: [`{id: ${model.brandedIdType}, execution: ${schemaMeta.actions.execution.interface}}`],
1024
1024
  returnType: `Promise<${model.brandedIdType}>`,
1025
1025
  },
1026
1026
  deleteMany: {
1027
1027
  jsDoc: [`Deletes multiple ${meta.userFriendlyNamePlural} and returns their ids.`],
1028
- parameters: [`{ids: ${model.brandedIdType}[], execution: ${schemaMeta.actions.actionExecution.interface}}`],
1028
+ parameters: [`{ids: ${model.brandedIdType}[], execution: ${schemaMeta.actions.execution.interface}}`],
1029
1029
  returnType: `Promise<${model.brandedIdType}[]>`,
1030
1030
  },
1031
1031
  };
@@ -93,7 +93,7 @@ export type SchemaMetaData = {
93
93
  * Path that may be used to import from the action module.
94
94
  */
95
95
  importPath: Types.BackendModulePath;
96
- actionExecution: {
96
+ execution: {
97
97
  /**
98
98
  * The name of the interface that handles the action execution.
99
99
  */
@@ -120,10 +120,24 @@ export type SchemaMetaData = {
120
120
  * The name of the interface that indicates a service can dispatch actions.
121
121
  */
122
122
  interface: Types.ClassName;
123
+ };
124
+ definition: {
125
+ /**
126
+ * The schema type to define action definitions.
127
+ */
128
+ schema: Types.TypeName;
129
+ /**
130
+ * The generated payload of the defined actions.
131
+ */
132
+ payload: Types.TypeName;
133
+ /**
134
+ * The generated result of the defined actions.
135
+ */
136
+ result: Types.TypeName;
123
137
  /**
124
- * The name of a util type that is used to create a dictionary of action payloads and results from the action descriptions.
138
+ * The transformer type that converts action definitions into results dictionary.
125
139
  */
126
- actionMethod: Types.TypeName;
140
+ resultDict: Types.TypeName;
127
141
  };
128
142
  };
129
143
  /**
package/dist/lib/meta.js CHANGED
@@ -81,7 +81,7 @@ function getSchemaMetadata({ config }) {
81
81
  actions: {
82
82
  moduleName: Types.toClassName(`ActionModule`),
83
83
  importPath: Types.toBackendModulePath(`@backend/actions`),
84
- actionExecution: {
84
+ execution: {
85
85
  interface: Types.toClassName(`IActionExecution`),
86
86
  class: Types.toClassName(`ActionExecution`),
87
87
  mock: Types.toClassName(`MockActionExecution`),
@@ -90,7 +90,12 @@ function getSchemaMetadata({ config }) {
90
90
  filePath: Types.toPath(`${config.paths.actionsPath}dispatcher.service`),
91
91
  class: Types.toClassName(`DispatcherService`),
92
92
  interface: Types.toClassName(`IDispatcher`),
93
- actionMethod: Types.toTypeName(`ActionMethod`),
93
+ },
94
+ definition: {
95
+ schema: Types.toTypeName('ActionDef'),
96
+ payload: Types.toTypeName('ActionDefPayload'),
97
+ result: Types.toTypeName('ActionDefResult'),
98
+ resultDict: Types.toTypeName('ActionDefResultDict'),
94
99
  },
95
100
  },
96
101
  businessLogic: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.56.1",
3
+ "version": "0.56.3",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {