@alevnyacow/nzmt 0.2.2 → 0.2.4

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 (2) hide show
  1. package/bin/cli.js +51 -9
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -99,6 +99,7 @@ function createDefaultConfig() {
99
99
  providers: './src/server/providers',
100
100
  controllers: './src/server/controllers',
101
101
  entities: './src/shared/entities',
102
+ valueObjects: './src/shared/value-objects',
102
103
  queries: './src/client/shared/queries'
103
104
  },
104
105
  store: {
@@ -246,31 +247,33 @@ if (command === 'init') {
246
247
  process.exit(0)
247
248
  }
248
249
 
249
- function generateStores(lowerCase, upperCase) {
250
+ function generateStores(lowerCase, upperCase, withEntityPreset) {
250
251
  const folder = config?.paths?.stores ? path.resolve(process.cwd(), config?.paths?.stores, entityName) : path.resolve(process.cwd(), entityName);
251
252
 
252
253
  fs.mkdirSync(folder, { recursive: true })
253
254
 
255
+ const withEntity = withEntityPreset || (options ?? []).includes('with-entity')
256
+
254
257
  // Contract
255
258
 
256
259
  fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
257
260
  "import type { Store } from '@alevnyacow/nzmt'",
258
- config?.paths?.entities ? `import { ${upperCase} } from '${config?.paths?.entities.replace('./src', '@')}/${entityName}'` : undefined,
261
+ withEntity ? `import { ${upperCase} } from '${config?.paths?.entities.replace('./src', '@')}/${entityName}'` : undefined,
259
262
  "",
260
263
  `export const ${lowerCase}StoreMetadata = {`,
261
264
  "\tmodels: {",
262
- config?.paths?.entities ? `\t\tlist: ${upperCase}.schema,` : "\t\tlist: z.object({ }),",
263
- config?.paths?.entities ? `\t\tdetails: ${upperCase}.schema,` : "\t\tdetails: z.object({ }),",
265
+ withEntity ? `\t\tlist: ${upperCase}.schema,` : "\t\tlist: z.object({ }),",
266
+ withEntity ? `\t\tdetails: ${upperCase}.schema,` : "\t\tdetails: z.object({ }),",
264
267
  "\t},",
265
268
  "",
266
269
  "\tsearchPayload: {",
267
- config?.paths?.entities ? `\t\tlist: ${upperCase}.schema.omit({ id: true }),` : "\t\tlist: z.object({ }),",
268
- config?.paths?.entities ? `\t\tspecific: ${upperCase}.schema.pick({ id: true }),` : "\t\tspecific: z.object({ }),",
270
+ withEntity ? `\t\tlist: ${upperCase}.schema.omit({ id: true }),` : "\t\tlist: z.object({ }),",
271
+ withEntity ? `\t\tspecific: ${upperCase}.schema.pick({ id: true }),` : "\t\tspecific: z.object({ }),",
269
272
  "\t},",
270
273
  "",
271
274
  "\tactionsPayload: {",
272
- config?.paths?.entities ? `\t\tcreate: ${upperCase}.schema.omit({ id: true }),` : "\t\tcreate: z.object({ }),",
273
- config?.paths?.entities ? `\t\tupdate: ${upperCase}.schema.omit({ id: true }).partial(),` : "\t\tupdate: z.object({ }),",
275
+ withEntity ? `\t\tcreate: ${upperCase}.schema.omit({ id: true }),` : "\t\tcreate: z.object({ }),",
276
+ withEntity ? `\t\tupdate: ${upperCase}.schema.omit({ id: true }).partial(),` : "\t\tupdate: z.object({ }),",
274
277
  "\t},",
275
278
  "",
276
279
  `\tname: '${upperCase}Store'`,
@@ -474,6 +477,45 @@ if (command === 'entity') {
474
477
  process.exit(0)
475
478
  }
476
479
 
480
+ function generateValueObject(upperCase) {
481
+ const folder = config?.paths?.valueObjects ? path.resolve(process.cwd(), config?.paths?.valueObjects, entityName) : path.resolve(process.cwd(), entityName);
482
+
483
+ fs.mkdirSync(folder, { recursive: true })
484
+
485
+ const body = [
486
+ "import z from 'zod'",
487
+ "",
488
+ `export type ${upperCase}Model = z.infer<typeof ${upperCase}.schema>`,
489
+ "",
490
+ `export class ${upperCase} {`,
491
+ "\tstatic schema = z.object({",
492
+ "\t\t",
493
+ "\t})",
494
+ "\t",
495
+ `\tprivate constructor(private readonly data: ${upperCase}Model) {}`,
496
+ "\t",
497
+ `\tstatic create = (data: ${upperCase}Model) => {`,
498
+ `\t\tconst parsedModel = ${upperCase}.schema.parse(data)`,
499
+ `\t\treturn new ${upperCase}(parsedModel)`,
500
+ "\t}",
501
+ "\t",
502
+ `\tget model(): ${upperCase}Model {`,
503
+ "\t\treturn this.data",
504
+ "\t}",
505
+ "}"
506
+ ].join('\n')
507
+
508
+ fs.writeFileSync(path.resolve(folder, `${entityName}.value-object.ts`), body)
509
+ fs.writeFileSync(path.resolve(folder, 'index.ts'), `export * from './${entityName}.value-object'`)
510
+ }
511
+
512
+ if (command === 'value-object') {
513
+ var [lowerCase, upperCase] = camelizeVariants(entityName)
514
+ generateValueObject(upperCase)
515
+ process.exit(0)
516
+ }
517
+
518
+
477
519
  function generateService(lowerCase, upperCase, withCrud) {
478
520
  const folder = config?.paths?.services ? path.resolve(process.cwd(), config?.paths?.services, entityName) : path.resolve(process.cwd(), entityName);
479
521
 
@@ -632,7 +674,7 @@ if (command === 'service') {
632
674
  if (command === 'crud') {
633
675
  var [lowerCase, upperCase] = camelizeVariants(entityName)
634
676
  generateEntity(upperCase)
635
- generateStores(lowerCase, upperCase)
677
+ generateStores(lowerCase, upperCase, true)
636
678
  generateService(lowerCase, upperCase, true)
637
679
  process.exit(0)
638
680
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "repository": {
6
6
  "type": "git",