@alevnyacow/nzmt 0.2.3 → 0.3.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.
Files changed (2) hide show
  1. package/bin/cli.js +23 -11
  2. package/package.json +1 -1
package/bin/cli.js CHANGED
@@ -247,31 +247,33 @@ if (command === 'init') {
247
247
  process.exit(0)
248
248
  }
249
249
 
250
- function generateStores(lowerCase, upperCase) {
250
+ function generateStores(lowerCase, upperCase, withEntityPreset) {
251
251
  const folder = config?.paths?.stores ? path.resolve(process.cwd(), config?.paths?.stores, entityName) : path.resolve(process.cwd(), entityName);
252
252
 
253
253
  fs.mkdirSync(folder, { recursive: true })
254
254
 
255
+ const withEntity = withEntityPreset || (options ?? []).includes('with-entity')
256
+
255
257
  // Contract
256
258
 
257
259
  fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
258
260
  "import type { Store } from '@alevnyacow/nzmt'",
259
- 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,
260
262
  "",
261
263
  `export const ${lowerCase}StoreMetadata = {`,
262
264
  "\tmodels: {",
263
- config?.paths?.entities ? `\t\tlist: ${upperCase}.schema,` : "\t\tlist: z.object({ }),",
264
- 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({ }),",
265
267
  "\t},",
266
268
  "",
267
269
  "\tsearchPayload: {",
268
- config?.paths?.entities ? `\t\tlist: ${upperCase}.schema.omit({ id: true }),` : "\t\tlist: z.object({ }),",
269
- 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({ }),",
270
272
  "\t},",
271
273
  "",
272
274
  "\tactionsPayload: {",
273
- config?.paths?.entities ? `\t\tcreate: ${upperCase}.schema.omit({ id: true }),` : "\t\tcreate: z.object({ }),",
274
- 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({ }),",
275
277
  "\t},",
276
278
  "",
277
279
  `\tname: '${upperCase}Store'`,
@@ -437,6 +439,7 @@ if (command === 'store') {
437
439
 
438
440
  function generateEntity(upperCase) {
439
441
  const folder = config?.paths?.entities ? path.resolve(process.cwd(), config?.paths?.entities, entityName) : path.resolve(process.cwd(), entityName);
442
+ const fields = options.filter(x => x.startsWith('f:')).flatMap(x => x.split(':')[1]).join(',').split(',').map(x => x.split('-'))
440
443
 
441
444
  fs.mkdirSync(folder, { recursive: true })
442
445
 
@@ -449,7 +452,11 @@ function generateEntity(upperCase) {
449
452
  `export class ${upperCase} {`,
450
453
  "\tstatic schema = z.object({",
451
454
  "\t\tid: Entities.Identifier.schema,",
452
- "\t\t",
455
+ fields.length ?
456
+ fields.map(([fieldName, description]) => {
457
+ return `\t\t${fieldName}: z.${description.split('.').join('().')}(),`
458
+ }).join('\n')
459
+ : "\t\t",
453
460
  "\t})",
454
461
  "\t",
455
462
  `\tprivate constructor(private readonly data: ${upperCase}Model) {}`,
@@ -477,6 +484,7 @@ if (command === 'entity') {
477
484
 
478
485
  function generateValueObject(upperCase) {
479
486
  const folder = config?.paths?.valueObjects ? path.resolve(process.cwd(), config?.paths?.valueObjects, entityName) : path.resolve(process.cwd(), entityName);
487
+ const fields = options.filter(x => x.startsWith('f:')).flatMap(x => x.split(':')[1]).join(',').split(',').map(x => x.split('-'))
480
488
 
481
489
  fs.mkdirSync(folder, { recursive: true })
482
490
 
@@ -487,7 +495,11 @@ function generateValueObject(upperCase) {
487
495
  "",
488
496
  `export class ${upperCase} {`,
489
497
  "\tstatic schema = z.object({",
490
- "\t\t",
498
+ fields.length ?
499
+ fields.map(([fieldName, description]) => {
500
+ return `\t\t${fieldName}: z.${description.split('.').join('().')}(),`
501
+ }).join('\n')
502
+ : "\t\t",
491
503
  "\t})",
492
504
  "\t",
493
505
  `\tprivate constructor(private readonly data: ${upperCase}Model) {}`,
@@ -672,7 +684,7 @@ if (command === 'service') {
672
684
  if (command === 'crud') {
673
685
  var [lowerCase, upperCase] = camelizeVariants(entityName)
674
686
  generateEntity(upperCase)
675
- generateStores(lowerCase, upperCase)
687
+ generateStores(lowerCase, upperCase, true)
676
688
  generateService(lowerCase, upperCase, true)
677
689
  process.exit(0)
678
690
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.2.3",
3
+ "version": "0.3.0",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "repository": {
6
6
  "type": "git",