@postxl/generator 0.63.5 → 0.65.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.
|
@@ -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'
|
|
@@ -259,10 +258,9 @@ function generateModelBusinessLogicUpdate({ model, meta }) {
|
|
|
259
258
|
return this.data.upsertMany({ items: data, execution })
|
|
260
259
|
}
|
|
261
260
|
|
|
262
|
-
|
|
263
|
-
${generateDeleteFunction({ model, meta, m })}
|
|
261
|
+
${deleteFn}
|
|
264
262
|
|
|
265
|
-
${
|
|
263
|
+
${deleteManyFn}
|
|
266
264
|
|
|
267
265
|
${generateCloneMethod({ model, meta, m })}
|
|
268
266
|
}
|
|
@@ -276,6 +274,10 @@ function generateDeleteFunction({ model, meta, m }) {
|
|
|
276
274
|
const backReferenceDelete = [];
|
|
277
275
|
const backReferenceNames = [];
|
|
278
276
|
for (const { referencingField, referencingModel } of model.references) {
|
|
277
|
+
// NOTE: We only delete back references that are required references.
|
|
278
|
+
if (!referencingField.isRequired) {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
279
281
|
const refModelMeta = (0, meta_1.getModelMetadata)({ model: referencingModel });
|
|
280
282
|
const refFieldMeta = (0, meta_1.getFieldMetadata)({ field: referencingField });
|
|
281
283
|
const ids = `${refModelMeta.internalSingularName}${(0, string_1.capitalize)(referencingField.name)}s`;
|
|
@@ -299,11 +301,16 @@ function generateDeleteFunction({ model, meta, m }) {
|
|
|
299
301
|
/**
|
|
300
302
|
* Returns a function that deletes multiple entities and all their related entities.
|
|
301
303
|
*/
|
|
302
|
-
function generateDeleteManyMethod({ model, meta, m }) {
|
|
304
|
+
function generateDeleteManyMethod({ imports, model, meta, m, }) {
|
|
303
305
|
const idArrays = [];
|
|
304
306
|
const idAssignments = [];
|
|
305
307
|
const deleteCalls = [];
|
|
306
308
|
for (const { referencingField, referencingModel } of model.references) {
|
|
309
|
+
// NOTE: We only delete back references that are required references.
|
|
310
|
+
if (!referencingField.isRequired) {
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
imports.addTypeImport({ items: [referencingModel.brandedIdType], from: meta.types.importPath });
|
|
307
314
|
const refModelMeta = (0, meta_1.getModelMetadata)({ model: referencingModel });
|
|
308
315
|
const refFieldMeta = (0, meta_1.getFieldMetadata)({ field: referencingField });
|
|
309
316
|
const idArray = `${refModelMeta.internalSingularName}${(0, string_1.capitalize)(referencingField.name)}s`;
|
|
@@ -318,7 +325,7 @@ function generateDeleteManyMethod({ model, meta, m }) {
|
|
|
318
325
|
deleteCalls.push(`await this.updateService.${refModelMeta.update.serviceVariableName}.deleteMany({ data: ${idArray}, execution })`);
|
|
319
326
|
}
|
|
320
327
|
let relatedEntities = '';
|
|
321
|
-
if (
|
|
328
|
+
if (idArrays.length > 0) {
|
|
322
329
|
relatedEntities = `
|
|
323
330
|
${idArrays.join('\n')}
|
|
324
331
|
|
|
@@ -101,7 +101,7 @@ export const ${modals.createComponentName} = ({
|
|
|
101
101
|
<Typed.Formik
|
|
102
102
|
initialValues={{ ...INITIAL_VALUES, ...data }}
|
|
103
103
|
validate={(values) => {
|
|
104
|
-
const errors:
|
|
104
|
+
const errors: { [K in keyof CreateInputData]?: string } = {}
|
|
105
105
|
|
|
106
106
|
${getFormikValidationCases({ model })}
|
|
107
107
|
|
|
@@ -311,7 +311,9 @@ function generateDeleteModalModelComponent({ model, meta }) {
|
|
|
311
311
|
});
|
|
312
312
|
return `
|
|
313
313
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
314
|
-
import {
|
|
314
|
+
import { TRPCClientError } from '@trpc/client'
|
|
315
|
+
|
|
316
|
+
import { Formik } from 'formik'
|
|
315
317
|
import React, { useCallback } from 'react'
|
|
316
318
|
import { toast } from 'react-hot-toast'
|
|
317
319
|
|
|
@@ -346,7 +348,12 @@ export const ${components.modals.deleteComponentName} = ({
|
|
|
346
348
|
{
|
|
347
349
|
loading: "Deleting ${meta.userFriendlyName}...",
|
|
348
350
|
success: "${meta.userFriendlyName} deleted!",
|
|
349
|
-
error:
|
|
351
|
+
error: (err) => {
|
|
352
|
+
if (err instanceof TRPCClientError) {
|
|
353
|
+
return err.message
|
|
354
|
+
}
|
|
355
|
+
return 'Something went wrong...'
|
|
356
|
+
},
|
|
350
357
|
},
|
|
351
358
|
)
|
|
352
359
|
} catch (err) {
|
|
@@ -404,7 +411,6 @@ function getFormImports({ model, meta }) {
|
|
|
404
411
|
import { TRPCClientError } from '@trpc/client'
|
|
405
412
|
import React from 'react'
|
|
406
413
|
import { toast } from 'react-hot-toast'
|
|
407
|
-
import { FormikErrors } from 'formik'
|
|
408
414
|
|
|
409
415
|
import { createTypedForm } from '@components/atoms/Form'
|
|
410
416
|
import { ConfirmButton, ModalWithNavigationContainer, Label, ModalWithActions } from '@components/atoms/Modal'
|