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