@postxl/generator 0.56.0 → 0.56.2

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.
@@ -24,6 +24,7 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
24
24
  [meta.businessLogic.view.serviceFilePath]: [meta.businessLogic.view.serviceClassName],
25
25
  [schemaMeta.actions.importPath]: [
26
26
  schemaMeta.actions.actionExecution.interface,
27
+ schemaMeta.actions.dispatcher.result,
27
28
  schemaMeta.actions.dispatcher.interface,
28
29
  schemaMeta.actions.dispatcher.actionMethod,
29
30
  ],
@@ -54,6 +55,12 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
54
55
  `@Inject(forwardRef(() => ${schemaMeta.businessLogic.view.serviceClassName})) private readonly viewService: ${schemaMeta.businessLogic.view.serviceClassName}`,
55
56
  ];
56
57
  const { zodCreateObject, zodUpdateObject, zodUpsertObject } = meta.businessLogic.update;
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<${schemaMeta.actions.dispatcher.result}<Actions, AM>>`;
57
64
  return /* ts */ `
58
65
  import { Inject, Injectable, forwardRef } from '@nestjs/common'
59
66
  import { ExhaustiveSwitchCheck, UnionOmit } from '@backend/common'
@@ -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<AM extends ${dispatcher.actionMethod}<Scope, Actions>>({
158
165
  action,
159
166
  execution,
160
167
  }: {
161
- action: UnionOmit<A, 'result'>
168
+ action: UnionOmit<AM, 'result'>
162
169
  execution: ${schemaMeta.actions.actionExecution.interface}
163
- }): Promise<A['result']> {
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,17 +59,18 @@ function generateModelBusinessLogicView({ model, meta }) {
59
59
  variableDefinition: `const ${relationVariableName} = ${relationVariableDefinition}`,
60
60
  });
61
61
  if (relation.relationToModel.typeName !== model.typeName) {
62
- imports.addImport({
63
- items: [refMeta.types.toBrandedIdTypeFnName],
64
- from: refMeta.types.filePath,
65
- });
66
- imports.addTypeImport({
67
- items: [refModel.brandedIdType, refMeta.types.typeName],
68
- from: refMeta.types.filePath,
69
- });
62
+ imports.addImport({ from: refMeta.types.filePath, items: [refMeta.types.toBrandedIdTypeFnName] });
63
+ imports.addTypeImport({ from: refMeta.types.filePath, items: [refModel.brandedIdType, refMeta.types.typeName] });
70
64
  }
71
65
  }
72
66
  const hasLinkedItems = variables.size > 0;
67
+ if (hasLinkedItems) {
68
+ // NOTE: If we need to generate the linked item type, we need to import the enum types.
69
+ for (const enumField of (0, fields_1.getEnumFields)(model)) {
70
+ const enumMeta = (0, meta_1.getEnumMetadata)(enumField);
71
+ imports.addTypeImport({ from: enumMeta.types.filePath, items: [enumField.typeName] });
72
+ }
73
+ }
73
74
  const linkedItemsGetterFn = `
74
75
  /**
75
76
  * Returns the linked ${meta.userFriendlyName} with the given id or null if it does not exist.
@@ -124,6 +124,10 @@ export type SchemaMetaData = {
124
124
  * The name of a util type that is used to create a dictionary of action payloads and results from the action descriptions.
125
125
  */
126
126
  actionMethod: Types.TypeName;
127
+ /**
128
+ * The name of the type that represents the result of the action method.
129
+ */
130
+ result: Types.TypeName;
127
131
  };
128
132
  };
129
133
  /**
package/dist/lib/meta.js CHANGED
@@ -91,6 +91,7 @@ function getSchemaMetadata({ config }) {
91
91
  class: Types.toClassName(`DispatcherService`),
92
92
  interface: Types.toClassName(`IDispatcher`),
93
93
  actionMethod: Types.toTypeName(`ActionMethod`),
94
+ result: Types.toTypeName(`ActionMethodResult`),
94
95
  },
95
96
  },
96
97
  businessLogic: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/generator",
3
- "version": "0.56.0",
3
+ "version": "0.56.2",
4
4
  "main": "./dist/generator.js",
5
5
  "typings": "./dist/generator.d.ts",
6
6
  "bin": {