@alevnyacow/nzmt 0.0.9 → 0.0.11
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 +236 -0
- package/package.json +2 -3
- package/bin/initialize-config.js +0 -58
- package/bin/new-store.js +0 -212
package/bin/cli.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
var args = process.argv.slice(2);
|
|
6
|
+
|
|
7
|
+
var [command, entityName] = args;
|
|
8
|
+
|
|
9
|
+
function findProjectRoot(startDir = process.cwd()) {
|
|
10
|
+
let dir = startDir;
|
|
11
|
+
while (dir !== path.parse(dir).root) {
|
|
12
|
+
if (fs.existsSync(path.join(dir, "package.json"))) {
|
|
13
|
+
return dir;
|
|
14
|
+
}
|
|
15
|
+
dir = path.dirname(dir);
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function loadConfig() {
|
|
21
|
+
const projectRoot = findProjectRoot();
|
|
22
|
+
if (!projectRoot) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const configPath = path.join(projectRoot, "nzmt.config.json");
|
|
27
|
+
|
|
28
|
+
if (!fs.existsSync(configPath)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const rawData = fs.readFileSync(configPath, "utf-8");
|
|
34
|
+
const config = JSON.parse(rawData);
|
|
35
|
+
return config;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
throw err;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const config = loadConfig();
|
|
42
|
+
|
|
43
|
+
if (!config && command === 'init-config') {
|
|
44
|
+
const projectRoot = findProjectRoot()
|
|
45
|
+
if (!projectRoot) {
|
|
46
|
+
throw 'No package.json was found'
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fs.writeFileSync(path.resolve(projectRoot, 'nzmt.config.json'), JSON.stringify({
|
|
50
|
+
paths: {
|
|
51
|
+
prismaImport: [
|
|
52
|
+
"import { prisma } from '@/backend/infrastructure/prisma'",
|
|
53
|
+
"import type { Prisma } from '@/backend/generated-prisma/client'",
|
|
54
|
+
],
|
|
55
|
+
stores: './backend/stores',
|
|
56
|
+
services: './backend/services',
|
|
57
|
+
providers: './backend/providers',
|
|
58
|
+
controllers: './backend/controllers'
|
|
59
|
+
}
|
|
60
|
+
}, null, '\t'))
|
|
61
|
+
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function camelizeVariants(str) {
|
|
66
|
+
const words = str.split("-");
|
|
67
|
+
|
|
68
|
+
const lowerCamel = words
|
|
69
|
+
.map((word, index) =>
|
|
70
|
+
index === 0 ? word.toLowerCase() : word[0].toUpperCase() + word.slice(1).toLowerCase()
|
|
71
|
+
)
|
|
72
|
+
.join("");
|
|
73
|
+
|
|
74
|
+
const upperCamel = words
|
|
75
|
+
.map(word => word[0].toUpperCase() + word.slice(1).toLowerCase())
|
|
76
|
+
.join("");
|
|
77
|
+
|
|
78
|
+
return [lowerCamel, upperCamel];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
var [lowerCase, upperCase] = camelizeVariants(entityName)
|
|
83
|
+
|
|
84
|
+
if (command === 'store') {
|
|
85
|
+
const folder = config ? path.resolve(process.cwd(), config?.paths?.stores) : path.resolve(process.cwd(), entityName);
|
|
86
|
+
|
|
87
|
+
fs.mkdirSync(folder, { recursive: true })
|
|
88
|
+
|
|
89
|
+
// Contract
|
|
90
|
+
|
|
91
|
+
fs.writeFileSync(path.resolve(folder, `${entityName}.store.ts`), [
|
|
92
|
+
"import z from 'zod'",
|
|
93
|
+
"import { Store } from '@alevnyacow/nzmt'",
|
|
94
|
+
"",
|
|
95
|
+
`export const ${lowerCase}StoreMetadata = {`,
|
|
96
|
+
"\tmodels: {",
|
|
97
|
+
"\t\tlist: z.object({ }),",
|
|
98
|
+
"\t\tdetails: z.object({ }),",
|
|
99
|
+
"\t},",
|
|
100
|
+
"",
|
|
101
|
+
"\tsearchPayload: {",
|
|
102
|
+
"\t\tlist: z.object({ }),",
|
|
103
|
+
"\t\tspecific: z.object({ }),",
|
|
104
|
+
"\t},",
|
|
105
|
+
"",
|
|
106
|
+
"\tactionsPayload: {",
|
|
107
|
+
"\t\tcreate: z.object({ }),",
|
|
108
|
+
"\t\tupdate: z.object({ }),",
|
|
109
|
+
"\t},",
|
|
110
|
+
"",
|
|
111
|
+
`\tname: '${upperCase}Store'`,
|
|
112
|
+
"} satisfies Store.Metadata",
|
|
113
|
+
"",
|
|
114
|
+
`export type ${upperCase}Store = Store.Contract<typeof ${lowerCase}StoreMetadata>`
|
|
115
|
+
].join('\n'))
|
|
116
|
+
|
|
117
|
+
// RAM
|
|
118
|
+
|
|
119
|
+
fs.writeFileSync(path.resolve(folder, `${entityName}.store.ram.ts`), [
|
|
120
|
+
"import { Store } from '@alevnyacow/nzmt'",
|
|
121
|
+
`import { type ${upperCase}Store, ${lowerCase}StoreMetadata } from './${entityName}.store'`,
|
|
122
|
+
"",
|
|
123
|
+
`const CRUDInRAM = Store.InRAM(${lowerCase}StoreMetadata)`,
|
|
124
|
+
"",
|
|
125
|
+
`export class ${upperCase}RAMStore extends CRUDInRAM implements ${upperCase}Store {`,
|
|
126
|
+
"\t",
|
|
127
|
+
"}"
|
|
128
|
+
].join('\n'))
|
|
129
|
+
|
|
130
|
+
// Prisma
|
|
131
|
+
|
|
132
|
+
fs.writeFileSync(path.resolve(folder, `${entityName}.store.prisma.ts`), [
|
|
133
|
+
...config?.paths?.['prismaImport'] ?? [],
|
|
134
|
+
"import { Store } from '@alevnyacow/nzmt'",
|
|
135
|
+
`import { type ${upperCase}Store, ${lowerCase}StoreMetadata } from './${entityName}.store'`,
|
|
136
|
+
"",
|
|
137
|
+
`type Types = Store.Types<${upperCase}Store>`,
|
|
138
|
+
"",
|
|
139
|
+
"const mappers = {",
|
|
140
|
+
`\ttoFindOnePayload: (source: Types['findOnePayload']): Prisma.${upperCase}WhereUniqueInput => {`,
|
|
141
|
+
"\t\treturn {",
|
|
142
|
+
"\t\t\t",
|
|
143
|
+
"\t\t};",
|
|
144
|
+
"\t},",
|
|
145
|
+
`\ttoFindListPayload: (source: Types['findListPayload']): Prisma.${upperCase}WhereInput => {`,
|
|
146
|
+
"\t\treturn {",
|
|
147
|
+
"\t\t\t",
|
|
148
|
+
"\t\t};",
|
|
149
|
+
"\t},",
|
|
150
|
+
`\ttoListModel: (source: Prisma.${upperCase}GetPayload<{}>): Types['listModel'] => {`,
|
|
151
|
+
"\t\treturn {",
|
|
152
|
+
"\t\t\t",
|
|
153
|
+
"\t\t};",
|
|
154
|
+
"\t},",
|
|
155
|
+
`\ttoDetails: (source: Prisma.${upperCase}GetPayload<{ include: { } }>): Types['details'] => {`,
|
|
156
|
+
"\t\treturn {",
|
|
157
|
+
"\t\t\t",
|
|
158
|
+
"\t\t};",
|
|
159
|
+
"\t},",
|
|
160
|
+
`\ttoCreatePayload: (source: Types['createPayload']): Prisma.${upperCase}CreateInput => {`,
|
|
161
|
+
"\t\treturn {",
|
|
162
|
+
"\t\t\t",
|
|
163
|
+
"\t\t};",
|
|
164
|
+
"\t},",
|
|
165
|
+
`\ttoUpdatePayload: (source: Types['updatePayload']): Prisma.${upperCase}UpdateInput => {`,
|
|
166
|
+
"\t\treturn {",
|
|
167
|
+
"\t\t\t",
|
|
168
|
+
"\t\t};",
|
|
169
|
+
"\t}",
|
|
170
|
+
"}",
|
|
171
|
+
"",
|
|
172
|
+
`export class ${upperCase}PrismaStore implements ${upperCase}Store {`,
|
|
173
|
+
`\tprivate method = Store.methods(${lowerCase}StoreMetadata);`,
|
|
174
|
+
"",
|
|
175
|
+
"\tlist = this.method('list', async ({ filter, pagination: { pageSize, zeroBasedIndex } = { pageSize: 1000, zeroBasedIndex: 0 }}) => {",
|
|
176
|
+
`\t\tconst list = await prisma.${lowerCase}.findMany({`,
|
|
177
|
+
"\t\t\twhere: mappers.toFindListPayload(filter),",
|
|
178
|
+
"\t\t\tskip: zeroBasedIndex * pageSize,",
|
|
179
|
+
"\t\t\ttake: pageSize",
|
|
180
|
+
"\t\t})",
|
|
181
|
+
"\t\t",
|
|
182
|
+
"\t\treturn list.map(mappers.toListModel)",
|
|
183
|
+
"\t});",
|
|
184
|
+
"",
|
|
185
|
+
"\tdetails = this.method('details', async ({ filter }) => {",
|
|
186
|
+
`\t\tconst details = await prisma.${lowerCase}.findUnique({`,
|
|
187
|
+
"\t\t\twhere: mappers.toFindOnePayload(filter),",
|
|
188
|
+
"\t\t\tinclude: {}",
|
|
189
|
+
"\t\t})",
|
|
190
|
+
"",
|
|
191
|
+
"\t\tif (!details) {",
|
|
192
|
+
"\t\t\treturn null",
|
|
193
|
+
"\t\t}",
|
|
194
|
+
"",
|
|
195
|
+
"\t\treturn mappers.toDetails(details)",
|
|
196
|
+
"\t});",
|
|
197
|
+
"",
|
|
198
|
+
"\tcreate = this.method('create', async ({ payload }) => {",
|
|
199
|
+
`\t\tconst { id } = await prisma.${lowerCase}.create({`,
|
|
200
|
+
"\t\t\tdata: mappers.toCreatePayload(payload),",
|
|
201
|
+
"\t\t\tselect: { id: true }",
|
|
202
|
+
"\t\t})",
|
|
203
|
+
"",
|
|
204
|
+
"\t\treturn { id }",
|
|
205
|
+
"\t});",
|
|
206
|
+
"",
|
|
207
|
+
"\tupdateOne = this.method('updateOne', async ({ filter, payload }) => {",
|
|
208
|
+
"\t\ttry {",
|
|
209
|
+
`\t\t\tawait prisma.${lowerCase}.update({`,
|
|
210
|
+
"\t\t\t\twhere: mappers.toFindOnePayload(filter),",
|
|
211
|
+
"\t\t\t\tdata: mappers.toUpdatePayload(payload),",
|
|
212
|
+
"\t\t\t})",
|
|
213
|
+
"",
|
|
214
|
+
"\t\t\treturn { success: true }",
|
|
215
|
+
"\t\t}",
|
|
216
|
+
"\t\tcatch {",
|
|
217
|
+
"\t\t\treturn { success: false }",
|
|
218
|
+
"\t\t}",
|
|
219
|
+
"\t});",
|
|
220
|
+
"",
|
|
221
|
+
"\tdeleteOne = this.method('deleteOne', async ({ filter }) => {",
|
|
222
|
+
"\t\ttry {",
|
|
223
|
+
`\t\t\tawait prisma.${lowerCase}.delete({`,
|
|
224
|
+
"\t\t\t\twhere: mappers.toFindOnePayload(filter),",
|
|
225
|
+
"\t\t\t})",
|
|
226
|
+
"",
|
|
227
|
+
"\t\t\treturn { success: true }",
|
|
228
|
+
"\t\t}",
|
|
229
|
+
"\t\tcatch {",
|
|
230
|
+
"\t\t\treturn { success: false }",
|
|
231
|
+
"\t\t}",
|
|
232
|
+
"\t});",
|
|
233
|
+
"};"
|
|
234
|
+
].join('\n'))
|
|
235
|
+
}
|
|
236
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alevnyacow/nzmt",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"description": "Next Zod Modules Toolkit",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
}
|
|
12
12
|
},
|
|
13
13
|
"bin": {
|
|
14
|
-
"
|
|
15
|
-
"init-config": "./bin/initialize-config.js"
|
|
14
|
+
"cli": "./bin/cli.js"
|
|
16
15
|
},
|
|
17
16
|
"main": "./dist/index.cjs",
|
|
18
17
|
"types": "./dist/index.d.ts",
|
package/bin/initialize-config.js
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
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
|
-
}
|
package/bin/new-store.js
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
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'))
|