@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.
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +4 -0
- package/dist/model/builder.d.mts +20 -0
- package/dist/model/builder.mjs +18 -0
- package/dist/model/config.d.mts +23 -0
- package/dist/model/core/joins.mjs +184 -0
- package/dist/model/core/projection.mjs +28 -0
- package/dist/model/core/runtime.mjs +198 -0
- package/dist/model/core/thenable.mjs +64 -0
- package/dist/model/core/transform.mjs +39 -0
- package/dist/model/core/where.mjs +130 -0
- package/dist/model/core/with.mjs +19 -0
- package/dist/model/dialect.d.mts +12 -0
- package/dist/model/format.d.mts +4 -0
- package/dist/model/index.d.mts +1 -0
- package/dist/model/index.mjs +3 -0
- package/dist/model/methods/exclude.d.mts +8 -0
- package/dist/model/methods/include.d.mts +6 -0
- package/dist/model/methods/insert.d.mts +7 -0
- package/dist/model/methods/query/where.d.mts +14 -0
- package/dist/model/methods/return.d.mts +12 -0
- package/dist/model/methods/select.d.mts +8 -0
- package/dist/model/methods/update.d.mts +7 -0
- package/dist/model/methods/upsert.d.mts +26 -0
- package/dist/model/methods/with.d.mts +16 -0
- package/dist/model/model.d.mts +105 -0
- package/dist/model/options.d.mts +28 -0
- package/dist/model/query/operations.d.mts +66 -0
- package/dist/model/relation.d.mts +68 -0
- package/dist/model/result.d.mts +34 -0
- package/dist/model/table.d.mts +69 -0
- package/dist/types.d.mts +10 -0
- package/drizzle.config.ts +6 -6
- package/package.json +1 -1
- package/src/model/builder.ts +37 -37
- package/src/model/config.ts +28 -25
- package/src/model/core/joins.ts +337 -252
- package/src/model/core/projection.ts +49 -35
- package/src/model/core/runtime.ts +302 -221
- package/src/model/core/thenable.ts +70 -61
- package/src/model/core/transform.ts +53 -33
- package/src/model/core/where.ts +228 -162
- package/src/model/core/with.ts +21 -21
- package/src/model/dialect.ts +12 -2
- package/src/model/foreigns.ts +6 -10
- package/src/model/format.ts +8 -8
- package/src/model/methods/include.ts +1 -1
- package/src/model/methods/insert.ts +13 -10
- package/src/model/methods/levels.ts +7 -1
- package/src/model/methods/query/where.ts +49 -36
- package/src/model/methods/return.ts +13 -12
- package/src/model/methods/select.ts +29 -29
- package/src/model/methods/update.ts +3 -1
- package/src/model/methods/upsert.ts +35 -36
- package/src/model/model.ts +115 -107
- package/src/model/options.ts +44 -37
- package/src/model/query/operations.ts +73 -72
- package/src/model/relation.ts +47 -46
- package/src/model/result.ts +79 -63
- package/src/model/shape.ts +5 -4
- package/src/model/table.ts +34 -33
- package/src/types.ts +3 -3
- package/tests/builder-v2-mysql.type-test.ts +31 -20
- package/tests/builder-v2.type-test.ts +246 -253
- package/tests/builder.test.ts +1 -1
- package/tests/db.ts +3 -3
- package/tests/find.test.ts +149 -138
- package/tests/insert.test.ts +217 -203
- package/tests/relations.ts +34 -34
- package/tests/schema.ts +34 -35
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
import
|
|
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
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
schema,
|
|
10
|
+
db,
|
|
11
|
+
relations,
|
|
12
|
+
dialect: "PostgreSQL",
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
const userModel = model("user", {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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
|
-
|
|
227
|
-
|
|
228
|
-
|
|
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
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
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
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
db
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
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
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
289
|
+
.where({
|
|
290
|
+
name: esc("Alex"),
|
|
291
|
+
})
|
|
292
|
+
.findFirst()
|
|
293
|
+
.raw();
|
|
303
294
|
|
|
304
295
|
const userModel2 = userModel.extend({
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
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
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
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
|
|
package/tests/builder.test.ts
CHANGED
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
|
-
|
|
12
|
-
|
|
11
|
+
schema,
|
|
12
|
+
relations,
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
// db.transaction
|