prisma-ts-select 0.0.33 → 0.0.34

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 CHANGED
@@ -1,8 +1,8 @@
1
1
  # prisma-ts-select
2
2
 
3
3
  ![npm version](https://img.shields.io/npm/v/prisma-ts-select)
4
+ ![build](https://github.com/adrianbrowning/prisma-ts-select/actions/workflows/CI.yml/badge.svg)
4
5
  ![license](https://img.shields.io/github/license/adrianbrowning/prisma-ts-select)
5
- <!--![build](https://github.com/adrianbrowning/prisma-ts-select/actions/workflows/build.yml/badge.svg)-->
6
6
 
7
7
  <!-- toc -->
8
8
 
@@ -13,18 +13,36 @@
13
13
  + [Generator](#generator)
14
14
  * [API](#api)
15
15
  + [`.$from`](#from)
16
+ - [Example](#example)
17
+ - [Example - With Table Alias](#example---with-table-alias)
18
+ * [SQL](#sql)
19
+ - [Example - Inline Alias Syntax](#example---inline-alias-syntax)
20
+ * [SQL](#sql-1)
21
+ + [Table Aliases](#table-aliases)
22
+ - [Table Alias Syntax Options](#table-alias-syntax-options)
23
+ - [Basic Table Alias](#basic-table-alias)
24
+ * [SQL](#sql-2)
25
+ - [Table Aliases with Joins](#table-aliases-with-joins)
26
+ * [Inline Alias Syntax](#inline-alias-syntax)
27
+ * [Object Syntax](#object-syntax)
28
+ * [SQL](#sql-3)
29
+ - [Self-Joins with Aliases](#self-joins-with-aliases)
30
+ * [SQL](#sql-4)
31
+ - [Table.* with Aliases](#table-with-aliases)
32
+ * [SQL](#sql-5)
33
+ * [SQL](#sql-6)
16
34
  + [Joins](#joins)
17
35
  - [`.join`](#join)
18
- * [Example](#example)
19
- * [SQL](#sql)
36
+ * [Example](#example-1)
37
+ * [SQL](#sql-7)
20
38
  * [Parameters](#parameters)
21
39
  - [`.joinUnsafeTypeEnforced`](#joinunsafetypeenforced)
22
- * [Example](#example-1)
23
- * [SQL](#sql-1)
40
+ * [Example](#example-2)
41
+ * [SQL](#sql-8)
24
42
  * [Parameters](#parameters-1)
25
43
  - [`.joinUnsafeIgnoreType`](#joinunsafeignoretype)
26
- * [Example](#example-2)
27
- * [SQL](#sql-2)
44
+ * [Example](#example-3)
45
+ * [SQL](#sql-9)
28
46
  * [Parameters](#parameters-2)
29
47
  + [Where](#where)
30
48
  - [`.where`](#where)
@@ -37,45 +55,53 @@
37
55
  + [$NOT](#not)
38
56
  + [$NOR](#nor)
39
57
  - [`.whereNotNull`](#wherenotnull)
40
- * [Example](#example-3)
41
- * [SQL](#sql-3)
42
- - [`.whereIsNull`](#whereisnull)
43
58
  * [Example](#example-4)
44
- * [SQL](#sql-4)
45
- - [`.whereRaw`](#whereraw)
59
+ * [SQL](#sql-10)
60
+ - [`.whereIsNull`](#whereisnull)
46
61
  * [Example](#example-5)
47
- * [SQL](#sql-5)
62
+ * [SQL](#sql-11)
63
+ - [`.whereRaw`](#whereraw)
64
+ * [Example](#example-6)
65
+ * [SQL](#sql-12)
48
66
  + [Group By](#group-by)
49
- - [Example](#example-6)
50
- - [SQL](#sql-6)
67
+ - [Example](#example-7)
68
+ - [SQL](#sql-13)
51
69
  + [Selecting](#selecting)
52
70
  - [`.selectDistinct`](#selectdistinct)
53
- - [Example](#example-7)
54
- - [SQL](#sql-7)
71
+ - [Example](#example-8)
72
+ - [SQL](#sql-14)
55
73
  - [`.selectAll`](#selectall)
56
74
  - [Example - Single Table](#example---single-table)
57
- * [SQL](#sql-8)
75
+ * [SQL](#sql-15)
58
76
  - [Example - Join table](#example---join-table)
59
- * [SQL](#sql-9)
77
+ * [SQL](#sql-16)
60
78
  - [`.select`](#select)
61
79
  - [Example - `*`](#example---)
62
- * [SQL](#sql-10)
80
+ * [SQL](#sql-17)
81
+ - [Example - `Table.*` (Single Table)](#example---table-single-table)
82
+ * [SQL](#sql-18)
83
+ - [Example - `Table.*` (With Join)](#example---table-with-join)
84
+ * [SQL](#sql-19)
63
85
  - [Example - Chained](#example---chained)
64
- * [SQL](#sql-11)
86
+ * [SQL](#sql-20)
65
87
  - [Example - Join + Chained](#example---join--chained)
66
- * [SQL](#sql-12)
88
+ * [SQL](#sql-21)
89
+ - [Example - Column Aliases](#example---column-aliases)
90
+ * [SQL](#sql-22)
91
+ - [Example - Aliases with Joins](#example---aliases-with-joins)
92
+ * [SQL](#sql-23)
67
93
  + [Having](#having)
68
- - [Example](#example-8)
69
- * [SQL](#sql-13)
70
- + [Order By](#order-by)
71
94
  - [Example](#example-9)
72
- * [SQL](#sql-14)
73
- + [Limit](#limit)
95
+ * [SQL](#sql-24)
96
+ + [Order By](#order-by)
74
97
  - [Example](#example-10)
75
- * [SQL](#sql-15)
76
- + [Offset](#offset)
98
+ * [SQL](#sql-25)
99
+ + [Limit](#limit)
77
100
  - [Example](#example-11)
78
- * [SQL](#sql-16)
101
+ * [SQL](#sql-26)
102
+ + [Offset](#offset)
103
+ - [Example](#example-12)
104
+ * [SQL](#sql-27)
79
105
  * [Future updates](#future-updates)
80
106
  * [Changelog / Versioning](#changelog--versioning)
81
107
  * [License](#license)
@@ -180,6 +206,141 @@ The way the methods are chained, are heavily inspired by [Dr Milan Milanović](h
180
206
  ### `.$from`
181
207
  This takes the `base` table to work from.
182
208
 
209
+ #### Example
210
+ ```typescript
211
+ prisma.$from("User")
212
+ ```
213
+
214
+ #### Example - With Table Alias
215
+ ```typescript
216
+ prisma.$from("User", "u")
217
+ ```
218
+
219
+ ##### SQL
220
+ ```sql
221
+ FROM User AS u
222
+ ```
223
+
224
+ **Note:** Table aliases are particularly useful for self-joins where you need to join a table to itself with different aliases.
225
+
226
+ #### Example - Inline Alias Syntax
227
+ ```typescript
228
+ prisma.$from("User u")
229
+ ```
230
+ ##### SQL
231
+ ```sql
232
+ FROM User AS u
233
+ ```
234
+ **Note:** Alias can be inline (space-separated) or as second parameter.
235
+
236
+ ### Table Aliases
237
+
238
+ Table aliases allow you to give tables shorter or more meaningful names in your queries. This is especially useful for:
239
+ - Self-joins (joining a table to itself)
240
+ - Long table names
241
+ - Clearer query readability
242
+
243
+ #### Table Alias Syntax Options
244
+
245
+ Multiple syntaxes supported:
246
+ - **Inline in .$from()**: `prisma.$from("User u")``
247
+ - **Inline in .join()**: `.join("Post p", "authorId", "User.id")`
248
+ - **Object syntax**: `.join({table: "Post", src: "authorId", on: "User.id", alias: "p"})`
249
+
250
+ #### Basic Table Alias
251
+
252
+ ```typescript
253
+ prisma.$from("User", "u")
254
+ .select("u.name")
255
+ .select("u.email")
256
+ .run();
257
+ ```
258
+
259
+ ##### SQL
260
+ ```sql
261
+ SELECT name, email FROM User AS u;
262
+ ```
263
+
264
+ #### Table Aliases with Joins
265
+
266
+ ##### Inline Alias Syntax
267
+ ```typescript
268
+ prisma.$from("User u")
269
+ .join("Post p", "authorId", "u.id")
270
+ .select("u.name")
271
+ .select("p.title")
272
+ .run();
273
+ ```
274
+
275
+ ##### Object Syntax
276
+ ```typescript
277
+ prisma.$from("User", "u")
278
+ .join({table: "Post", src: "authorId", on: "u.id", alias: "p"})
279
+ .select("u.name")
280
+ .select("p.title")
281
+ .run();
282
+ ```
283
+
284
+ ##### SQL
285
+ ```sql
286
+ SELECT name, title
287
+ FROM User AS u
288
+ JOIN Post AS p ON authorId = u.id;
289
+ ```
290
+
291
+ **Note:** The object syntax provides a foundation for future enhancements like multiple join conditions and complex WHERE-style conditions in joins.
292
+
293
+ #### Self-Joins with Aliases
294
+
295
+ Self-joins require aliases to distinguish between the different "instances" of the same table:
296
+
297
+ ```typescript
298
+ prisma.$from("User", "u1")
299
+ .joinUnsafeTypeEnforced("User", "id", "u1.id", "u2")
300
+ .select("u1.name", "user1Name")
301
+ .select("u2.name", "user2Name")
302
+ .run();
303
+ ```
304
+
305
+ ##### SQL
306
+ ```sql
307
+ SELECT u1.name AS `user1Name`, u2.name AS `user2Name`
308
+ FROM User AS u1
309
+ JOIN User AS u2 ON User.id = u1.id;
310
+ ```
311
+
312
+ #### Table.* with Aliases
313
+
314
+ You can use the `alias.*` syntax to select all columns from an aliased table:
315
+
316
+ ```typescript
317
+ prisma.$from("User", "u")
318
+ .select("u.*")
319
+ .run();
320
+ ```
321
+
322
+ ##### SQL
323
+ ```sql
324
+ SELECT u.id, u.email, u.name FROM User AS u;
325
+ ```
326
+
327
+ With joins:
328
+ ```typescript
329
+ prisma.$from("User", "u")
330
+ .join("Post", "authorId", "u.id", "p")
331
+ .select("u.*")
332
+ .select("p.*")
333
+ .run();
334
+ ```
335
+
336
+ ##### SQL
337
+ ```sql
338
+ SELECT u.id AS `u.id`, u.email AS `u.email`, u.name AS `u.name`,
339
+ p.id AS `p.id`, p.title AS `p.title`, p.content AS `p.content`
340
+ FROM User AS u
341
+ JOIN Post AS p ON authorId = u.id;
342
+ ```
343
+
183
344
  ### Joins
184
345
  #### `.join`
185
346
 
@@ -203,11 +364,25 @@ JOIN Post ON authorId = User.id;
203
364
  ```
204
365
 
205
366
  ##### Parameters
206
- | column | Description |
207
- |-------------|--------------------------------------------------------------------------------------------------------------------------------------|
208
- | `table` | The table to join on. <br/>TS autocomplete will show tables that can join with previously defined tables on. |
209
- | `field` | Column on table. <br/>TS autocomplete will show known columns that this table, can join with previously defined tables on. |
210
- | `reference` | `Table.Column` to a previously defined table (either the base, or another join), with a FK that is defined in the schema definition. |
367
+ | column | Description |
368
+ |-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
369
+ | `table` | The table to join on (supports inline alias: `"Post p"` or `"Post", "p"`). <br/>TS autocomplete will show tables that can join with previously defined tables on. |
370
+ | `field` | Column on table. <br/>TS autocomplete will show known columns that this table, can join with previously defined tables on. |
371
+ | `reference` | `Table.Column` to a previously defined table (either the base, or another join), with a FK that is defined in the schema definition. |
372
+
373
+ **Alternative Syntaxes:**
374
+ ```typescript
375
+ // Inline alias
376
+ .join("Post p", "authorId", "User.id")
377
+
378
+ // Object syntax
379
+ .join({
380
+ table: "Post",
381
+ src: "authorId",
382
+ on: "User.id",
383
+ alias: "p" // optional
384
+ })
385
+ ```
211
386
 
212
387
  #### `.joinUnsafeTypeEnforced`
213
388
 
@@ -229,11 +404,11 @@ JOIN Post ON Post.title = User.name;
229
404
  ```
230
405
 
231
406
  ##### Parameters
232
- | column | Description |
233
- |-------------|----------------------------------------------------------------------------------------------------------------------------|
234
- | `table` | The table to join on. <br/>TS autocomplete will show tables that can join with previously defined tables on. |
235
- | `field` | Column on table. <br/>TS autocomplete will show known columns that this table, can join with previously defined tables on. |
236
- | `reference` | `Table.Column` to a previously defined table (either the base, or another join), with a column that is of the same type. |
407
+ | column | Description |
408
+ |-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
409
+ | `table` | The table to join on (supports inline alias: `"Post p"` or `"Post", "p"`). <br/>TS autocomplete will show tables that can join with previously defined tables on. |
410
+ | `field` | Column on table. <br/>TS autocomplete will show known columns that this table, can join with previously defined tables on. |
411
+ | `reference` | `Table.Column` to a previously defined table (either the base, or another join), with a column that is of the same type. |
237
412
 
238
413
  #### `.joinUnsafeIgnoreType`
239
414
 
@@ -255,11 +430,11 @@ JOIN Post ON Post.id = User.name
255
430
  ```
256
431
 
257
432
  ##### Parameters
258
- | column | Description |
259
- |-------------|----------------------------------------------------------------------------------------------------------------------------|
260
- | `table` | The table to join on. <br/>TS autocomplete will show tables that can join with previously defined tables on. |
261
- | `field` | Column on table. <br/>TS autocomplete will show known columns that this table, can join with previously defined tables on. |
262
- | `reference` | `Table.Column` to a previously defined table (either the base, or another join). Referencing any column, of any type. |
433
+ | column | Description |
434
+ |-------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
435
+ | `table` | The table to join on (supports inline alias: `"Post p"` or `"Post", "p"`). <br/>TS autocomplete will show tables that can join with previously defined tables on. |
436
+ | `field` | Column on table. <br/>TS autocomplete will show known columns that this table, can join with previously defined tables on. |
437
+ | `reference` | `Table.Column` to a previously defined table (either the base, or another join). Referencing any column, of any type. |
263
438
 
264
439
  ### Where
265
440
 
@@ -515,7 +690,7 @@ JOIN Post ON authorId = User.id
515
690
 
516
691
  #### `.select`
517
692
 
518
- You can supply either; `*` OR `table.field` and then chain them together.
693
+ You can supply either; `*`, `Table.*` OR `table.field` and then chain them together.
519
694
 
520
695
  #### Example - `*`
521
696
  ```typescript
@@ -528,9 +703,49 @@ The resulting SQL will look like:
528
703
 
529
704
  ```sql
530
705
  SELECT *
531
- FROM User;
706
+ FROM User;
707
+ ```
708
+
709
+ #### Example - `Table.*` (Single Table)
710
+ ```typescript
711
+ prisma.$from("User")
712
+ .select("User.*");
713
+ ```
714
+
715
+ ##### SQL
716
+ The resulting SQL will look like:
717
+
718
+ ```sql
719
+ SELECT User.id, User.email, User.name
720
+ FROM User;
532
721
  ```
533
722
 
723
+ #### Example - `Table.*` (With Join)
724
+ ```typescript
725
+ prisma.$from("User")
726
+ .join("Post", "authorId", "User.id")
727
+ .select("User.*")
728
+ .select("Post.*");
729
+ ```
730
+
731
+ ##### SQL
732
+ The resulting SQL will look like:
733
+
734
+ ```sql
735
+ SELECT User.id AS `User.id`,
736
+ User.email AS `User.email`,
737
+ User.name AS `User.name`,
738
+ Post.id AS `Post.id`,
739
+ Post.title AS `Post.title`,
740
+ Post.content AS `Post.content`,
741
+ Post.published AS `Post.published`
742
+ FROM User
743
+ JOIN Post ON authorId = User.id;
744
+ ```
745
+
746
+ [!NOTE]
747
+ > When using `Table.*` with joins, all columns are automatically aliased with the table name prefix to avoid column name conflicts.
748
+
534
749
  #### Example - Chained
535
750
  ```typescript
536
751
  prisma.$from("User")
@@ -543,28 +758,78 @@ The resulting SQL will look like:
543
758
 
544
759
  ```sql
545
760
  SELECT name, email
546
- FROM User;
761
+ FROM User;
547
762
  ```
548
763
 
549
764
  #### Example - Join + Chained
550
765
  ```typescript
551
766
  prisma.$from("User")
552
- .join("Post", "authorId", "User.id")
767
+ .join("Post", "authorId", "User.id")
553
768
  .select("name")
554
769
  .select("Post.title");
555
770
  ```
556
771
 
557
- [!NOTE]
558
- > Support for `Table.*` isn't complete yet. This will be tracked [here](https://github.com/adrianbrowning/prisma-ts-select/issues/31).
772
+ ##### SQL
773
+ The resulting SQL will look like:
774
+
775
+ ```sql
776
+ SELECT name, Post.title
777
+ FROM User
778
+ JOIN Post ON authorId = User.id;
779
+ ```
780
+
781
+ #### Example - Column Aliases
782
+ ```typescript
783
+ // Basic alias
784
+ prisma.$from("User")
785
+ .select("User.name", "username");
786
+
787
+ // Multiple aliases
788
+ prisma.$from("User")
789
+ .select("User.id", "userId")
790
+ .select("User.email", "emailAddress");
791
+
792
+ // Mixing aliased and non-aliased columns
793
+ prisma.$from("User")
794
+ .select("User.id")
795
+ .select("User.name", "username")
796
+ .select("User.email");
797
+ ```
559
798
 
560
799
  ##### SQL
561
800
  The resulting SQL will look like:
562
801
 
563
802
  ```sql
564
- SELECT name, email
565
- FROM User;
803
+ -- Basic alias
804
+ SELECT User.name AS `username` FROM User;
805
+
806
+ -- Multiple aliases
807
+ SELECT User.id AS `userId`, User.email AS `emailAddress` FROM User;
808
+
809
+ -- Mixed
810
+ SELECT User.id, User.name AS `username`, User.email FROM User;
811
+ ```
812
+
813
+ #### Example - Aliases with Joins
814
+ ```typescript
815
+ prisma.$from("User")
816
+ .join("Post", "authorId", "User.id")
817
+ .select("User.name", "authorName")
818
+ .select("Post.title", "postTitle");
566
819
  ```
567
820
 
821
+ ##### SQL
822
+ The resulting SQL will look like:
823
+
824
+ ```sql
825
+ SELECT User.name AS `authorName`, Post.title AS `postTitle`
826
+ FROM User
827
+ JOIN Post ON authorId = User.id;
828
+ ```
829
+
830
+ [!NOTE]
831
+ > When using column aliases, you can reference the alias in `ORDER BY` clauses. The returned type will use the alias names instead of the original column names.
832
+
568
833
  ### Having
569
834
 
570
835
  `.having` uses the same syntax as [`.where`](#where). Please see the previous section for details.
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes