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