forj 0.1.6 → 0.1.8

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/src/model.ts CHANGED
@@ -4,6 +4,8 @@ import type {
4
4
  Operator, OrderDirection,
5
5
  WhereFn, WhereArgs,
6
6
  DBSchema, Pipe,
7
+ Value,
8
+ JoinArgs,
7
9
  } from './types'
8
10
 
9
11
  export default abstract class Model<TB extends keyof DB, DB> {
@@ -14,6 +16,10 @@ export default abstract class Model<TB extends keyof DB, DB> {
14
16
  static $table: string = ''
15
17
  static $schema?: DBSchema
16
18
 
19
+ static $timestamps?: boolean = false
20
+ static $createdAt?: boolean = false
21
+ static $updatedAt?: boolean = false
22
+
17
23
  static pipe<S, T>(): Pipe<S, T> {
18
24
  throw new Error(`Database connection not provided.`) // TODO: improv this message
19
25
  }
@@ -21,7 +27,35 @@ export default abstract class Model<TB extends keyof DB, DB> {
21
27
  static builder<S, T>() {
22
28
  const table = this.$table || pluralize(this.name.toLowerCase())
23
29
 
24
- return new QueryBuilder<S, T>(table, this.$schema, this.pipe<S, T>())
30
+ return new QueryBuilder<S, T>(table, this.$schema, this.pipe<S, T>()).opts({
31
+ timestamps: this.$timestamps,
32
+ createdAt: this.$createdAt,
33
+ updatedAt: this.$updatedAt,
34
+ })
35
+ }
36
+
37
+ static insert< // @ts-ignore
38
+ M extends typeof Model<TB, DB>,
39
+ I extends InstanceType<M>,
40
+ T extends I['$TShape'],
41
+ >(this: M, data: Record<keyof T, Value>) {
42
+ return this.builder<I['$DBShape'], T>().insert(data)
43
+ }
44
+
45
+ static update< // @ts-ignore
46
+ M extends typeof Model<TB, DB>,
47
+ I extends InstanceType<M>,
48
+ T extends I['$TShape'],
49
+ >(this: M, data: Record<keyof T, Value>) {
50
+ return this.builder<I['$DBShape'], T>().update(data)
51
+ }
52
+
53
+ static delete< // @ts-ignore
54
+ M extends typeof Model<TB, DB>,
55
+ I extends InstanceType<M>,
56
+ T extends I['$TShape'],
57
+ >(this: M) {
58
+ return this.builder<I['$DBShape'], T>().delete()
25
59
  }
26
60
 
27
61
  static select< // @ts-ignore
@@ -41,56 +75,278 @@ export default abstract class Model<TB extends keyof DB, DB> {
41
75
  return this.builder<I['$DBShape'], T>().distinct()
42
76
  }
43
77
 
44
- static where< // @ts-ignore
78
+ static join< // @ts-ignore
45
79
  M extends typeof Model<TB, DB>,
46
80
  I extends InstanceType<M>,
81
+ S extends I['$DBShape'],
47
82
  T extends I['$TShape'],
48
- C extends keyof T
49
- >(this: M, fn: WhereFn<T>): QueryBuilder<I['$DBShape'], T, C>
83
+ J extends keyof S,
84
+ >(this: M, table: J, fn: WhereFn<T>): QueryBuilder<S, T>
85
+ static join< // @ts-ignore
86
+ M extends typeof Model<TB, DB>,
87
+ I extends InstanceType<M>,
88
+ S extends I['$DBShape'],
89
+ T extends I['$TShape'],
90
+ J extends keyof S,
91
+ T1 extends S[J],
92
+ K extends keyof T1
93
+ >(table: J, column: K, value: T[K]): QueryBuilder<S, T>
94
+ static join< // @ts-ignore
95
+ M extends typeof Model<TB, DB>,
96
+ I extends InstanceType<M>,
97
+ S extends I['$DBShape'],
98
+ T extends I['$TShape'],
99
+ J extends keyof S,
100
+ T1 extends S[J],
101
+ K extends keyof T1
102
+ >(table: J, column: K, operator: Operator, value: T[K]): QueryBuilder<S, T>
103
+ static join< // @ts-ignore
104
+ M extends typeof Model<TB, DB>,
105
+ I extends InstanceType<M>,
106
+ S extends I['$DBShape'],
107
+ T extends I['$TShape'],
108
+ J extends keyof S,
109
+ T1 extends S[J],
110
+ K extends keyof T1,
111
+ J2 extends keyof S,
112
+ K2 extends keyof S[J2],
113
+ >(table: J, column: K, table2: J2, column2: K2): QueryBuilder<S, T>
114
+ static join< // @ts-ignore
115
+ M extends typeof Model<TB, DB>,
116
+ I extends InstanceType<M>,
117
+ S extends I['$DBShape'],
118
+ T extends I['$TShape'],
119
+ J extends keyof S,
120
+ T1 extends S[J],
121
+ K extends keyof T1,
122
+ J2 extends keyof S,
123
+ K2 extends keyof S[J2],
124
+ >(table: J, column: K, operator: Operator, table2: J2, column2: K2): QueryBuilder<S, T>
125
+ static join< // @ts-ignore
126
+ M extends typeof Model<TB, DB>,
127
+ I extends InstanceType<M>,
128
+ S extends I['$DBShape'],
129
+ J extends keyof S
130
+ >(this: M, table: J, ...args: JoinArgs<S, J>) { // @ts-ignore
131
+ return this.builder<S, I['$TShape']>().join(table, ...args)
132
+ }
133
+
134
+ static innerJoin< // @ts-ignore
135
+ M extends typeof Model<TB, DB>,
136
+ I extends InstanceType<M>,
137
+ S extends I['$DBShape'],
138
+ T extends I['$TShape'],
139
+ J extends keyof S,
140
+ >(this: M, table: J, fn: WhereFn<T>): QueryBuilder<S, T>
141
+ static innerJoin< // @ts-ignore
142
+ M extends typeof Model<TB, DB>,
143
+ I extends InstanceType<M>,
144
+ S extends I['$DBShape'],
145
+ T extends I['$TShape'],
146
+ J extends keyof S,
147
+ T1 extends S[J],
148
+ K extends keyof T1
149
+ >(table: J, column: K, value: T[K]): QueryBuilder<S, T>
150
+ static innerJoin< // @ts-ignore
151
+ M extends typeof Model<TB, DB>,
152
+ I extends InstanceType<M>,
153
+ S extends I['$DBShape'],
154
+ T extends I['$TShape'],
155
+ J extends keyof S,
156
+ T1 extends S[J],
157
+ K extends keyof T1
158
+ >(table: J, column: K, operator: Operator, value: T[K]): QueryBuilder<S, T>
159
+ static innerJoin< // @ts-ignore
160
+ M extends typeof Model<TB, DB>,
161
+ I extends InstanceType<M>,
162
+ S extends I['$DBShape'],
163
+ T extends I['$TShape'],
164
+ J extends keyof S,
165
+ T1 extends S[J],
166
+ K extends keyof T1,
167
+ J2 extends keyof S,
168
+ K2 extends keyof S[J2],
169
+ >(table: J, column: K, table2: J2, column2: K2): QueryBuilder<S, T>
170
+ static innerJoin< // @ts-ignore
171
+ M extends typeof Model<TB, DB>,
172
+ I extends InstanceType<M>,
173
+ S extends I['$DBShape'],
174
+ T extends I['$TShape'],
175
+ J extends keyof S,
176
+ T1 extends S[J],
177
+ K extends keyof T1,
178
+ J2 extends keyof S,
179
+ K2 extends keyof S[J2],
180
+ >(table: J, column: K, operator: Operator, table2: J2, column2: K2): QueryBuilder<S, T>
181
+ static innerJoin< // @ts-ignore
182
+ M extends typeof Model<TB, DB>,
183
+ I extends InstanceType<M>,
184
+ S extends I['$DBShape'],
185
+ J extends keyof S
186
+ >(this: M, table: J, ...args: JoinArgs<S, J>) { // @ts-ignore
187
+ return this.builder<S, I['$TShape']>().innerJoin(table, ...args)
188
+ }
189
+
190
+ static rightJoin< // @ts-ignore
191
+ M extends typeof Model<TB, DB>,
192
+ I extends InstanceType<M>,
193
+ S extends I['$DBShape'],
194
+ T extends I['$TShape'],
195
+ J extends keyof S,
196
+ >(this: M, table: J, fn: WhereFn<T>): QueryBuilder<S, T>
197
+ static rightJoin< // @ts-ignore
198
+ M extends typeof Model<TB, DB>,
199
+ I extends InstanceType<M>,
200
+ S extends I['$DBShape'],
201
+ T extends I['$TShape'],
202
+ J extends keyof S,
203
+ T1 extends S[J],
204
+ K extends keyof T1
205
+ >(table: J, column: K, value: T[K]): QueryBuilder<S, T>
206
+ static rightJoin< // @ts-ignore
207
+ M extends typeof Model<TB, DB>,
208
+ I extends InstanceType<M>,
209
+ S extends I['$DBShape'],
210
+ T extends I['$TShape'],
211
+ J extends keyof S,
212
+ T1 extends S[J],
213
+ K extends keyof T1
214
+ >(table: J, column: K, operator: Operator, value: T[K]): QueryBuilder<S, T>
215
+ static rightJoin< // @ts-ignore
216
+ M extends typeof Model<TB, DB>,
217
+ I extends InstanceType<M>,
218
+ S extends I['$DBShape'],
219
+ T extends I['$TShape'],
220
+ J extends keyof S,
221
+ T1 extends S[J],
222
+ K extends keyof T1,
223
+ J2 extends keyof S,
224
+ K2 extends keyof S[J2],
225
+ >(table: J, column: K, table2: J2, column2: K2): QueryBuilder<S, T>
226
+ static rightJoin< // @ts-ignore
227
+ M extends typeof Model<TB, DB>,
228
+ I extends InstanceType<M>,
229
+ S extends I['$DBShape'],
230
+ T extends I['$TShape'],
231
+ J extends keyof S,
232
+ T1 extends S[J],
233
+ K extends keyof T1,
234
+ J2 extends keyof S,
235
+ K2 extends keyof S[J2],
236
+ >(table: J, column: K, operator: Operator, table2: J2, column2: K2): QueryBuilder<S, T>
237
+ static rightJoin< // @ts-ignore
238
+ M extends typeof Model<TB, DB>,
239
+ I extends InstanceType<M>,
240
+ S extends I['$DBShape'],
241
+ J extends keyof S
242
+ >(this: M, table: J, ...args: JoinArgs<S, J>) { // @ts-ignore
243
+ return this.builder<S, I['$TShape']>().rightJoin(table, ...args)
244
+ }
245
+
246
+ static crossJoin< // @ts-ignore
247
+ M extends typeof Model<TB, DB>,
248
+ I extends InstanceType<M>,
249
+ S extends I['$DBShape'],
250
+ T extends I['$TShape'],
251
+ J extends keyof S,
252
+ >(this: M, table: J, fn: WhereFn<T>): QueryBuilder<S, T>
253
+ static crossJoin< // @ts-ignore
254
+ M extends typeof Model<TB, DB>,
255
+ I extends InstanceType<M>,
256
+ S extends I['$DBShape'],
257
+ T extends I['$TShape'],
258
+ J extends keyof S,
259
+ T1 extends S[J],
260
+ K extends keyof T1
261
+ >(table: J, column: K, value: T[K]): QueryBuilder<S, T>
262
+ static crossJoin< // @ts-ignore
263
+ M extends typeof Model<TB, DB>,
264
+ I extends InstanceType<M>,
265
+ S extends I['$DBShape'],
266
+ T extends I['$TShape'],
267
+ J extends keyof S,
268
+ T1 extends S[J],
269
+ K extends keyof T1
270
+ >(table: J, column: K, operator: Operator, value: T[K]): QueryBuilder<S, T>
271
+ static crossJoin< // @ts-ignore
272
+ M extends typeof Model<TB, DB>,
273
+ I extends InstanceType<M>,
274
+ S extends I['$DBShape'],
275
+ T extends I['$TShape'],
276
+ J extends keyof S,
277
+ T1 extends S[J],
278
+ K extends keyof T1,
279
+ J2 extends keyof S,
280
+ K2 extends keyof S[J2],
281
+ >(table: J, column: K, table2: J2, column2: K2): QueryBuilder<S, T>
282
+ static crossJoin< // @ts-ignore
283
+ M extends typeof Model<TB, DB>,
284
+ I extends InstanceType<M>,
285
+ S extends I['$DBShape'],
286
+ T extends I['$TShape'],
287
+ J extends keyof S,
288
+ T1 extends S[J],
289
+ K extends keyof T1,
290
+ J2 extends keyof S,
291
+ K2 extends keyof S[J2],
292
+ >(table: J, column: K, operator: Operator, table2: J2, column2: K2): QueryBuilder<S, T>
293
+ static crossJoin< // @ts-ignore
294
+ M extends typeof Model<TB, DB>,
295
+ I extends InstanceType<M>,
296
+ S extends I['$DBShape'],
297
+ J extends keyof S
298
+ >(this: M, table: J, ...args: JoinArgs<S, J>) { // @ts-ignore
299
+ return this.builder<S, I['$TShape']>().crossJoin(table, ...args)
300
+ }
301
+
302
+ static where< // @ts-ignore
303
+ M extends typeof Model<TB, DB>,
304
+ I extends InstanceType<M>,
305
+ T extends I['$TShape']
306
+ >(this: M, fn: WhereFn<T>): QueryBuilder<I['$DBShape'], T>
50
307
  static where< // @ts-ignore
51
308
  M extends typeof Model<TB, DB>,
52
309
  I extends InstanceType<M>,
53
310
  T extends I['$TShape'],
54
311
  C extends keyof T
55
- >(this: M, column: C, value: T[C]): QueryBuilder<I['$DBShape'], T, C>
312
+ >(this: M, column: C, value: T[C]): QueryBuilder<I['$DBShape'], T>
56
313
  static where< // @ts-ignore
57
314
  M extends typeof Model<TB, DB>,
58
315
  I extends InstanceType<M>,
59
316
  T extends I['$TShape'],
60
317
  C extends keyof T
61
- >(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I['$DBShape'], T, C>
318
+ >(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I['$DBShape'], T>
62
319
  static where< // @ts-ignore
63
320
  M extends typeof Model<TB, DB>,
64
321
  I extends InstanceType<M>,
65
322
  T extends I['$TShape']
66
- >(this: M, ...args: WhereArgs<T>) {
323
+ >(this: M, ...args: WhereArgs<T>) { // @ts-ignore
67
324
  return this.builder<I['$DBShape'], T>().where(...args)
68
325
  }
69
326
 
70
327
  static on< // @ts-ignore
71
328
  M extends typeof Model<TB, DB>,
72
329
  I extends InstanceType<M>,
73
- T extends I['$TShape'],
74
- C extends keyof T
75
- >(this: M, fn: WhereFn<T>): QueryBuilder<I['$DBShape'], T, C>
330
+ T extends I['$TShape']
331
+ >(this: M, fn: WhereFn<T>): QueryBuilder<I['$DBShape'], T>
76
332
  static on< // @ts-ignore
77
333
  M extends typeof Model<TB, DB>,
78
334
  I extends InstanceType<M>,
79
335
  T extends I['$TShape'],
80
336
  C extends keyof T
81
- >(this: M, column: C, value: T[C]): QueryBuilder<I['$DBShape'], T, C>
337
+ >(this: M, column: C, value: T[C]): QueryBuilder<I['$DBShape'], T>
82
338
  static on< // @ts-ignore
83
339
  M extends typeof Model<TB, DB>,
84
340
  I extends InstanceType<M>,
85
341
  T extends I['$TShape'],
86
342
  C extends keyof T
87
- >(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I['$DBShape'], T, C>
343
+ >(this: M, column: C, operator: Operator, value: T[C]): QueryBuilder<I['$DBShape'], T>
88
344
  static on< // @ts-ignore
89
345
  M extends typeof Model<TB, DB>,
90
346
  I extends InstanceType<M>,
91
347
  T extends I['$TShape']
92
- >(this: M, ...args: WhereArgs<T>) {
93
- return this.builder<I['$DBShape'], T>().where(...args)
348
+ >(this: M, ...args: WhereArgs<T>) { // @ts-ignore
349
+ return this.where(...args)
94
350
  }
95
351
 
96
352
  static whereIn< // @ts-ignore