vsrepo 1.2.6 → 1.2.7
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/README.md +296 -437
- package/{VSRepository → dist}/VSRepoError.d.ts +1 -1
- package/dist/VSRepoError.js +17 -0
- package/{VSRepository → dist}/VSRepository.d.ts +937 -907
- package/dist/VSRepository.js +204 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/internal/errors/types/vs-repo-error-type.type.js +2 -0
- package/dist/internal/errors/vs-repo.error.js +27 -0
- package/dist/internal/resolvers/base-methods.resolve.js +535 -0
- package/dist/internal/resolvers/create-update-payloads-with-relations.resolve.js +143 -0
- package/dist/internal/resolvers/data-payload-with-relations.resolve.js +60 -0
- package/dist/internal/resolvers/dbAndPrismaArgs.resolve.js +46 -0
- package/dist/internal/resolvers/dinamic-method-customization.resolve.js +45 -0
- package/dist/internal/resolvers/dinamic-method-info.resolve.js +265 -0
- package/dist/internal/resolvers/merge-wheres.resolve.js +22 -0
- package/dist/internal/resolvers/pretty-wheres.resolve.js +87 -0
- package/dist/internal/resolvers/select.resolve.js +7 -0
- package/dist/internal/resolvers/specific-where.resolve.js +77 -0
- package/dist/internal/resolvers/types/base-method-function.type.js +2 -0
- package/dist/internal/resolvers/types/dinamic-method-customization.type.js +2 -0
- package/dist/internal/resolvers/types/dinamic-method-info.type.js +2 -0
- package/dist/internal/resolvers/types/dinamic-method-where-ops.type.js +2 -0
- package/dist/internal/resolvers/types/pretty-where.type.js +2 -0
- package/dist/internal/resolvers/types/prisma-args.type.js +2 -0
- package/dist/internal/resolvers/types/repository-build-instance.type.js +2 -0
- package/dist/internal/resolvers/types/resolve-db-and-prisma-args-data.type.js +2 -0
- package/dist/internal/resolvers/types/ugly-where.type.js +2 -0
- package/dist/internal/resolvers/ugly-where.resolve.js +178 -0
- package/dist/internal/utils/logger.util.js +21 -0
- package/dist/internal/utils/schemas.util.js +10 -0
- package/dist/internal/utils/uncapitalize.util.js +6 -0
- package/dist/internal/validation/build-config.validate.js +84 -0
- package/dist/internal/validation/constructor-config.validate.js +68 -0
- package/dist/internal/validation/extension.validate.js +15 -0
- package/dist/internal/validation/is-object.validate.js +6 -0
- package/dist/internal/validation/method-options.validate.js +26 -0
- package/dist/internal/validation/obj-with-relations.validate.js +37 -0
- package/dist/internal/validation/prisma-client.validate.js +10 -0
- package/dist/internal/validation/types/base-methods.type.js +2 -0
- package/dist/internal/validation/types/build-config.type.js +2 -0
- package/dist/internal/validation/types/constructor-config.type.js +2 -0
- package/dist/internal/validation/types/method-options.type.js +2 -0
- package/dist/internal/validation/types/method.type.js +2 -0
- package/dist/internal/validation/types/pagination.type.js +2 -0
- package/dist/internal/validation/types/relation.type.js +2 -0
- package/dist/internal/validation/types/see-mode.type.js +2 -0
- package/package.json +72 -70
- package/scripts/configure-prisma-import.mjs +3 -3
- package/scripts/copy-types.mjs +24 -0
- package/VSRepository/VSRepoError.js +0 -33
- package/VSRepository/VSRepoUtils.js +0 -155
- package/VSRepository/VSRepository.js +0 -1445
- package/VSRepository/index.d.ts +0 -2
- package/VSRepository/index.js +0 -2
|
@@ -1,1445 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* BEM VINDO AO CÓDIGO DO VSREPOSITORY!!!
|
|
3
|
-
* Esse código foi totalmente desenvolvido apenas por João Pedro Azevedo
|
|
4
|
-
* O código não tá muito organizado, dá pra melhorar muito, mas já ta funcionando bunitin
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// * Exportando classes de erro
|
|
8
|
-
export * from "./VSRepoError.js";
|
|
9
|
-
|
|
10
|
-
// * Importando classes de erro
|
|
11
|
-
import { VSRepoBuildError, VSRepoExtendError, VSRepoRuntimeError } from "./VSRepoError.js";
|
|
12
|
-
import { mergeWheres, validateBuildConfig, validateConfig } from "./VSRepoUtils.js";
|
|
13
|
-
|
|
14
|
-
// * Essa é a classe pincipal, é a partir dela que serão criados os objetos dos repositories
|
|
15
|
-
export class VSRepository {
|
|
16
|
-
|
|
17
|
-
// * O vsrepocache é um Map que serve para cachear onde os argumentos dos métodos gerados dinamicamente devem ser injetados, retornando todos os argumentos do Prisma já organizados
|
|
18
|
-
vsrepocache;
|
|
19
|
-
// * tableName serve para saber qual o nome da tabela que deverá ser injetado na função do prisma q será chamada
|
|
20
|
-
tableName;
|
|
21
|
-
// * Nome da primary key da tabela para o get, o remove e o save funcionarem corretamente
|
|
22
|
-
pkName;
|
|
23
|
-
// * Aqui são os modelos de select que poderão ser usados no seu repository
|
|
24
|
-
selectModels;
|
|
25
|
-
// * Aqui é onde você define qual é o modelo principal que deve ser injetado nos métodos, porém você pode escolher outro ao criar ou ao chamar o método (OBS: se o defaultSelectModel não for fornecido e não passar um model no método o select será undefined e o prisma retornará as colunas padrão)
|
|
26
|
-
defaultSelectModel;
|
|
27
|
-
relations;
|
|
28
|
-
requiredWhere;
|
|
29
|
-
methods;
|
|
30
|
-
|
|
31
|
-
constructor(config) {
|
|
32
|
-
config = validateConfig(config);
|
|
33
|
-
|
|
34
|
-
this.vsrepocache = new Map();
|
|
35
|
-
this.tableName = config.tableName;
|
|
36
|
-
this.pkName = config.pkName;
|
|
37
|
-
this.selectModels = config.selectModels;
|
|
38
|
-
this.defaultSelectModel = config.defaultSelectModel;
|
|
39
|
-
this.relations = config.relations;
|
|
40
|
-
this.requiredWhere = config.requiredWhere;
|
|
41
|
-
this.methods = config.methods ?? {};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
extend(extensionFunc) {
|
|
45
|
-
if(typeof extensionFunc !== 'function'){
|
|
46
|
-
throw new VSRepoExtendError(`[VSRepository] (${this.tableName}: extend) 'extensionFunc' must be a valid funcion.`);
|
|
47
|
-
}
|
|
48
|
-
const extension = extensionFunc(this);
|
|
49
|
-
if(typeof extension !== 'object' || extension === null){
|
|
50
|
-
throw new VSRepoExtendError(`[VSRepository] (${this.tableName}: extend) The return of the 'extensionFunc' must be a valid object.`)
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const extended = Object.assign(Object.create(this), extension);
|
|
54
|
-
|
|
55
|
-
if(Object.isFrozen(this)) {
|
|
56
|
-
Object.freeze(extended);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return extended;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
build(prisma, config = { freeze: true, showWorking: false }) {
|
|
63
|
-
if(typeof prisma !== 'object' || prisma === null) {
|
|
64
|
-
throw new VSRepoBuildError(`[VSRepository] (${this.tableName}: build) 'prisma' must be a valid Prisma Client.`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const buildInstance = Object.create(this);
|
|
68
|
-
|
|
69
|
-
config = validateBuildConfig(config, buildInstance);
|
|
70
|
-
|
|
71
|
-
const showWorking = config.showWorking;
|
|
72
|
-
|
|
73
|
-
const relationsKeys = buildInstance.relations ? Object.keys(buildInstance.relations) : [];
|
|
74
|
-
|
|
75
|
-
const methods = buildInstance.methods;
|
|
76
|
-
const repositoryKeysToMap = Object.keys(methods);
|
|
77
|
-
|
|
78
|
-
if(showWorking) {
|
|
79
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: build) Keys to map:`, JSON.stringify(repositoryKeysToMap, null, 2));
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
for (let keyToMap of repositoryKeysToMap) {
|
|
83
|
-
|
|
84
|
-
let onlyBaseWheres = false;
|
|
85
|
-
let ignoreWhere = false;
|
|
86
|
-
let ignoreOrderByAndPagination = true;
|
|
87
|
-
let ignoreSelect = false;
|
|
88
|
-
let keyToMapReplaced;
|
|
89
|
-
let argsCount = 0;
|
|
90
|
-
let method;
|
|
91
|
-
let dataIndex;
|
|
92
|
-
let updateIndex;
|
|
93
|
-
let createIndex;
|
|
94
|
-
let originalKey = keyToMap;
|
|
95
|
-
keyToMap = methods[originalKey].proxyTo ?? keyToMap;
|
|
96
|
-
let existsMode = false;
|
|
97
|
-
let skipDuplicates;
|
|
98
|
-
let ignoreSkipDuplicates = true;
|
|
99
|
-
let whereIndex;
|
|
100
|
-
let prismaArgsIndex;
|
|
101
|
-
const whereParams = [];
|
|
102
|
-
const otherParams = [];
|
|
103
|
-
|
|
104
|
-
if(keyToMap === 'aggregate') {
|
|
105
|
-
keyToMapReplaced = keyToMap.replace('aggregate', '')
|
|
106
|
-
ignoreSelect = true;
|
|
107
|
-
ignoreWhere = true;
|
|
108
|
-
method = 'aggregate';
|
|
109
|
-
prismaArgsIndex = 0;
|
|
110
|
-
otherParams.push('prismaArgs');
|
|
111
|
-
argsCount++;
|
|
112
|
-
} else if(keyToMap === 'groupBy') {
|
|
113
|
-
keyToMapReplaced = keyToMap.replace('groupBy', '')
|
|
114
|
-
ignoreSelect = true;
|
|
115
|
-
ignoreWhere = true;
|
|
116
|
-
method = 'groupBy';
|
|
117
|
-
prismaArgsIndex = 0;
|
|
118
|
-
otherParams.push('prismaArgs');
|
|
119
|
-
argsCount++;
|
|
120
|
-
} else if(keyToMap.startsWith('findUniqueOrThrowBy')) {
|
|
121
|
-
keyToMapReplaced = keyToMap.replace('findUniqueOrThrowBy', '');
|
|
122
|
-
method = 'findUniqueOrThrow';
|
|
123
|
-
} else if(keyToMap.startsWith('findUniqueBy')) {
|
|
124
|
-
keyToMapReplaced = keyToMap.replace('findUniqueBy', '');
|
|
125
|
-
method = 'findUnique';
|
|
126
|
-
} else if(keyToMap.startsWith('findFirstOrThrowBy')) {
|
|
127
|
-
keyToMapReplaced = keyToMap.replace('findFirstOrThrowBy', '')
|
|
128
|
-
ignoreOrderByAndPagination = false;
|
|
129
|
-
method = 'findFirstOrThrow';
|
|
130
|
-
} else if(keyToMap.startsWith('findFirstOrThrow')) {
|
|
131
|
-
keyToMapReplaced = keyToMap.replace('findFirstOrThrow', '');
|
|
132
|
-
ignoreOrderByAndPagination = false;
|
|
133
|
-
ignoreWhere = true;
|
|
134
|
-
onlyBaseWheres = true;
|
|
135
|
-
method = 'findFirstOrThrow';
|
|
136
|
-
} else if(keyToMap.startsWith('findFirstBy')) {
|
|
137
|
-
keyToMapReplaced = keyToMap.replace('findFirstBy', '')
|
|
138
|
-
ignoreOrderByAndPagination = false;
|
|
139
|
-
method = 'findFirst';
|
|
140
|
-
} else if(keyToMap.startsWith('findFirst')) {
|
|
141
|
-
keyToMapReplaced = keyToMap.replace('findFirst', '');
|
|
142
|
-
ignoreOrderByAndPagination = false;
|
|
143
|
-
ignoreWhere = true;
|
|
144
|
-
onlyBaseWheres = true;
|
|
145
|
-
method = 'findFirst';
|
|
146
|
-
} else if(keyToMap.startsWith('countBy')) {
|
|
147
|
-
keyToMapReplaced = keyToMap.replace('countBy', '');
|
|
148
|
-
ignoreOrderByAndPagination = false;
|
|
149
|
-
ignoreSelect = true;
|
|
150
|
-
method = 'count';
|
|
151
|
-
} else if(keyToMap.startsWith('countWhere')) {
|
|
152
|
-
keyToMapReplaced = keyToMap.replace('countWhere', '');
|
|
153
|
-
ignoreOrderByAndPagination = false;
|
|
154
|
-
ignoreWhere = true;
|
|
155
|
-
onlyBaseWheres = true;
|
|
156
|
-
ignoreSelect = true;
|
|
157
|
-
method = 'count';
|
|
158
|
-
whereIndex = 0;
|
|
159
|
-
otherParams.push('where');
|
|
160
|
-
argsCount += 1;
|
|
161
|
-
} else if(keyToMap.startsWith('count')) {
|
|
162
|
-
keyToMapReplaced = keyToMap.replace('count', '');
|
|
163
|
-
ignoreOrderByAndPagination = false;
|
|
164
|
-
ignoreSelect = true;
|
|
165
|
-
ignoreWhere = true;
|
|
166
|
-
onlyBaseWheres = true;
|
|
167
|
-
method = 'count';
|
|
168
|
-
} else if(keyToMap.startsWith('existsBy')) {
|
|
169
|
-
keyToMapReplaced = keyToMap.replace('existsBy', '');
|
|
170
|
-
ignoreSelect = true;
|
|
171
|
-
existsMode = true;
|
|
172
|
-
method = 'findFirst';
|
|
173
|
-
} else if(keyToMap.startsWith('existsWhere')) {
|
|
174
|
-
keyToMapReplaced = keyToMap.replace('existsWhere', '');
|
|
175
|
-
ignoreSelect = true;
|
|
176
|
-
existsMode = true;
|
|
177
|
-
ignoreWhere = true;
|
|
178
|
-
onlyBaseWheres = true;
|
|
179
|
-
method = 'findFirst';
|
|
180
|
-
whereIndex = 0;
|
|
181
|
-
otherParams.push('where');
|
|
182
|
-
argsCount += 1;
|
|
183
|
-
} else if(keyToMap.startsWith('findManyBy')) {
|
|
184
|
-
keyToMapReplaced = keyToMap.replace('findManyBy', '');
|
|
185
|
-
ignoreOrderByAndPagination = false;
|
|
186
|
-
method = 'findMany';
|
|
187
|
-
} else if(keyToMap.startsWith('findMany')) {
|
|
188
|
-
keyToMapReplaced = keyToMap.replace('findMany', '');
|
|
189
|
-
ignoreOrderByAndPagination = false;
|
|
190
|
-
ignoreWhere = true;
|
|
191
|
-
onlyBaseWheres = true;
|
|
192
|
-
method = 'findMany';
|
|
193
|
-
} else if(keyToMap.startsWith('createManyAndReturn')) {
|
|
194
|
-
keyToMapReplaced = keyToMap.replace('createManyAndReturn', '')
|
|
195
|
-
ignoreWhere = true;
|
|
196
|
-
ignoreSkipDuplicates = false;
|
|
197
|
-
method = 'createManyAndReturn';
|
|
198
|
-
dataIndex = 0;
|
|
199
|
-
otherParams.push('data');
|
|
200
|
-
argsCount++;
|
|
201
|
-
} else if(keyToMap.startsWith('createMany')) {
|
|
202
|
-
keyToMapReplaced = keyToMap.replace('createMany', '')
|
|
203
|
-
ignoreWhere = true;
|
|
204
|
-
ignoreSelect = true;
|
|
205
|
-
ignoreSkipDuplicates = false;
|
|
206
|
-
method = 'createMany';
|
|
207
|
-
dataIndex = 0;
|
|
208
|
-
otherParams.push('data');
|
|
209
|
-
argsCount++;
|
|
210
|
-
} else if(keyToMap.startsWith('create')) {
|
|
211
|
-
keyToMapReplaced = keyToMap.replace('create', '')
|
|
212
|
-
ignoreWhere = true;
|
|
213
|
-
method = 'create';
|
|
214
|
-
dataIndex = 0
|
|
215
|
-
otherParams.push('data');
|
|
216
|
-
argsCount++;
|
|
217
|
-
} else if(keyToMap.startsWith('updateManyAndReturnBy')) {
|
|
218
|
-
keyToMapReplaced = keyToMap.replace('updateManyAndReturnBy', '')
|
|
219
|
-
method = 'updateManyAndReturn';
|
|
220
|
-
dataIndex = -2;
|
|
221
|
-
otherParams.push('data');
|
|
222
|
-
argsCount++;
|
|
223
|
-
} else if(keyToMap.startsWith('updateManyAndReturnWhere')) {
|
|
224
|
-
keyToMapReplaced = keyToMap.replace('updateManyAndReturnWhere', '')
|
|
225
|
-
ignoreWhere = true;
|
|
226
|
-
onlyBaseWheres = true;
|
|
227
|
-
method = 'updateManyAndReturn';
|
|
228
|
-
dataIndex = -2;
|
|
229
|
-
whereIndex = 0;
|
|
230
|
-
otherParams.push('where', 'data');
|
|
231
|
-
argsCount += 2;
|
|
232
|
-
} else if(keyToMap.startsWith('updateManyBy')) {
|
|
233
|
-
keyToMapReplaced = keyToMap.replace('updateManyBy', '')
|
|
234
|
-
ignoreSelect = true;
|
|
235
|
-
method = 'updateMany';
|
|
236
|
-
dataIndex = -2;
|
|
237
|
-
otherParams.push('data');
|
|
238
|
-
argsCount++;
|
|
239
|
-
} else if(keyToMap.startsWith('updateManyWhere')) {
|
|
240
|
-
keyToMapReplaced = keyToMap.replace('updateManyWhere', '')
|
|
241
|
-
ignoreSelect = true;
|
|
242
|
-
ignoreWhere = true;
|
|
243
|
-
onlyBaseWheres = true;
|
|
244
|
-
method = 'updateMany';
|
|
245
|
-
dataIndex = -2;
|
|
246
|
-
whereIndex = 0;
|
|
247
|
-
otherParams.push('where', 'data');
|
|
248
|
-
argsCount += 2;
|
|
249
|
-
} else if(keyToMap.startsWith('updateBy')) {
|
|
250
|
-
keyToMapReplaced = keyToMap.replace('updateBy', '')
|
|
251
|
-
method = 'update';
|
|
252
|
-
dataIndex = -2;
|
|
253
|
-
otherParams.push('data');
|
|
254
|
-
argsCount++;
|
|
255
|
-
} else if(keyToMap.startsWith('upsertBy')) {
|
|
256
|
-
keyToMapReplaced = keyToMap.replace('upsertBy', '')
|
|
257
|
-
method = 'upsert';
|
|
258
|
-
createIndex = -2;
|
|
259
|
-
updateIndex = -3;
|
|
260
|
-
otherParams.push('update');
|
|
261
|
-
otherParams.push('create');
|
|
262
|
-
argsCount += 2;
|
|
263
|
-
} else if(keyToMap.startsWith('deleteManyBy')) {
|
|
264
|
-
keyToMapReplaced = keyToMap.replace('deleteManyBy', '')
|
|
265
|
-
ignoreSelect = true;
|
|
266
|
-
method = 'deleteMany';
|
|
267
|
-
} else if(keyToMap.startsWith('deleteManyWhere')) {
|
|
268
|
-
keyToMapReplaced = keyToMap.replace('deleteManyWhere', '')
|
|
269
|
-
ignoreSelect = true;
|
|
270
|
-
ignoreWhere = true;
|
|
271
|
-
onlyBaseWheres = true;
|
|
272
|
-
method = 'deleteMany';
|
|
273
|
-
whereIndex = 0;
|
|
274
|
-
otherParams.push('where');
|
|
275
|
-
argsCount += 1;
|
|
276
|
-
} else if(keyToMap.startsWith('deleteBy')) {
|
|
277
|
-
keyToMapReplaced = keyToMap.replace('deleteBy', '')
|
|
278
|
-
method = 'delete';
|
|
279
|
-
} else if(keyToMap.startsWith('findWhere')) {
|
|
280
|
-
keyToMapReplaced = keyToMap.replace('findWhere', '');
|
|
281
|
-
ignoreOrderByAndPagination = false;
|
|
282
|
-
ignoreWhere = true;
|
|
283
|
-
onlyBaseWheres = true;
|
|
284
|
-
method = 'findFirst';
|
|
285
|
-
whereIndex = 0;
|
|
286
|
-
otherParams.push('where');
|
|
287
|
-
argsCount += 1;
|
|
288
|
-
} else if(keyToMap.startsWith('findOneWhere')) {
|
|
289
|
-
keyToMapReplaced = keyToMap.replace('findOneWhere', '');
|
|
290
|
-
ignoreOrderByAndPagination = false;
|
|
291
|
-
ignoreWhere = true;
|
|
292
|
-
onlyBaseWheres = true;
|
|
293
|
-
method = 'findFirst';
|
|
294
|
-
whereIndex = 0;
|
|
295
|
-
otherParams.push('where');
|
|
296
|
-
argsCount += 1;
|
|
297
|
-
} else if(keyToMap.startsWith('findListWhere')) {
|
|
298
|
-
keyToMapReplaced = keyToMap.replace('findListWhere', '');
|
|
299
|
-
ignoreOrderByAndPagination = false;
|
|
300
|
-
ignoreWhere = true;
|
|
301
|
-
onlyBaseWheres = true;
|
|
302
|
-
method = 'findMany';
|
|
303
|
-
whereIndex = 0;
|
|
304
|
-
otherParams.push('where');
|
|
305
|
-
argsCount += 1;
|
|
306
|
-
} else if(keyToMap.startsWith('findBy')) {
|
|
307
|
-
keyToMapReplaced = keyToMap.replace('findBy', '');
|
|
308
|
-
ignoreOrderByAndPagination = false;
|
|
309
|
-
method = methods[originalKey].fbMode === 'one' ? 'findFirst' : 'findMany';
|
|
310
|
-
} else if(keyToMap.startsWith('findOneBy')) {
|
|
311
|
-
keyToMapReplaced = keyToMap.replace('findOneBy', '');
|
|
312
|
-
ignoreOrderByAndPagination = false;
|
|
313
|
-
method = 'findFirst';
|
|
314
|
-
} else {
|
|
315
|
-
throw new VSRepoBuildError(`[VSRepository] (${buildInstance.tableName}: build) Unknown method: ${keyToMap}.`);
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
if(!ignoreSkipDuplicates) {
|
|
320
|
-
if(keyToMapReplaced.endsWith('SkipDuplicates')) {
|
|
321
|
-
keyToMapReplaced = keyToMapReplaced.replace('SkipDuplicates', '');
|
|
322
|
-
skipDuplicates = true;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
let orderPosition;
|
|
327
|
-
let paginationPosition;
|
|
328
|
-
let injectOrdenation;
|
|
329
|
-
let injectPagination;
|
|
330
|
-
|
|
331
|
-
if(!ignoreOrderByAndPagination) {
|
|
332
|
-
injectOrdenation = methods[originalKey].injectOrdenation;
|
|
333
|
-
injectPagination = methods[originalKey].injectPagination;
|
|
334
|
-
|
|
335
|
-
if(keyToMapReplaced.endsWith('PaginatedAndOrdered')) {
|
|
336
|
-
orderPosition = -2;
|
|
337
|
-
paginationPosition = -3;
|
|
338
|
-
|
|
339
|
-
otherParams.push('pagination');
|
|
340
|
-
otherParams.push('order');
|
|
341
|
-
|
|
342
|
-
keyToMapReplaced = keyToMapReplaced.replace('PaginatedAndOrdered', '')
|
|
343
|
-
|
|
344
|
-
argsCount+=2
|
|
345
|
-
} else if(keyToMapReplaced.endsWith('OrderedAndPaginated')) {
|
|
346
|
-
orderPosition = -3;
|
|
347
|
-
paginationPosition = -2;
|
|
348
|
-
|
|
349
|
-
otherParams.push('order');
|
|
350
|
-
otherParams.push('pagination');
|
|
351
|
-
|
|
352
|
-
keyToMapReplaced = keyToMapReplaced.replace('OrderedAndPaginated', '')
|
|
353
|
-
|
|
354
|
-
argsCount+=2
|
|
355
|
-
} else if(keyToMapReplaced.endsWith('Paginated')) {
|
|
356
|
-
paginationPosition = -2;
|
|
357
|
-
|
|
358
|
-
otherParams.push('pagination');
|
|
359
|
-
|
|
360
|
-
keyToMapReplaced = keyToMapReplaced.replace('Paginated', '')
|
|
361
|
-
|
|
362
|
-
argsCount++
|
|
363
|
-
} else if(keyToMapReplaced.endsWith('Ordered')) {
|
|
364
|
-
orderPosition = -2;
|
|
365
|
-
|
|
366
|
-
otherParams.push('order');
|
|
367
|
-
|
|
368
|
-
keyToMapReplaced = keyToMapReplaced.replace('Ordered', '')
|
|
369
|
-
|
|
370
|
-
argsCount++
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
const whereArgs = [];
|
|
376
|
-
let whereType = methods[originalKey].whereType ?? 'extending';
|
|
377
|
-
let pushWhere = methods[originalKey].pushWhere;
|
|
378
|
-
let whereResolved;
|
|
379
|
-
|
|
380
|
-
if(!ignoreWhere) {
|
|
381
|
-
|
|
382
|
-
function buildWhere(keySplitedAnd) {
|
|
383
|
-
|
|
384
|
-
const buildedWhere = {}
|
|
385
|
-
let nullMode;
|
|
386
|
-
|
|
387
|
-
if(keySplitedAnd.includes('IsNull')){
|
|
388
|
-
buildedWhere.pushProperty = '$$$';
|
|
389
|
-
buildedWhere.autoInjectVal = null;
|
|
390
|
-
nullMode = 'is';
|
|
391
|
-
keySplitedAnd = keySplitedAnd.replace('IsNull', '')
|
|
392
|
-
} else if(keySplitedAnd.includes('IsNotNull')){
|
|
393
|
-
buildedWhere.pushProperty = 'not';
|
|
394
|
-
buildedWhere.autoInjectVal = null;
|
|
395
|
-
nullMode = 'not';
|
|
396
|
-
keySplitedAnd = keySplitedAnd.replace('IsNotNull', '')
|
|
397
|
-
} else if(keySplitedAnd.includes('IsTrue')){
|
|
398
|
-
buildedWhere.pushProperty = '$$$';
|
|
399
|
-
buildedWhere.autoInjectVal = true;
|
|
400
|
-
keySplitedAnd = keySplitedAnd.replace('IsTrue', '')
|
|
401
|
-
} else if(keySplitedAnd.includes('IsFalse')){
|
|
402
|
-
buildedWhere.pushProperty = '$$$';
|
|
403
|
-
buildedWhere.autoInjectVal = false;
|
|
404
|
-
keySplitedAnd = keySplitedAnd.replace('IsFalse', '')
|
|
405
|
-
} else {
|
|
406
|
-
if(keySplitedAnd.includes('Insensitive')){
|
|
407
|
-
buildedWhere.properties = {}
|
|
408
|
-
buildedWhere.properties.mode = 'insensitive';
|
|
409
|
-
keySplitedAnd = keySplitedAnd.replace('Insensitive', '')
|
|
410
|
-
}
|
|
411
|
-
if(keySplitedAnd.includes('Optional')){
|
|
412
|
-
keySplitedAnd = keySplitedAnd.replace('Optional', '');
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
if(keySplitedAnd.includes('NotBetween')){
|
|
416
|
-
buildedWhere.pushProperty = 'not';
|
|
417
|
-
buildedWhere.betweenMode = true;
|
|
418
|
-
keySplitedAnd = keySplitedAnd.replace('NotBetween', '')
|
|
419
|
-
} else if(keySplitedAnd.includes('Between')){
|
|
420
|
-
buildedWhere.pushProperty = '$$$';
|
|
421
|
-
buildedWhere.betweenMode = true;
|
|
422
|
-
keySplitedAnd = keySplitedAnd.replace('Between', '')
|
|
423
|
-
} else if(keySplitedAnd.includes('NotStartsWith')){
|
|
424
|
-
buildedWhere.pushProperty = 'not.startsWith';
|
|
425
|
-
keySplitedAnd = keySplitedAnd.replace('NotStartsWith', '')
|
|
426
|
-
} else if(keySplitedAnd.includes('StartsWith')){
|
|
427
|
-
buildedWhere.pushProperty = 'startsWith';
|
|
428
|
-
keySplitedAnd = keySplitedAnd.replace('StartsWith', '')
|
|
429
|
-
} else if(keySplitedAnd.includes('NotEndsWith')){
|
|
430
|
-
buildedWhere.pushProperty = 'not.endsWith';
|
|
431
|
-
keySplitedAnd = keySplitedAnd.replace('NotEndsWith', '')
|
|
432
|
-
} else if(keySplitedAnd.includes('EndsWith')){
|
|
433
|
-
buildedWhere.pushProperty = 'endsWith';
|
|
434
|
-
keySplitedAnd = keySplitedAnd.replace('EndsWith', '')
|
|
435
|
-
} else if(keySplitedAnd.includes('NotContains')){
|
|
436
|
-
buildedWhere.pushProperty = 'not.contains';
|
|
437
|
-
keySplitedAnd = keySplitedAnd.replace('NotContains', '')
|
|
438
|
-
} else if(keySplitedAnd.includes('Contains')){
|
|
439
|
-
buildedWhere.pushProperty = 'contains';
|
|
440
|
-
keySplitedAnd = keySplitedAnd.replace('Contains', '')
|
|
441
|
-
} else if(keySplitedAnd.includes('LessThanEqual')){
|
|
442
|
-
buildedWhere.pushProperty = 'lte';
|
|
443
|
-
keySplitedAnd = keySplitedAnd.replace('LessThanEqual', '')
|
|
444
|
-
} else if(keySplitedAnd.includes('LessThan')){
|
|
445
|
-
buildedWhere.pushProperty = 'lt';
|
|
446
|
-
keySplitedAnd = keySplitedAnd.replace('LessThan', '')
|
|
447
|
-
} else if(keySplitedAnd.includes('GreaterThanEqual')){
|
|
448
|
-
buildedWhere.pushProperty = 'gte';
|
|
449
|
-
keySplitedAnd = keySplitedAnd.replace('GreaterThanEqual', '')
|
|
450
|
-
} else if(keySplitedAnd.includes('GreaterThan')){
|
|
451
|
-
buildedWhere.pushProperty = 'gt';
|
|
452
|
-
keySplitedAnd = keySplitedAnd.replace('GreaterThan', '')
|
|
453
|
-
} else if(keySplitedAnd.includes('NotIn')){
|
|
454
|
-
buildedWhere.pushProperty = 'notIn';
|
|
455
|
-
keySplitedAnd = keySplitedAnd.replace('NotIn', '')
|
|
456
|
-
} else if(keySplitedAnd.includes('In')){
|
|
457
|
-
buildedWhere.pushProperty = 'in';
|
|
458
|
-
keySplitedAnd = keySplitedAnd.replace('In', '')
|
|
459
|
-
} else if(keySplitedAnd.includes('Not')){
|
|
460
|
-
buildedWhere.pushProperty = 'not';
|
|
461
|
-
keySplitedAnd = keySplitedAnd.replace('Not', '')
|
|
462
|
-
} else {
|
|
463
|
-
buildedWhere.pushProperty = '$$$';
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function uncaptalize(text) {
|
|
468
|
-
return text[0].toLowerCase() + text.slice(1, text.length);
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
if(keySplitedAnd.includes('Without')){
|
|
472
|
-
const keySplitedConector = keySplitedAnd.split('Without');
|
|
473
|
-
const specificField = keySplitedConector[1];
|
|
474
|
-
if(!specificField) {
|
|
475
|
-
if (nullMode) {
|
|
476
|
-
buildedWhere.pushProperty = nullMode === 'not' ? 'is' : 'isNot';
|
|
477
|
-
} else {
|
|
478
|
-
buildedWhere.pushProperty = 'isNot';
|
|
479
|
-
buildedWhere.autoInjectVal = {};
|
|
480
|
-
}
|
|
481
|
-
} else {
|
|
482
|
-
buildedWhere.pushProperty = `isNot.${uncaptalize(specificField)}${buildedWhere.pushProperty === '$$$' ? '' : `.${buildedWhere.pushProperty}`}`;
|
|
483
|
-
}
|
|
484
|
-
keySplitedAnd = keySplitedConector[0];
|
|
485
|
-
} else if(keySplitedAnd.includes('With')){
|
|
486
|
-
const keySplitedConector = keySplitedAnd.split('With');
|
|
487
|
-
const specificField = keySplitedConector[1];
|
|
488
|
-
if(!specificField) {
|
|
489
|
-
if (nullMode) {
|
|
490
|
-
buildedWhere.pushProperty = nullMode === 'not' ? 'isNot' : 'is';
|
|
491
|
-
} else {
|
|
492
|
-
buildedWhere.pushProperty = 'is';
|
|
493
|
-
buildedWhere.autoInjectVal = {};
|
|
494
|
-
}
|
|
495
|
-
} else {
|
|
496
|
-
buildedWhere.pushProperty = `is.${uncaptalize(specificField)}${buildedWhere.pushProperty === '$$$' ? '' : `.${buildedWhere.pushProperty}`}`;
|
|
497
|
-
}
|
|
498
|
-
keySplitedAnd = keySplitedConector[0];
|
|
499
|
-
} else if(keySplitedAnd.includes('Some')){
|
|
500
|
-
const keySplitedConector = keySplitedAnd.split('Some');
|
|
501
|
-
const specificField = keySplitedConector[1];
|
|
502
|
-
if(!specificField) {
|
|
503
|
-
buildedWhere.pushProperty = 'some';
|
|
504
|
-
buildedWhere.autoInjectVal = {};
|
|
505
|
-
} else {
|
|
506
|
-
buildedWhere.pushProperty = `some.${uncaptalize(specificField)}${buildedWhere.pushProperty === '$$$' ? '' : `.${buildedWhere.pushProperty}`}`;
|
|
507
|
-
}
|
|
508
|
-
keySplitedAnd = keySplitedConector[0];
|
|
509
|
-
} else if(keySplitedAnd.includes('Every')){
|
|
510
|
-
const keySplitedConector = keySplitedAnd.split('Every');
|
|
511
|
-
const specificField = keySplitedConector[1];
|
|
512
|
-
if(!specificField) {
|
|
513
|
-
buildedWhere.pushProperty = 'every';
|
|
514
|
-
buildedWhere.autoInjectVal = {};
|
|
515
|
-
} else {
|
|
516
|
-
buildedWhere.pushProperty = `every.${uncaptalize(specificField)}${buildedWhere.pushProperty === '$$$' ? '' : `.${buildedWhere.pushProperty}`}`;
|
|
517
|
-
}
|
|
518
|
-
keySplitedAnd = keySplitedConector[0];
|
|
519
|
-
} else if(keySplitedAnd.includes('None')){
|
|
520
|
-
const keySplitedConector = keySplitedAnd.split('None');
|
|
521
|
-
const specificField = keySplitedConector[1];
|
|
522
|
-
if(!specificField) {
|
|
523
|
-
buildedWhere.pushProperty = 'none';
|
|
524
|
-
buildedWhere.autoInjectVal = {};
|
|
525
|
-
} else {
|
|
526
|
-
buildedWhere.pushProperty = `none.${uncaptalize(specificField)}${buildedWhere.pushProperty === '$$$' ? '' : `.${buildedWhere.pushProperty}`}`;
|
|
527
|
-
}
|
|
528
|
-
keySplitedAnd = keySplitedConector[0];
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
keySplitedAnd = uncaptalize(keySplitedAnd);
|
|
532
|
-
|
|
533
|
-
buildedWhere.name = keySplitedAnd;
|
|
534
|
-
|
|
535
|
-
return buildedWhere;
|
|
536
|
-
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
let ANDMode = false
|
|
540
|
-
let keysSplitedAND = keyToMapReplaced.split("AND");
|
|
541
|
-
if(keysSplitedAND.length > 1) {
|
|
542
|
-
ANDMode = true;
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
keysSplitedAND.forEach((keySplitedAND, idx)=>{
|
|
546
|
-
|
|
547
|
-
let orMode = false;
|
|
548
|
-
let keysSplitedOr = idx === 0 ? keySplitedAND.split('Or') : [keySplitedAND];
|
|
549
|
-
if(keysSplitedOr.length > 1){
|
|
550
|
-
orMode = true;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
for (let i = 0; i < keysSplitedOr.length; i++) {
|
|
554
|
-
|
|
555
|
-
if(keysSplitedOr[i]==='') continue;
|
|
556
|
-
|
|
557
|
-
const keysSplitedAnd = keysSplitedOr[i].split('And')
|
|
558
|
-
|
|
559
|
-
for (const keySplitedAnd of keysSplitedAnd) {
|
|
560
|
-
|
|
561
|
-
if(keySplitedAnd==='') continue;
|
|
562
|
-
|
|
563
|
-
const buildedWhere = buildWhere(keySplitedAnd);
|
|
564
|
-
|
|
565
|
-
if(!orMode) {
|
|
566
|
-
if(idx === 1 && ANDMode){
|
|
567
|
-
buildedWhere.name = `AND.${i}idx.${buildedWhere.name}`;
|
|
568
|
-
}
|
|
569
|
-
whereArgs.push(buildedWhere);
|
|
570
|
-
} else {
|
|
571
|
-
buildedWhere.name = `OR.${i}idx.${buildedWhere.name}`;
|
|
572
|
-
whereArgs.push(buildedWhere);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
if(buildedWhere.autoInjectVal === undefined) argsCount++;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
});
|
|
581
|
-
|
|
582
|
-
whereResolved = whereArgs.map(arg=>{
|
|
583
|
-
let context = [];
|
|
584
|
-
let otherProps
|
|
585
|
-
|
|
586
|
-
let argName = arg.name;
|
|
587
|
-
const agrNameSplitedOr = argName.split('OR.')
|
|
588
|
-
if(agrNameSplitedOr.length > 1) {
|
|
589
|
-
const agrNameSplitedOrIdx = agrNameSplitedOr[1].split('idx.')
|
|
590
|
-
context.push('OR')
|
|
591
|
-
context.push(Number(agrNameSplitedOrIdx[0]))
|
|
592
|
-
argName = agrNameSplitedOrIdx[1]
|
|
593
|
-
} else {
|
|
594
|
-
const agrNameSplitedAND = argName.split('AND.');
|
|
595
|
-
if(agrNameSplitedAND.length > 1) {
|
|
596
|
-
const agrNameSplitedANDIdx = agrNameSplitedAND[1].split('idx.')
|
|
597
|
-
context.push('AND')
|
|
598
|
-
context.push(Number(agrNameSplitedANDIdx[0]))
|
|
599
|
-
argName = agrNameSplitedANDIdx[1];
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
context.push(argName)
|
|
604
|
-
if(arg.pushProperty !== '$$$'){
|
|
605
|
-
const pushProperty = arg.pushProperty;
|
|
606
|
-
const pushPropertySplitedDot = pushProperty.split('.');
|
|
607
|
-
|
|
608
|
-
pushPropertySplitedDot.forEach(prop=>context.push(prop));
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
if(arg.properties !== undefined) {
|
|
612
|
-
const propertiesKeys = Object.keys(arg.properties);
|
|
613
|
-
otherProps = {}
|
|
614
|
-
for (let i = 0; i < propertiesKeys.length; i++) {
|
|
615
|
-
const key = propertiesKeys[i]
|
|
616
|
-
otherProps[key] = arg.properties[key]
|
|
617
|
-
}
|
|
618
|
-
if(arg.pushProperty === '$$$'){
|
|
619
|
-
context.push('equals');
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
|
|
623
|
-
whereParams.push(argName);
|
|
624
|
-
return { context, otherProps, argName, autoVal: arg.autoInjectVal, betweenMode: arg.betweenMode };
|
|
625
|
-
});
|
|
626
|
-
if(showWorking) {
|
|
627
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: build) Where object resolved to ${keyToMap}:\n`, JSON.stringify(whereResolved, null, 2));
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
|
|
631
|
-
let select;
|
|
632
|
-
if(!ignoreSelect) {
|
|
633
|
-
|
|
634
|
-
const providedSelectModel = methods[originalKey].selectModel;
|
|
635
|
-
|
|
636
|
-
if(providedSelectModel) {
|
|
637
|
-
select = buildInstance.selectModels[providedSelectModel];
|
|
638
|
-
} else if(buildInstance.defaultSelectModel && providedSelectModel !== false) {
|
|
639
|
-
select = buildInstance.selectModels[buildInstance.defaultSelectModel];
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
}
|
|
643
|
-
if(existsMode) {
|
|
644
|
-
select = { [buildInstance.pkName]: true };
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
buildInstance.vsrepocache.set(originalKey, (args, optionsSelectModel) => {
|
|
648
|
-
if (prismaArgsIndex !== undefined) return args.at(prismaArgsIndex);
|
|
649
|
-
|
|
650
|
-
const prismaArgs = {};
|
|
651
|
-
|
|
652
|
-
if(!ignoreSelect) {
|
|
653
|
-
prismaArgs.select = existsMode || optionsSelectModel === undefined ?
|
|
654
|
-
select :
|
|
655
|
-
optionsSelectModel === false ?
|
|
656
|
-
undefined :
|
|
657
|
-
buildInstance.selectModels[optionsSelectModel];
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
if(orderPosition !== undefined) {
|
|
661
|
-
prismaArgs.orderBy = args.at(orderPosition);
|
|
662
|
-
} else if(injectOrdenation !== undefined) {
|
|
663
|
-
prismaArgs.orderBy = injectOrdenation;
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
if(paginationPosition !== undefined) {
|
|
667
|
-
const paginate = args.at(paginationPosition);
|
|
668
|
-
prismaArgs.skip = paginate.skip;
|
|
669
|
-
prismaArgs.take = paginate.take;
|
|
670
|
-
prismaArgs.cursor = paginate.cursor;
|
|
671
|
-
} else if(injectPagination !== undefined) {
|
|
672
|
-
prismaArgs.skip = injectPagination.skip;
|
|
673
|
-
prismaArgs.take = injectPagination.take;
|
|
674
|
-
prismaArgs.cursor = injectPagination.cursor;
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
if(skipDuplicates!==undefined){
|
|
678
|
-
prismaArgs.skipDuplicates = skipDuplicates;
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
const assignWhere = (where) => {
|
|
682
|
-
if(whereType==='extending' && buildInstance.requiredWhere) {
|
|
683
|
-
let safeRequiredWhere = structuredClone(buildInstance.requiredWhere);
|
|
684
|
-
mergeWheres(where, safeRequiredWhere);
|
|
685
|
-
}
|
|
686
|
-
if(pushWhere !== undefined) {
|
|
687
|
-
let safePushWhere = structuredClone(pushWhere);
|
|
688
|
-
mergeWheres(where, safePushWhere);
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
|
|
692
|
-
if(!ignoreWhere) {
|
|
693
|
-
|
|
694
|
-
const where = {};
|
|
695
|
-
let OR;
|
|
696
|
-
let AND;
|
|
697
|
-
|
|
698
|
-
let adjust = 0;
|
|
699
|
-
for (let j = 0; j < whereResolved.length; j++) {
|
|
700
|
-
let path = {}
|
|
701
|
-
let current = path
|
|
702
|
-
let ormode = false;
|
|
703
|
-
let andmode = false;
|
|
704
|
-
let modeIdx;
|
|
705
|
-
const currentWhereRslvd = whereResolved[j];
|
|
706
|
-
const context = currentWhereRslvd.context
|
|
707
|
-
const contextLength = context.length;
|
|
708
|
-
const contextLengthM1 = contextLength-1;
|
|
709
|
-
for (let i = 0; i < contextLength; i++) {
|
|
710
|
-
if(i<contextLengthM1){
|
|
711
|
-
if(context[i]==='OR'){
|
|
712
|
-
ormode = true;
|
|
713
|
-
} else if(context[i]==='AND'){
|
|
714
|
-
andmode = true;
|
|
715
|
-
} else if(typeof context[i] === 'number'){
|
|
716
|
-
modeIdx = context[i];
|
|
717
|
-
} else {
|
|
718
|
-
if(!current[context[i]]) current[context[i]] = {};
|
|
719
|
-
current = current[context[i]];
|
|
720
|
-
}
|
|
721
|
-
} else {
|
|
722
|
-
if(currentWhereRslvd.autoVal !== undefined) {
|
|
723
|
-
current[context[i]] = currentWhereRslvd.autoVal;
|
|
724
|
-
adjust++
|
|
725
|
-
} else if (currentWhereRslvd.betweenMode && args[j - adjust] !== undefined) {
|
|
726
|
-
current[context[i]] = {};
|
|
727
|
-
current[context[i]]["gte"] = args[j - adjust][0];
|
|
728
|
-
current[context[i]]["lte"] = args[j - adjust][1];
|
|
729
|
-
} else {
|
|
730
|
-
current[context[i]] = args[j - adjust];
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
}
|
|
734
|
-
|
|
735
|
-
if(currentWhereRslvd.otherProps !== undefined) {
|
|
736
|
-
Object.assign(path[currentWhereRslvd.argName], currentWhereRslvd.otherProps)
|
|
737
|
-
}
|
|
738
|
-
if (ormode) {
|
|
739
|
-
if(!OR) OR = [];
|
|
740
|
-
if(!OR[modeIdx]) OR[modeIdx] = {}
|
|
741
|
-
Object.assign(OR[modeIdx], path)
|
|
742
|
-
} else if(andmode) {
|
|
743
|
-
if(!AND) AND = [];
|
|
744
|
-
if(!AND[modeIdx]) AND[modeIdx] = {}
|
|
745
|
-
Object.assign(AND[modeIdx], path)
|
|
746
|
-
} else {
|
|
747
|
-
Object.assign(where, path);
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
if(OR) where.OR = OR.filter(x=>x!==undefined);
|
|
752
|
-
if(AND) where.AND = AND.filter(x=>x!==undefined);
|
|
753
|
-
|
|
754
|
-
assignWhere(where);
|
|
755
|
-
|
|
756
|
-
prismaArgs.where = where;
|
|
757
|
-
} else if(onlyBaseWheres) {
|
|
758
|
-
const where = whereIndex !== undefined ? args.at(whereIndex) : {};
|
|
759
|
-
|
|
760
|
-
assignWhere(where);
|
|
761
|
-
|
|
762
|
-
prismaArgs.where = where;
|
|
763
|
-
}
|
|
764
|
-
|
|
765
|
-
if(dataIndex !== undefined) {
|
|
766
|
-
prismaArgs.data = args.at(dataIndex);
|
|
767
|
-
}
|
|
768
|
-
if(updateIndex !== undefined) {
|
|
769
|
-
prismaArgs.update = args.at(updateIndex);
|
|
770
|
-
}
|
|
771
|
-
if(createIndex !== undefined) {
|
|
772
|
-
prismaArgs.create = args.at(createIndex);
|
|
773
|
-
}
|
|
774
|
-
|
|
775
|
-
return prismaArgs;
|
|
776
|
-
})
|
|
777
|
-
|
|
778
|
-
buildInstance[originalKey] = async (...args) => {
|
|
779
|
-
|
|
780
|
-
let db = prisma;
|
|
781
|
-
let optionsSelectModel;
|
|
782
|
-
if(args.length < argsCount) {
|
|
783
|
-
const missingParams = whereParams.concat(otherParams).slice(args.length);
|
|
784
|
-
throw new VSRepoRuntimeError(`[VSRepository] (${buildInstance.tableName}: runtime) Missing parameters: ${missingParams.join(', ')}`);
|
|
785
|
-
} else if(args.length > argsCount) {
|
|
786
|
-
const optionsArg = args[args.length - 1];
|
|
787
|
-
db = optionsArg.db ?? db;
|
|
788
|
-
optionsSelectModel = optionsArg.selectModel;
|
|
789
|
-
} else {
|
|
790
|
-
args.push('1')
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
const prismaArgs = buildInstance.vsrepocache.get(originalKey)(args, optionsSelectModel);
|
|
794
|
-
|
|
795
|
-
let start;
|
|
796
|
-
if(showWorking){
|
|
797
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing ${method}.`);
|
|
798
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to ${method}:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
799
|
-
start = performance.now();
|
|
800
|
-
}
|
|
801
|
-
|
|
802
|
-
try {
|
|
803
|
-
const result = await db[buildInstance.tableName][method](prismaArgs);
|
|
804
|
-
|
|
805
|
-
if(showWorking) {
|
|
806
|
-
const end = performance.now();
|
|
807
|
-
const duration = (end - start).toFixed(2);
|
|
808
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed ${method} (took: ${duration}ms).`);
|
|
809
|
-
}
|
|
810
|
-
|
|
811
|
-
if(existsMode) {
|
|
812
|
-
return !!result;
|
|
813
|
-
}
|
|
814
|
-
return result;
|
|
815
|
-
} catch (err) {
|
|
816
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when executing ${method}:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
817
|
-
throw err;
|
|
818
|
-
}
|
|
819
|
-
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
if(config.baseMethods?.get?.active !== false) {
|
|
825
|
-
buildInstance.get = async(pk, options = {}) => {
|
|
826
|
-
let db = options?.db ?? prisma;
|
|
827
|
-
const prismaArgs = {};
|
|
828
|
-
|
|
829
|
-
const where = { [buildInstance.pkName]: pk };
|
|
830
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.get?.ignoreRequiredWhere) {
|
|
831
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
832
|
-
}
|
|
833
|
-
prismaArgs.where = where;
|
|
834
|
-
|
|
835
|
-
let selectModelKey = options?.selectModel ?? config.baseMethods?.get?.defaultSelect ?? buildInstance.defaultSelectModel;
|
|
836
|
-
if(selectModelKey){
|
|
837
|
-
prismaArgs.select = buildInstance.selectModels[selectModelKey];
|
|
838
|
-
}
|
|
839
|
-
|
|
840
|
-
let start;
|
|
841
|
-
if(showWorking){
|
|
842
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing get.`);
|
|
843
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to get:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
844
|
-
start = performance.now();
|
|
845
|
-
}
|
|
846
|
-
|
|
847
|
-
try {
|
|
848
|
-
const result = await db[buildInstance.tableName].findUnique(prismaArgs);
|
|
849
|
-
|
|
850
|
-
if(showWorking) {
|
|
851
|
-
const end = performance.now();
|
|
852
|
-
const duration = (end - start).toFixed(2);
|
|
853
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed get (took: ${duration}ms).`);
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
return result;
|
|
857
|
-
} catch (err) {
|
|
858
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to get:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
859
|
-
throw err;
|
|
860
|
-
}
|
|
861
|
-
}
|
|
862
|
-
}
|
|
863
|
-
if(config.baseMethods?.getOrThrow?.active !== false) {
|
|
864
|
-
buildInstance.getOrThrow = async(pk, options = {}) => {
|
|
865
|
-
let db = options?.db ?? prisma;
|
|
866
|
-
const prismaArgs = {};
|
|
867
|
-
|
|
868
|
-
const where = { [buildInstance.pkName]: pk };
|
|
869
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.getOrThrow?.ignoreRequiredWhere) {
|
|
870
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
871
|
-
}
|
|
872
|
-
prismaArgs.where = where;
|
|
873
|
-
|
|
874
|
-
let selectModelKey = options?.selectModel ?? config.baseMethods?.getOrThrow?.defaultSelect ?? buildInstance.defaultSelectModel;
|
|
875
|
-
if(selectModelKey){
|
|
876
|
-
prismaArgs.select = buildInstance.selectModels[selectModelKey];
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
let start;
|
|
880
|
-
if(showWorking){
|
|
881
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing getOrThrow.`);
|
|
882
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to getOrThrow:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
883
|
-
start = performance.now();
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
try {
|
|
887
|
-
const result = await db[buildInstance.tableName].findUniqueOrThrow(prismaArgs);
|
|
888
|
-
|
|
889
|
-
if(showWorking) {
|
|
890
|
-
const end = performance.now();
|
|
891
|
-
const duration = (end - start).toFixed(2);
|
|
892
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed getOrThrow (took: ${duration}ms).`);
|
|
893
|
-
}
|
|
894
|
-
|
|
895
|
-
return result;
|
|
896
|
-
} catch (err) {
|
|
897
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to getOrThrow:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
898
|
-
throw err;
|
|
899
|
-
}
|
|
900
|
-
}
|
|
901
|
-
}
|
|
902
|
-
if(config.baseMethods?.remove?.active !== false) {
|
|
903
|
-
buildInstance.remove = async (pk, options = {}) => {
|
|
904
|
-
let db = options?.db ?? prisma;
|
|
905
|
-
const prismaArgs = {};
|
|
906
|
-
|
|
907
|
-
const where = { [buildInstance.pkName]: pk };
|
|
908
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.remove?.ignoreRequiredWhere) {
|
|
909
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
910
|
-
}
|
|
911
|
-
prismaArgs.where = where;
|
|
912
|
-
|
|
913
|
-
let selectModelKey = options?.selectModel ?? config.baseMethods?.remove?.defaultSelect ?? buildInstance.defaultSelectModel;
|
|
914
|
-
if(selectModelKey){
|
|
915
|
-
prismaArgs.select = buildInstance.selectModels[selectModelKey];
|
|
916
|
-
}
|
|
917
|
-
|
|
918
|
-
let start;
|
|
919
|
-
if(showWorking){
|
|
920
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing remove.`);
|
|
921
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to remove:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
922
|
-
start = performance.now();
|
|
923
|
-
}
|
|
924
|
-
try {
|
|
925
|
-
const result = await db[buildInstance.tableName].delete(prismaArgs);
|
|
926
|
-
|
|
927
|
-
if(showWorking) {
|
|
928
|
-
const end = performance.now();
|
|
929
|
-
const duration = (end - start).toFixed(2);
|
|
930
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed remove (took: ${duration}ms).`);
|
|
931
|
-
}
|
|
932
|
-
|
|
933
|
-
return result;
|
|
934
|
-
} catch (err) {
|
|
935
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to remove:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
936
|
-
throw err;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
}
|
|
940
|
-
if(config.baseMethods?.save?.active !== false) {
|
|
941
|
-
buildInstance.save = async (obj, options = {}) => {
|
|
942
|
-
let db = options?.db ?? prisma;
|
|
943
|
-
const prismaArgs = {};
|
|
944
|
-
|
|
945
|
-
let selectModelKey = options?.selectModel ?? config.baseMethods?.save?.defaultSelect ?? buildInstance.defaultSelectModel;
|
|
946
|
-
if(selectModelKey){
|
|
947
|
-
prismaArgs.select = buildInstance.selectModels[selectModelKey];
|
|
948
|
-
}
|
|
949
|
-
|
|
950
|
-
let start;
|
|
951
|
-
if(showWorking){
|
|
952
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing save.`);
|
|
953
|
-
start = performance.now();
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
try {
|
|
957
|
-
let result;
|
|
958
|
-
if(obj[buildInstance.pkName] !== undefined){
|
|
959
|
-
prismaArgs.create = {};
|
|
960
|
-
prismaArgs.update = {};
|
|
961
|
-
|
|
962
|
-
const keys = Object.keys(obj);
|
|
963
|
-
for (let i = 0; i < keys.length; i++) {
|
|
964
|
-
const key = keys[i];
|
|
965
|
-
const field = obj[key];
|
|
966
|
-
|
|
967
|
-
if(field === undefined) continue;
|
|
968
|
-
|
|
969
|
-
if(relationsKeys.includes(key)){
|
|
970
|
-
const relation = buildInstance.relations[key];
|
|
971
|
-
const relationMode = relation.mode;
|
|
972
|
-
const relationRestriction = relation.restriction;
|
|
973
|
-
const relationPk = relation.pk;
|
|
974
|
-
|
|
975
|
-
if(relationMode === 'oto' || relationMode === 'mto') {
|
|
976
|
-
if(field === null) {
|
|
977
|
-
if(relationMode === 'oto' && relationRestriction === 'set') {
|
|
978
|
-
prismaArgs.update[key] = { delete: true };
|
|
979
|
-
} else if(relationMode === 'mto' && relation.nullAble) {
|
|
980
|
-
prismaArgs.update[key] = { disconnect: true };
|
|
981
|
-
}
|
|
982
|
-
} else if (field !== undefined) {
|
|
983
|
-
const relationFieldPk = field[relationPk];
|
|
984
|
-
if(relationFieldPk){
|
|
985
|
-
const connectOrCreate = {
|
|
986
|
-
where: { [relationPk]: relationFieldPk },
|
|
987
|
-
create: field
|
|
988
|
-
};
|
|
989
|
-
|
|
990
|
-
prismaArgs.create[key] = { connectOrCreate };
|
|
991
|
-
|
|
992
|
-
if(relationRestriction === 'add') {
|
|
993
|
-
prismaArgs.update[key] = { connectOrCreate };
|
|
994
|
-
} else {
|
|
995
|
-
const update = {...field};
|
|
996
|
-
delete update[relationPk];
|
|
997
|
-
|
|
998
|
-
prismaArgs.update[key] = {
|
|
999
|
-
upsert: {
|
|
1000
|
-
where: { [relationPk]: relationFieldPk },
|
|
1001
|
-
create: field,
|
|
1002
|
-
update: update
|
|
1003
|
-
}
|
|
1004
|
-
};
|
|
1005
|
-
}
|
|
1006
|
-
} else {
|
|
1007
|
-
prismaArgs.create[key] = { create: field };
|
|
1008
|
-
prismaArgs.update[key] = relationRestriction === 'add' ?
|
|
1009
|
-
{ create: field } :
|
|
1010
|
-
{ upsert: { create: field, update: field } };
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
} else if(Array.isArray(field)) {
|
|
1014
|
-
const dataWithPk = field.filter(data=>data[relationPk]!==undefined);
|
|
1015
|
-
const dataWithoutPk = field.filter(data=>data[relationPk]===undefined);
|
|
1016
|
-
|
|
1017
|
-
const connectOrCreate = dataWithPk.map(data=>({
|
|
1018
|
-
where: { [relationPk]: data[relationPk] },
|
|
1019
|
-
create: data
|
|
1020
|
-
}));
|
|
1021
|
-
|
|
1022
|
-
prismaArgs.create[key] = {
|
|
1023
|
-
create: dataWithoutPk,
|
|
1024
|
-
connectOrCreate
|
|
1025
|
-
};
|
|
1026
|
-
|
|
1027
|
-
if(relationRestriction === 'add') {
|
|
1028
|
-
if(relationMode === 'mtm') {
|
|
1029
|
-
prismaArgs.update[key] = {
|
|
1030
|
-
create: dataWithoutPk,
|
|
1031
|
-
connectOrCreate
|
|
1032
|
-
}
|
|
1033
|
-
} else {
|
|
1034
|
-
prismaArgs.update[key] = {
|
|
1035
|
-
create: dataWithoutPk,
|
|
1036
|
-
upsert: dataWithPk.map(data=>{
|
|
1037
|
-
const update = { ...data };
|
|
1038
|
-
delete update[relationPk];
|
|
1039
|
-
return {
|
|
1040
|
-
where: { [relationPk]: data[relationPk] },
|
|
1041
|
-
create: data, update
|
|
1042
|
-
}
|
|
1043
|
-
})
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
} else {
|
|
1047
|
-
if(relationMode === 'mtm') {
|
|
1048
|
-
prismaArgs.update[key] = {
|
|
1049
|
-
set: [],
|
|
1050
|
-
create: dataWithoutPk,
|
|
1051
|
-
connectOrCreate
|
|
1052
|
-
}
|
|
1053
|
-
} else {
|
|
1054
|
-
prismaArgs.update[key] = {
|
|
1055
|
-
deleteMany: { [relationPk]: { notIn: dataWithPk.map(data=>data[relationPk]) } },
|
|
1056
|
-
create: dataWithoutPk,
|
|
1057
|
-
upsert: dataWithPk.map(data=>{
|
|
1058
|
-
const update = { ...data };
|
|
1059
|
-
delete update[relationPk];
|
|
1060
|
-
return {
|
|
1061
|
-
where: { [relationPk]: data[relationPk] },
|
|
1062
|
-
create: data, update
|
|
1063
|
-
}
|
|
1064
|
-
})
|
|
1065
|
-
}
|
|
1066
|
-
}
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
} else {
|
|
1070
|
-
prismaArgs.create[key] = field;
|
|
1071
|
-
if(key !== buildInstance.pkName) {
|
|
1072
|
-
prismaArgs.update[key] = field;
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
|
|
1077
|
-
const where = { [buildInstance.pkName]: obj[buildInstance.pkName] };
|
|
1078
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.save?.ignoreRequiredWhere) {
|
|
1079
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
1080
|
-
}
|
|
1081
|
-
prismaArgs.where = where;
|
|
1082
|
-
|
|
1083
|
-
if(showWorking) {
|
|
1084
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to save:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1085
|
-
}
|
|
1086
|
-
|
|
1087
|
-
result = await db[buildInstance.tableName].upsert(prismaArgs);
|
|
1088
|
-
} else {
|
|
1089
|
-
prismaArgs.data = {};
|
|
1090
|
-
|
|
1091
|
-
const keys = Object.keys(obj);
|
|
1092
|
-
for (let i = 0; i < keys.length; i++) {
|
|
1093
|
-
const key = keys[i];
|
|
1094
|
-
const field = obj[key];
|
|
1095
|
-
|
|
1096
|
-
if(field == null) continue;
|
|
1097
|
-
|
|
1098
|
-
if(relationsKeys.includes(key)){
|
|
1099
|
-
const relation = buildInstance.relations[key];
|
|
1100
|
-
const relationMode = relation.mode;
|
|
1101
|
-
const relationPk = relation.pk;
|
|
1102
|
-
|
|
1103
|
-
if(relationMode === 'otm' || relationMode === 'mtm') {
|
|
1104
|
-
const dataWithPk = field.filter(data=>data[relationPk]!==undefined);
|
|
1105
|
-
const dataWithoutPk = field.filter(data=>data[relationPk]===undefined);
|
|
1106
|
-
|
|
1107
|
-
prismaArgs.data[key] = {
|
|
1108
|
-
create: dataWithoutPk,
|
|
1109
|
-
connectOrCreate: dataWithPk.map(data=>({
|
|
1110
|
-
where: { [relationPk]: data[relationPk] },
|
|
1111
|
-
create: data
|
|
1112
|
-
}))
|
|
1113
|
-
}
|
|
1114
|
-
} else {
|
|
1115
|
-
const relationFieldPk = field[relationPk];
|
|
1116
|
-
if(relationFieldPk !== undefined) {
|
|
1117
|
-
prismaArgs.data[key] = {
|
|
1118
|
-
connectOrCreate: {
|
|
1119
|
-
where: { [relationPk]: relationFieldPk },
|
|
1120
|
-
create: field
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
} else {
|
|
1124
|
-
prismaArgs.data[key] = {
|
|
1125
|
-
create: field
|
|
1126
|
-
}
|
|
1127
|
-
}
|
|
1128
|
-
}
|
|
1129
|
-
} else {
|
|
1130
|
-
prismaArgs.data[key] = field;
|
|
1131
|
-
}
|
|
1132
|
-
}
|
|
1133
|
-
|
|
1134
|
-
if(showWorking) {
|
|
1135
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to save:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1136
|
-
}
|
|
1137
|
-
|
|
1138
|
-
result = await db[buildInstance.tableName].create(prismaArgs);
|
|
1139
|
-
}
|
|
1140
|
-
|
|
1141
|
-
if(showWorking) {
|
|
1142
|
-
const end = performance.now();
|
|
1143
|
-
const duration = (end - start).toFixed(2);
|
|
1144
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed save (took: ${duration}ms).`);
|
|
1145
|
-
}
|
|
1146
|
-
|
|
1147
|
-
return result;
|
|
1148
|
-
} catch (err) {
|
|
1149
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to save:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
1150
|
-
throw err;
|
|
1151
|
-
}
|
|
1152
|
-
}
|
|
1153
|
-
}
|
|
1154
|
-
if(config.baseMethods?.removeList?.active !== false) {
|
|
1155
|
-
buildInstance.removeList = async (pks, options = {}) => {
|
|
1156
|
-
let db = options?.db ?? prisma;
|
|
1157
|
-
const prismaArgs = {};
|
|
1158
|
-
|
|
1159
|
-
const where = { [buildInstance.pkName]: { in: pks } };
|
|
1160
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.removeList?.ignoreRequiredWhere) {
|
|
1161
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
1162
|
-
}
|
|
1163
|
-
prismaArgs.where = where;
|
|
1164
|
-
|
|
1165
|
-
let start;
|
|
1166
|
-
if(showWorking){
|
|
1167
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing removeList.`);
|
|
1168
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to removeList:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1169
|
-
start = performance.now();
|
|
1170
|
-
}
|
|
1171
|
-
try {
|
|
1172
|
-
const result = await db[buildInstance.tableName].deleteMany(prismaArgs);
|
|
1173
|
-
if(showWorking) {
|
|
1174
|
-
const end = performance.now();
|
|
1175
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed removeList (took: ${(end - start).toFixed(2)}ms).`);
|
|
1176
|
-
}
|
|
1177
|
-
return result;
|
|
1178
|
-
} catch (err) {
|
|
1179
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to removeList:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
1180
|
-
throw err;
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
if(config.baseMethods?.getAll?.active !== false) {
|
|
1185
|
-
buildInstance.getAll = async (options = {}) => {
|
|
1186
|
-
let db = options?.db ?? prisma;
|
|
1187
|
-
const prismaArgs = {};
|
|
1188
|
-
|
|
1189
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.getAll?.ignoreRequiredWhere) {
|
|
1190
|
-
prismaArgs.where = { ...buildInstance.requiredWhere };
|
|
1191
|
-
}
|
|
1192
|
-
|
|
1193
|
-
let selectModelKey = options?.selectModel ?? config.baseMethods?.getAll?.defaultSelect ?? buildInstance.defaultSelectModel;
|
|
1194
|
-
if(selectModelKey){
|
|
1195
|
-
prismaArgs.select = buildInstance.selectModels[selectModelKey];
|
|
1196
|
-
}
|
|
1197
|
-
|
|
1198
|
-
if(options.pagination) {
|
|
1199
|
-
prismaArgs.skip = options.pagination.skip;
|
|
1200
|
-
prismaArgs.take = options.pagination.take;
|
|
1201
|
-
prismaArgs.cursor = options.pagination.cursor;
|
|
1202
|
-
}
|
|
1203
|
-
if(options.order) {
|
|
1204
|
-
prismaArgs.orderBy = options.order;
|
|
1205
|
-
}
|
|
1206
|
-
|
|
1207
|
-
let start;
|
|
1208
|
-
if(showWorking){
|
|
1209
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing getAll.`);
|
|
1210
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to getAll:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1211
|
-
start = performance.now();
|
|
1212
|
-
}
|
|
1213
|
-
try {
|
|
1214
|
-
const result = await db[buildInstance.tableName].findMany(prismaArgs);
|
|
1215
|
-
if(showWorking) {
|
|
1216
|
-
const end = performance.now();
|
|
1217
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed getAll (took: ${(end - start).toFixed(2)}ms).`);
|
|
1218
|
-
}
|
|
1219
|
-
return result;
|
|
1220
|
-
} catch (err) {
|
|
1221
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to getAll:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
1222
|
-
throw err;
|
|
1223
|
-
}
|
|
1224
|
-
}
|
|
1225
|
-
}
|
|
1226
|
-
if(config.baseMethods?.total?.active !== false) {
|
|
1227
|
-
buildInstance.total = async (options = {}) => {
|
|
1228
|
-
let db = options?.db ?? prisma;
|
|
1229
|
-
const prismaArgs = {};
|
|
1230
|
-
|
|
1231
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.total?.ignoreRequiredWhere) {
|
|
1232
|
-
prismaArgs.where = { ...buildInstance.requiredWhere };
|
|
1233
|
-
}
|
|
1234
|
-
|
|
1235
|
-
let start;
|
|
1236
|
-
if(showWorking){
|
|
1237
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing total.`);
|
|
1238
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to total:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1239
|
-
start = performance.now();
|
|
1240
|
-
}
|
|
1241
|
-
try {
|
|
1242
|
-
const result = await db[buildInstance.tableName].count(prismaArgs);
|
|
1243
|
-
if(showWorking) {
|
|
1244
|
-
const end = performance.now();
|
|
1245
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed total (took: ${(end - start).toFixed(2)}ms).`);
|
|
1246
|
-
}
|
|
1247
|
-
return result;
|
|
1248
|
-
} catch (err) {
|
|
1249
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to total:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
1250
|
-
throw err;
|
|
1251
|
-
}
|
|
1252
|
-
}
|
|
1253
|
-
}
|
|
1254
|
-
if(config.baseMethods?.has?.active !== false) {
|
|
1255
|
-
buildInstance.has = async (pk, options = {}) => {
|
|
1256
|
-
let db = options?.db ?? prisma;
|
|
1257
|
-
const prismaArgs = {};
|
|
1258
|
-
|
|
1259
|
-
const where = { [buildInstance.pkName]: pk };
|
|
1260
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.has?.ignoreRequiredWhere) {
|
|
1261
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
1262
|
-
}
|
|
1263
|
-
prismaArgs.where = where;
|
|
1264
|
-
prismaArgs.select = { [buildInstance.pkName]: true };
|
|
1265
|
-
|
|
1266
|
-
let start;
|
|
1267
|
-
if(showWorking){
|
|
1268
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing has.`);
|
|
1269
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to has:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1270
|
-
start = performance.now();
|
|
1271
|
-
}
|
|
1272
|
-
try {
|
|
1273
|
-
const result = await db[buildInstance.tableName].findUnique(prismaArgs);
|
|
1274
|
-
if(showWorking) {
|
|
1275
|
-
const end = performance.now();
|
|
1276
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed has (took: ${(end - start).toFixed(2)}ms).`);
|
|
1277
|
-
}
|
|
1278
|
-
return !!result;
|
|
1279
|
-
} catch (err) {
|
|
1280
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to has:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
1281
|
-
throw err;
|
|
1282
|
-
}
|
|
1283
|
-
}
|
|
1284
|
-
}
|
|
1285
|
-
if(config.baseMethods?.patch?.active !== false) {
|
|
1286
|
-
buildInstance.patch = async (pk, obj, options = {}) => {
|
|
1287
|
-
let db = options?.db ?? prisma;
|
|
1288
|
-
const prismaArgs = {};
|
|
1289
|
-
|
|
1290
|
-
let selectModelKey = options?.selectModel ?? config.baseMethods?.patch?.defaultSelect ?? buildInstance.defaultSelectModel;
|
|
1291
|
-
if(selectModelKey){
|
|
1292
|
-
prismaArgs.select = buildInstance.selectModels[selectModelKey];
|
|
1293
|
-
}
|
|
1294
|
-
|
|
1295
|
-
let start;
|
|
1296
|
-
if(showWorking){
|
|
1297
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executing patch.`);
|
|
1298
|
-
start = performance.now();
|
|
1299
|
-
}
|
|
1300
|
-
|
|
1301
|
-
try {
|
|
1302
|
-
let result;
|
|
1303
|
-
prismaArgs.data = {};
|
|
1304
|
-
|
|
1305
|
-
const keys = Object.keys(obj);
|
|
1306
|
-
for (let i = 0; i < keys.length; i++) {
|
|
1307
|
-
const key = keys[i];
|
|
1308
|
-
const field = obj[key];
|
|
1309
|
-
|
|
1310
|
-
if(field === undefined) continue;
|
|
1311
|
-
|
|
1312
|
-
if(relationsKeys.includes(key)){
|
|
1313
|
-
const relation = buildInstance.relations[key];
|
|
1314
|
-
const relationMode = relation.mode;
|
|
1315
|
-
const relationRestriction = relation.restriction;
|
|
1316
|
-
const relationPk = relation.pk;
|
|
1317
|
-
|
|
1318
|
-
if(relationMode === 'oto' || relationMode === 'mto') {
|
|
1319
|
-
if(field === null) {
|
|
1320
|
-
if(relationMode === 'oto' && relationRestriction === 'set') {
|
|
1321
|
-
prismaArgs.data[key] = { delete: true };
|
|
1322
|
-
} else if(relationMode === 'mto' && relation.nullAble) {
|
|
1323
|
-
prismaArgs.data[key] = { disconnect: true };
|
|
1324
|
-
}
|
|
1325
|
-
} else if (field !== undefined) {
|
|
1326
|
-
const relationFieldPk = field[relationPk];
|
|
1327
|
-
if(relationFieldPk){
|
|
1328
|
-
const connectOrCreate = {
|
|
1329
|
-
where: { [relationPk]: relationFieldPk },
|
|
1330
|
-
create: field
|
|
1331
|
-
};
|
|
1332
|
-
|
|
1333
|
-
if(relationRestriction === 'add') {
|
|
1334
|
-
prismaArgs.data[key] = { connectOrCreate };
|
|
1335
|
-
} else {
|
|
1336
|
-
const update = {...field};
|
|
1337
|
-
delete update[relationPk];
|
|
1338
|
-
|
|
1339
|
-
prismaArgs.data[key] = {
|
|
1340
|
-
upsert: {
|
|
1341
|
-
where: { [relationPk]: relationFieldPk },
|
|
1342
|
-
create: field,
|
|
1343
|
-
update: update
|
|
1344
|
-
}
|
|
1345
|
-
};
|
|
1346
|
-
}
|
|
1347
|
-
} else {
|
|
1348
|
-
prismaArgs.data[key] = relationRestriction === 'add' ?
|
|
1349
|
-
{ create: field } :
|
|
1350
|
-
{ upsert: { create: field, update: field } };
|
|
1351
|
-
}
|
|
1352
|
-
}
|
|
1353
|
-
} else if(Array.isArray(field)) {
|
|
1354
|
-
const dataWithPk = field.filter(data=>data[relationPk]!==undefined);
|
|
1355
|
-
const dataWithoutPk = field.filter(data=>data[relationPk]===undefined);
|
|
1356
|
-
|
|
1357
|
-
const connectOrCreate = dataWithPk.map(data=>({
|
|
1358
|
-
where: { [relationPk]: data[relationPk] },
|
|
1359
|
-
create: data
|
|
1360
|
-
}));
|
|
1361
|
-
|
|
1362
|
-
if(relationRestriction === 'add') {
|
|
1363
|
-
if(relationMode === 'mtm') {
|
|
1364
|
-
prismaArgs.data[key] = {
|
|
1365
|
-
create: dataWithoutPk,
|
|
1366
|
-
connectOrCreate
|
|
1367
|
-
}
|
|
1368
|
-
} else {
|
|
1369
|
-
prismaArgs.data[key] = {
|
|
1370
|
-
create: dataWithoutPk,
|
|
1371
|
-
upsert: dataWithPk.map(data=>{
|
|
1372
|
-
const update = { ...data };
|
|
1373
|
-
delete update[relationPk];
|
|
1374
|
-
return {
|
|
1375
|
-
where: { [relationPk]: data[relationPk] },
|
|
1376
|
-
create: data, update
|
|
1377
|
-
}
|
|
1378
|
-
})
|
|
1379
|
-
}
|
|
1380
|
-
}
|
|
1381
|
-
} else {
|
|
1382
|
-
if(relationMode === 'mtm') {
|
|
1383
|
-
prismaArgs.data[key] = {
|
|
1384
|
-
set: [],
|
|
1385
|
-
create: dataWithoutPk,
|
|
1386
|
-
connectOrCreate
|
|
1387
|
-
}
|
|
1388
|
-
} else {
|
|
1389
|
-
prismaArgs.data[key] = {
|
|
1390
|
-
deleteMany: { [relationPk]: { notIn: dataWithPk.map(data=>data[relationPk]) } },
|
|
1391
|
-
create: dataWithoutPk,
|
|
1392
|
-
upsert: dataWithPk.map(data=>{
|
|
1393
|
-
const update = { ...data };
|
|
1394
|
-
delete update[relationPk];
|
|
1395
|
-
return {
|
|
1396
|
-
where: { [relationPk]: data[relationPk] },
|
|
1397
|
-
create: data, update
|
|
1398
|
-
}
|
|
1399
|
-
})
|
|
1400
|
-
}
|
|
1401
|
-
}
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
} else {
|
|
1405
|
-
if (key !== buildInstance.pkName) {
|
|
1406
|
-
prismaArgs.data[key] = field;
|
|
1407
|
-
}
|
|
1408
|
-
}
|
|
1409
|
-
}
|
|
1410
|
-
|
|
1411
|
-
const where = { [buildInstance.pkName]: pk };
|
|
1412
|
-
if(buildInstance.requiredWhere && !config.baseMethods?.patch?.ignoreRequiredWhere) {
|
|
1413
|
-
Object.assign(where, buildInstance.requiredWhere);
|
|
1414
|
-
}
|
|
1415
|
-
prismaArgs.where = where;
|
|
1416
|
-
|
|
1417
|
-
if(showWorking) {
|
|
1418
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Built arguments to patch:\n`, JSON.stringify(prismaArgs, null, 2));
|
|
1419
|
-
}
|
|
1420
|
-
|
|
1421
|
-
result = await db[buildInstance.tableName].update(prismaArgs);
|
|
1422
|
-
|
|
1423
|
-
if(showWorking) {
|
|
1424
|
-
const end = performance.now();
|
|
1425
|
-
const duration = (end - start).toFixed(2);
|
|
1426
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Executed patch (took: ${duration}ms).`);
|
|
1427
|
-
}
|
|
1428
|
-
|
|
1429
|
-
return result;
|
|
1430
|
-
} catch (err) {
|
|
1431
|
-
console.log(`[VSRepository] (${buildInstance.tableName}: runtime) Fatal error when trying to patch:\n`, JSON.stringify({ prismaArgs }, null, 2));
|
|
1432
|
-
throw err;
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
}
|
|
1436
|
-
|
|
1437
|
-
buildInstance.prisma = prisma;
|
|
1438
|
-
|
|
1439
|
-
if(config.freeze) Object.freeze(buildInstance);
|
|
1440
|
-
return buildInstance;
|
|
1441
|
-
}
|
|
1442
|
-
|
|
1443
|
-
}
|
|
1444
|
-
|
|
1445
|
-
export const setupVSRepo = () => (config) => new VSRepository(config);
|