forj 0.1.6 → 0.1.7
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/package.json +5 -5
- package/src/clause-builder.ts +26 -26
- package/src/d1/model.ts +6 -5
- package/src/dynamodb/schema.ts +45 -17
- package/src/model.ts +270 -14
- package/src/query-builder.ts +245 -43
- package/src/types.ts +49 -58
- package/src/utils.ts +127 -37
package/src/query-builder.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { Timestamp } from 't0n'
|
|
1
2
|
import ClauseBuilder from './clause-builder'
|
|
2
3
|
import {
|
|
4
|
+
types,
|
|
3
5
|
isOperator,
|
|
4
6
|
parseColumn, parseSelectColumn,
|
|
5
7
|
formatValue,
|
|
@@ -8,15 +10,18 @@ import {
|
|
|
8
10
|
sqlName,
|
|
9
11
|
} from './utils'
|
|
10
12
|
import type {
|
|
11
|
-
IJoinBuilder, IClauseBuilder,
|
|
12
13
|
OrderDirection,
|
|
13
14
|
JoinType,
|
|
14
15
|
WhereArgs,
|
|
15
|
-
Values,
|
|
16
|
+
Value, Values,
|
|
16
17
|
Item,
|
|
17
18
|
JoinArgs,
|
|
18
19
|
DBSchema,
|
|
19
20
|
Pipe,
|
|
21
|
+
QueryType,
|
|
22
|
+
TableOpts,
|
|
23
|
+
WhereFn,
|
|
24
|
+
Operator,
|
|
20
25
|
} from './types'
|
|
21
26
|
|
|
22
27
|
export default class QueryBuilder<
|
|
@@ -27,9 +32,13 @@ export default class QueryBuilder<
|
|
|
27
32
|
// T, // = any,,
|
|
28
33
|
// C extends keyof T = keyof T
|
|
29
34
|
// > {
|
|
30
|
-
>
|
|
35
|
+
> {
|
|
31
36
|
#table!: string
|
|
32
37
|
#schema?: DBSchema
|
|
38
|
+
#type: number = 1
|
|
39
|
+
// #data: Partial<Record<C, Value>> = {}
|
|
40
|
+
#keys: string[] = []
|
|
41
|
+
#values: Value[] = []
|
|
33
42
|
#selects: string[] = []
|
|
34
43
|
#clauses!: ClauseBuilder<T>
|
|
35
44
|
#groups: string[] = []
|
|
@@ -43,6 +52,7 @@ export default class QueryBuilder<
|
|
|
43
52
|
#joins: string[] = []
|
|
44
53
|
|
|
45
54
|
#pipe?: Pipe<S, T, C>
|
|
55
|
+
#opts: TableOpts = {}
|
|
46
56
|
|
|
47
57
|
constructor(
|
|
48
58
|
table: string,
|
|
@@ -55,6 +65,11 @@ export default class QueryBuilder<
|
|
|
55
65
|
this.#clauses = new ClauseBuilder<T>(table, schema)
|
|
56
66
|
}
|
|
57
67
|
|
|
68
|
+
opts(opts: TableOpts) {
|
|
69
|
+
this.#opts = opts
|
|
70
|
+
return this
|
|
71
|
+
}
|
|
72
|
+
|
|
58
73
|
async run() {
|
|
59
74
|
if (!this.#pipe?.run)
|
|
60
75
|
throw new Error(`No database connection.`)
|
|
@@ -62,6 +77,34 @@ export default class QueryBuilder<
|
|
|
62
77
|
return await this.#pipe?.run(this)
|
|
63
78
|
}
|
|
64
79
|
|
|
80
|
+
#setType(type: QueryType, data?: Partial<Record<C, Value>>) {
|
|
81
|
+
this.#type = type
|
|
82
|
+
if (data) {
|
|
83
|
+
this.#keys = Object.keys(data)
|
|
84
|
+
this.#values = Object.values(data)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return this
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
insert(data: Partial<Record<C, Value>>) {
|
|
91
|
+
if (this.#opts.timestamps || this.#opts.createdAt) // @ts-ignore
|
|
92
|
+
data.created_at = Timestamp.now()
|
|
93
|
+
|
|
94
|
+
return this.#setType(types.INSERT, data)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
update(data: Partial<Record<C, Value>>) {
|
|
98
|
+
if (this.#opts.timestamps || this.#opts.updatedAt) // @ts-ignore
|
|
99
|
+
data.updated_at = Timestamp.now()
|
|
100
|
+
|
|
101
|
+
return this.#setType(types.UPDATE, data)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
delete() {
|
|
105
|
+
return this.#setType(types.DELETE)
|
|
106
|
+
}
|
|
107
|
+
|
|
65
108
|
async first<K extends keyof T>(...columns: K[] | K[][]): Promise<null | Item<T, C>> {
|
|
66
109
|
columns?.length && this.select(...columns)
|
|
67
110
|
const resp = await this.run()
|
|
@@ -75,6 +118,7 @@ export default class QueryBuilder<
|
|
|
75
118
|
}
|
|
76
119
|
|
|
77
120
|
select<K extends keyof T>(...columns: K[] | K[][]): QueryBuilder<S, T, K> {
|
|
121
|
+
this.#type = types.SELECT
|
|
78
122
|
this.#selects.push(...columns.flat(Infinity) as string[])
|
|
79
123
|
return this as any
|
|
80
124
|
}
|
|
@@ -123,7 +167,7 @@ export default class QueryBuilder<
|
|
|
123
167
|
|
|
124
168
|
if (!isJoinCompare(value, this.#schema)) {
|
|
125
169
|
if (this.#schema && !zSame(col.replace(/"/g, ''), value, this.#schema))
|
|
126
|
-
throw new Error(`Table column '${col}' of type '${zType(col, this.#schema)}' is not assignable as type of '${typeof value}'.`)
|
|
170
|
+
throw new Error(`Table column '${col.replace(/"/g, '')}' of type '${zType(col.replace(/"/g, ''), this.#schema)}' is not assignable as type of '${typeof value}'.`)
|
|
127
171
|
// @ts-ignore
|
|
128
172
|
this.#clauses.args = [value] // @ts-ignore // TODO: https://developers.cloudflare.com/d1/worker-api/#type-conversion
|
|
129
173
|
value = '?'
|
|
@@ -134,135 +178,272 @@ export default class QueryBuilder<
|
|
|
134
178
|
return this
|
|
135
179
|
}
|
|
136
180
|
|
|
181
|
+
join<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
182
|
+
join<
|
|
183
|
+
J extends keyof S,
|
|
184
|
+
T extends S[J],
|
|
185
|
+
K extends keyof T
|
|
186
|
+
>(table: J, column: K, value: T[K]): this
|
|
187
|
+
join<
|
|
188
|
+
J extends keyof S,
|
|
189
|
+
T extends S[J],
|
|
190
|
+
K extends keyof T
|
|
191
|
+
>(table: J, column: K, operator: Operator, value: T[K]): this
|
|
192
|
+
join<
|
|
193
|
+
J extends keyof S,
|
|
194
|
+
T extends S[J],
|
|
195
|
+
K extends keyof T,
|
|
196
|
+
J2 extends keyof S,
|
|
197
|
+
K2 extends keyof S[J2],
|
|
198
|
+
>(table: J, column: K, table2: J2, column2: K2): this
|
|
199
|
+
join<
|
|
200
|
+
J extends keyof S,
|
|
201
|
+
T extends S[J],
|
|
202
|
+
K extends keyof T,
|
|
203
|
+
J2 extends keyof S,
|
|
204
|
+
K2 extends keyof S[J2],
|
|
205
|
+
>(table: J, column: K, operator: Operator, table2: J2, column2: K2): this
|
|
137
206
|
join<J extends keyof S>(table: J, ...args: JoinArgs<S, J>) {
|
|
138
207
|
return this.#join(undefined, table, ...args)
|
|
139
208
|
}
|
|
140
209
|
|
|
210
|
+
innerJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
211
|
+
innerJoin<
|
|
212
|
+
J extends keyof S,
|
|
213
|
+
T extends S[J],
|
|
214
|
+
K extends keyof T
|
|
215
|
+
>(table: J, column: K, value: T[K]): this
|
|
216
|
+
innerJoin<
|
|
217
|
+
J extends keyof S,
|
|
218
|
+
T extends S[J],
|
|
219
|
+
K extends keyof T
|
|
220
|
+
>(table: J, column: K, operator: Operator, value: T[K]): this
|
|
221
|
+
innerJoin<
|
|
222
|
+
J extends keyof S,
|
|
223
|
+
T extends S[J],
|
|
224
|
+
K extends keyof T,
|
|
225
|
+
J2 extends keyof S,
|
|
226
|
+
K2 extends keyof S[J2],
|
|
227
|
+
>(table: J, column: K, table2: J2, column2: K2): this
|
|
228
|
+
innerJoin<
|
|
229
|
+
J extends keyof S,
|
|
230
|
+
T extends S[J],
|
|
231
|
+
K extends keyof T,
|
|
232
|
+
J2 extends keyof S,
|
|
233
|
+
K2 extends keyof S[J2],
|
|
234
|
+
>(table: J, column: K, operator: Operator, table2: J2, column2: K2): this
|
|
141
235
|
innerJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>) {
|
|
142
236
|
return this.#join('INNER', table, ...args)
|
|
143
237
|
}
|
|
144
238
|
|
|
239
|
+
leftJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
240
|
+
leftJoin<
|
|
241
|
+
J extends keyof S,
|
|
242
|
+
T extends S[J],
|
|
243
|
+
K extends keyof T
|
|
244
|
+
>(table: J, column: K, value: T[K]): this
|
|
245
|
+
leftJoin<
|
|
246
|
+
J extends keyof S,
|
|
247
|
+
T extends S[J],
|
|
248
|
+
K extends keyof T
|
|
249
|
+
>(table: J, column: K, operator: Operator, value: T[K]): this
|
|
250
|
+
leftJoin<
|
|
251
|
+
J extends keyof S,
|
|
252
|
+
T extends S[J],
|
|
253
|
+
K extends keyof T,
|
|
254
|
+
J2 extends keyof S,
|
|
255
|
+
K2 extends keyof S[J2],
|
|
256
|
+
>(table: J, column: K, table2: J2, column2: K2): this
|
|
257
|
+
leftJoin<
|
|
258
|
+
J extends keyof S,
|
|
259
|
+
T extends S[J],
|
|
260
|
+
K extends keyof T,
|
|
261
|
+
J2 extends keyof S,
|
|
262
|
+
K2 extends keyof S[J2],
|
|
263
|
+
>(table: J, column: K, operator: Operator, table2: J2, column2: K2): this
|
|
145
264
|
leftJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>) {
|
|
146
265
|
return this.#join('LEFT', table, ...args)
|
|
147
266
|
}
|
|
148
267
|
|
|
268
|
+
rightJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
269
|
+
rightJoin<
|
|
270
|
+
J extends keyof S,
|
|
271
|
+
T extends S[J],
|
|
272
|
+
K extends keyof T
|
|
273
|
+
>(table: J, column: K, value: T[K]): this
|
|
274
|
+
rightJoin<
|
|
275
|
+
J extends keyof S,
|
|
276
|
+
T extends S[J],
|
|
277
|
+
K extends keyof T
|
|
278
|
+
>(table: J, column: K, operator: Operator, value: T[K]): this
|
|
279
|
+
rightJoin<
|
|
280
|
+
J extends keyof S,
|
|
281
|
+
T extends S[J],
|
|
282
|
+
K extends keyof T,
|
|
283
|
+
J2 extends keyof S,
|
|
284
|
+
K2 extends keyof S[J2],
|
|
285
|
+
>(table: J, column: K, table2: J2, column2: K2): this
|
|
286
|
+
rightJoin<
|
|
287
|
+
J extends keyof S,
|
|
288
|
+
T extends S[J],
|
|
289
|
+
K extends keyof T,
|
|
290
|
+
J2 extends keyof S,
|
|
291
|
+
K2 extends keyof S[J2],
|
|
292
|
+
>(table: J, column: K, operator: Operator, table2: J2, column2: K2): this
|
|
149
293
|
rightJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>) {
|
|
150
294
|
return this.#join('RIGHT', table, ...args)
|
|
151
295
|
}
|
|
152
296
|
|
|
297
|
+
crossJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
298
|
+
crossJoin<
|
|
299
|
+
J extends keyof S,
|
|
300
|
+
T extends S[J],
|
|
301
|
+
K extends keyof T
|
|
302
|
+
>(table: J, column: K, value: T[K]): this
|
|
303
|
+
crossJoin<
|
|
304
|
+
J extends keyof S,
|
|
305
|
+
T extends S[J],
|
|
306
|
+
K extends keyof T
|
|
307
|
+
>(table: J, column: K, operator: Operator, value: T[K]): this
|
|
308
|
+
crossJoin<
|
|
309
|
+
J extends keyof S,
|
|
310
|
+
T extends S[J],
|
|
311
|
+
K extends keyof T,
|
|
312
|
+
J2 extends keyof S,
|
|
313
|
+
K2 extends keyof S[J2],
|
|
314
|
+
>(table: J, column: K, table2: J2, column2: K2): this
|
|
315
|
+
crossJoin<
|
|
316
|
+
J extends keyof S,
|
|
317
|
+
T extends S[J],
|
|
318
|
+
K extends keyof T,
|
|
319
|
+
J2 extends keyof S,
|
|
320
|
+
K2 extends keyof S[J2],
|
|
321
|
+
>(table: J, column: K, operator: Operator, table2: J2, column2: K2): this
|
|
153
322
|
crossJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>) {
|
|
154
323
|
return this.#join('CROSS', table, ...args)
|
|
155
324
|
}
|
|
156
325
|
|
|
326
|
+
where(fn: WhereFn<T>): this
|
|
327
|
+
where<K extends keyof T>(column: K, value: T[K]): this
|
|
328
|
+
where<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
157
329
|
where(...args: WhereArgs<T>) {
|
|
158
330
|
this.#clauses.where(...args)
|
|
159
331
|
return this
|
|
160
332
|
}
|
|
161
|
-
on(
|
|
333
|
+
on(fn: WhereFn<T>): this
|
|
334
|
+
on<K extends keyof T>(column: K, value: T[K]): this
|
|
335
|
+
on<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
336
|
+
on(...args: WhereArgs<T>) { // @ts-ignore
|
|
162
337
|
return this.where(...args)
|
|
163
338
|
}
|
|
164
339
|
|
|
340
|
+
orWhere(fn: WhereFn<T>): this
|
|
341
|
+
orWhere<K extends keyof T>(column: K, value: T[K]): this
|
|
342
|
+
orWhere<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
165
343
|
orWhere(...args: WhereArgs<T>) {
|
|
166
344
|
this.#clauses.orWhere(...args)
|
|
167
345
|
return this
|
|
168
346
|
}
|
|
169
|
-
orOn(
|
|
347
|
+
orOn(fn: WhereFn<T>): this
|
|
348
|
+
orOn<K extends keyof T>(column: K, value: T[K]): this
|
|
349
|
+
orOn<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
350
|
+
orOn(...args: WhereArgs<T>) { // @ts-ignore
|
|
170
351
|
return this.orWhere(...args)
|
|
171
352
|
}
|
|
172
353
|
|
|
173
|
-
whereIn(column:
|
|
354
|
+
whereIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
174
355
|
this.#clauses.whereIn(column, values)
|
|
175
356
|
return this
|
|
176
357
|
}
|
|
177
|
-
in(column:
|
|
358
|
+
in<K extends keyof T>(column: K, values: T[K][]) {
|
|
178
359
|
return this.whereIn(column, values)
|
|
179
360
|
}
|
|
180
361
|
|
|
181
|
-
whereNotIn(column:
|
|
362
|
+
whereNotIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
182
363
|
this.#clauses.whereNotIn(column, values)
|
|
183
364
|
return this
|
|
184
365
|
}
|
|
185
|
-
notIn(column:
|
|
366
|
+
notIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
186
367
|
return this.whereNotIn(column, values)
|
|
187
368
|
}
|
|
188
369
|
|
|
189
|
-
orWhereIn(column:
|
|
370
|
+
orWhereIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
190
371
|
this.#clauses.orWhereIn(column, values)
|
|
191
372
|
return this
|
|
192
373
|
}
|
|
193
|
-
orIn(column:
|
|
374
|
+
orIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
194
375
|
return this.orWhereIn(column, values)
|
|
195
376
|
}
|
|
196
377
|
|
|
197
|
-
orWhereNotIn(column:
|
|
378
|
+
orWhereNotIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
198
379
|
this.#clauses.orWhereNotIn(column, values)
|
|
199
380
|
return this
|
|
200
381
|
}
|
|
201
|
-
orNotIn(column:
|
|
382
|
+
orNotIn<K extends keyof T>(column: K, values: T[K][]) {
|
|
202
383
|
return this.orWhereNotIn(column, values)
|
|
203
384
|
}
|
|
204
385
|
|
|
205
|
-
whereBetween(column:
|
|
386
|
+
whereBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
206
387
|
this.#clauses.whereBetween(column, one, two)
|
|
207
388
|
return this
|
|
208
389
|
}
|
|
209
|
-
between(column:
|
|
390
|
+
between<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
210
391
|
return this.whereBetween(column, one, two)
|
|
211
392
|
}
|
|
212
393
|
|
|
213
|
-
orWhereBetween(column:
|
|
394
|
+
orWhereBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
214
395
|
this.#clauses.orWhereBetween(column, one, two)
|
|
215
396
|
return this
|
|
216
397
|
}
|
|
217
|
-
orBetween(column:
|
|
398
|
+
orBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
218
399
|
return this.orWhereBetween(column, one, two)
|
|
219
400
|
}
|
|
220
401
|
|
|
221
|
-
whereNotBetween(column:
|
|
402
|
+
whereNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
222
403
|
this.#clauses.whereNotBetween(column, one, two)
|
|
223
404
|
return this
|
|
224
405
|
}
|
|
225
|
-
notBetween(column:
|
|
406
|
+
notBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
226
407
|
return this.whereNotBetween(column, one, two)
|
|
227
408
|
}
|
|
228
409
|
|
|
229
|
-
orWhereNotBetween(column:
|
|
410
|
+
orWhereNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
230
411
|
this.#clauses.orWhereNotBetween(column, one, two)
|
|
231
412
|
return this
|
|
232
413
|
}
|
|
233
|
-
orNotBetween(column:
|
|
414
|
+
orNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]) {
|
|
234
415
|
return this.orWhereNotBetween(column, one, two)
|
|
235
416
|
}
|
|
236
417
|
|
|
237
|
-
whereNull(column:
|
|
418
|
+
whereNull<K extends keyof T>(column: K) {
|
|
238
419
|
this.#clauses.whereNull(column)
|
|
239
420
|
return this
|
|
240
421
|
}
|
|
241
|
-
onNull(column:
|
|
422
|
+
onNull<K extends keyof T>(column: K) {
|
|
242
423
|
return this.whereNull(column)
|
|
243
424
|
}
|
|
244
425
|
|
|
245
|
-
orWhereNull(column:
|
|
426
|
+
orWhereNull<K extends keyof T>(column: K) {
|
|
246
427
|
this.#clauses.orWhereNull(column)
|
|
247
428
|
return this
|
|
248
429
|
}
|
|
249
|
-
orOnNull(column:
|
|
430
|
+
orOnNull<K extends keyof T>(column: K) {
|
|
250
431
|
return this.orWhereNull(column)
|
|
251
432
|
}
|
|
252
433
|
|
|
253
|
-
whereNotNull(column:
|
|
434
|
+
whereNotNull<K extends keyof T>(column: K) {
|
|
254
435
|
this.#clauses.whereNotNull(column)
|
|
255
436
|
return this
|
|
256
437
|
}
|
|
257
|
-
onNotNull(column:
|
|
438
|
+
onNotNull<K extends keyof T>(column: K) {
|
|
258
439
|
return this.whereNotNull(column)
|
|
259
440
|
}
|
|
260
441
|
|
|
261
|
-
orWhereNotNull(column:
|
|
442
|
+
orWhereNotNull<K extends keyof T>(column: K) {
|
|
262
443
|
this.#clauses.orWhereNotNull(column)
|
|
263
444
|
return this
|
|
264
445
|
}
|
|
265
|
-
orNotNull(column:
|
|
446
|
+
orNotNull<K extends keyof T>(column: K) {
|
|
266
447
|
return this.orWhereNotNull(column)
|
|
267
448
|
}
|
|
268
449
|
|
|
@@ -317,6 +498,9 @@ export default class QueryBuilder<
|
|
|
317
498
|
}
|
|
318
499
|
|
|
319
500
|
get args() {
|
|
501
|
+
if (this.#values.length)
|
|
502
|
+
return [...this.#values, ...this.#clauses.args]
|
|
503
|
+
|
|
320
504
|
return this.#clauses.args
|
|
321
505
|
}
|
|
322
506
|
get arguments() {
|
|
@@ -329,25 +513,43 @@ export default class QueryBuilder<
|
|
|
329
513
|
get query() {
|
|
330
514
|
let sql = ''
|
|
331
515
|
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
516
|
+
switch (this.#type) {
|
|
517
|
+
case types.SELECT:
|
|
518
|
+
const selects = new Set<string>()
|
|
519
|
+
this.#selects.forEach(column => {
|
|
520
|
+
column = parseSelectColumn(column, this.#table, this.#hasJoin)
|
|
521
|
+
!selects.has(column) && selects.add(column)
|
|
522
|
+
})
|
|
337
523
|
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
524
|
+
sql += `SELECT ${this.#distinct ? 'DISTINCT ' : ''}${
|
|
525
|
+
selects.size ? [...selects].join(', ') : '*'
|
|
526
|
+
}`
|
|
341
527
|
|
|
342
|
-
|
|
528
|
+
sql += ' FROM '+ this.#table
|
|
343
529
|
|
|
344
|
-
|
|
345
|
-
|
|
530
|
+
if (this.#joins.length)
|
|
531
|
+
sql += ' '+ this.#joins.join(' ')
|
|
532
|
+
|
|
533
|
+
break
|
|
534
|
+
case types.INSERT:
|
|
535
|
+
sql += `INSERT INTO ${this.#table} (${this.#keys.map(key => sqlName(key)).join(', ')})`
|
|
536
|
+
sql += ` VALUES (${Array.from({ length: this.#keys.length }, () => '?').join(', ')})`
|
|
537
|
+
|
|
538
|
+
break
|
|
539
|
+
case types.UPDATE:
|
|
540
|
+
sql += `UPDATE ${this.#table} SET ${this.#keys.map(key => key + ` = ?`).join(', ')}`
|
|
541
|
+
|
|
542
|
+
break
|
|
543
|
+
case types.DELETE:
|
|
544
|
+
sql += `DELETE FROM ` + this.#table
|
|
545
|
+
|
|
546
|
+
break
|
|
547
|
+
}
|
|
346
548
|
|
|
347
549
|
if (this.#clauses.length)
|
|
348
550
|
sql += ' WHERE '+ this.#clauses.clauses.join(' ')
|
|
349
551
|
|
|
350
|
-
if (this.#groups.length)
|
|
552
|
+
if (this.#type == types.SELECT && this.#groups.length)
|
|
351
553
|
sql += ' GROUP BY '+ this.#groups.join(', ')
|
|
352
554
|
|
|
353
555
|
if (this.#orders.length)
|
|
@@ -363,7 +565,7 @@ export default class QueryBuilder<
|
|
|
363
565
|
}
|
|
364
566
|
|
|
365
567
|
get sql() {
|
|
366
|
-
return this.#bind(this.query, this
|
|
568
|
+
return this.#bind(this.query, this.args)
|
|
367
569
|
}
|
|
368
570
|
get raw() {
|
|
369
571
|
return this.sql
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import z from 'zod'
|
|
2
2
|
import QueryBuilder from './query-builder'
|
|
3
|
+
import { types } from './utils'
|
|
3
4
|
|
|
4
5
|
export type text = string
|
|
5
6
|
export type real = number
|
|
@@ -15,18 +16,26 @@ export type Values = Value[]
|
|
|
15
16
|
// export type WriteType = Primitive | ArrayBuffer | ArrayBufferView | undefined
|
|
16
17
|
// export type ReadType = Primitive | any[]
|
|
17
18
|
|
|
19
|
+
export type QueryType = typeof types[keyof typeof types]
|
|
20
|
+
export type TableOpts = {
|
|
21
|
+
timestamps?: boolean,
|
|
22
|
+
createdAt?: boolean,
|
|
23
|
+
updatedAt?: boolean,
|
|
24
|
+
}
|
|
25
|
+
|
|
18
26
|
export type Operator = '=' | '!=' | '<' | '>' | '<=' | '>=' | 'LIKE' // | 'IN' | 'NOT IN' | 'BETWEEN' | 'IS NULL' | 'IS NOT NULL'
|
|
19
27
|
export type OrderDirection = 'ASC' | 'DESC' | 'asc' | 'desc'
|
|
20
28
|
|
|
21
29
|
export type JoinType = 'INNER' | 'LEFT' | 'RIGHT' | 'CROSS'
|
|
22
30
|
|
|
23
|
-
export type DBSchema = z.ZodObject<
|
|
31
|
+
export type DBSchema = z.ZodObject<z.ZodRawShape>
|
|
32
|
+
|
|
33
|
+
export type SchemaObject = z.ZodRawShape
|
|
24
34
|
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
TSchema extends z.ZodObject<infer TShape>
|
|
35
|
+
export type SchemaKeys<TSchema extends DBSchema | SchemaObject> =
|
|
36
|
+
TSchema extends z.ZodObject<infer TShape extends z.ZodRawShape>
|
|
28
37
|
? keyof TShape
|
|
29
|
-
: TSchema extends
|
|
38
|
+
: TSchema extends z.ZodRawShape
|
|
30
39
|
? keyof TSchema
|
|
31
40
|
: never
|
|
32
41
|
|
|
@@ -53,65 +62,57 @@ export type Item<B, S extends keyof B, T = Pick<B, S>> = { [K in keyof T]: T[K]
|
|
|
53
62
|
|
|
54
63
|
export type ClauseOperator = 'AND' | 'OR'
|
|
55
64
|
|
|
56
|
-
export type WhereFn<T
|
|
57
|
-
export type WhereArgs<T, C extends keyof T = keyof T> = [WhereFn<T
|
|
65
|
+
export type WhereFn<T> = (q: IClauseBuilder<T>) => void
|
|
66
|
+
export type WhereArgs<T, C extends keyof T = keyof T> = [WhereFn<T>] | [C, T[C]] | [C, Operator, T[C]]
|
|
58
67
|
|
|
59
|
-
export interface IClauseBuilder<T
|
|
60
|
-
where(
|
|
61
|
-
where(column:
|
|
62
|
-
where(column: C, operator: Operator, value: T[C]): this
|
|
63
|
-
where(...args: WhereArgs<T>): this
|
|
68
|
+
export interface IClauseBuilder<T> {
|
|
69
|
+
where<K extends keyof T>(column: K, value: T[K]): this
|
|
70
|
+
where<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
64
71
|
|
|
65
|
-
on(
|
|
66
|
-
on(column:
|
|
67
|
-
on(column: C, operator: Operator, value: T[C]): this
|
|
68
|
-
on(...args: WhereArgs<T>): this
|
|
72
|
+
on<K extends keyof T>(column: K, value: T[K]): this
|
|
73
|
+
on<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
69
74
|
|
|
70
|
-
orWhere(
|
|
71
|
-
orWhere(column:
|
|
72
|
-
orWhere(column: C, operator: Operator, value: T[C]): this
|
|
73
|
-
orWhere(...args: WhereArgs<T>): this
|
|
75
|
+
orWhere<K extends keyof T>(column: K, value: T[K]): this
|
|
76
|
+
orWhere<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
74
77
|
|
|
75
|
-
orOn(
|
|
76
|
-
orOn(column:
|
|
77
|
-
orOn(column: C, operator: Operator, value: T[C]): this
|
|
78
|
-
orOn(...args: WhereArgs<T>): this
|
|
78
|
+
orOn<K extends keyof T>(column: K, value: T[K]): this
|
|
79
|
+
orOn<K extends keyof T>(column: K, operator: Operator, value: T[K]): this
|
|
79
80
|
|
|
80
|
-
whereIn(column:
|
|
81
|
-
in(column:
|
|
81
|
+
whereIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
82
|
+
in<K extends keyof T>(column: K, values: T[K][]): this
|
|
82
83
|
|
|
83
|
-
whereNotIn(column:
|
|
84
|
-
notIn(column:
|
|
84
|
+
whereNotIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
85
|
+
notIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
85
86
|
|
|
86
|
-
orWhereIn(column:
|
|
87
|
-
orIn(column:
|
|
87
|
+
orWhereIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
88
|
+
orIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
88
89
|
|
|
89
|
-
orWhereNotIn(column:
|
|
90
|
-
orNotIn(column:
|
|
90
|
+
orWhereNotIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
91
|
+
orNotIn<K extends keyof T>(column: K, values: T[K][]): this
|
|
91
92
|
|
|
92
|
-
whereBetween(column:
|
|
93
|
-
between(column:
|
|
93
|
+
whereBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
94
|
+
between<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
94
95
|
|
|
95
|
-
orWhereBetween(column:
|
|
96
|
-
orBetween(column:
|
|
96
|
+
orWhereBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
97
|
+
orBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
97
98
|
|
|
98
|
-
whereNotBetween(column:
|
|
99
|
-
notBetween(column:
|
|
99
|
+
whereNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
100
|
+
notBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
100
101
|
|
|
101
|
-
orWhereNotBetween(column:
|
|
102
|
-
orNotBetween(column:
|
|
102
|
+
orWhereNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
103
|
+
orNotBetween<K extends keyof T>(column: K, one: T[K], two: T[K]): this
|
|
103
104
|
|
|
104
|
-
whereNull(column:
|
|
105
|
-
onNull(column:
|
|
105
|
+
whereNull<K extends keyof T>(column: K): this
|
|
106
|
+
onNull<K extends keyof T>(column: K): this
|
|
106
107
|
|
|
107
|
-
orWhereNull(column:
|
|
108
|
-
orOnNull(column:
|
|
108
|
+
orWhereNull<K extends keyof T>(column: K): this
|
|
109
|
+
orOnNull<K extends keyof T>(column: K): this
|
|
109
110
|
|
|
110
|
-
whereNotNull(column:
|
|
111
|
-
onNotNull(column:
|
|
111
|
+
whereNotNull<K extends keyof T>(column: K): this
|
|
112
|
+
onNotNull<K extends keyof T>(column: K): this
|
|
112
113
|
|
|
113
|
-
orWhereNotNull(column:
|
|
114
|
-
orNotNull(column:
|
|
114
|
+
orWhereNotNull<K extends keyof T>(column: K): this
|
|
115
|
+
orNotNull<K extends keyof T>(column: K): this
|
|
115
116
|
}
|
|
116
117
|
|
|
117
118
|
export type JoinArgs<S, J extends keyof S> =
|
|
@@ -123,7 +124,6 @@ export type JoinArgs<S, J extends keyof S> =
|
|
|
123
124
|
| [keyof S[J], Operator, keyof S, keyof S[keyof S]]
|
|
124
125
|
|
|
125
126
|
export interface IJoinBuilder<S> {
|
|
126
|
-
join<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
127
127
|
join<
|
|
128
128
|
J extends keyof S,
|
|
129
129
|
T extends S[J],
|
|
@@ -148,9 +148,7 @@ export interface IJoinBuilder<S> {
|
|
|
148
148
|
J2 extends keyof S,
|
|
149
149
|
C2 extends keyof S[J2],
|
|
150
150
|
>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
|
|
151
|
-
join<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
|
|
152
151
|
|
|
153
|
-
innerJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
154
152
|
innerJoin<
|
|
155
153
|
J extends keyof S,
|
|
156
154
|
T extends S[J],
|
|
@@ -175,9 +173,7 @@ export interface IJoinBuilder<S> {
|
|
|
175
173
|
J2 extends keyof S,
|
|
176
174
|
C2 extends keyof S[J2],
|
|
177
175
|
>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
|
|
178
|
-
innerJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
|
|
179
176
|
|
|
180
|
-
leftJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
181
177
|
leftJoin<
|
|
182
178
|
J extends keyof S,
|
|
183
179
|
T extends S[J],
|
|
@@ -202,9 +198,7 @@ export interface IJoinBuilder<S> {
|
|
|
202
198
|
J2 extends keyof S,
|
|
203
199
|
C2 extends keyof S[J2],
|
|
204
200
|
>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
|
|
205
|
-
leftJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
|
|
206
201
|
|
|
207
|
-
rightJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
208
202
|
rightJoin<
|
|
209
203
|
J extends keyof S,
|
|
210
204
|
T extends S[J],
|
|
@@ -229,9 +223,7 @@ export interface IJoinBuilder<S> {
|
|
|
229
223
|
J2 extends keyof S,
|
|
230
224
|
C2 extends keyof S[J2],
|
|
231
225
|
>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
|
|
232
|
-
rightJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
|
|
233
226
|
|
|
234
|
-
crossJoin<J extends keyof S>(table: J, fn: WhereFn<S[J]>): this
|
|
235
227
|
crossJoin<
|
|
236
228
|
J extends keyof S,
|
|
237
229
|
T extends S[J],
|
|
@@ -256,5 +248,4 @@ export interface IJoinBuilder<S> {
|
|
|
256
248
|
J2 extends keyof S,
|
|
257
249
|
C2 extends keyof S[J2],
|
|
258
250
|
>(table: J, column: C, operator: Operator, table2: J2, column2: C2): this
|
|
259
|
-
crossJoin<J extends keyof S>(table: J, ...args: JoinArgs<S, J>): this
|
|
260
251
|
}
|