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.
- package/README.md +725 -0
- package/assets/groupBy.gif +0 -0
- package/assets/joinUnsafeIgnoreType.gif +0 -0
- package/assets/joinUnsafeTypeEnforced.gif +0 -0
- package/assets/typesafe-join.gif +0 -0
- package/assets/typesafe-join.png +0 -0
- package/assets/whereNotNull.gif +0 -0
- package/assets/whereisNull.gif +0 -0
- package/built/extend.cjs +565 -0
- package/built/extend.d.cts +451 -0
- package/built/extend.d.ts +451 -0
- package/built/extend.js +563 -0
- package/dist/bin.cjs +5 -0
- package/dist/bin.d.cts +1 -0
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +2 -0
- package/dist/chunk-G66FOFCO.cjs +195 -0
- package/dist/chunk-GBXPF5FT.js +187 -0
- package/dist/extend/extend.cjs +277 -0
- package/dist/extend/extend.d.cts +222 -0
- package/dist/extend/extend.d.ts +222 -0
- package/dist/extend/extend.js +275 -0
- package/dist/generator.cjs +4 -0
- package/dist/generator.d.cts +2 -0
- package/dist/generator.d.ts +2 -0
- package/dist/generator.js +1 -0
- package/package.json +170 -0
package/README.md
ADDED
|
@@ -0,0 +1,725 @@
|
|
|
1
|
+
# prisma-ts-select
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
<!---->
|
|
6
|
+
|
|
7
|
+
<!-- toc -->
|
|
8
|
+
|
|
9
|
+
* [Summary](#summary)
|
|
10
|
+
* [Installation](#installation)
|
|
11
|
+
* [Supported DBs](#supported-dbs)
|
|
12
|
+
* [Usage](#usage)
|
|
13
|
+
+ [Generator](#generator)
|
|
14
|
+
* [API](#api)
|
|
15
|
+
+ [`.$from`](#from)
|
|
16
|
+
+ [Joins](#joins)
|
|
17
|
+
- [`.join`](#join)
|
|
18
|
+
* [Example](#example)
|
|
19
|
+
* [SQL](#sql)
|
|
20
|
+
* [Parameters](#parameters)
|
|
21
|
+
- [`.joinUnsafeTypeEnforced`](#joinunsafetypeenforced)
|
|
22
|
+
* [Example](#example-1)
|
|
23
|
+
* [SQL](#sql-1)
|
|
24
|
+
* [Parameters](#parameters-1)
|
|
25
|
+
- [`.joinUnsafeIgnoreType`](#joinunsafeignoretype)
|
|
26
|
+
* [Example](#example-2)
|
|
27
|
+
* [SQL](#sql-2)
|
|
28
|
+
* [Parameters](#parameters-2)
|
|
29
|
+
+ [Where](#where)
|
|
30
|
+
- [`.where`](#where)
|
|
31
|
+
* [TypeSyntax](#typesyntax)
|
|
32
|
+
* [Operation types](#operation-types)
|
|
33
|
+
* [Examples](#examples)
|
|
34
|
+
+ [Columns](#columns)
|
|
35
|
+
+ [$AND](#and)
|
|
36
|
+
+ [$OR](#or)
|
|
37
|
+
+ [$NOT](#not)
|
|
38
|
+
+ [$NOR](#nor)
|
|
39
|
+
- [`.whereNotNull`](#wherenotnull)
|
|
40
|
+
* [Example](#example-3)
|
|
41
|
+
* [SQL](#sql-3)
|
|
42
|
+
- [`.whereIsNull`](#whereisnull)
|
|
43
|
+
* [Example](#example-4)
|
|
44
|
+
* [SQL](#sql-4)
|
|
45
|
+
- [`.whereRaw`](#whereraw)
|
|
46
|
+
* [Example](#example-5)
|
|
47
|
+
* [SQL](#sql-5)
|
|
48
|
+
+ [Group By](#group-by)
|
|
49
|
+
- [Example](#example-6)
|
|
50
|
+
- [SQL](#sql-6)
|
|
51
|
+
+ [Selecting](#selecting)
|
|
52
|
+
- [`.selectDistinct`](#selectdistinct)
|
|
53
|
+
- [Example](#example-7)
|
|
54
|
+
- [SQL](#sql-7)
|
|
55
|
+
- [`.selectAll`](#selectall)
|
|
56
|
+
- [Example - Single Table](#example---single-table)
|
|
57
|
+
* [SQL](#sql-8)
|
|
58
|
+
- [Example - Join table](#example---join-table)
|
|
59
|
+
* [SQL](#sql-9)
|
|
60
|
+
- [`.select`](#select)
|
|
61
|
+
- [Example - `*`](#example---)
|
|
62
|
+
* [SQL](#sql-10)
|
|
63
|
+
- [Example - Chained](#example---chained)
|
|
64
|
+
* [SQL](#sql-11)
|
|
65
|
+
- [Example - Join + Chained](#example---join--chained)
|
|
66
|
+
* [SQL](#sql-12)
|
|
67
|
+
+ [Having](#having)
|
|
68
|
+
- [Example](#example-8)
|
|
69
|
+
* [SQL](#sql-13)
|
|
70
|
+
+ [Order By](#order-by)
|
|
71
|
+
- [Example](#example-9)
|
|
72
|
+
* [SQL](#sql-14)
|
|
73
|
+
+ [Limit](#limit)
|
|
74
|
+
- [Example](#example-10)
|
|
75
|
+
* [SQL](#sql-15)
|
|
76
|
+
+ [Offset](#offset)
|
|
77
|
+
- [Example](#example-11)
|
|
78
|
+
* [SQL](#sql-16)
|
|
79
|
+
* [Future updates](#future-updates)
|
|
80
|
+
* [Changelog / Versioning](#changelog--versioning)
|
|
81
|
+
* [License](#license)
|
|
82
|
+
- [prisma-ts-select](#prisma-ts-select)
|
|
83
|
+
* [Install](#install)
|
|
84
|
+
* [Setup](#setup)
|
|
85
|
+
+ [Extract](#extract)
|
|
86
|
+
* [Usage](#usage-1)
|
|
87
|
+
|
|
88
|
+
<!-- tocstop -->
|
|
89
|
+
|
|
90
|
+
## Summary
|
|
91
|
+
|
|
92
|
+
**prisma-ts-select** is a TypeScript utility for enhancing the Prisma ORM.
|
|
93
|
+
It simplifies the selection of fields in Prisma queries, ensuring type safety and reducing boilerplate when working with nested fields.
|
|
94
|
+
Ideal for developers seeking an efficient, type-safe way to select data with Prisma in TypeScript.
|
|
95
|
+
|
|
96
|
+
[!NOTE]
|
|
97
|
+
> This has been built mostly around MySQL. Most methods should work across the board.<br/>
|
|
98
|
+
> Known exceptions include:
|
|
99
|
+
> - HAVING
|
|
100
|
+
> - SQLite
|
|
101
|
+
> - Requires you to have either an aggregate function in the `SELECT` or make use of `GROUP BY`
|
|
102
|
+
> - Can only use columns that are specified in `SELECT` or `GROUP BY`
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
## Installation
|
|
106
|
+
|
|
107
|
+
Install via:
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
npm install prisma-ts-select
|
|
111
|
+
pnpm add prisma-ts-select
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
## Supported DBs
|
|
115
|
+
|
|
116
|
+
I have tested this currently on the following databases.
|
|
117
|
+
|
|
118
|
+
- SQLite
|
|
119
|
+
- MySQL
|
|
120
|
+
|
|
121
|
+
Most items should also work for
|
|
122
|
+
|
|
123
|
+
- PostgreSQL
|
|
124
|
+
|
|
125
|
+
Other DBs will be added when I have chance.
|
|
126
|
+
|
|
127
|
+
## Usage
|
|
128
|
+
|
|
129
|
+
### Generator
|
|
130
|
+
|
|
131
|
+
Set up the needed generator.
|
|
132
|
+
|
|
133
|
+
```prisma
|
|
134
|
+
|
|
135
|
+
generator prisma-ts-select {
|
|
136
|
+
provider = "prisma-ts-select"
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Run the prisma generator to build the needed files
|
|
142
|
+
|
|
143
|
+
```shell
|
|
144
|
+
pnpm exec prisma generate --generator prisma-ts-select
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
After that is done, we can extend the PrismaClient:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { PrismaClient } from "@prisma/client";
|
|
151
|
+
import prismaTSSelect from "prisma-ts-select/extend";
|
|
152
|
+
|
|
153
|
+
const prisma = new PrismaClient().$extends(prismaTSSelect);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Then we can use it like:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const results = await prisma.$from("<table>")
|
|
160
|
+
.select("<column>")
|
|
161
|
+
.run()
|
|
162
|
+
console.log(results);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
## API
|
|
166
|
+
|
|
167
|
+
The way the methods are chained, are heavily inspired by [Dr Milan Milanović](https://substack.com/@techworldwithmilan) with his [How To Learn SQL? > Queries Execution Order](https://newsletter.techworld-with-milan.com/i/112786032/sql-queries-execution-order) post.
|
|
168
|
+
|
|
169
|
+
1. Sources
|
|
170
|
+
1. `from`
|
|
171
|
+
2. `join`(s)
|
|
172
|
+
2. `where`
|
|
173
|
+
3. `groupBy`
|
|
174
|
+
4. `select`
|
|
175
|
+
5. `having`
|
|
176
|
+
6. `orderBy`
|
|
177
|
+
7. `limit`
|
|
178
|
+
8. `offset`
|
|
179
|
+
|
|
180
|
+
### `.$from`
|
|
181
|
+
This takes the `base` table to work from.
|
|
182
|
+
|
|
183
|
+
### Joins
|
|
184
|
+
#### `.join`
|
|
185
|
+
|
|
186
|
+
Using the defined links (foreign keys) defined in the schema, provides a type-safe way of joining on tables.
|
|
187
|
+
|
|
188
|
+
##### Example
|
|
189
|
+
```typescript
|
|
190
|
+
prisma.$from("User")
|
|
191
|
+
.join("Post", "authorId", "User.id");
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+

|
|
195
|
+
|
|
196
|
+
##### SQL
|
|
197
|
+
|
|
198
|
+
The resulting SQL will look like:
|
|
199
|
+
|
|
200
|
+
```sql
|
|
201
|
+
FROM User
|
|
202
|
+
JOIN Post ON authorId = User.id;
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
##### 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. |
|
|
211
|
+
|
|
212
|
+
#### `.joinUnsafeTypeEnforced`
|
|
213
|
+
|
|
214
|
+
Unlike the `.join` command, this will allow you to join on columns that are not explicitly linked by a FK, but have the same type.
|
|
215
|
+
|
|
216
|
+
##### Example
|
|
217
|
+
```typescript
|
|
218
|
+
prisma.$from("User")
|
|
219
|
+
.joinUnsafeTypeEnforced("Post", "title", "User.name");
|
|
220
|
+
```
|
|
221
|
+

|
|
222
|
+
|
|
223
|
+
##### SQL
|
|
224
|
+
The resulting SQL will look like:
|
|
225
|
+
|
|
226
|
+
```sql
|
|
227
|
+
FROM User
|
|
228
|
+
JOIN Post ON Post.title = User.name;
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
##### 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. |
|
|
237
|
+
|
|
238
|
+
#### `.joinUnsafeIgnoreType`
|
|
239
|
+
|
|
240
|
+
Unlike the `.joinUnsafeIgnoreType` command, this will allow you to join on columns that are not explicitly linked by a FK, and do not have the same type.
|
|
241
|
+
|
|
242
|
+
##### Example
|
|
243
|
+
```typescript
|
|
244
|
+
prisma.$from("User")
|
|
245
|
+
.joinUnsafeIgnoreType("Post", "id", "User.name");
|
|
246
|
+
```
|
|
247
|
+

|
|
248
|
+
|
|
249
|
+
##### SQL
|
|
250
|
+
The resulting SQL will look like:
|
|
251
|
+
|
|
252
|
+
```sql
|
|
253
|
+
FROM User
|
|
254
|
+
JOIN Post ON Post.id = User.name
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
##### 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. |
|
|
263
|
+
|
|
264
|
+
### Where
|
|
265
|
+
|
|
266
|
+
#### `.where`
|
|
267
|
+
|
|
268
|
+
The `where` syntax takes inspiration from how mongoDB does queries.
|
|
269
|
+
|
|
270
|
+
##### TypeSyntax
|
|
271
|
+
```TypeScript
|
|
272
|
+
type WhereClause = {
|
|
273
|
+
"Table.Column": <value> | { "op": "<condition>", "value": <value> }
|
|
274
|
+
"$AND": [WhereClause, ...Array<WhereClause>],
|
|
275
|
+
"$OR": [WhereClause, ...Array<WhereClause>],
|
|
276
|
+
"$NOT": [WhereClause, ...Array<WhereClause>],
|
|
277
|
+
"$NOR": [WhereClause, ...Array<WhereClause>]
|
|
278
|
+
}
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
##### Operation types
|
|
282
|
+
| Op | Description | Supported Types |
|
|
283
|
+
|-------------|-------------|-----------------------|
|
|
284
|
+
| IN | | Numbers, String, Date |
|
|
285
|
+
| NOT IN | | Numbers, String, Date |
|
|
286
|
+
| BETWEEN | | Numbers, Date |
|
|
287
|
+
| LIKE | | String |
|
|
288
|
+
| NOT LIKE | | String |
|
|
289
|
+
| IS NULL | | * |
|
|
290
|
+
| IS NOT NULL | | * |
|
|
291
|
+
| \> | | Numbers, Date |
|
|
292
|
+
| \>= | | Numbers, Date |
|
|
293
|
+
| < | | Numbers, Date |
|
|
294
|
+
| <= | | Numbers, Date |
|
|
295
|
+
| != | | Numbers, String, Date |
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
##### Examples
|
|
299
|
+
| Type | Description | Example | SQL |
|
|
300
|
+
|--------------|--------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------|
|
|
301
|
+
| Table.Column | A particular Table.Column name | <pre>.where({ <br /> "User.age": 20,<br /> "User.name": {op: "LIKE", value:"Stuart%"},<br />}) | `(User.age = 20 AND User.name LIKE "Stuart%")` |
|
|
302
|
+
| $AND | Will join all items with a `AND` | <pre>.where({ <br /> $AND:[<br /> {"User.age": {op: ">", value:20}},<br /> {"User.age": {op: "<", value:60}},<br />]})</pre> | `(User.age > 20 AND User.age < 60)` |
|
|
303
|
+
| $OR | Will join all items with a `OR` | <pre>.where({ <br /> $OR:[<br /> {"User.name": {op: "LIKE", value:"a%"}},<br /> {"User.name": {op: "LIKE", value:"d%"}},<br />]})</pre> | `(User.name LIKE "a%" OR User.name LIKE "d%")` |
|
|
304
|
+
| $NOT | Will wrap statement in a `NOT (/*...*/)` and join any items with a `AND` | <pre>.where({ <br /> $NOT:[<br /> {"User.age": 20 },<br /> {<br /> "User.age": {op: "=", value:60},<br /> "User.name": "Bob",<br /> },<br />]})</pre> | `(NOT (User.age = 20 AND (User.age = 60 AND User.name = "Bob")))` |
|
|
305
|
+
| $NOR | Will wrap statement in a `NOT (/*...*/)` and join any items with a `OR` | <pre>.where({ <br /> $NOR:[<br /> {"User.age": 20 },<br /> {<br /> "User.age": {op: "!=", value:60},<br /> "User.name": "Bob",<br /> },<br />]})</pre> | `(NOT (User.age = 20 OR (User.age != 60 AND User.name = "Bob")))` |
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
###### Columns
|
|
309
|
+
```typescript
|
|
310
|
+
prisma.$from("User")
|
|
311
|
+
.join("Post", "id", "User.name")
|
|
312
|
+
.where({
|
|
313
|
+
"User.age": 20,
|
|
314
|
+
"User.name": {op: "LIKE", value: "Stuart%"},
|
|
315
|
+
});
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
###### $AND
|
|
319
|
+
```typescript
|
|
320
|
+
prisma.$from("User")
|
|
321
|
+
.join("Post", "id", "User.name")
|
|
322
|
+
.where({
|
|
323
|
+
$AND: [
|
|
324
|
+
{"User.age": {op: ">", value: 20}},
|
|
325
|
+
{"User.age": {op: "<", value: 60}},
|
|
326
|
+
]
|
|
327
|
+
});
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
###### $OR
|
|
331
|
+
```typescript
|
|
332
|
+
prisma.$from("User")
|
|
333
|
+
.join("Post", "id", "User.name")
|
|
334
|
+
.where({
|
|
335
|
+
$OR: [
|
|
336
|
+
{"User.name": {op: "LIKE", value: "a%"}},
|
|
337
|
+
{"User.name": {op: "LIKE", value: "d%"}},
|
|
338
|
+
]
|
|
339
|
+
});
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
###### $NOT
|
|
343
|
+
```typescript
|
|
344
|
+
prisma.$from("User")
|
|
345
|
+
.join("Post", "id", "User.name")
|
|
346
|
+
.where({
|
|
347
|
+
$NOT: [
|
|
348
|
+
{"User.age": 20},
|
|
349
|
+
{
|
|
350
|
+
"User.age": {op: "=", value: 60},
|
|
351
|
+
"User.name": "Bob",
|
|
352
|
+
},
|
|
353
|
+
]
|
|
354
|
+
});
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
###### $NOR
|
|
358
|
+
```typescript
|
|
359
|
+
prisma.$from("User")
|
|
360
|
+
.join("Post", "id", "User.name")
|
|
361
|
+
.where({
|
|
362
|
+
$NOR: [
|
|
363
|
+
{"User.age": 20},
|
|
364
|
+
{
|
|
365
|
+
"User.age": {op: "!=", value: 60},
|
|
366
|
+
"User.name": "Bob",
|
|
367
|
+
},
|
|
368
|
+
]
|
|
369
|
+
});
|
|
370
|
+
```
|
|
371
|
+
|
|
372
|
+
#### `.whereNotNull`
|
|
373
|
+
|
|
374
|
+
This will remove the `null` type from the union of types of the current table column.
|
|
375
|
+
To use `.whereNotNull`, you need to add it before a `.where`.
|
|
376
|
+
|
|
377
|
+
##### Example
|
|
378
|
+
```typescript
|
|
379
|
+
prisma.$from("User")
|
|
380
|
+
.join("Post", "authorId", "User.id")
|
|
381
|
+
.whereNotNull("User.name");
|
|
382
|
+
```
|
|
383
|
+

|
|
384
|
+
|
|
385
|
+
##### SQL
|
|
386
|
+
The resulting SQL will look like:
|
|
387
|
+
|
|
388
|
+
```sql
|
|
389
|
+
FROM User
|
|
390
|
+
JOIN Post ON authorId = User.id
|
|
391
|
+
WHERE User.name IS NOT NULL;
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
#### `.whereIsNull`
|
|
395
|
+
|
|
396
|
+
This will remove the NonNull type from the union of types of the current table column.
|
|
397
|
+
To use `.whereIsNull`, you need to add it before a `.where`.
|
|
398
|
+
|
|
399
|
+
##### Example
|
|
400
|
+
```typescript
|
|
401
|
+
prisma.$from("User")
|
|
402
|
+
.join("Post", "authorId", "User.id")
|
|
403
|
+
.whereIsNull("Post.content");
|
|
404
|
+
```
|
|
405
|
+

|
|
406
|
+
|
|
407
|
+
##### SQL
|
|
408
|
+
The resulting SQL will look like:
|
|
409
|
+
|
|
410
|
+
```sql
|
|
411
|
+
FROM User
|
|
412
|
+
JOIN Post ON authorId = User.id
|
|
413
|
+
WHERE Post.content IS NULL;
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
#### `.whereRaw`
|
|
417
|
+
|
|
418
|
+
When you want to write a complex `where`, or you just don't want the TypeSafety offered by the other methods, you can use `.whereRaw`.
|
|
419
|
+
|
|
420
|
+
##### Example
|
|
421
|
+
```typescript
|
|
422
|
+
prisma.$from("User")
|
|
423
|
+
.join("Post", "authorId", "User.id")
|
|
424
|
+
.whereRaw("this is a raw where statement");
|
|
425
|
+
```
|
|
426
|
+
|
|
427
|
+
##### SQL
|
|
428
|
+
The resulting SQL will look like:
|
|
429
|
+
|
|
430
|
+
```sql
|
|
431
|
+
FROM User
|
|
432
|
+
JOIN Post ON authorId = User.id
|
|
433
|
+
WHERE this is a raw where statement;
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+
### Group By
|
|
438
|
+
|
|
439
|
+
Will allow you to pass a list of columns, that haven been specified from the `.$from` and any `.join` methods.
|
|
440
|
+
|
|
441
|
+
#### Example
|
|
442
|
+
```typescript
|
|
443
|
+
prisma.$from("User")
|
|
444
|
+
.join("Post", "authorId", "User.id")
|
|
445
|
+
.groupBy(["name", "Post.content"]);
|
|
446
|
+
```
|
|
447
|
+

|
|
448
|
+
|
|
449
|
+
#### SQL
|
|
450
|
+
The resulting SQL will look like:
|
|
451
|
+
|
|
452
|
+
```sql
|
|
453
|
+
FROM User
|
|
454
|
+
JOIN Post ON authorId = User.id
|
|
455
|
+
GROUP BY name, Post.content;
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
### Selecting
|
|
459
|
+
|
|
460
|
+
#### `.selectDistinct`
|
|
461
|
+
|
|
462
|
+
Will add the keyword `DISTINCT` after the select.
|
|
463
|
+
|
|
464
|
+
#### Example
|
|
465
|
+
```typescript
|
|
466
|
+
prisma.$from("User")
|
|
467
|
+
.selectDistinct();
|
|
468
|
+
```
|
|
469
|
+
|
|
470
|
+
#### SQL
|
|
471
|
+
The resulting SQL will look like:
|
|
472
|
+
|
|
473
|
+
```sql
|
|
474
|
+
SELECT DISTINCT
|
|
475
|
+
FROM User;
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
#### `.selectAll`
|
|
479
|
+
|
|
480
|
+
Works slightly differently to `*`. The limitation of `*` in JS, is that if you have 2 tables with the same name, you will only get back the last, based on a join.
|
|
481
|
+
This method will explicitly list all the tables from the `$from` and `.join`. So you get the `table.column` in the respose.
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
#### Example - Single Table
|
|
485
|
+
```typescript
|
|
486
|
+
prisma.$from("User")
|
|
487
|
+
.selectAll();
|
|
488
|
+
```
|
|
489
|
+
|
|
490
|
+
##### SQL
|
|
491
|
+
The resulting SQL will look like:
|
|
492
|
+
|
|
493
|
+
```sql
|
|
494
|
+
SELECT id, email, name
|
|
495
|
+
FROM User
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
#### Example - Join table
|
|
499
|
+
```typescript
|
|
500
|
+
prisma.$from("User")
|
|
501
|
+
.join("Post", "authorId", "User.id")
|
|
502
|
+
.selectAll();
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
##### SQL
|
|
506
|
+
The resulting SQL will look like:
|
|
507
|
+
|
|
508
|
+
```sql
|
|
509
|
+
SELECT User.id, User. email, User.name, Post.id, Post.title, Post.content, Post.published, Post.author, Post.authorId, Post.LastModifiedBy, Post.lastModifiedById
|
|
510
|
+
FROM User
|
|
511
|
+
JOIN Post ON authorId = User.id
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
[//]: # (#### `.selectAllOmit`)
|
|
515
|
+
|
|
516
|
+
#### `.select`
|
|
517
|
+
|
|
518
|
+
You can supply either; `*` OR `table.field` and then chain them together.
|
|
519
|
+
|
|
520
|
+
#### Example - `*`
|
|
521
|
+
```typescript
|
|
522
|
+
prisma.$from("User")
|
|
523
|
+
.select("*");
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
##### SQL
|
|
527
|
+
The resulting SQL will look like:
|
|
528
|
+
|
|
529
|
+
```sql
|
|
530
|
+
SELECT *
|
|
531
|
+
FROM User;
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
#### Example - Chained
|
|
535
|
+
```typescript
|
|
536
|
+
prisma.$from("User")
|
|
537
|
+
.select("name")
|
|
538
|
+
.select("email");
|
|
539
|
+
```
|
|
540
|
+
|
|
541
|
+
##### SQL
|
|
542
|
+
The resulting SQL will look like:
|
|
543
|
+
|
|
544
|
+
```sql
|
|
545
|
+
SELECT name, email
|
|
546
|
+
FROM User;
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
#### Example - Join + Chained
|
|
550
|
+
```typescript
|
|
551
|
+
prisma.$from("User")
|
|
552
|
+
.join("Post", "authorId", "User.id")
|
|
553
|
+
.select("name")
|
|
554
|
+
.select("Post.title");
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
[!NOTE]
|
|
558
|
+
> Support for `Table.*` isn't complete yet. This will be tracked [here](https://github.com/adrianbrowning/prisma-ts-select/issues/31).
|
|
559
|
+
|
|
560
|
+
##### SQL
|
|
561
|
+
The resulting SQL will look like:
|
|
562
|
+
|
|
563
|
+
```sql
|
|
564
|
+
SELECT name, email
|
|
565
|
+
FROM User;
|
|
566
|
+
```
|
|
567
|
+
|
|
568
|
+
### Having
|
|
569
|
+
|
|
570
|
+
`.having` uses the same syntax as [`.where`](#where). Please see the previous section for details.
|
|
571
|
+
|
|
572
|
+
#### Example
|
|
573
|
+
|
|
574
|
+
```typescript
|
|
575
|
+
prisma.$from("User")
|
|
576
|
+
.join("Post", "authorId", "User.id")
|
|
577
|
+
.groupBy(["name", "Post.content"])
|
|
578
|
+
.having({
|
|
579
|
+
"User.name": {
|
|
580
|
+
"op": "LIKE",
|
|
581
|
+
"value": "bob%"
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
```typescript
|
|
587
|
+
prisma.$from("User")
|
|
588
|
+
.join("Post", "authorId", "User.id")
|
|
589
|
+
.having({
|
|
590
|
+
"User.name": {
|
|
591
|
+
"op": "LIKE",
|
|
592
|
+
"value": "stuart%"
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
|
|
598
|
+
##### SQL
|
|
599
|
+
|
|
600
|
+
```SQL
|
|
601
|
+
FROM User
|
|
602
|
+
JOIN Post ON authorId = User.id
|
|
603
|
+
GROUP BY name, Post.content
|
|
604
|
+
HAVING (User.name LIKE 'bob%');
|
|
605
|
+
```
|
|
606
|
+
|
|
607
|
+
```SQL
|
|
608
|
+
FROM User
|
|
609
|
+
JOIN Post ON authorId = User.id
|
|
610
|
+
HAVING (User.name LIKE 'stuart%');
|
|
611
|
+
```
|
|
612
|
+
|
|
613
|
+
### Order By
|
|
614
|
+
|
|
615
|
+
`.orderBy`, takes an array of column names, with the optional suffix of `ASC` or `DESC`.
|
|
616
|
+
|
|
617
|
+
#### Example
|
|
618
|
+
|
|
619
|
+
```typescript
|
|
620
|
+
prisma.$from("User")
|
|
621
|
+
.join("Post", "authorId", "User.id")
|
|
622
|
+
.orderBy(["name", "Post.content DESC"]);
|
|
623
|
+
```
|
|
624
|
+
|
|
625
|
+
##### SQL
|
|
626
|
+
|
|
627
|
+
```sql
|
|
628
|
+
FROM User
|
|
629
|
+
JOIN Post ON authorId = User.id
|
|
630
|
+
ORDER BY name, Post.content DESC;
|
|
631
|
+
```
|
|
632
|
+
|
|
633
|
+
### Limit
|
|
634
|
+
|
|
635
|
+
`.limit`, takes the number of rows you would like to return.
|
|
636
|
+
|
|
637
|
+
#### Example
|
|
638
|
+
|
|
639
|
+
```typescript
|
|
640
|
+
prisma.$from("User")
|
|
641
|
+
.join("Post", "authorId", "User.id")
|
|
642
|
+
.limit(1);
|
|
643
|
+
```
|
|
644
|
+
|
|
645
|
+
##### SQL
|
|
646
|
+
|
|
647
|
+
```SQL
|
|
648
|
+
FROM User
|
|
649
|
+
JOIN Post ON authorId = User.id
|
|
650
|
+
LIMIT 1;
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
### Offset
|
|
654
|
+
|
|
655
|
+
`.offSet`, the number of rows to skip. Requires `.limit` to have been used first.
|
|
656
|
+
|
|
657
|
+
#### Example
|
|
658
|
+
```typescript
|
|
659
|
+
prisma.$from("User")
|
|
660
|
+
.join("Post", "authorId", "User.id")
|
|
661
|
+
.limit(1)
|
|
662
|
+
.offset(1);
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
##### SQL
|
|
666
|
+
|
|
667
|
+
```SQL
|
|
668
|
+
FROM User
|
|
669
|
+
JOIN Post ON authorId = User.id
|
|
670
|
+
LIMIT 1
|
|
671
|
+
OFFSET 1
|
|
672
|
+
```
|
|
673
|
+
|
|
674
|
+
## Future updates
|
|
675
|
+
|
|
676
|
+
- Support specifying `JOIN` type [issue#2](https://github.com/adrianbrowning/prisma-ts-select/issues/2)
|
|
677
|
+
- Support Select Functions
|
|
678
|
+
- [Aggregation #4](https://github.com/adrianbrowning/prisma-ts-select/issues/4)
|
|
679
|
+
- [String #5](https://github.com/adrianbrowning/prisma-ts-select/issues/5)
|
|
680
|
+
- [Date & Time #6](https://github.com/adrianbrowning/prisma-ts-select/issues/6)
|
|
681
|
+
- [Math #7](https://github.com/adrianbrowning/prisma-ts-select/issues/7)
|
|
682
|
+
- [Control Flow #8](https://github.com/adrianbrowning/prisma-ts-select/issues/8)
|
|
683
|
+
- [JSON #9](https://github.com/adrianbrowning/prisma-ts-select/issues/9)
|
|
684
|
+
- [Support a `Many-To-Many` join #19](https://github.com/adrianbrowning/prisma-ts-select/issues/19)
|
|
685
|
+
- [Select column alias #27](https://github.com/adrianbrowning/prisma-ts-select/issues/27)
|
|
686
|
+
- [Table name alias #28](https://github.com/adrianbrowning/prisma-ts-select/issues/28)
|
|
687
|
+
- [whereRaw supporting Prisma.sql](https://github.com/adrianbrowning/prisma-ts-select/issues/29)
|
|
688
|
+
|
|
689
|
+
## Changelog / Versioning
|
|
690
|
+
Changelog is available [here](https://github.com/adrianbrowning/prisma-ts-select/releases). We use [semantic versioning](https://semver.org/) for versioning.
|
|
691
|
+
|
|
692
|
+
## License
|
|
693
|
+
This project is licensed under the MIT License. See the LICENSE file for details.
|
|
694
|
+
|
|
695
|
+
Things of note!!!!
|
|
696
|
+
|
|
697
|
+
- remove typeof from
|
|
698
|
+
- `type _db = DeepWriteable<typeof DB>;`
|
|
699
|
+
- `}[keyof typeof DB];`
|
|
700
|
+
- Merge Items missing //@ts-expect-error - might not be needed
|
|
701
|
+
- groupBy -> having,
|
|
702
|
+
- missing @deprecated
|
|
703
|
+
- ts-exptect-error - might not be needed
|
|
704
|
+
- GetColsFromTableType missing ts-expect-error - might not be needed
|
|
705
|
+
- DB needs to be in the same file.
|
|
706
|
+
|
|
707
|
+
|
|
708
|
+
|
|
709
|
+
# prisma-ts-select
|
|
710
|
+
|
|
711
|
+
## Install
|
|
712
|
+
|
|
713
|
+
```shell
|
|
714
|
+
npm i prisma-ts-select
|
|
715
|
+
pnpm add prisma-ts-select
|
|
716
|
+
```
|
|
717
|
+
|
|
718
|
+
## Setup
|
|
719
|
+
|
|
720
|
+
### Extract
|
|
721
|
+
|
|
722
|
+
|
|
723
|
+
## Usage
|
|
724
|
+
|
|
725
|
+
|