@apisr/drizzle-model 0.0.1 → 0.0.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 (70) hide show
  1. package/dist/index.d.mts +3 -0
  2. package/dist/index.mjs +4 -0
  3. package/dist/model/builder.d.mts +20 -0
  4. package/dist/model/builder.mjs +18 -0
  5. package/dist/model/config.d.mts +23 -0
  6. package/dist/model/core/joins.mjs +184 -0
  7. package/dist/model/core/projection.mjs +28 -0
  8. package/dist/model/core/runtime.mjs +198 -0
  9. package/dist/model/core/thenable.mjs +64 -0
  10. package/dist/model/core/transform.mjs +39 -0
  11. package/dist/model/core/where.mjs +130 -0
  12. package/dist/model/core/with.mjs +19 -0
  13. package/dist/model/dialect.d.mts +12 -0
  14. package/dist/model/format.d.mts +4 -0
  15. package/dist/model/index.d.mts +1 -0
  16. package/dist/model/index.mjs +3 -0
  17. package/dist/model/methods/exclude.d.mts +8 -0
  18. package/dist/model/methods/include.d.mts +6 -0
  19. package/dist/model/methods/insert.d.mts +7 -0
  20. package/dist/model/methods/query/where.d.mts +14 -0
  21. package/dist/model/methods/return.d.mts +12 -0
  22. package/dist/model/methods/select.d.mts +8 -0
  23. package/dist/model/methods/update.d.mts +7 -0
  24. package/dist/model/methods/upsert.d.mts +26 -0
  25. package/dist/model/methods/with.d.mts +16 -0
  26. package/dist/model/model.d.mts +105 -0
  27. package/dist/model/options.d.mts +28 -0
  28. package/dist/model/query/operations.d.mts +66 -0
  29. package/dist/model/relation.d.mts +68 -0
  30. package/dist/model/result.d.mts +34 -0
  31. package/dist/model/table.d.mts +69 -0
  32. package/dist/types.d.mts +10 -0
  33. package/drizzle.config.ts +6 -6
  34. package/package.json +1 -1
  35. package/src/model/builder.ts +37 -37
  36. package/src/model/config.ts +28 -25
  37. package/src/model/core/joins.ts +337 -252
  38. package/src/model/core/projection.ts +49 -35
  39. package/src/model/core/runtime.ts +302 -221
  40. package/src/model/core/thenable.ts +70 -61
  41. package/src/model/core/transform.ts +53 -33
  42. package/src/model/core/where.ts +228 -162
  43. package/src/model/core/with.ts +21 -21
  44. package/src/model/dialect.ts +12 -2
  45. package/src/model/foreigns.ts +6 -10
  46. package/src/model/format.ts +8 -8
  47. package/src/model/methods/include.ts +1 -1
  48. package/src/model/methods/insert.ts +13 -10
  49. package/src/model/methods/levels.ts +7 -1
  50. package/src/model/methods/query/where.ts +49 -36
  51. package/src/model/methods/return.ts +13 -12
  52. package/src/model/methods/select.ts +29 -29
  53. package/src/model/methods/update.ts +3 -1
  54. package/src/model/methods/upsert.ts +35 -36
  55. package/src/model/model.ts +115 -107
  56. package/src/model/options.ts +44 -37
  57. package/src/model/query/operations.ts +73 -72
  58. package/src/model/relation.ts +47 -46
  59. package/src/model/result.ts +79 -63
  60. package/src/model/shape.ts +5 -4
  61. package/src/model/table.ts +34 -33
  62. package/src/types.ts +3 -3
  63. package/tests/builder-v2-mysql.type-test.ts +31 -20
  64. package/tests/builder-v2.type-test.ts +246 -253
  65. package/tests/builder.test.ts +1 -1
  66. package/tests/db.ts +3 -3
  67. package/tests/find.test.ts +149 -138
  68. package/tests/insert.test.ts +217 -203
  69. package/tests/relations.ts +34 -34
  70. package/tests/schema.ts +34 -35
@@ -1,40 +1,40 @@
1
- import * as schema from "./schema";
2
- import { db } from "./db";
3
- import { relations } from "./relations";
1
+ import { eq, gte, sql } from "drizzle-orm";
4
2
  import { modelBuilder } from "src/model";
5
- import { gte, sql, eq, or } from "drizzle-orm";
6
3
  import { esc } from "@/model/query/operations";
4
+ import { db } from "./db";
5
+ import { relations } from "./relations";
6
+ import * as schema from "./schema";
7
7
 
8
8
  const model = modelBuilder({
9
- schema,
10
- db,
11
- relations,
12
- dialect: "PostgreSQL"
9
+ schema,
10
+ db,
11
+ relations,
12
+ dialect: "PostgreSQL",
13
13
  });
14
14
 
15
15
  const userModel = model("user", {
16
- methods: {
17
- whereName(name: string) {
18
- return userModel.where({
19
- name: esc(eq, name)
20
- });
21
- },
22
- findByName(name: string) {
23
- return userModel
24
- .where({
25
- name: esc(name)
26
- })
27
- .findFirst()
28
- .select({
29
- id: true,
30
- name: true
31
- });
32
- }
33
- },
34
- format: ({ secretField, ...rest }) => ({
35
- ...rest,
36
- customField: "123"
37
- })
16
+ methods: {
17
+ whereName(name: string) {
18
+ return userModel.where({
19
+ name: esc(eq, name),
20
+ });
21
+ },
22
+ findByName(name: string) {
23
+ return userModel
24
+ .where({
25
+ name: esc(name),
26
+ })
27
+ .findFirst()
28
+ .select({
29
+ id: true,
30
+ name: true,
31
+ });
32
+ },
33
+ },
34
+ format: ({ secretField, ...rest }) => ({
35
+ ...rest,
36
+ customField: "123",
37
+ }),
38
38
  });
39
39
  const userIdeasModel = model("userIdeas", {});
40
40
 
@@ -44,50 +44,47 @@ const userIdeasModel = model("userIdeas", {});
44
44
  // userModel.
45
45
 
46
46
  const testRaw = await userModel
47
- .where({
48
- id: {
49
- or: [
50
- esc(1),
51
- esc(2),
52
- ]
53
- }
54
- })
55
- .findFirst()
56
- .with({
57
- posts: {
58
- comments: true,
59
- },
60
- })
61
- .select({
62
- age: true,
63
- posts: {
64
- title: true,
65
- comments: {
66
- content: true,
67
- },
68
- },
69
- });
47
+ .where({
48
+ id: {
49
+ or: [esc(1), esc(2)],
50
+ },
51
+ })
52
+ .findFirst()
53
+ .with({
54
+ posts: {
55
+ comments: true,
56
+ },
57
+ })
58
+ .select({
59
+ age: true,
60
+ posts: {
61
+ title: true,
62
+ comments: {
63
+ content: true,
64
+ },
65
+ },
66
+ });
70
67
 
71
68
  // testRaw.posts[0]?.comments[0]?.content;
72
69
  const testRaw1 = await userModel
73
- .where({
74
- age: esc(12)
75
- })
76
- .findFirst()
77
- .with({
78
- posts: {
79
- comments: true,
80
- },
81
- })
82
- .exclude({
83
- age: true,
84
- posts: {
85
- id: true,
86
- comments: {
87
- content: true,
88
- },
89
- },
90
- });
70
+ .where({
71
+ age: esc(12),
72
+ })
73
+ .findFirst()
74
+ .with({
75
+ posts: {
76
+ comments: true,
77
+ },
78
+ })
79
+ .exclude({
80
+ age: true,
81
+ posts: {
82
+ id: true,
83
+ comments: {
84
+ content: true,
85
+ },
86
+ },
87
+ });
91
88
 
92
89
  // testRaw1.posts[0]?.comments[0].;
93
90
 
@@ -95,117 +92,119 @@ const postsModel = model("userPosts", {});
95
92
  const commentsModel = model("postComments", {});
96
93
 
97
94
  const testRaw2 = await userModel
98
- .where({
99
- age: esc(12)
100
- })
101
- .findFirst()
102
- .with({
103
- // Fix is relations in here are from userModel not from posts
104
- posts: postsModel.where({
105
- id: esc(12)
106
- }).include({
107
- comments: true,
108
- }),
109
- });
95
+ .where({
96
+ age: esc(12),
97
+ })
98
+ .findFirst()
99
+ .with({
100
+ // Fix is relations in here are from userModel not from posts
101
+ posts: postsModel
102
+ .where({
103
+ id: esc(12),
104
+ })
105
+ .include({
106
+ comments: true,
107
+ }),
108
+ });
110
109
 
111
110
  // testRaw2.posts[0]?.comments;
112
111
 
113
112
  const testRaw3 = await userModel
114
- .where({
115
- id: {
116
- or: [
117
- {
118
- equal: 1,
119
- },
120
- esc(12),
121
- ],
122
- }
123
- })
124
- .findFirst()
125
- .with({
126
- // Fix is relations in here are from userModel not from posts
127
- posts: postsModel
128
- .where({
129
- id: esc(12)
130
- })
131
- .include({
132
- comments: commentsModel.where({ id: esc(12) }).include({
133
- author: true
134
- }),
135
- }),
136
- });
113
+ .where({
114
+ id: {
115
+ or: [
116
+ {
117
+ equal: 1,
118
+ },
119
+ esc(12),
120
+ ],
121
+ },
122
+ })
123
+ .findFirst()
124
+ .with({
125
+ // Fix is relations in here are from userModel not from posts
126
+ posts: postsModel
127
+ .where({
128
+ id: esc(12),
129
+ })
130
+ .include({
131
+ comments: commentsModel.where({ id: esc(12) }).include({
132
+ author: true,
133
+ }),
134
+ }),
135
+ });
137
136
 
138
137
  // testRaw3.posts[0]?.comments[0]?.author;
139
138
 
140
139
  const testRaw4 = await userModel
141
- .where({
142
- age: {
143
- or: [
144
- {
145
- equal: 1,
146
- },
147
- esc(2),
148
- ],
149
- }
150
- })
151
- .findMany()
152
- .with({
153
- // Fix is relations in here are from userModel not from posts
154
- posts: postsModel
155
- .where({
156
- id: esc(12)
157
- })
158
- .include({
159
- comments: true,
160
- }),
161
- })
162
- .select({
163
- posts: {
164
- description: true,
165
- comments: true,
166
- },
167
- });
140
+ .where({
141
+ age: {
142
+ or: [
143
+ {
144
+ equal: 1,
145
+ },
146
+ esc(2),
147
+ ],
148
+ },
149
+ })
150
+ .findMany()
151
+ .with({
152
+ // Fix is relations in here are from userModel not from posts
153
+ posts: postsModel
154
+ .where({
155
+ id: esc(12),
156
+ })
157
+ .include({
158
+ comments: true,
159
+ }),
160
+ })
161
+ .select({
162
+ posts: {
163
+ description: true,
164
+ comments: true,
165
+ },
166
+ });
168
167
 
169
168
  // testRaw4[0]?.posts[0].
170
169
 
171
170
  const testRaw5 = await userModel
172
- .where({
173
- age: {
174
- or: [
175
- {
176
- equal: 1,
177
- },
178
- esc(12),
179
- ]
180
- },
181
- })
182
- .findMany()
183
- .with({
184
- // Fix is relations in here are from userModel not from posts
185
- posts: postsModel
186
- .where({
187
- id: esc(12)
188
- })
189
- .include({
190
- comments: true,
191
- }),
192
- })
193
- .exclude({
194
- posts: {
195
- description: true,
196
- comments: true,
197
- },
198
- });
171
+ .where({
172
+ age: {
173
+ or: [
174
+ {
175
+ equal: 1,
176
+ },
177
+ esc(12),
178
+ ],
179
+ },
180
+ })
181
+ .findMany()
182
+ .with({
183
+ // Fix is relations in here are from userModel not from posts
184
+ posts: postsModel
185
+ .where({
186
+ id: esc(12),
187
+ })
188
+ .include({
189
+ comments: true,
190
+ }),
191
+ })
192
+ .exclude({
193
+ posts: {
194
+ description: true,
195
+ comments: true,
196
+ },
197
+ });
199
198
 
200
199
  // testRaw5[0]?.posts[0].
201
200
 
202
201
  const testRaw6 = await userModel
203
- .insert({
204
- email: "email@email",
205
- name: "Nameie",
206
- age: 123,
207
- })
208
- .return();
202
+ .insert({
203
+ email: "email@email",
204
+ name: "Nameie",
205
+ age: 123,
206
+ })
207
+ .return();
209
208
 
210
209
  // testRaw6.
211
210
 
@@ -214,75 +213,67 @@ const testRaw6 = await userModel
214
213
  // testRaw6.
215
214
 
216
215
  const testRaw7 = await userModel
217
- .where({ id: esc(12) })
218
- .update({
219
- age: 12
220
- })
221
- .return();
216
+ .where({ id: esc(12) })
217
+ .update({
218
+ age: 12,
219
+ })
220
+ .return();
222
221
 
223
222
  // testRaw7?.[0]?.;
224
223
 
225
224
  const testRaw8 = await userModel
226
- .where({ id: esc(12) })
227
- .delete()
228
- .return();
225
+ .where({ id: esc(12) })
226
+ .delete()
227
+ .return();
229
228
 
230
229
  // testRaw8[0].
231
230
 
232
231
  // postsModel.
233
232
 
234
233
  const testRaw9 = await postsModel
235
- .where({
236
- user: {
237
- id: esc(12)
238
- },
239
- })
240
- .update({
241
- description: ""
242
- })
243
- .return();
234
+ .where({
235
+ user: {
236
+ id: esc(12),
237
+ },
238
+ })
239
+ .update({
240
+ description: "",
241
+ })
242
+ .return();
244
243
 
245
244
  const testRaw10 = await postsModel
246
- .where(
247
- userModel.where({ id: esc(12) })
248
- )
249
- .update({
250
- description: ""
251
- })
252
- .return();
245
+ .where(userModel.where({ id: esc(12) }))
246
+ .update({
247
+ description: "",
248
+ })
249
+ .return();
253
250
 
254
251
  const testRaw11 = await postsModel
255
- .where(
256
- sql``
257
- )
258
- .update({
259
- description: ""
260
- })
261
- .return();
252
+ .where(sql``)
253
+ .update({
254
+ description: "",
255
+ })
256
+ .return();
262
257
 
263
258
  // testRaw11[0].
264
259
 
265
260
  const testRaw12 = await postsModel
266
- .where(
267
- gte(schema.postComments.id, 123)
268
- )
269
- .update({
270
- description: ""
271
- })
272
- .return();
273
-
274
- db.transaction(async tx => {
275
- const result = await postsModel
276
- .db(tx)
277
- .where(
278
- gte(schema.postComments.id, 123)
279
- )
280
- .update({
281
- description: ""
282
- })
283
- .return({
284
- id: true
285
- });
261
+ .where(gte(schema.postComments.id, 123))
262
+ .update({
263
+ description: "",
264
+ })
265
+ .return();
266
+
267
+ db.transaction(async (tx) => {
268
+ const result = await postsModel
269
+ .db(tx)
270
+ .where(gte(schema.postComments.id, 123))
271
+ .update({
272
+ description: "",
273
+ })
274
+ .return({
275
+ id: true,
276
+ });
286
277
  });
287
278
 
288
279
  const testRaw13 = await userModel.whereName("Alex").findFirst();
@@ -295,46 +286,48 @@ const testRaw14 = await userModel.findByName("Alex");
295
286
  // userModel.w
296
287
 
297
288
  const testRaw15 = await userModel
298
- .where({
299
- name: esc("Alex"),
300
- })
301
- .findFirst()
302
- .raw();
289
+ .where({
290
+ name: esc("Alex"),
291
+ })
292
+ .findFirst()
293
+ .raw();
303
294
 
304
295
  const userModel2 = userModel.extend({
305
- methods: {
306
- whereName(name: string) {
307
- return userModel2.where({
308
- name: esc(name)
309
- });
310
- }
311
- },
312
- format: (output) => {
313
- return {
314
- ...output,
315
- isJoke: true
316
- };
317
- }
296
+ methods: {
297
+ whereName(name: string) {
298
+ return userModel2.where({
299
+ name: esc(name),
300
+ });
301
+ },
302
+ },
303
+ format: (output) => {
304
+ return {
305
+ ...output,
306
+ isJoke: true,
307
+ };
308
+ },
318
309
  });
319
310
 
320
- const testRaw16 = await userModel2.upsert({
321
- insert: {
322
- email: "123",
323
- name: "123",
324
- age: 123,
325
- },
326
- update: (c) => ({
327
- name: c.inserted("name")
328
- }),
329
- updateWhere: (c) => ({
330
- name: {
331
- not: c.excluded("name")
332
- }
333
- }),
334
- target: ["id"]
335
- }).return({
336
- age: true
337
- });
311
+ const testRaw16 = await userModel2
312
+ .upsert({
313
+ insert: {
314
+ email: "123",
315
+ name: "123",
316
+ age: 123,
317
+ },
318
+ update: (c) => ({
319
+ name: c.inserted("name"),
320
+ }),
321
+ updateWhere: (c) => ({
322
+ name: {
323
+ not: c.excluded("name"),
324
+ },
325
+ }),
326
+ target: ["id"],
327
+ })
328
+ .return({
329
+ age: true,
330
+ });
338
331
 
339
332
  // testRaw16.
340
333
 
@@ -1,7 +1,7 @@
1
1
  import { modelBuilder } from "../src";
2
- import * as schema from "./schema";
3
2
  import { db } from "./db";
4
3
  import { relations } from "./relations";
4
+ import * as schema from "./schema";
5
5
 
6
6
  const model = modelBuilder({
7
7
  schema,
package/tests/db.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { drizzle } from "drizzle-orm/node-postgres";
2
+ import { relations } from "./relations";
2
3
  // import { drizzle as drizzleMysql } from "drizzle-orm/mysql2";
3
4
  import * as schema from "./schema";
4
- import { relations } from "./relations";
5
5
 
6
6
  // export const mysqlDb = drizzleMysql("nothing");
7
7
 
8
8
  // mysqlDb.insert().values().$returningId;
9
9
 
10
10
  export const db = drizzle(process.env.DATABASE_URL!, {
11
- schema,
12
- relations,
11
+ schema,
12
+ relations,
13
13
  });
14
14
 
15
15
  // db.transaction