@alevnyacow/nzmt 0.0.7 → 0.0.9

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.
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env node
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+ function findProjectRoot(startDir = process.cwd()) {
6
+ let dir = startDir;
7
+ while (dir !== path.parse(dir).root) {
8
+ if (fs.existsSync(path.join(dir, "package.json"))) {
9
+ return dir;
10
+ }
11
+ dir = path.dirname(dir);
12
+ }
13
+ return null;
14
+ }
15
+
16
+
17
+ function loadConfig() {
18
+ const projectRoot = findProjectRoot();
19
+ if (!projectRoot) {
20
+ return null;
21
+ }
22
+
23
+ const configPath = path.join(projectRoot, "nzmt.config.json");
24
+
25
+ if (!fs.existsSync(configPath)) {
26
+ return null;
27
+ }
28
+
29
+ try {
30
+ const rawData = fs.readFileSync(configPath, "utf-8");
31
+ const config = JSON.parse(rawData);
32
+ return config;
33
+ } catch (err) {
34
+ throw err;
35
+ }
36
+ }
37
+
38
+ const config = loadConfig();
39
+
40
+ if (!config) {
41
+ const projectRoot = findProjectRoot()
42
+ if (!projectRoot) {
43
+ throw 'No package.json was found'
44
+ }
45
+
46
+ fs.writeFileSync(path.resolve(projectRoot, 'nzmt.config.json'), JSON.stringify({
47
+ paths: {
48
+ prismaImport: [
49
+ "import { prisma } from '@/backend/infrastructure/prisma'",
50
+ "import type { Prisma } from '@/backend/generated-prisma/client'",
51
+ ],
52
+ stores: './backend/stores',
53
+ services: './backend/services',
54
+ providers: './backend/providers',
55
+ controllers: './backend/controllers'
56
+ }
57
+ }, null, '\t'))
58
+ }
@@ -0,0 +1,212 @@
1
+ #!/usr/bin/env node
2
+ import fs from "fs";
3
+ import path from "path";
4
+
5
+
6
+ function findProjectRoot(startDir = process.cwd()) {
7
+ let dir = startDir;
8
+ while (dir !== path.parse(dir).root) {
9
+ if (fs.existsSync(path.join(dir, "package.json"))) {
10
+ return dir;
11
+ }
12
+ dir = path.dirname(dir);
13
+ }
14
+ return null;
15
+ }
16
+
17
+
18
+ function loadConfig() {
19
+ const projectRoot = findProjectRoot();
20
+ if (!projectRoot) {
21
+ return null;
22
+ }
23
+
24
+ const configPath = path.join(projectRoot, "nzmt.config.json");
25
+
26
+ if (!fs.existsSync(configPath)) {
27
+ return null;
28
+ }
29
+
30
+ try {
31
+ const rawData = fs.readFileSync(configPath, "utf-8");
32
+ const config = JSON.parse(rawData);
33
+ return config;
34
+ } catch (err) {
35
+ throw err;
36
+ }
37
+ }
38
+
39
+ const config = loadConfig();
40
+
41
+ function camelizeVariants(str) {
42
+ const words = str.split("-");
43
+
44
+ const lowerCamel = words
45
+ .map((word, index) =>
46
+ index === 0 ? word.toLowerCase() : word[0].toUpperCase() + word.slice(1).toLowerCase()
47
+ )
48
+ .join("");
49
+
50
+ const upperCamel = words
51
+ .map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())
52
+ .join("");
53
+
54
+ return [lowerCamel, upperCamel];
55
+ }
56
+
57
+ var args = process.argv.slice(2);
58
+
59
+ var entityName = args[0];
60
+
61
+ var [lowerCase, upperCase] = camelizeVariants(entityName)
62
+
63
+ const folder = config ? path.resolve(process.cwd(), config?.paths?.stores) : path.resolve(process.cwd(), entityName);
64
+
65
+ fs.mkdirSync(folder, { recursive: true })
66
+
67
+ // Contract
68
+
69
+ fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
70
+ "import z from 'zod'",
71
+ "import { Store } from '@alevnyacow/nzmt'",
72
+ "",
73
+ `export const ${lowerCase}StoreMetadata = {`,
74
+ "\tmodels: {",
75
+ "\t\tlist: z.object({ }),",
76
+ "\t\tdetails: z.object({ }),",
77
+ "\t},",
78
+ "",
79
+ "\tsearchPayload: {",
80
+ "\t\tlist: z.object({ }),",
81
+ "\t\tspecific: z.object({ }),",
82
+ "\t},",
83
+ "",
84
+ "\tactionsPayload: {",
85
+ "\t\tcreate: z.object({ }),",
86
+ "\t\tupdate: z.object({ }),",
87
+ "\t},",
88
+ "",
89
+ `\tname: '${upperCase}Store'`,
90
+ "} satisfies Store.Metadata",
91
+ "",
92
+ `export type ${upperCase}Store = Store.Contract<typeof ${lowerCase}StoreMetadata>`
93
+ ].join('\n'))
94
+
95
+ // RAM
96
+
97
+ fs.writeFileSync(path.resolve(folder, `${entityName}.store.ram.ts`), [
98
+ "import { Store } from '@alevnyacow/nzmt'",
99
+ `import { type ${upperCase}Store, ${lowerCase}StoreMetadata } from './${entityName}.store'`,
100
+ "",
101
+ `const CRUDInRAM = Store.InRAM(${lowerCase}StoreMetadata)`,
102
+ "",
103
+ `export class ${upperCase}RAMStore extends CRUDInRAM implements ${upperCase}Store {`,
104
+ "\t",
105
+ "}"
106
+ ].join('\n'))
107
+
108
+ // Prisma
109
+
110
+ fs.writeFileSync(path.resolve(folder, `${entityName}.store.prisma.ts`), [
111
+ ...config?.paths?.['prismaImport'] ?? [],
112
+ "import { Store } from '@alevnyacow/nzmt'",
113
+ `import { type ${upperCase}Store, ${lowerCase}StoreMetadata } from './${entityName}.store'`,
114
+ "",
115
+ `type Types = Store.Types<${upperCase}Store>`,
116
+ "",
117
+ "const mappers = {",
118
+ `\ttoFindOnePayload: (source: Types['findOnePayload']): Prisma.${upperCase}WhereUniqueInput => {`,
119
+ "\t\treturn {",
120
+ "\t\t\t",
121
+ "\t\t};",
122
+ "\t},",
123
+ `\ttoFindListPayload: (source: Types['findListPayload']): Prisma.${upperCase}WhereInput => {`,
124
+ "\t\treturn {",
125
+ "\t\t\t",
126
+ "\t\t};",
127
+ "\t},",
128
+ `\ttoListModel: (source: Prisma.${upperCase}GetPayload<{}>): Types['listModel'] => {`,
129
+ "\t\treturn {",
130
+ "\t\t\t",
131
+ "\t\t};",
132
+ "\t},",
133
+ `\ttoDetails: (source: Prisma.${upperCase}GetPayload<{ include: { } }>): Types['details'] => {`,
134
+ "\t\treturn {",
135
+ "\t\t\t",
136
+ "\t\t};",
137
+ "\t},",
138
+ `\ttoCreatePayload: (source: Types['createPayload']): Prisma.${upperCase}CreateInput => {`,
139
+ "\t\treturn {",
140
+ "\t\t\t",
141
+ "\t\t};",
142
+ "\t},",
143
+ `\ttoUpdatePayload: (source: Types['updatePayload']): Prisma.${upperCase}UpdateInput => {`,
144
+ "\t\treturn {",
145
+ "\t\t\t",
146
+ "\t\t};",
147
+ "\t}",
148
+ "}",
149
+ "",
150
+ `export class ${upperCase}PrismaStore implements ${upperCase}Store {`,
151
+ `\tprivate method = Store.methods(${lowerCase}StoreMetadata);`,
152
+ "",
153
+ "\tlist = this.method('list', async ({ filter, pagination: { pageSize, zeroBasedIndex } = { pageSize: 1000, zeroBasedIndex: 0 }}) => {",
154
+ `\t\tconst list = await prisma.${lowerCase}.findMany({`,
155
+ "\t\t\twhere: mappers.toFindListPayload(filter),",
156
+ "\t\t\tskip: zeroBasedIndex * pageSize,",
157
+ "\t\t\ttake: pageSize",
158
+ "\t\t})",
159
+ "\t\t",
160
+ "\t\treturn list.map(mappers.toListModel)",
161
+ "\t});",
162
+ "",
163
+ "\tdetails = this.method('details', async ({ filter }) => {",
164
+ `\t\tconst details = await prisma.${lowerCase}.findUnique({`,
165
+ "\t\t\twhere: mappers.toFindOnePayload(filter),",
166
+ "\t\t\tinclude: {}",
167
+ "\t\t})",
168
+ "",
169
+ "\t\tif (!details) {",
170
+ "\t\t\treturn null",
171
+ "\t\t}",
172
+ "",
173
+ "\t\treturn mappers.toDetails(details)",
174
+ "\t});",
175
+ "",
176
+ "\tcreate = this.method('create', async ({ payload }) => {",
177
+ `\t\tconst { id } = await prisma.${lowerCase}.create({`,
178
+ "\t\t\tdata: mappers.toCreatePayload(payload),",
179
+ "\t\t\tselect: { id: true }",
180
+ "\t\t})",
181
+ "",
182
+ "\t\treturn { id }",
183
+ "\t});",
184
+ "",
185
+ "\tupdateOne = this.method('updateOne', async ({ filter, payload }) => {",
186
+ "\t\ttry {",
187
+ `\t\t\tawait prisma.${lowerCase}.update({`,
188
+ "\t\t\t\twhere: mappers.toFindOnePayload(filter),",
189
+ "\t\t\t\tdata: mappers.toUpdatePayload(payload),",
190
+ "\t\t\t})",
191
+ "",
192
+ "\t\t\treturn { success: true }",
193
+ "\t\t}",
194
+ "\t\tcatch {",
195
+ "\t\t\treturn { success: false }",
196
+ "\t\t}",
197
+ "\t});",
198
+ "",
199
+ "\tdeleteOne = this.method('deleteOne', async ({ filter }) => {",
200
+ "\t\ttry {",
201
+ `\t\t\tawait prisma.${lowerCase}.delete({`,
202
+ "\t\t\t\twhere: mappers.toFindOnePayload(filter),",
203
+ "\t\t\t})",
204
+ "",
205
+ "\t\t\treturn { success: true }",
206
+ "\t\t}",
207
+ "\t\tcatch {",
208
+ "\t\t\treturn { success: false }",
209
+ "\t\t}",
210
+ "\t});",
211
+ "};"
212
+ ].join('\n'))
package/dist/index.cjs CHANGED
@@ -418,7 +418,6 @@ const methods = (schemas)=>{
418
418
  const data = mapStoreSchemasToModuleMetadata(schemas, schemas.name);
419
419
  return zodModuleMethodFactory(data);
420
420
  };
421
- const external_uuid_namespaceObject = require("uuid");
422
421
  const InRAM = (schemas, options)=>{
423
422
  class RAMStore {
424
423
  ___data = [];
@@ -436,7 +435,7 @@ const InRAM = (schemas, options)=>{
436
435
  ___mapDetailToList = (entity)=>entity;
437
436
  ___mapCreatePayloadToDetail = (payload)=>({
438
437
  ...payload,
439
- id: (0, external_uuid_namespaceObject.v4)()
438
+ id: Math.random().toString()
440
439
  });
441
440
  ___mapUpdatePayloadToDetail = (prevValue, update)=>({
442
441
  ...prevValue,
package/dist/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { __webpack_require__ } from "./rslib-runtime.js";
2
2
  import { NextResponse } from "next/server";
3
3
  import zod from "zod";
4
- import { v4 } from "uuid";
5
4
  var store_namespaceObject = {};
6
5
  __webpack_require__.r(store_namespaceObject);
7
6
  __webpack_require__.d(store_namespaceObject, {
@@ -397,7 +396,7 @@ const InRAM = (schemas, options)=>{
397
396
  ___mapDetailToList = (entity)=>entity;
398
397
  ___mapCreatePayloadToDetail = (payload)=>({
399
398
  ...payload,
400
- id: v4()
399
+ id: Math.random().toString()
401
400
  });
402
401
  ___mapUpdatePayloadToDetail = (prevValue, update)=>({
403
402
  ...prevValue,
@@ -1,5 +1,5 @@
1
1
  import z from 'zod';
2
- import { Pagination, PaginationModel } from './store.pagination.entity';
2
+ import { Pagination } from './store.pagination.entity';
3
3
  import type { CRUD, Types } from './store.shared-models.utils';
4
4
  import { type Contract, type Metadata } from './store.zod.utils';
5
5
  export declare const InRAM: <T extends Metadata>(schemas: T, options?: {
@@ -741,7 +741,7 @@ export declare const InRAM: <T extends Metadata>(schemas: T, options?: {
741
741
  update: AUpdate;
742
742
  };
743
743
  } : never : never : never)["SearchPayload"]["list"];
744
- pagination?: PaginationModel;
744
+ pagination?: import("./store.pagination.entity").PaginationModel;
745
745
  }) => Promise<(Contract<T> extends infer T_2 ? T_2 extends Contract<T> ? T_2 extends {
746
746
  list: (data: {
747
747
  filter: infer SList;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alevnyacow/nzmt",
3
- "version": "0.0.7",
3
+ "version": "0.0.9",
4
4
  "description": "Next Zod Modules Toolkit",
5
5
  "type": "module",
6
6
  "exports": {
@@ -10,6 +10,10 @@
10
10
  "require": "./dist/index.cjs"
11
11
  }
12
12
  },
13
+ "bin": {
14
+ "store": "./bin/new-store.js",
15
+ "init-config": "./bin/initialize-config.js"
16
+ },
13
17
  "main": "./dist/index.cjs",
14
18
  "types": "./dist/index.d.ts",
15
19
  "files": [
@@ -37,7 +41,6 @@
37
41
  },
38
42
  "private": false,
39
43
  "dependencies": {
40
- "uuid": "^13.0.0",
41
44
  "zod": "^4.3.6"
42
45
  }
43
46
  }