@postxl/generator 0.0.20 → 0.0.22

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.
Files changed (31) hide show
  1. package/README.md +2 -2
  2. package/dist/jest.config.js +17 -0
  3. package/dist/src/generator.js +13 -13
  4. package/dist/src/generators/enums/react.generator.js +55 -55
  5. package/dist/src/generators/enums/types.generator.js +8 -8
  6. package/dist/src/generators/indices/datamockmodule.generator.js +46 -46
  7. package/dist/src/generators/indices/datamodule.generator.js +76 -76
  8. package/dist/src/generators/indices/dataservice.generator.js +26 -26
  9. package/dist/src/generators/indices/repositories.generator.js +3 -3
  10. package/dist/src/generators/indices/testdataservice.generator.js +23 -22
  11. package/dist/src/generators/models/react.generator/context.generator.js +47 -47
  12. package/dist/src/generators/models/react.generator/index.js +8 -8
  13. package/dist/src/generators/models/react.generator/library.generator.js +66 -66
  14. package/dist/src/generators/models/react.generator/lookup.generator.js +75 -75
  15. package/dist/src/generators/models/react.generator/modals.generator.js +261 -261
  16. package/dist/src/generators/models/repository.generator.js +239 -239
  17. package/dist/src/generators/models/route.generator.js +45 -45
  18. package/dist/src/generators/models/seed.generator.js +14 -14
  19. package/dist/src/generators/models/stub.generator.js +19 -19
  20. package/dist/src/generators/models/types.generator.js +39 -39
  21. package/dist/src/lib/vfs.js +2 -2
  22. package/dist/tsconfig.tsbuildinfo +1 -1
  23. package/package.json +8 -2
  24. package/changelog.md +0 -115
  25. package/jest.config.ts +0 -18
  26. package/tests/attributes.test.ts +0 -91
  27. package/tests/file.test.ts +0 -32
  28. package/tests/schemas/la/la.prisma +0 -862
  29. package/tests/schemas/mca/mca.prisma +0 -528
  30. package/tests/utils/random.ts +0 -11
  31. package/tests/vfs.test.ts +0 -92
@@ -39,84 +39,84 @@ const imports_1 = require("../../../lib/imports");
39
39
  function generateModelCreateModalComponent({ model, meta }) {
40
40
  const { fields } = model;
41
41
  const { react: { components: { modals }, }, trpc, } = meta;
42
- return `
43
- ${getFormImports({ model, meta })}
44
-
45
- type CreateInputData = {
46
- ${getFormInputFields({ model, nullable: true })}
47
- }
48
-
49
- /**
50
- * A modal component that lets the user create a new ${meta.userFriendlyName} instance.
51
- */
52
- export const ${modals.createComponentName} = ({ show, onHide }: { show: boolean; onHide: () => void }) => {
53
- const cache = trpc.useContext()
54
- const mutation = trpc.${trpc.create.reactQueryMethod}.useMutation()
55
-
56
- return (
57
- <ModalWithNavigationContainer label="Create new ${meta.userFriendlyName}" show={show} onHide={onHide} fixed wide>
58
- <Formik<CreateInputData>
59
- initialValues={{
42
+ return `
43
+ ${getFormImports({ model, meta })}
44
+
45
+ type CreateInputData = {
46
+ ${getFormInputFields({ model, nullable: true })}
47
+ }
48
+
49
+ /**
50
+ * A modal component that lets the user create a new ${meta.userFriendlyName} instance.
51
+ */
52
+ export const ${modals.createComponentName} = ({ show, onHide }: { show: boolean; onHide: () => void }) => {
53
+ const cache = trpc.useContext()
54
+ const mutation = trpc.${trpc.create.reactQueryMethod}.useMutation()
55
+
56
+ return (
57
+ <ModalWithNavigationContainer label="Create new ${meta.userFriendlyName}" show={show} onHide={onHide} fixed wide>
58
+ <Formik<CreateInputData>
59
+ initialValues={{
60
60
  ${fields
61
61
  .filter((f) => f.kind !== 'id')
62
62
  .map((field) => `${getFormikFieldName(field.name)}: null,`)
63
- .join('\n')}
64
- }}
65
- validate={(values) => {
66
- const errors: FormikErrors<CreateInputData> = {}
67
-
68
- ${getFormikValidationCases({ model, includeId: false })}
69
-
70
- return errors
71
- }}
72
- onSubmit={async (values) => {
73
-
74
- try {
75
- const res = await toast.promise(
76
- mutation.mutateAsync(
77
- {
78
- ${getFormikMutationData({ model, includeId: false })}
79
- },
80
- {
81
- async onSuccess() {
82
- await cache.${trpc.getMap.reactQueryMethod}.invalidate()
83
- },
84
- },
85
- ),
86
- {
87
- loading: "Creating new ${meta.userFriendlyName}...",
88
- success: "New ${meta.userFriendlyName} created!",
89
- error: "Something went wrong...",
90
- },
91
- )
92
-
93
- if (res.id) {
94
- onHide()
95
- }
96
- } catch (err) {
97
- console.error(err)
98
- }
99
- }}
100
- >
101
- {({ isSubmitting, handleSubmit, isValid }) => (
102
- <ModalWithActions actions={
103
- <ConfirmButton
104
- color="primary"
105
- label="Create"
106
- fill="fill"
107
- onClick={handleSubmit}
108
- loading={isSubmitting}
109
- disabled={!isValid}
110
- />
111
- }>
112
- ${getFormFieldComponents({ model })}
113
- </ModalWithActions>
114
- )}
115
- </Formik>
116
- </ModalWithNavigationContainer>
117
- )
118
- }
119
-
63
+ .join('\n')}
64
+ }}
65
+ validate={(values) => {
66
+ const errors: FormikErrors<CreateInputData> = {}
67
+
68
+ ${getFormikValidationCases({ model, includeId: false })}
69
+
70
+ return errors
71
+ }}
72
+ onSubmit={async (values) => {
73
+
74
+ try {
75
+ const res = await toast.promise(
76
+ mutation.mutateAsync(
77
+ {
78
+ ${getFormikMutationData({ model, includeId: false })}
79
+ },
80
+ {
81
+ async onSuccess() {
82
+ await cache.${trpc.getMap.reactQueryMethod}.invalidate()
83
+ },
84
+ },
85
+ ),
86
+ {
87
+ loading: "Creating new ${meta.userFriendlyName}...",
88
+ success: "New ${meta.userFriendlyName} created!",
89
+ error: "Something went wrong...",
90
+ },
91
+ )
92
+
93
+ if (res.id) {
94
+ onHide()
95
+ }
96
+ } catch (err) {
97
+ console.error(err)
98
+ }
99
+ }}
100
+ >
101
+ {({ isSubmitting, handleSubmit, isValid }) => (
102
+ <ModalWithActions actions={
103
+ <ConfirmButton
104
+ color="primary"
105
+ label="Create"
106
+ fill="fill"
107
+ onClick={handleSubmit}
108
+ loading={isSubmitting}
109
+ disabled={!isValid}
110
+ />
111
+ }>
112
+ ${getFormFieldComponents({ model })}
113
+ </ModalWithActions>
114
+ )}
115
+ </Formik>
116
+ </ModalWithNavigationContainer>
117
+ )
118
+ }
119
+
120
120
  `;
121
121
  }
122
122
  exports.generateModelCreateModalComponent = generateModelCreateModalComponent;
@@ -126,30 +126,30 @@ exports.generateModelCreateModalComponent = generateModelCreateModalComponent;
126
126
  function generateEditModalModelComponent({ model, meta }) {
127
127
  const { fields } = model;
128
128
  const { react: { components }, trpc, } = meta;
129
- return `
130
- ${getFormImports({ model, meta })}
131
-
132
-
133
- type EditInputData = {
134
- ${getFormikFieldName(model.idField.name)}: ${model.brandedIdType}
135
- ${getFormInputFields({ model, nullable: false })}
136
- }
137
-
138
- /**
139
- * Modal component that may be used to edit a ${model.name} instance.
140
- */
141
- export const ${components.modals.editComponentName} = ({
142
- data,
143
- show,
144
- onHide
145
- }: { data: ${model.typeName}; show: boolean; onHide: () => void }) => {
146
- const cache = trpc.useContext()
147
- const mutation = trpc.${trpc.update.reactQueryMethod}.useMutation()
148
-
149
- return (
150
- <ModalWithNavigationContainer label="Edit ${meta.userFriendlyName}" show={show} onHide={onHide} fixed wide>
151
- <Formik<EditInputData>
152
- initialValues={{
129
+ return `
130
+ ${getFormImports({ model, meta })}
131
+
132
+
133
+ type EditInputData = {
134
+ ${getFormikFieldName(model.idField.name)}: ${model.brandedIdType}
135
+ ${getFormInputFields({ model, nullable: false })}
136
+ }
137
+
138
+ /**
139
+ * Modal component that may be used to edit a ${model.name} instance.
140
+ */
141
+ export const ${components.modals.editComponentName} = ({
142
+ data,
143
+ show,
144
+ onHide
145
+ }: { data: ${model.typeName}; show: boolean; onHide: () => void }) => {
146
+ const cache = trpc.useContext()
147
+ const mutation = trpc.${trpc.update.reactQueryMethod}.useMutation()
148
+
149
+ return (
150
+ <ModalWithNavigationContainer label="Edit ${meta.userFriendlyName}" show={show} onHide={onHide} fixed wide>
151
+ <Formik<EditInputData>
152
+ initialValues={{
153
153
  ${fields
154
154
  .map((field) => {
155
155
  switch (field.kind) {
@@ -166,60 +166,60 @@ export const ${components.modals.editComponentName} = ({
166
166
  (0, assert_never_1.default)(field);
167
167
  }
168
168
  })
169
- .join('\n')}
170
- }}
171
- validate={(values) => {
172
- const errors: { [K in keyof EditInputData]?: string } = {}
173
-
174
- ${getFormikValidationCases({ model, includeId: true })}
175
-
176
- return errors
177
- }}
178
- onSubmit={async (values) => {
179
- try {
180
- await toast.promise(
181
- mutation.mutateAsync(
182
- {
183
- ${getFormikMutationData({ model, includeId: true })}
184
- },
185
- {
186
- async onSuccess() {
187
- await cache.${trpc.getMap.reactQueryMethod}.invalidate()
188
- },
189
- },
190
- ),
191
- {
192
- loading: "Updating ${meta.userFriendlyName}...",
193
- success: "${meta.userFriendlyName} updated!",
194
- error: "Something went wrong...",
195
- },
196
- )
197
-
198
- onHide()
199
- } catch (err) {
200
- console.error(err)
201
- }
202
- }}
203
- >
204
- {({ isSubmitting, isValid, handleSubmit }) => (
205
- <ModalWithActions
206
- actions={
207
- <ConfirmButton
208
- label="Save"
209
- color="primary"
210
- onClick={handleSubmit}
211
- loading={isSubmitting}
212
- disabled={!isValid}
213
- />
214
- }
215
- >
216
- ${getFormFieldComponents({ model })}
217
- </ModalWithActions>
218
- )}
219
- </Formik>
220
- </ModalWithNavigationContainer>
221
- )
222
- }
169
+ .join('\n')}
170
+ }}
171
+ validate={(values) => {
172
+ const errors: { [K in keyof EditInputData]?: string } = {}
173
+
174
+ ${getFormikValidationCases({ model, includeId: true })}
175
+
176
+ return errors
177
+ }}
178
+ onSubmit={async (values) => {
179
+ try {
180
+ await toast.promise(
181
+ mutation.mutateAsync(
182
+ {
183
+ ${getFormikMutationData({ model, includeId: true })}
184
+ },
185
+ {
186
+ async onSuccess() {
187
+ await cache.${trpc.getMap.reactQueryMethod}.invalidate()
188
+ },
189
+ },
190
+ ),
191
+ {
192
+ loading: "Updating ${meta.userFriendlyName}...",
193
+ success: "${meta.userFriendlyName} updated!",
194
+ error: "Something went wrong...",
195
+ },
196
+ )
197
+
198
+ onHide()
199
+ } catch (err) {
200
+ console.error(err)
201
+ }
202
+ }}
203
+ >
204
+ {({ isSubmitting, isValid, handleSubmit }) => (
205
+ <ModalWithActions
206
+ actions={
207
+ <ConfirmButton
208
+ label="Save"
209
+ color="primary"
210
+ onClick={handleSubmit}
211
+ loading={isSubmitting}
212
+ disabled={!isValid}
213
+ />
214
+ }
215
+ >
216
+ ${getFormFieldComponents({ model })}
217
+ </ModalWithActions>
218
+ )}
219
+ </Formik>
220
+ </ModalWithNavigationContainer>
221
+ )
222
+ }
223
223
  `;
224
224
  }
225
225
  exports.generateEditModalModelComponent = generateEditModalModelComponent;
@@ -232,62 +232,62 @@ function generateDeleteModalModelComponent({ model, meta }) {
232
232
  items: [model.brandedIdType],
233
233
  from: meta.types.importPath,
234
234
  });
235
- return `
236
- import { Formik, FormikErrors } from 'formik'
237
- import React, { useCallback } from 'react'
238
- import { toast } from 'react-hot-toast'
239
-
240
- import { ConfirmationModal } from '@components/atoms/Modal'
241
- import { trpc } from '@lib/trpc'
242
-
243
- ${imports.generate()}
244
-
245
- /**
246
- * Modal components that shows a confirmation modal before deleting a ${meta.userFriendlyName}.
247
- */
248
- export const ${components.modals.deleteComponentName} = ({
249
- id,
250
- show,
251
- onHide
252
- }: {
253
- id: ${model.brandedIdType};
254
- show: boolean;
255
- onHide: () => void
256
- }) => {
257
- const cache = trpc.useContext()
258
- const mutation = trpc.${trpc.delete.reactQueryMethod}.useMutation()
259
-
260
- const handleDelete = useCallback(async () => {
261
- try {
262
- await toast.promise(
263
- mutation.mutateAsync(id, {
264
- async onSuccess() {
265
- await cache.${trpc.getMap.reactQueryMethod}.invalidate()
266
- },
267
- }),
268
- {
269
- loading: "Deleting ${meta.userFriendlyName}...",
270
- success: "${meta.userFriendlyName} deleted!",
271
- error: "Something went wrong...",
272
- },
273
- )
274
- } catch (err) {
275
- console.error(err)
276
- }
277
- }, [mutation, id, cache])
278
-
279
- return (
280
- <ConfirmationModal
281
- show={show}
282
- onHide={onHide}
283
- onConfirm={handleDelete}
284
- label="Delete ${meta.userFriendlyName}"
285
- description="Please confirm that you want to delete this ${meta.userFriendlyName}."
286
- variant="danger"
287
- loading={mutation.isLoading}
288
- />
289
- )
290
- }
235
+ return `
236
+ import { Formik, FormikErrors } from 'formik'
237
+ import React, { useCallback } from 'react'
238
+ import { toast } from 'react-hot-toast'
239
+
240
+ import { ConfirmationModal } from '@components/atoms/Modal'
241
+ import { trpc } from '@lib/trpc'
242
+
243
+ ${imports.generate()}
244
+
245
+ /**
246
+ * Modal components that shows a confirmation modal before deleting a ${meta.userFriendlyName}.
247
+ */
248
+ export const ${components.modals.deleteComponentName} = ({
249
+ id,
250
+ show,
251
+ onHide
252
+ }: {
253
+ id: ${model.brandedIdType};
254
+ show: boolean;
255
+ onHide: () => void
256
+ }) => {
257
+ const cache = trpc.useContext()
258
+ const mutation = trpc.${trpc.delete.reactQueryMethod}.useMutation()
259
+
260
+ const handleDelete = useCallback(async () => {
261
+ try {
262
+ await toast.promise(
263
+ mutation.mutateAsync(id, {
264
+ async onSuccess() {
265
+ await cache.${trpc.getMap.reactQueryMethod}.invalidate()
266
+ },
267
+ }),
268
+ {
269
+ loading: "Deleting ${meta.userFriendlyName}...",
270
+ success: "${meta.userFriendlyName} deleted!",
271
+ error: "Something went wrong...",
272
+ },
273
+ )
274
+ } catch (err) {
275
+ console.error(err)
276
+ }
277
+ }, [mutation, id, cache])
278
+
279
+ return (
280
+ <ConfirmationModal
281
+ show={show}
282
+ onHide={onHide}
283
+ onConfirm={handleDelete}
284
+ label="Delete ${meta.userFriendlyName}"
285
+ description="Please confirm that you want to delete this ${meta.userFriendlyName}."
286
+ variant="danger"
287
+ loading={mutation.isLoading}
288
+ />
289
+ )
290
+ }
291
291
  `;
292
292
  }
293
293
  exports.generateDeleteModalModelComponent = generateDeleteModalModelComponent;
@@ -322,20 +322,20 @@ function getFormImports({ model, meta }) {
322
322
  from: enumMeta.react.folderPath,
323
323
  });
324
324
  }
325
- return `
326
- import React from 'react'
327
- import { toast } from 'react-hot-toast'
328
- import { Formik, FormikErrors } from 'formik'
329
-
330
- import { CheckBoxField } from '@components/atoms/CheckBoxInput'
331
- import { ConfirmButton, ModalWithNavigationContainer, Label, ModalWithActions } from '@components/atoms/Modal'
332
- import { TextAreaField } from '@components/atoms/TextAreaInput'
333
- import { TextField } from '@components/atoms/TextInput'
334
- import { NumberField } from '@components/atoms/NumberInput'
335
-
336
- import { trpc } from '@lib/trpc'
337
-
338
- ${imports.generate()}
325
+ return `
326
+ import React from 'react'
327
+ import { toast } from 'react-hot-toast'
328
+ import { Formik, FormikErrors } from 'formik'
329
+
330
+ import { CheckBoxField } from '@components/atoms/CheckBoxInput'
331
+ import { ConfirmButton, ModalWithNavigationContainer, Label, ModalWithActions } from '@components/atoms/Modal'
332
+ import { TextAreaField } from '@components/atoms/TextAreaInput'
333
+ import { TextField } from '@components/atoms/TextInput'
334
+ import { NumberField } from '@components/atoms/NumberInput'
335
+
336
+ import { trpc } from '@lib/trpc'
337
+
338
+ ${imports.generate()}
339
339
  `;
340
340
  }
341
341
  /**
@@ -395,11 +395,11 @@ function getFormFieldComponents({ model }) {
395
395
  // Each scalar field has a different generic component based on the provided type.
396
396
  scalar: switch (field.typeName) {
397
397
  case 'string':
398
- form.append(`
399
- <div>
400
- <Label>${label}</Label>
401
- <TextField placeholder="Type..." name="${formikFieldName}" />
402
- </div>
398
+ form.append(`
399
+ <div>
400
+ <Label>${label}</Label>
401
+ <TextField placeholder="Type..." name="${formikFieldName}" />
402
+ </div>
403
403
  `);
404
404
  break scalar;
405
405
  case 'number':
@@ -407,19 +407,19 @@ function getFormFieldComponents({ model }) {
407
407
  if (((_a = field.validation) === null || _a === void 0 ? void 0 : _a.type) === 'float') {
408
408
  decimals = 2;
409
409
  }
410
- form.append(`
411
- <div>
412
- <Label>${label}</Label>
413
- <NumberField placeholder="2511" name="${formikFieldName}" decimals={${decimals}}/>
414
- </div>
410
+ form.append(`
411
+ <div>
412
+ <Label>${label}</Label>
413
+ <NumberField placeholder="2511" name="${formikFieldName}" decimals={${decimals}}/>
414
+ </div>
415
415
  `);
416
416
  break scalar;
417
417
  case 'boolean':
418
- form.append(`
419
- <div>
420
- <Label>Is ${label}</Label>
421
- <CheckBoxField label="${label}" name="${formikFieldName}" />
422
- </div>
418
+ form.append(`
419
+ <div>
420
+ <Label>Is ${label}</Label>
421
+ <CheckBoxField label="${label}" name="${formikFieldName}" />
422
+ </div>
423
423
  `);
424
424
  break scalar;
425
425
  case 'Date':
@@ -433,21 +433,21 @@ function getFormFieldComponents({ model }) {
433
433
  }
434
434
  case 'relation': {
435
435
  const refMeta = (0, meta_1.getModelMetadata)({ model: field.relationToModel });
436
- form.append(`
437
- <div>
438
- <Label>${refMeta.userFriendlyName}</Label>
439
- <${refMeta.react.components.forms.searchFieldName} name="${formikFieldName}" placeholder="Search..." />
440
- </div>
436
+ form.append(`
437
+ <div>
438
+ <Label>${refMeta.userFriendlyName}</Label>
439
+ <${refMeta.react.components.forms.searchFieldName} name="${formikFieldName}" placeholder="Search..." />
440
+ </div>
441
441
  `);
442
442
  break;
443
443
  }
444
444
  case 'enum': {
445
445
  const enumMeta = (0, meta_1.getEnumMetadata)({ enumerator: field.enumerator });
446
- form.append(`
447
- <div>
448
- <Label>${label}</Label>
449
- <${enumMeta.react.selectFieldName} name="${formikFieldName}" placeholder="Search..." />
450
- </div>
446
+ form.append(`
447
+ <div>
448
+ <Label>${label}</Label>
449
+ <${enumMeta.react.selectFieldName} name="${formikFieldName}" placeholder="Search..." />
450
+ </div>
451
451
  `);
452
452
  break;
453
453
  }
@@ -467,10 +467,10 @@ function getFormikValidationCases({ model, includeId }) {
467
467
  continue;
468
468
  const formikFieldName = getFormikFieldName(field.name);
469
469
  if (field.isRequired) {
470
- form.append(`
471
- if (!values.${formikFieldName}) {
472
- errors.${formikFieldName} = "Required..."
473
- }
470
+ form.append(`
471
+ if (!values.${formikFieldName}) {
472
+ errors.${formikFieldName} = "Required..."
473
+ }
474
474
  `);
475
475
  }
476
476
  }
@@ -487,25 +487,25 @@ function getFormikMutationData({ model: { fields }, includeId }) {
487
487
  return '';
488
488
  }
489
489
  if (field.isRequired) {
490
- return `
491
- // NOTE: We force unwrap values, because we validate values using Formik.
492
- ${field.name}: values.${formikFieldName}!,
490
+ return `
491
+ // NOTE: We force unwrap values, because we validate values using Formik.
492
+ ${field.name}: values.${formikFieldName}!,
493
493
  `;
494
494
  }
495
495
  return `${field.name}: values.${formikFieldName},`;
496
496
  case 'relation':
497
497
  if (field.isRequired) {
498
- return `
499
- // NOTE: We force unwrap values, because we validate values using Formik.
500
- ${field.name}: values.${formikFieldName}!.id,
498
+ return `
499
+ // NOTE: We force unwrap values, because we validate values using Formik.
500
+ ${field.name}: values.${formikFieldName}!.id,
501
501
  `;
502
502
  }
503
503
  return `${field.name}: values.${formikFieldName}?.id ? values.${formikFieldName}!.id : null,`;
504
504
  case 'enum':
505
505
  if (field.isRequired) {
506
- return `
507
- // NOTE: We force unwrap values, because we validate values using Formik.
508
- ${field.name}: values.${formikFieldName}!.id,
506
+ return `
507
+ // NOTE: We force unwrap values, because we validate values using Formik.
508
+ ${field.name}: values.${formikFieldName}!.id,
509
509
  `;
510
510
  }
511
511
  return `${field.name}: values.${formikFieldName}?.id ? values.${formikFieldName}!.id : null,`;