prisma-ts-select 0.0.33

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.
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,565 @@
1
+ 'use strict';
2
+
3
+ var extension = require('@prisma/client/extension');
4
+ var tsPattern = require('ts-pattern');
5
+
6
+ const DB = {
7
+ "User": {
8
+ "fields": {
9
+ "id": "Int",
10
+ "email": "String",
11
+ "name": "?String"
12
+ },
13
+ "relations": {
14
+ "Post": {
15
+ "id": [
16
+ "authorId",
17
+ "lastModifiedById"
18
+ ]
19
+ },
20
+ "LikedPosts": {
21
+ "id": [
22
+ "authorId"
23
+ ]
24
+ }
25
+ }
26
+ },
27
+ "Post": {
28
+ "fields": {
29
+ "id": "Int",
30
+ "title": "String",
31
+ "content": "?String",
32
+ "published": "Boolean",
33
+ "authorId": "Int",
34
+ "lastModifiedById": "Int"
35
+ },
36
+ "relations": {
37
+ "User": {
38
+ "authorId": [
39
+ "id"
40
+ ],
41
+ "lastModifiedById": [
42
+ "id"
43
+ ]
44
+ },
45
+ "PostsImages": {
46
+ "id": [
47
+ "postId"
48
+ ]
49
+ },
50
+ "LikedPosts": {
51
+ "id": [
52
+ "postId"
53
+ ]
54
+ }
55
+ }
56
+ },
57
+ "PostsImages": {
58
+ "fields": {
59
+ "id": "Int",
60
+ "url": "String",
61
+ "postId": "Int"
62
+ },
63
+ "relations": {
64
+ "Post": {
65
+ "postId": [
66
+ "id"
67
+ ]
68
+ }
69
+ }
70
+ },
71
+ "LikedPosts": {
72
+ "fields": {
73
+ "id": "Int",
74
+ "postId": "Int",
75
+ "authorId": "Int"
76
+ },
77
+ "relations": {
78
+ "Post": {
79
+ "postId": [
80
+ "id"
81
+ ]
82
+ },
83
+ "User": {
84
+ "authorId": [
85
+ "id"
86
+ ]
87
+ }
88
+ }
89
+ },
90
+ "MFId_Category": {
91
+ "fields": {
92
+ "id": "Int",
93
+ "name": "String"
94
+ },
95
+ "relations": {
96
+ "MFId_CategoryPost": {
97
+ "id": [
98
+ "categoryId"
99
+ ]
100
+ }
101
+ }
102
+ },
103
+ "MFId_CategoryPost": {
104
+ "fields": {
105
+ "categoryId": "Int",
106
+ "postId": "Int"
107
+ },
108
+ "relations": {
109
+ "MFId_Category": {
110
+ "categoryId": [
111
+ "id"
112
+ ]
113
+ },
114
+ "MFId_Post": {
115
+ "postId": [
116
+ "id"
117
+ ]
118
+ }
119
+ }
120
+ },
121
+ "MFId_Post": {
122
+ "relations": {
123
+ "MFId_CategoryPost": {
124
+ "id": [
125
+ "postId"
126
+ ]
127
+ }
128
+ },
129
+ "fields": {
130
+ "id": "Int",
131
+ "title": "String"
132
+ }
133
+ },
134
+ "M2M_Post": {
135
+ "fields": {
136
+ "id": "Int",
137
+ "title": "String"
138
+ },
139
+ "relations": {
140
+ "_M2M_CategoryToM2M_Post": {
141
+ "id": [
142
+ "B"
143
+ ]
144
+ }
145
+ }
146
+ },
147
+ "_M2M_CategoryToM2M_Post": {
148
+ "fields": {
149
+ "A": "Int",
150
+ "B": "Int"
151
+ },
152
+ "relations": {
153
+ "M2M_Post": {
154
+ "B": [
155
+ "id"
156
+ ]
157
+ },
158
+ "M2M_Category": {
159
+ "A": [
160
+ "id"
161
+ ]
162
+ }
163
+ }
164
+ },
165
+ "M2M_Category": {
166
+ "fields": {
167
+ "id": "Int",
168
+ "name": "String"
169
+ },
170
+ "relations": {
171
+ "_M2M_CategoryToM2M_Post": {
172
+ "id": [
173
+ "A"
174
+ ]
175
+ }
176
+ }
177
+ },
178
+ "M2M_NC_Category": {
179
+ "fields": {
180
+ "id": "Int",
181
+ "name": "String"
182
+ },
183
+ "relations": {
184
+ "_M2M_NC": {
185
+ "id": [
186
+ "A"
187
+ ]
188
+ }
189
+ }
190
+ },
191
+ "_M2M_NC": {
192
+ "fields": {
193
+ "A": "Int",
194
+ "B": "Int"
195
+ },
196
+ "relations": {
197
+ "M2M_NC_Category": {
198
+ "A": [
199
+ "id"
200
+ ]
201
+ },
202
+ "M2M_NC_Post": {
203
+ "B": [
204
+ "id"
205
+ ]
206
+ }
207
+ }
208
+ },
209
+ "M2M_NC_Post": {
210
+ "fields": {
211
+ "id": "Int",
212
+ "title": "String"
213
+ },
214
+ "relations": {
215
+ "_M2M_NC": {
216
+ "id": [
217
+ "B"
218
+ ]
219
+ }
220
+ }
221
+ },
222
+ "MMM_Category": {
223
+ "fields": {
224
+ "id": "Int",
225
+ "name": "String"
226
+ },
227
+ "relations": {
228
+ "_M2M_NC_M1": {
229
+ "id": [
230
+ "A"
231
+ ]
232
+ },
233
+ "_M2M_NC_M2": {
234
+ "id": [
235
+ "A"
236
+ ]
237
+ }
238
+ }
239
+ },
240
+ "_M2M_NC_M1": {
241
+ "fields": {
242
+ "A": "Int",
243
+ "B": "Int"
244
+ },
245
+ "relations": {
246
+ "MMM_Category": {
247
+ "A": [
248
+ "id"
249
+ ]
250
+ },
251
+ "MMM_Post": {
252
+ "B": [
253
+ "id"
254
+ ]
255
+ }
256
+ }
257
+ },
258
+ "_M2M_NC_M2": {
259
+ "fields": {
260
+ "A": "Int",
261
+ "B": "Int"
262
+ },
263
+ "relations": {
264
+ "MMM_Category": {
265
+ "A": [
266
+ "id"
267
+ ]
268
+ },
269
+ "MMM_Post": {
270
+ "B": [
271
+ "id"
272
+ ]
273
+ }
274
+ }
275
+ },
276
+ "MMM_Post": {
277
+ "fields": {
278
+ "id": "Int",
279
+ "title": "String"
280
+ },
281
+ "relations": {
282
+ "_M2M_NC_M1": {
283
+ "id": [
284
+ "B"
285
+ ]
286
+ },
287
+ "_M2M_NC_M2": {
288
+ "id": [
289
+ "B"
290
+ ]
291
+ }
292
+ }
293
+ }
294
+ };
295
+ class DbSelect {
296
+ constructor(db) {
297
+ this.db = db;
298
+ }
299
+ from(database) {
300
+ return new _fJoin(this.db, { database, selects: [] });
301
+ }
302
+ }
303
+ class _fRun {
304
+ constructor(db, values) {
305
+ this.db = db;
306
+ this.values = values;
307
+ this.values.limit = typeof this.values.limit === "number" ? this.values.limit : void 0;
308
+ this.values.offset = typeof this.values.offset === "number" ? this.values.offset : void 0;
309
+ }
310
+ run() {
311
+ return this.db.$queryRawUnsafe(
312
+ this.getSQL()
313
+ );
314
+ }
315
+ getTables() {
316
+ return {};
317
+ }
318
+ getFields() {
319
+ return {};
320
+ }
321
+ getSQL(formatted = false) {
322
+ function processCondition(condition, formatted2) {
323
+ return "(" + Object.keys(condition).map((field) => {
324
+ const value = condition[field];
325
+ if (typeof value === "object" && value !== null && !Array.isArray(value) && "op" in value) {
326
+ switch (value.op) {
327
+ case "IN":
328
+ case "NOT IN":
329
+ const valuesList = value.values.map((v) => typeof v === "string" ? `'${v}'` : v).join(", ");
330
+ return `${String(field)} ${value.op} (${valuesList})`;
331
+ case "BETWEEN":
332
+ if (value.values.length > 2) throw new Error("Too many items supplied to op BETWEEN");
333
+ const [start, end] = value.values;
334
+ return `${String(field)} BETWEEN ${typeof start === "string" ? `'${start}'` : start} AND ${typeof end === "string" ? `'${end}'` : end}`;
335
+ case "LIKE":
336
+ case "NOT LIKE":
337
+ return `${String(field)} ${value.op} '${value.value}'`;
338
+ case "IS NULL":
339
+ case "IS NOT NULL":
340
+ return `${String(field)} ${value.op}`;
341
+ case ">":
342
+ case ">=":
343
+ case "<":
344
+ case "<=":
345
+ case "!=":
346
+ return `${String(field)} ${value.op} ${typeof value.value === "string" ? `'${value.value}'` : value.value}`;
347
+ default:
348
+ throw new Error(`Unsupported operation: ${value.op}`);
349
+ }
350
+ } else if (Array.isArray(value)) {
351
+ const valuesList = value.map((v) => typeof v === "string" ? `'${v}'` : v).join(", ");
352
+ return `${String(field)} IN (${valuesList})`;
353
+ } else if (value === null) {
354
+ return `${String(field)} IS NULL`;
355
+ } else {
356
+ return `${String(field)} = ${typeof value === "string" ? `'${value}'` : value}`;
357
+ }
358
+ }).join(" AND " + (" ")) + " )";
359
+ }
360
+ function processCriteria(main, joinType = "AND", formatted2 = false) {
361
+ const results = [];
362
+ for (const criteria of main) {
363
+ if (typeof criteria === "string") {
364
+ results.push(criteria);
365
+ continue;
366
+ }
367
+ for (const criterion in criteria) {
368
+ results.push(tsPattern.match(criterion).returnType().with("$AND", (criterion2) => {
369
+ return "(" + //@ts-expect-error criterion
370
+ processCriteria(criteria[criterion2], "AND", formatted2) + ")";
371
+ }).with("$OR", (criterion2) => {
372
+ return "(" + //@ts-expect-error criterion
373
+ processCriteria(criteria[criterion2], "OR", formatted2) + ")";
374
+ }).with("$NOT", (criterion2) => {
375
+ return "(NOT(" + //@ts-expect-error criterion
376
+ processCriteria(criteria[criterion2], "AND", formatted2) + "))";
377
+ }).with("$NOR", (criterion2) => {
378
+ return "(NOT(" + //@ts-expect-error criterion
379
+ processCriteria(criteria[criterion2], "OR", formatted2) + "))";
380
+ }).with(tsPattern.P.string, () => {
381
+ return processCondition(criteria);
382
+ }).exhaustive());
383
+ }
384
+ }
385
+ return results.join((formatted2 ? "\n" : " ") + joinType + (formatted2 ? "\n" : " "));
386
+ }
387
+ const whereClause = this.values.where !== void 0 ? processCriteria(this.values.where, "AND", formatted) : void 0;
388
+ const havingClause = this.values.having !== void 0 ? processCriteria(this.values.having, "AND", formatted) : void 0;
389
+ return [
390
+ this.values.selects.length === 0 ? "" : "SELECT " + (this.values.selectDistinct === true ? "DISTINCT " : "") + this.values.selects.join(", "),
391
+ `FROM ${this.values.database}`,
392
+ this.values.tables?.map(({
393
+ table,
394
+ local,
395
+ remote
396
+ }) => `JOIN ${table} ON ${local} = ${remote}`).join(formatted ? "\n" : " ") ?? "",
397
+ !whereClause ? "" : `WHERE ${whereClause}`,
398
+ !this.values.groupBy?.length ? "" : `GROUP BY ${this.values.groupBy.join(", ")}`,
399
+ !havingClause ? "" : `HAVING ${havingClause}`,
400
+ !(this.values.orderBy && this.values.orderBy.length > 0) ? "" : "ORDER BY " + this.values.orderBy.join(", "),
401
+ !this.values.limit ? "" : `LIMIT ${this.values.limit}`,
402
+ !this.values.offset ? "" : `OFFSET ${this.values.offset}`
403
+ ].filter(Boolean).join(formatted ? "\n" : " ").trim() + ";";
404
+ }
405
+ }
406
+ class _fOffset extends _fRun {
407
+ offset(offset) {
408
+ return new _fRun(this.db, { ...this.values, offset });
409
+ }
410
+ }
411
+ class _fLimit extends _fRun {
412
+ limit(limit) {
413
+ return new _fOffset(this.db, { ...this.values, limit });
414
+ }
415
+ }
416
+ class _fOrderBy extends _fLimit {
417
+ orderBy(orderBy) {
418
+ return new _fLimit(this.db, { ...this.values, orderBy });
419
+ }
420
+ }
421
+ class _fSelect extends _fOrderBy {
422
+ select(select) {
423
+ return new _fSelect(this.db, {
424
+ ...this.values,
425
+ selects: [...this.values.selects, select]
426
+ });
427
+ }
428
+ }
429
+ class _fSelectDistinct extends _fSelect {
430
+ selectDistinct() {
431
+ return new _fSelect(this.db, { ...this.values, selectDistinct: true });
432
+ }
433
+ selectAll() {
434
+ const selects = function(values) {
435
+ if (values.tables && values.tables.length > 0) {
436
+ return [values.database, ...values.tables.map((t) => t.table)].reduce((acc, table) => {
437
+ return acc.concat(Object.keys(DB[table].fields).map((field) => `${table}.${field} AS \`${table}.${field}\``));
438
+ }, []);
439
+ }
440
+ return Object.keys(DB[values.database].fields);
441
+ }(this.values);
442
+ return new _fOrderBy(this.db, {
443
+ ...this.values,
444
+ selects
445
+ });
446
+ }
447
+ //TODO
448
+ // selectAllOmit() {
449
+ // throw new Error("Not implemented yet")
450
+ // }
451
+ }
452
+ class _fHaving extends _fSelectDistinct {
453
+ // TODO Allowed Fields
454
+ // - specified in groupBy
455
+ having(criteria) {
456
+ return new _fSelectDistinct(this.db, {
457
+ ...this.values,
458
+ having: [criteria]
459
+ });
460
+ }
461
+ }
462
+ class _fGroupBy extends _fHaving {
463
+ //TODO this should only accept columns for tables in play
464
+ groupBy(groupBy) {
465
+ return new _fHaving(this.db, { ...this.values, groupBy });
466
+ }
467
+ }
468
+ class _fWhere extends _fGroupBy {
469
+ whereNotNull(col) {
470
+ return new _fWhere(this.db, {
471
+ ...this.values,
472
+ where: [
473
+ ...this.values.where || [],
474
+ {
475
+ $AND: (
476
+ //@ts-expect-error todo comeback to, col is a string or never
477
+ [{ [col]: { op: "IS NOT NULL" } }]
478
+ )
479
+ }
480
+ ]
481
+ });
482
+ }
483
+ whereIsNull(col) {
484
+ return new _fWhere(this.db, {
485
+ ...this.values,
486
+ where: [
487
+ ...this.values.where || [],
488
+ {
489
+ $AND: (
490
+ //@ts-expect-error todo comeback to, col is a string or never
491
+ [{ [col]: { op: "IS NULL" } }]
492
+ )
493
+ }
494
+ ]
495
+ });
496
+ }
497
+ where(criteria) {
498
+ return new _fGroupBy(this.db, {
499
+ ...this.values,
500
+ where: [...this.values.where || [], criteria]
501
+ });
502
+ }
503
+ whereRaw(where) {
504
+ return new _fGroupBy(this.db, {
505
+ ...this.values,
506
+ where: [...this.values.where || [], where.replace(/^\s*where\s*/i, "").trim()]
507
+ });
508
+ }
509
+ }
510
+ class _fJoin extends _fWhere {
511
+ join(table, field, reference) {
512
+ return new _fJoin(this.db, {
513
+ ...this.values,
514
+ tables: [...this.values.tables || [], { table, local: field, remote: reference }]
515
+ });
516
+ }
517
+ joinUnsafeTypeEnforced(table, field, reference) {
518
+ return new _fJoin(this.db, {
519
+ ...this.values,
520
+ tables: [...this.values.tables || [], {
521
+ table,
522
+ local: `${String(table)}.${String(field)}`,
523
+ remote: reference
524
+ }]
525
+ });
526
+ }
527
+ joinUnsafeIgnoreType(table, field, reference) {
528
+ return new _fJoin(this.db, {
529
+ ...this.values,
530
+ tables: [...this.values.tables || [], {
531
+ table,
532
+ local: `${String(table)}.${String(field)}`,
533
+ remote: reference
534
+ }]
535
+ });
536
+ }
537
+ // innerJoin(table: TTables, col1:string, col2:string){
538
+ // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
539
+ // }
540
+ // leftJoin(table: TTables, col1:string, col2:string){
541
+ // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
542
+ // }
543
+ // rightJoin(table: TTables, col1:string, col2:string){
544
+ // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
545
+ // }
546
+ // fullJoin(table: TTables, col1:string, col2:string){
547
+ // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
548
+ // }
549
+ // crossJoin(table: TTables, col1:string, col2:string){
550
+ // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
551
+ // }
552
+ // outerJoin(table: TTables, col1:string, col2:string){
553
+ // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
554
+ // }
555
+ }
556
+ var extend_default = {
557
+ client: {
558
+ $from(table) {
559
+ const client = extension.Prisma.getExtensionContext(this);
560
+ return new DbSelect(client).from(table);
561
+ }
562
+ }
563
+ };
564
+
565
+ module.exports = extend_default;