prisma-ts-select 0.0.34 → 0.1.2

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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1242 -315
  3. package/assets/groupBy.gif +0 -0
  4. package/assets/joinUnsafeIgnoreType.gif +0 -0
  5. package/assets/joinUnsafeTypeEnforced.gif +0 -0
  6. package/assets/typesafe-join.gif +0 -0
  7. package/assets/typesafe-join.png +0 -0
  8. package/assets/whereNotNull.gif +0 -0
  9. package/assets/whereisNull.gif +0 -0
  10. package/dist/bin.cjs +1 -1
  11. package/dist/bin.js +1 -1
  12. package/dist/chunk-47KZVQLD.js +283 -0
  13. package/dist/chunk-54D2J5AR.cjs +291 -0
  14. package/dist/extend/dialects/index.d.ts +13 -0
  15. package/dist/extend/dialects/index.js +186 -0
  16. package/dist/extend/dialects/mysql-v6.d.ts +100 -0
  17. package/dist/extend/dialects/mysql-v6.js +152 -0
  18. package/dist/extend/dialects/mysql-v7.d.ts +6 -0
  19. package/dist/extend/dialects/mysql-v7.js +138 -0
  20. package/dist/extend/dialects/mysql.d.ts +90 -0
  21. package/dist/extend/dialects/mysql.js +156 -0
  22. package/dist/extend/dialects/postgresql-v6.d.ts +97 -0
  23. package/dist/extend/dialects/postgresql-v6.js +136 -0
  24. package/dist/extend/dialects/postgresql-v7.d.ts +97 -0
  25. package/dist/extend/dialects/postgresql-v7.js +136 -0
  26. package/dist/extend/dialects/postgresql.d.ts +89 -0
  27. package/dist/extend/dialects/postgresql.js +147 -0
  28. package/dist/extend/dialects/shared.d.ts +6 -0
  29. package/dist/extend/dialects/shared.js +5 -0
  30. package/dist/extend/dialects/sqlite.d.ts +63 -0
  31. package/dist/extend/dialects/sqlite.js +138 -0
  32. package/dist/extend/dialects/types.d.ts +12 -0
  33. package/dist/extend/dialects/types.js +4 -0
  34. package/dist/extend/extend.d.ts +286 -43
  35. package/dist/extend/extend.js +735 -163
  36. package/dist/extend/sql-expr-BaKWzJ-r.d.ts +10 -0
  37. package/dist/extend/types-D84lxYVc.d.ts +5 -0
  38. package/dist/generator.cjs +1 -1
  39. package/dist/generator.js +1 -1
  40. package/package.json +46 -42
  41. package/built/extend.cjs +0 -680
  42. package/built/extend.d.cts +0 -520
  43. package/built/extend.d.ts +0 -520
  44. package/built/extend.js +0 -678
  45. package/dist/chunk-TBO3MX7Q.cjs +0 -195
  46. package/dist/chunk-X3N5N5KQ.js +0 -187
  47. package/dist/extend/extend.cjs +0 -357
  48. package/dist/extend/extend.d.cts +0 -264
package/built/extend.cjs DELETED
@@ -1,680 +0,0 @@
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
- "Employee": {
91
- "fields": {
92
- "id": "Int",
93
- "name": "String",
94
- "managerId": "?Int"
95
- },
96
- "relations": {
97
- "Employee": {
98
- "managerId": [
99
- "id"
100
- ],
101
- "id": [
102
- "managerId"
103
- ]
104
- },
105
- "_ManagerSubordinates": {
106
- "id": [
107
- "A"
108
- ]
109
- }
110
- }
111
- },
112
- "_ManagerSubordinates": {
113
- "fields": {
114
- "A": "Int",
115
- "B": ""
116
- },
117
- "relations": {
118
- "Employee": {
119
- "A": [
120
- "id"
121
- ]
122
- }
123
- }
124
- },
125
- "MFId_Category": {
126
- "fields": {
127
- "id": "Int",
128
- "name": "String"
129
- },
130
- "relations": {
131
- "MFId_CategoryPost": {
132
- "id": [
133
- "categoryId"
134
- ]
135
- }
136
- }
137
- },
138
- "MFId_CategoryPost": {
139
- "fields": {
140
- "categoryId": "Int",
141
- "postId": "Int"
142
- },
143
- "relations": {
144
- "MFId_Category": {
145
- "categoryId": [
146
- "id"
147
- ]
148
- },
149
- "MFId_Post": {
150
- "postId": [
151
- "id"
152
- ]
153
- }
154
- }
155
- },
156
- "MFId_Post": {
157
- "relations": {
158
- "MFId_CategoryPost": {
159
- "id": [
160
- "postId"
161
- ]
162
- }
163
- },
164
- "fields": {
165
- "id": "Int",
166
- "title": "String"
167
- }
168
- },
169
- "M2M_Post": {
170
- "fields": {
171
- "id": "Int",
172
- "title": "String"
173
- },
174
- "relations": {
175
- "_M2M_CategoryToM2M_Post": {
176
- "id": [
177
- "B"
178
- ]
179
- }
180
- }
181
- },
182
- "_M2M_CategoryToM2M_Post": {
183
- "fields": {
184
- "A": "Int",
185
- "B": "Int"
186
- },
187
- "relations": {
188
- "M2M_Post": {
189
- "B": [
190
- "id"
191
- ]
192
- },
193
- "M2M_Category": {
194
- "A": [
195
- "id"
196
- ]
197
- }
198
- }
199
- },
200
- "M2M_Category": {
201
- "fields": {
202
- "id": "Int",
203
- "name": "String"
204
- },
205
- "relations": {
206
- "_M2M_CategoryToM2M_Post": {
207
- "id": [
208
- "A"
209
- ]
210
- }
211
- }
212
- },
213
- "M2M_NC_Category": {
214
- "fields": {
215
- "id": "Int",
216
- "name": "String"
217
- },
218
- "relations": {
219
- "_M2M_NC": {
220
- "id": [
221
- "A"
222
- ]
223
- }
224
- }
225
- },
226
- "_M2M_NC": {
227
- "fields": {
228
- "A": "Int",
229
- "B": "Int"
230
- },
231
- "relations": {
232
- "M2M_NC_Category": {
233
- "A": [
234
- "id"
235
- ]
236
- },
237
- "M2M_NC_Post": {
238
- "B": [
239
- "id"
240
- ]
241
- }
242
- }
243
- },
244
- "M2M_NC_Post": {
245
- "fields": {
246
- "id": "Int",
247
- "title": "String"
248
- },
249
- "relations": {
250
- "_M2M_NC": {
251
- "id": [
252
- "B"
253
- ]
254
- }
255
- }
256
- },
257
- "MMM_Category": {
258
- "fields": {
259
- "id": "Int",
260
- "name": "String"
261
- },
262
- "relations": {
263
- "_M2M_NC_M1": {
264
- "id": [
265
- "A"
266
- ]
267
- },
268
- "_M2M_NC_M2": {
269
- "id": [
270
- "A"
271
- ]
272
- }
273
- }
274
- },
275
- "_M2M_NC_M1": {
276
- "fields": {
277
- "A": "Int",
278
- "B": "Int"
279
- },
280
- "relations": {
281
- "MMM_Category": {
282
- "A": [
283
- "id"
284
- ]
285
- },
286
- "MMM_Post": {
287
- "B": [
288
- "id"
289
- ]
290
- }
291
- }
292
- },
293
- "_M2M_NC_M2": {
294
- "fields": {
295
- "A": "Int",
296
- "B": "Int"
297
- },
298
- "relations": {
299
- "MMM_Category": {
300
- "A": [
301
- "id"
302
- ]
303
- },
304
- "MMM_Post": {
305
- "B": [
306
- "id"
307
- ]
308
- }
309
- }
310
- },
311
- "MMM_Post": {
312
- "fields": {
313
- "id": "Int",
314
- "title": "String"
315
- },
316
- "relations": {
317
- "_M2M_NC_M1": {
318
- "id": [
319
- "B"
320
- ]
321
- },
322
- "_M2M_NC_M2": {
323
- "id": [
324
- "B"
325
- ]
326
- }
327
- }
328
- }
329
- };
330
- class DbSelect {
331
- constructor(db) {
332
- this.db = db;
333
- }
334
- from(baseTable, alias) {
335
- return new _fJoin(this.db, {
336
- tables: [{
337
- table: baseTable,
338
- alias
339
- }],
340
- selects: []
341
- });
342
- }
343
- }
344
- class _fRun {
345
- constructor(db, values) {
346
- this.db = db;
347
- this.values = values;
348
- this.values.limit = typeof this.values.limit === "number" ? this.values.limit : void 0;
349
- this.values.offset = typeof this.values.offset === "number" ? this.values.offset : void 0;
350
- }
351
- run() {
352
- return this.db.$queryRawUnsafe(
353
- this.getSQL()
354
- );
355
- }
356
- getTables() {
357
- return {};
358
- }
359
- getFields() {
360
- return {};
361
- }
362
- getResultType() {
363
- return {};
364
- }
365
- getSQL(formatted = false) {
366
- function processCondition(condition, formatted2) {
367
- return "(" + Object.keys(condition).map((field) => {
368
- const value = condition[field];
369
- if (typeof value === "object" && value !== null && !Array.isArray(value) && "op" in value) {
370
- switch (value.op) {
371
- case "IN":
372
- case "NOT IN":
373
- const valuesList = value.values.map((v) => typeof v === "string" ? `'${v}'` : v).join(", ");
374
- return `${String(field)} ${value.op} (${valuesList})`;
375
- case "BETWEEN":
376
- if (value.values.length > 2) throw new Error("Too many items supplied to op BETWEEN");
377
- const [start, end] = value.values;
378
- return `${String(field)} BETWEEN ${typeof start === "string" ? `'${start}'` : start} AND ${typeof end === "string" ? `'${end}'` : end}`;
379
- case "LIKE":
380
- case "NOT LIKE":
381
- return `${String(field)} ${value.op} '${value.value}'`;
382
- case "IS NULL":
383
- case "IS NOT NULL":
384
- return `${String(field)} ${value.op}`;
385
- case ">":
386
- case ">=":
387
- case "<":
388
- case "<=":
389
- case "!=":
390
- return `${String(field)} ${value.op} ${typeof value.value === "string" ? `'${value.value}'` : value.value}`;
391
- default:
392
- throw new Error(`Unsupported operation: ${value.op}`);
393
- }
394
- } else if (Array.isArray(value)) {
395
- const valuesList = value.map((v) => typeof v === "string" ? `'${v}'` : v).join(", ");
396
- return `${String(field)} IN (${valuesList})`;
397
- } else if (value === null) {
398
- return `${String(field)} IS NULL`;
399
- } else {
400
- return `${String(field)} = ${typeof value === "string" ? `'${value}'` : value}`;
401
- }
402
- }).join(" AND " + (" ")) + " )";
403
- }
404
- function processCriteria(main, joinType = "AND", formatted2 = false) {
405
- const results = [];
406
- for (const criteria of main) {
407
- if (typeof criteria === "string") {
408
- results.push(criteria);
409
- continue;
410
- }
411
- for (const criterion in criteria) {
412
- results.push(tsPattern.match(criterion).returnType().with("$AND", (criterion2) => {
413
- return "(" + //@ts-expect-error criterion
414
- processCriteria(criteria[criterion2], "AND", formatted2) + ")";
415
- }).with("$OR", (criterion2) => {
416
- return "(" + //@ts-expect-error criterion
417
- processCriteria(criteria[criterion2], "OR", formatted2) + ")";
418
- }).with("$NOT", (criterion2) => {
419
- return "(NOT(" + //@ts-expect-error criterion
420
- processCriteria(criteria[criterion2], "AND", formatted2) + "))";
421
- }).with("$NOR", (criterion2) => {
422
- return "(NOT(" + //@ts-expect-error criterion
423
- processCriteria(criteria[criterion2], "OR", formatted2) + "))";
424
- }).with(tsPattern.P.string, () => {
425
- return processCondition(criteria);
426
- }).exhaustive());
427
- }
428
- }
429
- return results.join((formatted2 ? "\n" : " ") + joinType + (formatted2 ? "\n" : " "));
430
- }
431
- const whereClause = this.values.where !== void 0 ? processCriteria(this.values.where, "AND", formatted) : void 0;
432
- const havingClause = this.values.having !== void 0 ? processCriteria(this.values.having, "AND", formatted) : void 0;
433
- const [base, ...joins] = this.values.tables;
434
- const baseTable = base.alias ? `${base.table} AS \`${base.alias}\`` : base.table;
435
- return [
436
- this.values.selects.length === 0 ? "" : "SELECT " + (this.values.selectDistinct === true ? "DISTINCT " : "") + this.values.selects.join(", "),
437
- `FROM ${baseTable}`,
438
- joins.map(({
439
- table,
440
- local,
441
- remote,
442
- alias
443
- }) => {
444
- const tLocal = (alias || table) + "." + local;
445
- return `JOIN ${!!alias ? table + " AS `" + alias + "`" : table} ON ${tLocal} = ${remote}`;
446
- }).join(formatted ? "\n" : " ") ?? "",
447
- !whereClause ? "" : `WHERE ${whereClause}`,
448
- !this.values.groupBy?.length ? "" : `GROUP BY ${this.values.groupBy.join(", ")}`,
449
- !havingClause ? "" : `HAVING ${havingClause}`,
450
- !(this.values.orderBy && this.values.orderBy.length > 0) ? "" : "ORDER BY " + this.values.orderBy.join(", "),
451
- !this.values.limit ? "" : `LIMIT ${this.values.limit}`,
452
- !this.values.offset ? "" : `OFFSET ${this.values.offset}`
453
- ].filter(Boolean).join(formatted ? "\n" : " ").trim() + ";";
454
- }
455
- }
456
- class _fOffset extends _fRun {
457
- offset(offset) {
458
- return new _fRun(this.db, { ...this.values, offset });
459
- }
460
- }
461
- class _fLimit extends _fRun {
462
- limit(limit) {
463
- return new _fOffset(this.db, { ...this.values, limit });
464
- }
465
- }
466
- class _fOrderBy extends _fLimit {
467
- orderBy(orderBy) {
468
- return new _fLimit(this.db, { ...this.values, orderBy });
469
- }
470
- }
471
- class _fSelect extends _fOrderBy {
472
- select(select, alias) {
473
- const tableColMatch = select.match(/^(\w+)\.(.*?)$/);
474
- if (tableColMatch) {
475
- const [, tableName, colName] = tableColMatch;
476
- const tableObject = this.values.tables.find((t) => (t.alias || t.table) === tableName);
477
- if (!tableObject) throw new Error(`Table "${tableName}" not found in query`);
478
- const tableFields = DB[tableObject.table];
479
- if (!tableFields) {
480
- throw new Error(`Table "${tableName}" not found in database schema`);
481
- }
482
- if (colName === "*") {
483
- const hasMultipleTables = this.values.tables && this.values.tables.length > 1 || false;
484
- const expandedSelects = Object.keys(tableFields.fields).map((field) => {
485
- if (hasMultipleTables) {
486
- return `${tableName}.${field} AS \`${tableName}.${field}\``;
487
- }
488
- return `${field}`;
489
- });
490
- return new _fSelect(this.db, {
491
- ...this.values,
492
- selects: [...this.values.selects, ...expandedSelects]
493
- });
494
- } else if (!alias && !!colName) {
495
- const currentTablesWithFields = this.values.tables.reduce((acc, table) => {
496
- const { table: real } = table;
497
- for (const col in DB[real].fields) {
498
- acc[col] = acc[col] ? acc[col] + 1 : 1;
499
- }
500
- return acc;
501
- }, {});
502
- if (!currentTablesWithFields[colName]) {
503
- throw new Error(`Column "${colName}" not found in database schema`);
504
- }
505
- if (currentTablesWithFields[colName] > 1) {
506
- return new _fSelect(this.db, {
507
- ...this.values,
508
- selects: [...this.values.selects, `${select} AS \`${select}\``]
509
- });
510
- } else {
511
- return new _fSelect(this.db, {
512
- ...this.values,
513
- selects: [...this.values.selects, `${colName}`]
514
- });
515
- }
516
- }
517
- }
518
- if (alias !== void 0) {
519
- return new _fSelect(this.db, {
520
- ...this.values,
521
- selects: [...this.values.selects, `${select} AS \`${alias}\``]
522
- });
523
- }
524
- return new _fSelect(this.db, {
525
- ...this.values,
526
- selects: [...this.values.selects, select]
527
- });
528
- }
529
- }
530
- class _fSelectDistinct extends _fSelect {
531
- selectDistinct() {
532
- return new _fSelect(this.db, { ...this.values, selectDistinct: true });
533
- }
534
- selectAll() {
535
- const selects = function(values) {
536
- if (values.tables && values.tables.length > 1) {
537
- return [
538
- /*values.baseTable,*/
539
- ...values.tables.map((t) => t.table)
540
- ].reduce((acc, table) => {
541
- return acc.concat(Object.keys(DB[table].fields).map((field) => `${table}.${field} AS \`${table}.${field}\``));
542
- }, []);
543
- }
544
- return Object.keys(DB[values.tables[0].table].fields);
545
- }(this.values);
546
- return new _fOrderBy(this.db, {
547
- ...this.values,
548
- selects
549
- });
550
- }
551
- //TODO
552
- // selectAllOmit() {
553
- // throw new Error("Not implemented yet")
554
- // }
555
- }
556
- class _fHaving extends _fSelectDistinct {
557
- // TODO Allowed Fields
558
- // - specified in groupBy
559
- having(criteria) {
560
- return new _fSelectDistinct(this.db, {
561
- ...this.values,
562
- having: [criteria]
563
- });
564
- }
565
- }
566
- class _fGroupBy extends _fHaving {
567
- //TODO this should only accept columns for tables in play
568
- groupBy(groupBy) {
569
- return new _fHaving(this.db, { ...this.values, groupBy });
570
- }
571
- }
572
- class _fWhere extends _fGroupBy {
573
- whereNotNull(col) {
574
- return new _fWhere(this.db, {
575
- ...this.values,
576
- where: [
577
- ...this.values.where || [],
578
- {
579
- $AND: (
580
- //@ts-expect-error todo comeback to, col is a string or never
581
- [{ [col]: { op: "IS NOT NULL" } }]
582
- )
583
- }
584
- ]
585
- });
586
- }
587
- whereIsNull(col) {
588
- return new _fWhere(this.db, {
589
- ...this.values,
590
- where: [
591
- ...this.values.where || [],
592
- {
593
- $AND: (
594
- //@ts-expect-error todo comeback to, col is a string or never
595
- [{ [col]: { op: "IS NULL" } }]
596
- )
597
- }
598
- ]
599
- });
600
- }
601
- where(criteria) {
602
- return new _fGroupBy(this.db, {
603
- ...this.values,
604
- where: [...this.values.where || [], criteria]
605
- });
606
- }
607
- whereRaw(where) {
608
- return new _fGroupBy(this.db, {
609
- ...this.values,
610
- where: [...this.values.where || [], where.replace(/^\s*where\s*/i, "").trim()]
611
- });
612
- }
613
- }
614
- class _fJoin extends _fWhere {
615
- // Implementation
616
- join(tableOrOptions, field, reference) {
617
- let table;
618
- let local;
619
- let remote;
620
- let tableAlias;
621
- if (typeof tableOrOptions === "object" && "table" in tableOrOptions) {
622
- table = tableOrOptions.table.trim();
623
- local = tableOrOptions.src;
624
- remote = tableOrOptions.on;
625
- tableAlias = tableOrOptions.alias?.trim();
626
- } else {
627
- const parts = tableOrOptions.split(" ");
628
- table = parts[0];
629
- tableAlias = parts[1]?.trim();
630
- local = field;
631
- remote = reference;
632
- }
633
- return new _fJoin(this.db, {
634
- ...this.values,
635
- tables: [...this.values.tables || [], {
636
- table,
637
- local,
638
- remote,
639
- alias: tableAlias
640
- }]
641
- });
642
- }
643
- // Implementation
644
- joinUnsafeTypeEnforced(tableOrOptions, field, reference) {
645
- return this.join(tableOrOptions, field, reference);
646
- }
647
- // Implementation
648
- joinUnsafeIgnoreType(tableOrOptions, field, reference) {
649
- return this.join(tableOrOptions, field, reference);
650
- }
651
- // innerJoin(table: TTableSources, col1:string, col2:string){
652
- // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
653
- // }
654
- // leftJoin(table: TTableSources, col1:string, col2:string){
655
- // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
656
- // }
657
- // rightJoin(table: TTableSources, col1:string, col2:string){
658
- // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
659
- // }
660
- // fullJoin(table: TTableSources, col1:string, col2:string){
661
- // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
662
- // }
663
- // crossJoin(table: TTableSources, col1:string, col2:string){
664
- // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
665
- // }
666
- // outerJoin(table: TTableSources, col1:string, col2:string){
667
- // return new _fJoin<TDBBase>(this.db, {...this.values, tables: [...this.values.tables || [], table]});
668
- // }
669
- }
670
- var extend_default = {
671
- client: {
672
- $from(table) {
673
- const client = extension.Prisma.getExtensionContext(this);
674
- const [base, ...aliases] = table.split(" ");
675
- return new DbSelect(client).from(base.trim(), aliases.join().trim() || void 0);
676
- }
677
- }
678
- };
679
-
680
- module.exports = extend_default;