@alevnyacow/nzmt 0.0.25 → 0.0.27
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.
- package/bin/cli.js +112 -7
- package/package.json +1 -1
package/bin/cli.js
CHANGED
|
@@ -4,7 +4,7 @@ import path from "path";
|
|
|
4
4
|
|
|
5
5
|
var args = process.argv.slice(2);
|
|
6
6
|
|
|
7
|
-
var [command, entityName] = args;
|
|
7
|
+
var [command, entityName, options = ''] = args;
|
|
8
8
|
|
|
9
9
|
function camelizeVariants(str) {
|
|
10
10
|
if (!str.includes('-')) {
|
|
@@ -80,7 +80,11 @@ function createDefaultConfig() {
|
|
|
80
80
|
entities: './src/shared/entities',
|
|
81
81
|
queries: './src/client/shared/queries'
|
|
82
82
|
},
|
|
83
|
-
dependencyInjection:
|
|
83
|
+
dependencyInjection: {
|
|
84
|
+
inversifyjs: {
|
|
85
|
+
storeTokensPrefix: "import { DIStores } from '@/backend/di'"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
84
88
|
}, null, '\t'))
|
|
85
89
|
}
|
|
86
90
|
|
|
@@ -100,7 +104,7 @@ function generateStores(lowerCase, upperCase) {
|
|
|
100
104
|
|
|
101
105
|
fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
|
|
102
106
|
"import z from 'zod'",
|
|
103
|
-
"import { Store } from '@alevnyacow/nzmt'",
|
|
107
|
+
"import type { Store } from '@alevnyacow/nzmt'",
|
|
104
108
|
config?.paths?.entities ? `import { ${upperCase} } from '${config?.paths?.entities.replace('./src', '@')}/${entityName}'` : undefined,
|
|
105
109
|
"",
|
|
106
110
|
`export const ${lowerCase}StoreMetadata = {`,
|
|
@@ -128,13 +132,13 @@ function generateStores(lowerCase, upperCase) {
|
|
|
128
132
|
// RAM
|
|
129
133
|
|
|
130
134
|
fs.writeFileSync(path.resolve(folder, `${entityName}.store.ram.ts`), [
|
|
131
|
-
config?.dependencyInjection
|
|
135
|
+
config?.dependencyInjection?.inversifyjs ? "import { injectable } from 'inversify'" : undefined,
|
|
132
136
|
"import { Store } from '@alevnyacow/nzmt'",
|
|
133
137
|
`import { type ${upperCase}Store, ${lowerCase}StoreMetadata } from './${entityName}.store'`,
|
|
134
138
|
"",
|
|
135
139
|
`const CRUDInRAM = Store.InRAM(${lowerCase}StoreMetadata)`,
|
|
136
140
|
"",
|
|
137
|
-
config?.dependencyInjection
|
|
141
|
+
config?.dependencyInjection?.inversifyjs ? "@injectable()" : undefined,
|
|
138
142
|
`export class ${upperCase}RAMStore extends CRUDInRAM implements ${upperCase}Store {`,
|
|
139
143
|
"\t",
|
|
140
144
|
"}"
|
|
@@ -144,7 +148,7 @@ function generateStores(lowerCase, upperCase) {
|
|
|
144
148
|
|
|
145
149
|
fs.writeFileSync(path.resolve(folder, `${entityName}.store.prisma.ts`), [
|
|
146
150
|
...config?.paths?.['prismaImport'] ?? [],
|
|
147
|
-
config?.dependencyInjection
|
|
151
|
+
config?.dependencyInjection?.inversifyjs ? "import { injectable } from 'inversify'" : undefined,
|
|
148
152
|
"import { Store } from '@alevnyacow/nzmt'",
|
|
149
153
|
`import { type ${upperCase}Store, ${lowerCase}StoreMetadata } from './${entityName}.store'`,
|
|
150
154
|
"",
|
|
@@ -183,7 +187,7 @@ function generateStores(lowerCase, upperCase) {
|
|
|
183
187
|
"\t}",
|
|
184
188
|
"}",
|
|
185
189
|
"",
|
|
186
|
-
config?.dependencyInjection
|
|
190
|
+
config?.dependencyInjection?.inversifyjs ? "@injectable()" : undefined,
|
|
187
191
|
`export class ${upperCase}PrismaStore implements ${upperCase}Store {`,
|
|
188
192
|
`\tprivate method = Store.methods(${lowerCase}StoreMetadata);`,
|
|
189
193
|
"",
|
|
@@ -303,8 +307,109 @@ if (command === 'entity') {
|
|
|
303
307
|
process.exit(0)
|
|
304
308
|
}
|
|
305
309
|
|
|
310
|
+
function generateService(lowerCase, upperCase, withCrud) {
|
|
311
|
+
const folder = config?.paths?.services ? path.resolve(process.cwd(), config?.paths?.services, entityName) : path.resolve(process.cwd(), entityName);
|
|
312
|
+
|
|
313
|
+
fs.mkdirSync(folder, { recursive: true })
|
|
314
|
+
|
|
315
|
+
// Metadata
|
|
316
|
+
|
|
317
|
+
if (withCrud) {
|
|
318
|
+
fs.writeFileSync(path.resolve(folder, `${entityName}.service.metadata.ts`), [
|
|
319
|
+
"import { type Module, Entities } from '@alevnyacow/nzmt'",
|
|
320
|
+
"import z from 'zod'",
|
|
321
|
+
`import { ${lowerCase}StoreMetadata } from '${config?.paths?.stores?.replace('./src', '@')}/${entityName}'`,
|
|
322
|
+
"",
|
|
323
|
+
`export const ${lowerCase}sServiceMetadata = {`,
|
|
324
|
+
`\tname: '${upperCase}sService',`,
|
|
325
|
+
"\tschemas: {",
|
|
326
|
+
"\t\tgetSpecific: {",
|
|
327
|
+
`\t\t\tpayload: z.object({`
|
|
328
|
+
`\t\t\tfilter: ${lowerCase}StoreMetadata.searchPayload.specific`,
|
|
329
|
+
`\t\t}),`,
|
|
330
|
+
`\t\t\tresponse: z.object({`
|
|
331
|
+
`\t\t\t\titem: ${lowerCase}StoreMetadata.models.details.nullable()`,
|
|
332
|
+
`\t\t\t})`,
|
|
333
|
+
`\t\t},`,
|
|
334
|
+
`\t\tgetList: {`,
|
|
335
|
+
`\t\t\tpayload: z.object({`
|
|
336
|
+
`\t\t\t\tfilter: ${lowerCase}StoreMetadata.searchPayload.list,`
|
|
337
|
+
`\t\t\t\tpagination: Entities.Pagination.schema.optional()`,
|
|
338
|
+
`\t\t\t})`,
|
|
339
|
+
`\t\t\tresponse: z.object({`,
|
|
340
|
+
`\t\t\t\titems: z.array(${lowerCase}StoreMetadata.models.list)`,
|
|
341
|
+
`\t\t\t})`,
|
|
342
|
+
`\t\t},`,
|
|
343
|
+
`\t\tupdateOne: {`,
|
|
344
|
+
`\t\t\tpayload: z.object({`,
|
|
345
|
+
`\t\t\t\tfilter: ${lowerCase}StoreMetadata.searchPayload.specific,`,
|
|
346
|
+
`\t\t\t\tpayload: userStoreMetadata.actionsPayload.update`,
|
|
347
|
+
`\t\t\t}),`,
|
|
348
|
+
`\t\t\tresponse: z.object({})`
|
|
349
|
+
`\t\t},`,
|
|
350
|
+
`\t\tcreate: {`,
|
|
351
|
+
`\t\t\tpayload: z.object({`,
|
|
352
|
+
`\t\t\t\tpayload: ${lowerCase}StoreMetadata.actionsPayload.create`,
|
|
353
|
+
`\t\t\t},`,
|
|
354
|
+
`\t\t\tresponse: z.object({`,
|
|
355
|
+
`\t\t\t\tid: Entities.Identifier.schema`,
|
|
356
|
+
`\t\t\t}),`,
|
|
357
|
+
`\t\t},`
|
|
358
|
+
`\t\tdeleteOne: {`,
|
|
359
|
+
`\t\t\tpayload: z.object({`,
|
|
360
|
+
`\t\t\t\tfilter: ${lowerCase}StoreMetadata.searchPayload.specific`,
|
|
361
|
+
`\t\t\t}),`,
|
|
362
|
+
`\t\t\tresponse: z.object({})`,
|
|
363
|
+
`\t\t}),`,
|
|
364
|
+
"\t}",
|
|
365
|
+
"} satisfies Module.Metadata",
|
|
366
|
+
"",
|
|
367
|
+
`export type ${upperCase}sServiceDTOs = Module.DTOs<typeof ${lowerCase}sServiceMetadata>"`
|
|
368
|
+
].filter(x => typeof x === 'string').join('\n'))
|
|
369
|
+
} else {
|
|
370
|
+
fs.writeFileSync(path.resolve(folder, `${entityName}.service.metadata.ts`), [
|
|
371
|
+
"import type { Module } from '@alevnyacow/nzmt'",
|
|
372
|
+
"",
|
|
373
|
+
`export const ${lowerCase}ServiceMetadata = {`,
|
|
374
|
+
`\tname: '${upperCase}Service',`,
|
|
375
|
+
"\tschemas: {}",
|
|
376
|
+
"} satisfies Module.Metadata",
|
|
377
|
+
"",
|
|
378
|
+
`export type ${upperCase}ServiceDTOs = Module.DTOs<typeof ${lowerCase}ServiceMetadata>"`
|
|
379
|
+
].filter(x => typeof x === 'string').join('\n'))
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Service body
|
|
383
|
+
|
|
384
|
+
fs.writeFileSync(path.resolve(folder, `${entityName}.service.ts`), [
|
|
385
|
+
config?.dependencyInjection?.inversifyjs ? "import { injectable } from 'inversify'" : undefined,
|
|
386
|
+
`import { ${lowerCase}ServiceMetadata } from './${entityName}.service.metadata'`,
|
|
387
|
+
"import { Module } from '@alevnyacow/nzmt'",
|
|
388
|
+
"",
|
|
389
|
+
config?.dependencyInjection === 'inversifyjs' ? "@injectable()" : undefined,
|
|
390
|
+
`export class ${upperCase}Service {`,
|
|
391
|
+
`\tprivate methods = Module.methods(${lowerCase}ServiceMetadata)`,
|
|
392
|
+
"}"
|
|
393
|
+
].filter(x => typeof x === 'string').join('\n'))
|
|
394
|
+
|
|
395
|
+
// Barrel
|
|
396
|
+
|
|
397
|
+
fs.writeFileSync(path.resolve(folder, 'index.ts'), [
|
|
398
|
+
`export * from ${entityName}.service.metadata`,
|
|
399
|
+
`export * from ${entityName}.service`
|
|
400
|
+
])
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (command === 'service') {
|
|
404
|
+
var [lowerCase, upperCase] = camelizeVariants(entityName)
|
|
405
|
+
generateService(lowerCase, upperCase, false)
|
|
406
|
+
process.exit(0)
|
|
407
|
+
|
|
408
|
+
}
|
|
409
|
+
|
|
306
410
|
if (command === 'crud') {
|
|
307
411
|
var [lowerCase, upperCase] = camelizeVariants(entityName)
|
|
308
412
|
generateEntity(upperCase)
|
|
309
413
|
generateStores(lowerCase, upperCase)
|
|
414
|
+
generateService(entityName + 's', lowerCase, upperCase, true)
|
|
310
415
|
}
|