@alevnyacow/nzmt 0.9.5 → 0.10.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 (3) hide show
  1. package/README.md +3 -3
  2. package/bin/cli.js +24 -8
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -32,7 +32,7 @@ npx nzmt init prismaClientPath:@/app/generated/prisma/client
32
32
 
33
33
  4. Scaffold your first entity. Example for a `Product` with `title` and `price`:
34
34
  ```bash
35
- # Field syntax: f:<name>-<type>[.<zod-validators>]
35
+ # Field syntax: f:<name>-<zod-rules>
36
36
  npx nzmt entity product f:title-string,price-int.positive
37
37
  ```
38
38
  This will generate the entity, its Zod schema and related types.
@@ -41,8 +41,8 @@ This will generate the entity, its Zod schema and related types.
41
41
  ```bash
42
42
  # product store (with Prisma implementation, RAM implementation and DI)
43
43
  npx nzmt store product
44
- # product service with injected product store (with DI)
45
- npx nzmt service product i:ProductStore
44
+ # product service proxying all product store methods (with DI)
45
+ npx nzmt service product p:ProductStore
46
46
  # shop controller with injected product service and logger (with DI)
47
47
  npx nzmt controller shop i:Logger,ProductService
48
48
  ```
package/bin/cli.js CHANGED
@@ -393,7 +393,7 @@ function generateStores(lowerCase, upperCase, withEntityPreset) {
393
393
  // Contract
394
394
 
395
395
  fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
396
- "import type { Store } from '@alevnyacow/nzmt'",
396
+ "import { Store } from '@alevnyacow/nzmt'",
397
397
  withEntity ? `import { ${upperCase} } from '@${config?.paths?.entities}/${entityName}'` : undefined,
398
398
  "",
399
399
  `export const ${lowerCase}StoreMetadata = {`,
@@ -415,6 +415,8 @@ function generateStores(lowerCase, upperCase, withEntityPreset) {
415
415
  `\tname: '${upperCase}Store'`,
416
416
  "} satisfies Store.Metadata",
417
417
  "",
418
+ `export const { schemas: ${lowerCase}StoreSchemas } = Store.toModuleMetadata(${lowerCase}StoreMetadata)`,
419
+ "",
418
420
  `export type ${upperCase}Store = Store.Contract<typeof ${lowerCase}StoreMetadata>`
419
421
  ].filter(x => typeof x === 'string').join('\n'))
420
422
 
@@ -744,7 +746,15 @@ function toKebabFromPascal(str) {
744
746
  function generateService(lowerCase, upperCase) {
745
747
  const folder = config?.paths?.services ? path.resolve(process.cwd(), `${config.coreFolder}${config?.paths?.services}`, entityName) : path.resolve(process.cwd(), entityName);
746
748
 
747
- const injections = options.filter(x => x.startsWith('i:')).flatMap(x => x.split(':')[1]).join(',').split(',').filter(x => !!x.length)
749
+ const proxiedStore = options.find(x => x.startsWith('p:'))?.split(':')?.at(-1)
750
+ if (proxiedStore && !proxiedStore.endsWith('Store')) {
751
+ throw 'Only stores can be proxied in services!'
752
+ }
753
+
754
+ let injections = options.filter(x => x.startsWith('i:')).flatMap(x => x.split(':')[1]).join(',').split(',').filter(x => !!x.length)
755
+ if (proxiedStore && !injections.includes(proxiedStore)) {
756
+ injections = injections.concat(proxiedStore)
757
+ }
748
758
 
749
759
  const importInjections = injections.map((i) => {
750
760
  if (i.endsWith('Service') || i.endsWith('Controller')) {
@@ -752,10 +762,10 @@ function generateService(lowerCase, upperCase) {
752
762
  }
753
763
 
754
764
  if (i.endsWith('Store')) {
755
- return `import { ${i} } from '@${config?.paths?.stores}/${toKebabFromPascal(i).slice(0, -'-store'.length)}'`
765
+ return `import type { ${i} } from '@${config?.paths?.stores}/${toKebabFromPascal(i).slice(0, -'-store'.length)}'`
756
766
  }
757
767
 
758
- return `import { ${i} } from '@${config?.paths?.infrastructure}/${toKebabFromPascal(i)}'`
768
+ return `import type { ${i} } from '@${config?.paths?.infrastructure}/${toKebabFromPascal(i)}'`
759
769
  })
760
770
 
761
771
  fs.mkdirSync(folder, { recursive: true })
@@ -764,10 +774,11 @@ function generateService(lowerCase, upperCase) {
764
774
 
765
775
  fs.writeFileSync(path.resolve(folder, `${entityName}.service.metadata.ts`), [
766
776
  "import type { Module } from '@alevnyacow/nzmt'",
777
+ proxiedStore ? `import { ${proxiedStore.substring(0, 1).toLowerCase() + proxiedStore.substring(1)}Schemas } from '@${config?.paths?.stores}/${toKebabFromPascal(proxiedStore).slice(0, -'-store'.length)}'` : undefined,
767
778
  "",
768
779
  `export const ${lowerCase}ServiceMetadata = {`,
769
780
  `\tname: '${upperCase}Service',`,
770
- "\tschemas: {}",
781
+ proxiedStore ? `\tschemas: { ...${proxiedStore.substring(0, 1).toLowerCase() + proxiedStore.substring(1)}Schemas }` : "\tschemas: {}",
771
782
  "} satisfies Module.Metadata",
772
783
  "",
773
784
  `export type ${upperCase}ServiceDTOs = Module.DTOs<typeof ${lowerCase}ServiceMetadata>`
@@ -791,6 +802,11 @@ function generateService(lowerCase, upperCase) {
791
802
  ...injections.map(x => `\t\t@inject('${x}' satisfies DITokens) private readonly ${x.charAt(0).toLowerCase() + x.slice(1)}: ${x},`),
792
803
  `\t) {}`,
793
804
  ``,
805
+ proxiedStore ? `\tgetList = this.methods('list', this.${proxiedStore.charAt(0).toLowerCase() + proxiedStore.slice(1)}.list)` : undefined,
806
+ proxiedStore ? `\tgetDetails = this.methods('details', this.${proxiedStore.charAt(0).toLowerCase() + proxiedStore.slice(1)}.details)` : undefined,
807
+ proxiedStore ? `\tcreate = this.methods('create', this.${proxiedStore.charAt(0).toLowerCase() + proxiedStore.slice(1)}.create)` : undefined,
808
+ proxiedStore ? `\tupdate = this.methods('updateOne', this.${proxiedStore.charAt(0).toLowerCase() + proxiedStore.slice(1)}.updateOne)` : undefined,
809
+ proxiedStore ? `\tdelete = this.methods('deleteOne', this.${proxiedStore.charAt(0).toLowerCase() + proxiedStore.slice(1)}.deleteOne)` : undefined,
794
810
  "}"
795
811
  ].filter(x => typeof x === 'string').join('\n'))
796
812
 
@@ -836,14 +852,14 @@ function generateController(upperCase, lowerCase) {
836
852
  }
837
853
 
838
854
  if (i.endsWith('Store')) {
839
- return `import { ${i} } from '@${config?.paths?.stores}/${toKebabFromPascal(i).slice(0, -'-store'.length)}'`
855
+ return `import type { ${i} } from '@${config?.paths?.stores}/${toKebabFromPascal(i).slice(0, -'-store'.length)}'`
840
856
  }
841
857
 
842
858
  if (i.endsWith('Service')) {
843
- return `import { ${i} } from '@${config?.paths?.services}/${toKebabFromPascal(i).slice(0, -'-service'.length)}'`
859
+ return `import type { ${i} } from '@${config?.paths?.services}/${toKebabFromPascal(i).slice(0, -'-service'.length)}'`
844
860
  }
845
861
 
846
- return `import { ${i} } from '@${config?.paths?.infrastructure}/${toKebabFromPascal(i)}'`
862
+ return `import type { ${i} } from '@${config?.paths?.infrastructure}/${toKebabFromPascal(i)}'`
847
863
  })
848
864
 
849
865
  fs.mkdirSync(folder, { recursive: true })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.9.5",
3
+ "version": "0.10.0",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "repository": {
6
6
  "type": "git",