@postxl/generator 0.64.0 → 0.65.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.
|
@@ -74,7 +74,7 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
|
|
|
74
74
|
[schemaMeta.view.serviceLocation.import]: schemaMeta.view.serviceClassName,
|
|
75
75
|
});
|
|
76
76
|
for (const relation of (0, fields_1.getRelationFields)(model)) {
|
|
77
|
-
// NOTE: We add
|
|
77
|
+
// NOTE: We add `toBrandedType` functions for foreign models for decoders.
|
|
78
78
|
if (relation.relationToModel.typeName === model.typeName) {
|
|
79
79
|
continue;
|
|
80
80
|
}
|
|
@@ -84,9 +84,6 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
|
|
|
84
84
|
from: refMeta.types.importPath,
|
|
85
85
|
});
|
|
86
86
|
}
|
|
87
|
-
for (const { referencingModel } of model.references) {
|
|
88
|
-
imports.addTypeImport({ items: [referencingModel.brandedIdType], from: meta.types.importPath });
|
|
89
|
-
}
|
|
90
87
|
/**
|
|
91
88
|
* The name of the variable that holds the repository instance for the current model
|
|
92
89
|
* (e.g. when we generate business logic service for Aggregation, the AggregationRepository
|
|
@@ -100,6 +97,8 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
|
|
|
100
97
|
];
|
|
101
98
|
const decoders = meta.update.decoders;
|
|
102
99
|
const { view, update } = meta;
|
|
100
|
+
const deleteFn = generateDeleteFunction({ model, meta, m });
|
|
101
|
+
const deleteManyFn = generateDeleteManyMethod({ imports, model, meta, m });
|
|
103
102
|
/* prettier-ignore */
|
|
104
103
|
return /* ts */ `
|
|
105
104
|
import { Inject, Injectable, forwardRef } from '@nestjs/common'
|
|
@@ -116,48 +115,24 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
|
|
|
116
115
|
result: ${m.typeName}
|
|
117
116
|
}
|
|
118
117
|
|
|
119
|
-
${(0, jsdoc_1.toJsDocComment)([`Creates multiple new ${meta.userFriendlyNamePlural} and returns them.`])}
|
|
120
|
-
createMany: {
|
|
121
|
-
payload: ${m.decoders.createType}[]
|
|
122
|
-
result: ${m.typeName}[]
|
|
123
|
-
}
|
|
124
|
-
|
|
125
118
|
${(0, jsdoc_1.toJsDocComment)([`Updates a ${meta.userFriendlyName} and returns it.`])}
|
|
126
119
|
update: {
|
|
127
120
|
payload: ${m.decoders.updateType}
|
|
128
121
|
result: ${m.typeName}
|
|
129
122
|
}
|
|
130
123
|
|
|
131
|
-
${(0, jsdoc_1.toJsDocComment)([`Updates multiple ${meta.userFriendlyNamePlural} and returns them.`])}
|
|
132
|
-
updateMany: {
|
|
133
|
-
payload: ${m.decoders.updateType}[]
|
|
134
|
-
result: ${m.typeName}[]
|
|
135
|
-
}
|
|
136
|
-
|
|
137
124
|
${(0, jsdoc_1.toJsDocComment)([`Creates or updates a ${meta.userFriendlyName} and returns it.`])}
|
|
138
125
|
upsert: {
|
|
139
126
|
payload: ${m.decoders.upsertType}
|
|
140
127
|
result: ${m.typeName}
|
|
141
128
|
}
|
|
142
129
|
|
|
143
|
-
${(0, jsdoc_1.toJsDocComment)([`Creates or updates multiple ${meta.userFriendlyNamePlural} and returns them.`])}
|
|
144
|
-
upsertMany: {
|
|
145
|
-
payload: ${m.decoders.upsertType}[]
|
|
146
|
-
result: ${m.typeName}[]
|
|
147
|
-
}
|
|
148
|
-
|
|
149
130
|
${(0, jsdoc_1.toJsDocComment)([`Deletes a ${meta.userFriendlyName} and returns its id.`])}
|
|
150
131
|
delete: {
|
|
151
132
|
payload: ${m.brandedId}
|
|
152
133
|
result: ${m.brandedId}
|
|
153
134
|
}
|
|
154
135
|
|
|
155
|
-
${(0, jsdoc_1.toJsDocComment)([`Deletes multiple ${meta.userFriendlyNamePlural} and returns their ids.`])}
|
|
156
|
-
deleteMany: {
|
|
157
|
-
payload: ${m.brandedId}[]
|
|
158
|
-
result: ${m.brandedId}[]
|
|
159
|
-
}
|
|
160
|
-
|
|
161
136
|
${(0, jsdoc_1.toJsDocComment)([`Clones a ${meta.userFriendlyName} and returns the clone.`])}
|
|
162
137
|
clone: {
|
|
163
138
|
payload: ${m.decoders.cloneType}
|
|
@@ -234,35 +209,19 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
|
|
|
234
209
|
return this.data.create({ item: data, execution })
|
|
235
210
|
}
|
|
236
211
|
|
|
237
|
-
${(0, jsdoc_1.toJsDocComment)([`Creates multiple new ${meta.userFriendlyNamePlural} and returns them.`])}
|
|
238
|
-
public async createMany({ data, execution }: { data: ${m.decoders.createType}[]; execution: ${m.iExecution} }): Promise<${m.typeName}[]> {
|
|
239
|
-
return this.data.createMany({ items: data, execution })
|
|
240
|
-
}
|
|
241
|
-
|
|
242
212
|
${(0, jsdoc_1.toJsDocComment)([`Updates a ${meta.userFriendlyName} and returns it.`])}
|
|
243
213
|
public async update({ data, execution }: { data: ${m.decoders.updateType}; execution: ${m.iExecution} }): Promise<${m.typeName}> {
|
|
244
214
|
return this.data.update({ item: data, execution })
|
|
245
215
|
}
|
|
246
216
|
|
|
247
|
-
${(0, jsdoc_1.toJsDocComment)([`Updates multiple ${meta.userFriendlyNamePlural} and returns them.`])}
|
|
248
|
-
public async updateMany({ data, execution }: { data: ${m.decoders.updateType}[]; execution: ${m.iExecution} }): Promise<${m.typeName}[]> {
|
|
249
|
-
return this.data.updateMany({ items: data, execution })
|
|
250
|
-
}
|
|
251
|
-
|
|
252
217
|
${(0, jsdoc_1.toJsDocComment)([`Creates or updates a ${meta.userFriendlyName} and returns it.`])}
|
|
253
218
|
public async upsert({ data, execution }: { data: ${m.decoders.upsertType}; execution: ${m.iExecution} }): Promise<${m.typeName}> {
|
|
254
219
|
return this.data.upsert({ item: data, execution })
|
|
255
220
|
}
|
|
256
221
|
|
|
257
|
-
${
|
|
258
|
-
public async upsertMany({ data, execution }: { data: ${m.decoders.upsertType}[]; execution: ${m.iExecution} }): Promise<${m.typeName}[]> {
|
|
259
|
-
return this.data.upsertMany({ items: data, execution })
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
${generateDeleteFunction({ model, meta, m })}
|
|
222
|
+
${deleteFn}
|
|
264
223
|
|
|
265
|
-
${
|
|
224
|
+
${deleteManyFn}
|
|
266
225
|
|
|
267
226
|
${generateCloneMethod({ model, meta, m })}
|
|
268
227
|
}
|
|
@@ -303,11 +262,16 @@ function generateDeleteFunction({ model, meta, m }) {
|
|
|
303
262
|
/**
|
|
304
263
|
* Returns a function that deletes multiple entities and all their related entities.
|
|
305
264
|
*/
|
|
306
|
-
function generateDeleteManyMethod({ model, meta, m }) {
|
|
265
|
+
function generateDeleteManyMethod({ imports, model, meta, m, }) {
|
|
307
266
|
const idArrays = [];
|
|
308
267
|
const idAssignments = [];
|
|
309
268
|
const deleteCalls = [];
|
|
310
269
|
for (const { referencingField, referencingModel } of model.references) {
|
|
270
|
+
// NOTE: We only delete back references that are required references.
|
|
271
|
+
if (!referencingField.isRequired) {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
imports.addTypeImport({ items: [referencingModel.brandedIdType], from: meta.types.importPath });
|
|
311
275
|
const refModelMeta = (0, meta_1.getModelMetadata)({ model: referencingModel });
|
|
312
276
|
const refFieldMeta = (0, meta_1.getFieldMetadata)({ field: referencingField });
|
|
313
277
|
const idArray = `${refModelMeta.internalSingularName}${(0, string_1.capitalize)(referencingField.name)}s`;
|
|
@@ -322,7 +286,7 @@ function generateDeleteManyMethod({ model, meta, m }) {
|
|
|
322
286
|
deleteCalls.push(`await this.updateService.${refModelMeta.update.serviceVariableName}.deleteMany({ data: ${idArray}, execution })`);
|
|
323
287
|
}
|
|
324
288
|
let relatedEntities = '';
|
|
325
|
-
if (
|
|
289
|
+
if (idArrays.length > 0) {
|
|
326
290
|
relatedEntities = `
|
|
327
291
|
${idArrays.join('\n')}
|
|
328
292
|
|
|
@@ -64,34 +64,18 @@ export const ${meta.trpc.routerName} = router({
|
|
|
64
64
|
.input(${decoders.name}.create)
|
|
65
65
|
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "create", payload: input})),
|
|
66
66
|
|
|
67
|
-
createMany: procedure
|
|
68
|
-
.input(z.array(${decoders.name}.create))
|
|
69
|
-
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "createMany", payload: input})),
|
|
70
|
-
|
|
71
67
|
update: procedure
|
|
72
68
|
.input(${decoders.name}.update)
|
|
73
69
|
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "update", payload: input})),
|
|
74
70
|
|
|
75
|
-
updateMany: procedure
|
|
76
|
-
.input(z.array(${decoders.name}.update))
|
|
77
|
-
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "updateMany", payload: input})),
|
|
78
|
-
|
|
79
71
|
upsert: procedure
|
|
80
72
|
.input(${decoders.name}.upsert)
|
|
81
73
|
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "upsert", payload: input})),
|
|
82
74
|
|
|
83
|
-
upsertMany: procedure
|
|
84
|
-
.input(z.array(${decoders.name}.upsert))
|
|
85
|
-
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "upsertMany", payload: input})),
|
|
86
|
-
|
|
87
75
|
delete: procedure
|
|
88
76
|
.input(${meta.types.zodDecoderFnNames.id})
|
|
89
77
|
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "delete", payload: input})),
|
|
90
78
|
|
|
91
|
-
deleteMany: procedure
|
|
92
|
-
.input(z.array(${meta.types.zodDecoderFnNames.id}))
|
|
93
|
-
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "deleteMany", payload: input})),
|
|
94
|
-
|
|
95
79
|
clone: procedure
|
|
96
80
|
.input(${decoders.name}.clone)
|
|
97
81
|
.mutation(({ input, ctx }) => ctx.dispatch({scope: "${scopeName}", type: "clone", payload: input})),
|