@guren/orm 2.6.0 → 2.6.1
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/drizzle/mysql.d.ts +3 -4
- package/dist/drizzle/mysql.js +2 -5
- package/dist/drizzle/pg.d.ts +3 -2
- package/dist/drizzle/pg.js +2 -5
- package/dist/drizzle/sqlite.d.ts +3 -2
- package/dist/drizzle/sqlite.js +2 -5
- package/dist/drizzle.d.ts +6 -7
- package/dist/drizzle.js +19 -38
- package/dist/index.d.ts +1473 -1465
- package/dist/index.js +3634 -3767
- package/package.json +8 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { SQL } from
|
|
2
|
-
import { PostgresJsDatabase } from
|
|
3
|
-
import postgres from
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import { SQL } from "drizzle-orm";
|
|
2
|
+
import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
|
|
3
|
+
import postgres from "postgres";
|
|
4
|
+
import { AwsDataApiPgDatabase } from "drizzle-orm/aws-data-api/pg";
|
|
5
|
+
import { SQLiteBunDatabase } from "drizzle-orm/bun-sqlite";
|
|
6
|
+
import { MySql2Database } from "drizzle-orm/mysql2";
|
|
7
|
+
import { RDSDataClientConfig } from "@aws-sdk/client-rds-data";
|
|
8
|
+
//#region src/attributes.d.ts
|
|
10
9
|
/** Accessor function: transforms a record field value after reading from DB. */
|
|
11
10
|
type AccessorFn<T = unknown> = (record: PlainObject) => T;
|
|
12
11
|
/** Mutator function: transforms a field value before writing to DB. */
|
|
@@ -19,7 +18,8 @@ type MutatorDefinitions = Record<string, MutatorFn>;
|
|
|
19
18
|
declare function applyAccessors<T extends PlainObject>(record: T, accessors?: AccessorDefinitions): T;
|
|
20
19
|
/** Apply mutators to data before persistence, returning a new object. */
|
|
21
20
|
declare function applyMutators(data: PlainObject, mutators?: MutatorDefinitions): PlainObject;
|
|
22
|
-
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/QueryBuilder.d.ts
|
|
23
23
|
type FieldKey<TRecord extends PlainObject> = keyof TRecord & string;
|
|
24
24
|
/**
|
|
25
25
|
* Key for the prepared-payload update terminal.
|
|
@@ -32,16 +32,16 @@ declare const PREPARED_UPDATE: unique symbol;
|
|
|
32
32
|
type WhereOperator = '=' | '!=' | '>' | '<' | '>=' | '<=' | 'like' | 'in' | 'not in' | 'is null' | 'is not null';
|
|
33
33
|
/** A single field-level condition. */
|
|
34
34
|
interface SimpleCondition {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
type: 'simple';
|
|
36
|
+
field: string;
|
|
37
|
+
operator: WhereOperator;
|
|
38
|
+
value: unknown;
|
|
39
39
|
}
|
|
40
40
|
/** A group of conditions joined by AND or OR. */
|
|
41
41
|
interface GroupCondition {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
type: 'group';
|
|
43
|
+
boolean: 'and' | 'or';
|
|
44
|
+
conditions: WhereCondition[];
|
|
45
45
|
}
|
|
46
46
|
/** A where condition - either simple or grouped. */
|
|
47
47
|
type WhereCondition = SimpleCondition | GroupCondition;
|
|
@@ -56,14 +56,14 @@ type WhereCondition = SimpleCondition | GroupCondition;
|
|
|
56
56
|
type WhereGroupCallback<TRecord extends PlainObject = PlainObject> = (query: QueryBuilder<TRecord>) => void;
|
|
57
57
|
/** Options carried by the QueryBuilder for query execution. */
|
|
58
58
|
interface QueryBuilderOptions {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
59
|
+
orderBy: Array<{
|
|
60
|
+
column: string;
|
|
61
|
+
direction: OrderDirection;
|
|
62
|
+
}>;
|
|
63
|
+
limitValue?: number;
|
|
64
|
+
offsetValue?: number;
|
|
65
|
+
selectFields?: readonly string[];
|
|
66
|
+
trx?: unknown;
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* A callback that further constrains the query used to fetch an eager-loaded
|
|
@@ -100,280 +100,281 @@ type EagerLoadConstraints = ReadonlyMap<string, EagerLoadConstraint>;
|
|
|
100
100
|
* const page = await Post.where('status', 'published').paginate(1, 20)
|
|
101
101
|
*/
|
|
102
102
|
declare class QueryBuilder<TRecord extends PlainObject = PlainObject, TResult extends PlainObject = TRecord> {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
103
|
+
private conditions;
|
|
104
|
+
private options;
|
|
105
|
+
private modelClass;
|
|
106
|
+
private table;
|
|
107
|
+
private adapter;
|
|
108
|
+
private eagerLoad;
|
|
109
|
+
private eagerLoadConstraints;
|
|
110
|
+
constructor(modelClass: typeof Model, options?: {
|
|
111
|
+
trx?: unknown;
|
|
112
|
+
});
|
|
113
|
+
/**
|
|
114
|
+
* Add a where condition (AND).
|
|
115
|
+
*
|
|
116
|
+
* Supports four calling signatures:
|
|
117
|
+
* - `where(field, value)` - equality check
|
|
118
|
+
* - `where(field, operator, value)` - comparison
|
|
119
|
+
* - `where(object)` - multiple equality conditions
|
|
120
|
+
* - `where(callback)` - parenthesized condition group, AND-ed with the rest
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* // (title LIKE ? OR excerpt LIKE ?) AND published = true
|
|
124
|
+
* await Post.where((q) => {
|
|
125
|
+
* q.where('title', 'like', pattern).orWhere('excerpt', 'like', pattern)
|
|
126
|
+
* }).where('published', true)
|
|
127
|
+
*/
|
|
128
|
+
where(callback: WhereGroupCallback<TRecord>): this;
|
|
129
|
+
where<TKey extends FieldKey<TRecord>>(field: TKey, value: TRecord[TKey]): this;
|
|
130
|
+
where<TKey extends FieldKey<TRecord>>(field: TKey, operator: WhereOperator, value: unknown): this;
|
|
131
|
+
where(conditions: Partial<Record<FieldKey<TRecord>, unknown>>): this;
|
|
132
|
+
/**
|
|
133
|
+
* Add an OR where condition.
|
|
134
|
+
*
|
|
135
|
+
* Same overloads as `where()`, but joins with OR logic.
|
|
136
|
+
* Creates an OR group containing the new condition(s).
|
|
137
|
+
* With a callback, the group's conditions are parenthesized as a whole:
|
|
138
|
+
* `(preceding conditions) OR (callback group)`.
|
|
139
|
+
*/
|
|
140
|
+
orWhere(callback: WhereGroupCallback<TRecord>): this;
|
|
141
|
+
orWhere<TKey extends FieldKey<TRecord>>(field: TKey, value: TRecord[TKey]): this;
|
|
142
|
+
orWhere<TKey extends FieldKey<TRecord>>(field: TKey, operator: WhereOperator, value: unknown): this;
|
|
143
|
+
orWhere(conditions: Partial<Record<FieldKey<TRecord>, unknown>>): this;
|
|
144
|
+
/**
|
|
145
|
+
* Add a WHERE NULL condition.
|
|
146
|
+
* @param field - Column to check for NULL
|
|
147
|
+
*/
|
|
148
|
+
whereNull(field: FieldKey<TRecord>): this;
|
|
149
|
+
/**
|
|
150
|
+
* Add a WHERE NOT NULL condition.
|
|
151
|
+
* @param field - Column to check for NOT NULL
|
|
152
|
+
*/
|
|
153
|
+
whereNotNull(field: FieldKey<TRecord>): this;
|
|
154
|
+
/**
|
|
155
|
+
* Add a WHERE IN condition.
|
|
156
|
+
* @param field - Column to check
|
|
157
|
+
* @param values - Array of values to match against
|
|
158
|
+
*/
|
|
159
|
+
whereIn<TKey extends FieldKey<TRecord>>(field: TKey, values: readonly TRecord[TKey][]): this;
|
|
160
|
+
/**
|
|
161
|
+
* Add a WHERE NOT IN condition.
|
|
162
|
+
* @param field - Column to check
|
|
163
|
+
* @param values - Array of values to exclude
|
|
164
|
+
*/
|
|
165
|
+
whereNotIn<TKey extends FieldKey<TRecord>>(field: TKey, values: readonly TRecord[TKey][]): this;
|
|
166
|
+
/**
|
|
167
|
+
* Add an ORDER BY clause. Can be called multiple times to sort by multiple columns.
|
|
168
|
+
* @param field - Column to sort by
|
|
169
|
+
* @param direction - Sort direction (default: 'asc')
|
|
170
|
+
*/
|
|
171
|
+
orderBy(field: FieldKey<TRecord>, direction?: OrderDirection): this;
|
|
172
|
+
/**
|
|
173
|
+
* Set the maximum number of records to return.
|
|
174
|
+
* @param n - Maximum record count
|
|
175
|
+
*/
|
|
176
|
+
limit(n: number): this;
|
|
177
|
+
/**
|
|
178
|
+
* Set the number of records to skip.
|
|
179
|
+
* @param n - Number of records to skip
|
|
180
|
+
*/
|
|
181
|
+
offset(n: number): this;
|
|
182
|
+
/**
|
|
183
|
+
* Limit the columns returned in the result.
|
|
184
|
+
* @param fields - Column names to select
|
|
185
|
+
*/
|
|
186
|
+
select<TKey extends FieldKey<TRecord>>(...fields: readonly TKey[]): QueryBuilder<TRecord, Pick<TRecord, TKey>>;
|
|
187
|
+
/**
|
|
188
|
+
* Apply a named query scope defined on the model.
|
|
189
|
+
*
|
|
190
|
+
* @param name - The scope name
|
|
191
|
+
* @returns this (for chaining)
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* const results = await Post.where('author', 'John')
|
|
195
|
+
* .scope('published')
|
|
196
|
+
* .scope('popular')
|
|
197
|
+
* .get()
|
|
198
|
+
*/
|
|
199
|
+
scope(name: string): this;
|
|
200
|
+
/**
|
|
201
|
+
* Eager-load relationships on query results.
|
|
202
|
+
*
|
|
203
|
+
* Supports string names, arrays, dot notation for nested relations,
|
|
204
|
+
* and constraint callbacks.
|
|
205
|
+
*
|
|
206
|
+
* A constraint callback receives the query builder used to fetch that
|
|
207
|
+
* relation, and runs after the foreign-key filter is already on it, so a
|
|
208
|
+
* `where()` narrows that filter. Each object key constrains exactly the
|
|
209
|
+
* level it names, in any order: `posts` constrains the head,
|
|
210
|
+
* `posts.comments` constrains the leaf and leaves `posts` unfiltered.
|
|
211
|
+
*
|
|
212
|
+
* Three things behave differently than they may look:
|
|
213
|
+
* - `orWhere()` at the top level of a callback *widens* the query rather
|
|
214
|
+
* than narrowing it, because it ORs against the foreign-key filter. Group
|
|
215
|
+
* it (`q.where((g) => g.where(...).orWhere(...))`) to keep it contained.
|
|
216
|
+
* - `select()` must include the column the relation is keyed on, or the
|
|
217
|
+
* loader cannot match rows back to their parent and the relation is empty.
|
|
218
|
+
* - `limit()` applies to the single batched query, not per parent record.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* // Simple
|
|
222
|
+
* await User.where('active', true).with('posts').get()
|
|
223
|
+
*
|
|
224
|
+
* // Multiple
|
|
225
|
+
* await User.where('active', true).with('posts', 'comments').get()
|
|
226
|
+
*
|
|
227
|
+
* // Nested (dot notation)
|
|
228
|
+
* await User.where('active', true).with('posts.comments').get()
|
|
229
|
+
*
|
|
230
|
+
* // Constrained
|
|
231
|
+
* await User.newQuery().with({ posts: (q) => q.where('published', true) }).get()
|
|
232
|
+
*
|
|
233
|
+
* // Constrained at both levels
|
|
234
|
+
* await User.newQuery()
|
|
235
|
+
* .with({ posts: (q) => q.where('published', true), 'posts.comments': (q) => q.orderBy('id') })
|
|
236
|
+
* .get()
|
|
237
|
+
*/
|
|
238
|
+
with(...relations: (string | Record<string, EagerLoadConstraint>)[]): this;
|
|
239
|
+
/**
|
|
240
|
+
* Execute the query and return all matching records.
|
|
241
|
+
* @returns Array of matching records
|
|
242
|
+
*/
|
|
243
|
+
get(): Promise<TResult[]>;
|
|
244
|
+
/**
|
|
245
|
+
* Execute the query and return the first matching record.
|
|
246
|
+
* @returns The first record or null
|
|
247
|
+
*/
|
|
248
|
+
first(): Promise<TResult | null>;
|
|
249
|
+
/**
|
|
250
|
+
* Execute the query and return the first matching record, or throw.
|
|
251
|
+
* @returns The first record
|
|
252
|
+
* @throws ModelNotFoundException (404) if no record matches
|
|
253
|
+
*/
|
|
254
|
+
firstOrFail(): Promise<TResult>;
|
|
255
|
+
/**
|
|
256
|
+
* Count the number of records matching the current conditions.
|
|
257
|
+
* @returns The count of matching records
|
|
258
|
+
*/
|
|
259
|
+
count(): Promise<number>;
|
|
260
|
+
/**
|
|
261
|
+
* Paginate the query results.
|
|
262
|
+
*
|
|
263
|
+
* Accepts either positional arguments or the same options object shape as
|
|
264
|
+
* `Model.paginate()` so the two APIs stay interchangeable:
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* await Post.where('published', true).paginate(2, 10)
|
|
268
|
+
* await Post.where('published', true).paginate({ page: 2, perPage: 10 })
|
|
269
|
+
*/
|
|
270
|
+
paginate(page?: number, perPage?: number): Promise<PaginatedResult<TResult>>;
|
|
271
|
+
paginate(options: {
|
|
272
|
+
page?: number;
|
|
273
|
+
perPage?: number;
|
|
274
|
+
}): Promise<PaginatedResult<TResult>>;
|
|
275
|
+
/**
|
|
276
|
+
* Bulk update records matching the current conditions.
|
|
277
|
+
*
|
|
278
|
+
* Mass-assignment protection applies exactly as in `Model.update()`:
|
|
279
|
+
* with a `fillable` allowlist, out-of-allowlist keys throw a
|
|
280
|
+
* MassAssignmentException. The payload also runs through the same
|
|
281
|
+
* persistence preparation as `Model.update()` — mutators, casts, and
|
|
282
|
+
* `preparePersistencePayload` overrides (e.g. password hashing on
|
|
283
|
+
* authenticatable models). Per-record hooks and observers are still
|
|
284
|
+
* skipped by design.
|
|
285
|
+
*
|
|
286
|
+
* @param data - Data to set on matching records
|
|
287
|
+
* @returns The updated record (adapter-dependent)
|
|
288
|
+
*/
|
|
289
|
+
update(data: PlainObject): Promise<TRecord>;
|
|
290
|
+
/**
|
|
291
|
+
* Bulk update bypassing mass-assignment protection. Use for trusted,
|
|
292
|
+
* server-side-assembled data — never for raw request input.
|
|
293
|
+
*/
|
|
294
|
+
forceUpdate(data: PlainObject): Promise<TRecord>;
|
|
295
|
+
private runBulkUpdate;
|
|
296
|
+
/**
|
|
297
|
+
* Bulk update using a payload the caller has already run through
|
|
298
|
+
* mass-assignment filtering and persistence preparation. This exists so the
|
|
299
|
+
* static `Model.update()` write path can borrow the builder's
|
|
300
|
+
* global-scope-carrying `conditions` without re-preparing the payload —
|
|
301
|
+
* running mutators twice would, for example, double-hash a hashed column.
|
|
302
|
+
*
|
|
303
|
+
* Keyed by a symbol that this package never re-exports, because a named
|
|
304
|
+
* public method here would be a supported way to write arbitrary columns:
|
|
305
|
+
* it skips both `filterFillable` and `prepareBulkPersistencePayload`, so
|
|
306
|
+
* `User.where(…)[name](requestBody)` would bypass mass-assignment protection
|
|
307
|
+
* entirely. `@internal` is only a doc tag — declarations still emit it.
|
|
308
|
+
*/
|
|
309
|
+
[PREPARED_UPDATE](payload: PlainObject): Promise<TRecord>;
|
|
310
|
+
/**
|
|
311
|
+
* Bulk delete records matching the current conditions.
|
|
312
|
+
* @returns Number of deleted records (adapter-dependent)
|
|
313
|
+
*/
|
|
314
|
+
delete(): Promise<number | PlainObject | void>;
|
|
315
|
+
/**
|
|
316
|
+
* Makes QueryBuilder a thenable so it can be directly awaited.
|
|
317
|
+
* Resolves to the result of `get()`.
|
|
318
|
+
*/
|
|
319
|
+
then<TResult1 = TResult[], TResult2 = never>(onfulfilled?: ((value: TResult[]) => TResult1 | PromiseLike<TResult1>) | null, onrejected?: ((reason: unknown) => TResult2 | PromiseLike<TResult2>) | null): Promise<TResult1 | TResult2>;
|
|
320
|
+
/**
|
|
321
|
+
* Catch handler for the thenable interface.
|
|
322
|
+
*/
|
|
323
|
+
catch<TCatch = never>(onrejected?: ((reason: unknown) => TCatch | PromiseLike<TCatch>) | null): Promise<TResult[] | TCatch>;
|
|
324
|
+
/** Get the internal conditions (used by adapters). */
|
|
325
|
+
getConditions(): WhereCondition[];
|
|
326
|
+
/** Get the internal query options (used by adapters). */
|
|
327
|
+
getOptions(): QueryBuilderOptions;
|
|
328
|
+
/**
|
|
329
|
+
* Run a grouping callback against a nested builder, fold the collected
|
|
330
|
+
* conditions into a single node, and push it joined by `boolean`.
|
|
331
|
+
*
|
|
332
|
+
* The nested builder intentionally skips global scopes (`defaultScope`,
|
|
333
|
+
* SoftDeletes): the outer builder already carries them, and re-applying
|
|
334
|
+
* them inside every group would duplicate — or, inside an OR group,
|
|
335
|
+
* weaken — their filters.
|
|
336
|
+
*/
|
|
337
|
+
private pushCallbackGroup;
|
|
338
|
+
private addSimpleCondition;
|
|
339
|
+
private executeQuery;
|
|
340
|
+
/**
|
|
341
|
+
* Attempt to convert the current conditions to a simple WhereClause
|
|
342
|
+
* for backward compatibility with basic adapters.
|
|
343
|
+
* Returns null if conditions are too complex.
|
|
344
|
+
*/
|
|
345
|
+
private toSimpleWhereClause;
|
|
346
|
+
/**
|
|
347
|
+
* Load eager relations onto fetched results.
|
|
348
|
+
* Supports dot notation for nested relations at any depth
|
|
349
|
+
* (e.g., 'posts.comments.author') — the paths are delegated to
|
|
350
|
+
* Model.loadRelationsInto, which recurses through the relation chain.
|
|
351
|
+
* Constraint callbacks registered by the object form of `with()` travel
|
|
352
|
+
* alongside, keyed by the path segment each one constrains.
|
|
353
|
+
*
|
|
354
|
+
* The builder's `trx` goes with it: a relation loaded for rows read inside
|
|
355
|
+
* a transaction has to be read on that same handle, or the parents come
|
|
356
|
+
* from the transaction and their children from the pool — which on
|
|
357
|
+
* Postgres/MySQL means uncommitted parents get a null relation back.
|
|
358
|
+
*/
|
|
359
|
+
private readonly loadEagerRelations;
|
|
360
360
|
}
|
|
361
361
|
/**
|
|
362
362
|
* Extended adapter interface for advanced query builder features.
|
|
363
363
|
* Adapters can optionally implement these methods for full QueryBuilder support.
|
|
364
364
|
*/
|
|
365
365
|
interface ORMAdapterAdvanced extends ORMAdapter {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
366
|
+
findManyAdvanced?<TRecord extends PlainObject = PlainObject>(table: unknown, conditions: WhereCondition[], options: {
|
|
367
|
+
orderBy?: OrderByClause;
|
|
368
|
+
limit?: number;
|
|
369
|
+
offset?: number;
|
|
370
|
+
select?: readonly string[];
|
|
371
|
+
}, queryOptions?: AdapterQueryOptions): Promise<TRecord[]>;
|
|
372
|
+
countAdvanced?<TRecord extends PlainObject = PlainObject>(table: unknown, conditions: WhereCondition[], queryOptions?: AdapterQueryOptions): Promise<number>;
|
|
373
|
+
updateAdvanced?<TRecord extends PlainObject = PlainObject>(table: unknown, conditions: WhereCondition[], data: PlainObject, writeOptions?: AdapterQueryOptions): Promise<TRecord>;
|
|
374
|
+
deleteAdvanced?<TRecord extends PlainObject = PlainObject>(table: unknown, conditions: WhereCondition[], writeOptions?: AdapterQueryOptions): Promise<number | PlainObject | void>;
|
|
375
375
|
}
|
|
376
|
-
|
|
376
|
+
//#endregion
|
|
377
|
+
//#region src/GlobalScopeRegistry.d.ts
|
|
377
378
|
/** A named global scope function. */
|
|
378
379
|
type ScopeFunction = (q: QueryBuilder<any>) => QueryBuilder<any>;
|
|
379
380
|
/**
|
|
@@ -383,26 +384,27 @@ type ScopeFunction = (q: QueryBuilder<any>) => QueryBuilder<any>;
|
|
|
383
384
|
* Used internally by Model to manage global query constraints.
|
|
384
385
|
*/
|
|
385
386
|
declare class GlobalScopeRegistry {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
387
|
+
private scopes;
|
|
388
|
+
add(name: string, fn: ScopeFunction): void;
|
|
389
|
+
remove(name: string): void;
|
|
390
|
+
has(name: string): boolean;
|
|
391
|
+
names(): string[];
|
|
392
|
+
/** Apply all scopes (or all except excluded) to a query builder. */
|
|
393
|
+
apply<T extends PlainObject>(builder: QueryBuilder<T>, except?: string[]): QueryBuilder<T>;
|
|
394
|
+
clear(): void;
|
|
395
|
+
/**
|
|
396
|
+
* Copy this registry's entries into a new one.
|
|
397
|
+
*
|
|
398
|
+
* A subclass that registers its own scope needs a registry of its own, but it
|
|
399
|
+
* must keep the ones it inherited: starting from empty is how a model that
|
|
400
|
+
* mixes in `SoftDeletes` and then adds a `tenant` scope silently loses the
|
|
401
|
+
* `softDelete` filter.
|
|
402
|
+
*/
|
|
403
|
+
clone(): GlobalScopeRegistry;
|
|
404
|
+
get size(): number;
|
|
404
405
|
}
|
|
405
|
-
|
|
406
|
+
//#endregion
|
|
407
|
+
//#region src/hooks.d.ts
|
|
406
408
|
/**
|
|
407
409
|
* Model lifecycle hook names.
|
|
408
410
|
*
|
|
@@ -426,9 +428,7 @@ type HookCallback<T = Record<string, unknown>> = (data: T) => void | Promise<voi
|
|
|
426
428
|
* Map of hook names to their callbacks.
|
|
427
429
|
* All hooks are optional.
|
|
428
430
|
*/
|
|
429
|
-
type ModelHooks = {
|
|
430
|
-
[K in HookName]?: HookCallback;
|
|
431
|
-
};
|
|
431
|
+
type ModelHooks = { [K in HookName]?: HookCallback; };
|
|
432
432
|
/**
|
|
433
433
|
* Execute a model hook if it exists.
|
|
434
434
|
*
|
|
@@ -438,7 +438,8 @@ type ModelHooks = {
|
|
|
438
438
|
* @returns `false` if the hook explicitly returned false (operation should be aborted), otherwise `true`
|
|
439
439
|
*/
|
|
440
440
|
declare function executeHook(hooks: ModelHooks | undefined, name: HookName, data: Record<string, unknown>): Promise<boolean>;
|
|
441
|
-
|
|
441
|
+
//#endregion
|
|
442
|
+
//#region src/ModelObserver.d.ts
|
|
442
443
|
/**
|
|
443
444
|
* Base interface for model observers.
|
|
444
445
|
*
|
|
@@ -454,14 +455,14 @@ declare function executeHook(hooks: ModelHooks | undefined, name: HookName, data
|
|
|
454
455
|
* User.observe(UserObserver)
|
|
455
456
|
*/
|
|
456
457
|
interface ModelObserver {
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
458
|
+
creating?(data: PlainObject): void | Promise<void> | false | Promise<false>;
|
|
459
|
+
created?(data: PlainObject): void | Promise<void>;
|
|
460
|
+
updating?(data: PlainObject): void | Promise<void> | false | Promise<false>;
|
|
461
|
+
updated?(data: PlainObject): void | Promise<void>;
|
|
462
|
+
deleting?(data: PlainObject): void | Promise<void> | false | Promise<false>;
|
|
463
|
+
deleted?(data: PlainObject): void | Promise<void>;
|
|
464
|
+
saving?(data: PlainObject): void | Promise<void> | false | Promise<false>;
|
|
465
|
+
saved?(data: PlainObject): void | Promise<void>;
|
|
465
466
|
}
|
|
466
467
|
type ModelObserverConstructor = new () => ModelObserver;
|
|
467
468
|
/**
|
|
@@ -469,19 +470,20 @@ type ModelObserverConstructor = new () => ModelObserver;
|
|
|
469
470
|
* Returns false if any observer aborts the operation.
|
|
470
471
|
*/
|
|
471
472
|
declare function executeObservers(observers: ModelObserver[] | undefined, name: HookName, data: PlainObject): Promise<boolean>;
|
|
472
|
-
|
|
473
|
+
//#endregion
|
|
474
|
+
//#region src/Model.d.ts
|
|
473
475
|
/** Generic plain object type used throughout the ORM. */
|
|
474
476
|
type PlainObject = Record<string, unknown>;
|
|
475
477
|
type RelationShape = Record<string, unknown>;
|
|
476
478
|
type TableShape<TSelect extends PlainObject = PlainObject, TInsert extends PlainObject = PlainObject> = {
|
|
477
|
-
|
|
478
|
-
|
|
479
|
+
$inferSelect: TSelect;
|
|
480
|
+
$inferInsert?: TInsert;
|
|
479
481
|
};
|
|
480
482
|
type InferModelRecord<TTable> = TTable extends {
|
|
481
|
-
|
|
483
|
+
$inferSelect: infer TRecord;
|
|
482
484
|
} ? TRecord extends PlainObject ? TRecord : PlainObject : PlainObject;
|
|
483
485
|
type InferModelInsert<TTable> = TTable extends {
|
|
484
|
-
|
|
486
|
+
$inferInsert: infer TInsert;
|
|
485
487
|
} ? TInsert extends PlainObject ? TInsert : PlainObject : PlainObject;
|
|
486
488
|
/** Supported cast types for attribute casting. */
|
|
487
489
|
type CastType = 'json' | 'date' | 'boolean' | 'number' | 'string';
|
|
@@ -504,15 +506,13 @@ type WhereValue<Value> = Value | readonly Value[] | null;
|
|
|
504
506
|
* // Multiple conditions (AND)
|
|
505
507
|
* { status: 'active', role: 'admin' }
|
|
506
508
|
*/
|
|
507
|
-
type WhereClause<TRecord extends PlainObject = PlainObject> = Partial<{
|
|
508
|
-
[K in keyof TRecord & string]?: WhereValue<TRecord[K]>;
|
|
509
|
-
}>;
|
|
509
|
+
type WhereClause<TRecord extends PlainObject = PlainObject> = Partial<{ [K in keyof TRecord & string]?: WhereValue<TRecord[K]>; }>;
|
|
510
510
|
/** Sort direction for ordering queries. */
|
|
511
511
|
type OrderDirection = 'asc' | 'desc';
|
|
512
512
|
/** Normalized order definition with column and direction. */
|
|
513
513
|
type OrderDefinition<TRecord extends PlainObject = PlainObject> = {
|
|
514
|
-
|
|
515
|
-
|
|
514
|
+
column: keyof TRecord & string;
|
|
515
|
+
direction: OrderDirection;
|
|
516
516
|
};
|
|
517
517
|
/**
|
|
518
518
|
* Flexible order expression input.
|
|
@@ -522,8 +522,8 @@ type OrderDefinition<TRecord extends PlainObject = PlainObject> = {
|
|
|
522
522
|
* { column: 'createdAt', direction: 'desc' } // Object form
|
|
523
523
|
*/
|
|
524
524
|
type OrderExpression<TRecord extends PlainObject = PlainObject> = (keyof TRecord & string) | readonly [keyof TRecord & string, OrderDirection] | {
|
|
525
|
-
|
|
526
|
-
|
|
525
|
+
column: keyof TRecord & string;
|
|
526
|
+
direction?: OrderDirection;
|
|
527
527
|
};
|
|
528
528
|
/** Input for orderBy - single expression or array of expressions. */
|
|
529
529
|
type OrderByInput<TRecord extends PlainObject = PlainObject> = OrderExpression<TRecord> | readonly OrderExpression<TRecord>[];
|
|
@@ -531,80 +531,80 @@ type OrderByInput<TRecord extends PlainObject = PlainObject> = OrderExpression<T
|
|
|
531
531
|
type OrderByClause<TRecord extends PlainObject = PlainObject> = readonly OrderDefinition<TRecord>[];
|
|
532
532
|
/** Options for findMany queries. */
|
|
533
533
|
interface FindManyOptions<TRecord extends PlainObject = PlainObject> {
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
534
|
+
/** Filter conditions */
|
|
535
|
+
where?: WhereClause<TRecord>;
|
|
536
|
+
/** Sort order */
|
|
537
|
+
orderBy?: OrderByClause<TRecord>;
|
|
538
|
+
/** Maximum number of records to return */
|
|
539
|
+
limit?: number;
|
|
540
|
+
/** Number of records to skip */
|
|
541
|
+
offset?: number;
|
|
542
542
|
}
|
|
543
543
|
/** Options for paginated queries. */
|
|
544
544
|
interface PaginateOptions<TRecord extends PlainObject = PlainObject> {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
545
|
+
/** Page number (1-based, default: 1) */
|
|
546
|
+
page?: number;
|
|
547
|
+
/** Records per page (default: 15) */
|
|
548
|
+
perPage?: number;
|
|
549
|
+
/** Filter conditions */
|
|
550
|
+
where?: WhereClause<TRecord>;
|
|
551
|
+
/** Sort order */
|
|
552
|
+
orderBy?: OrderByInput<TRecord>;
|
|
553
553
|
}
|
|
554
554
|
/** Pagination metadata returned with paginated results. */
|
|
555
555
|
interface ModelPaginationMeta {
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
556
|
+
/** Total number of records matching the query */
|
|
557
|
+
total: number;
|
|
558
|
+
/** Number of records per page */
|
|
559
|
+
perPage: number;
|
|
560
|
+
/** Current page number (1-based) */
|
|
561
|
+
currentPage: number;
|
|
562
|
+
/** Total number of pages */
|
|
563
|
+
totalPages: number;
|
|
564
|
+
/** Whether there are more pages after this one */
|
|
565
|
+
hasMore: boolean;
|
|
566
|
+
/** Index of first record on this page (1-based) */
|
|
567
|
+
from: number;
|
|
568
|
+
/** Index of last record on this page */
|
|
569
|
+
to: number;
|
|
570
570
|
}
|
|
571
571
|
/** Result of a paginated query. */
|
|
572
572
|
interface PaginatedResult<TRecord extends PlainObject = PlainObject> {
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
573
|
+
/** Records for the current page */
|
|
574
|
+
data: TRecord[];
|
|
575
|
+
/** Pagination metadata */
|
|
576
|
+
meta: ModelPaginationMeta;
|
|
577
577
|
}
|
|
578
578
|
/**
|
|
579
579
|
* Interface for ORM adapters that power the Model class.
|
|
580
580
|
* The default adapter is DrizzleAdapter.
|
|
581
581
|
*/
|
|
582
582
|
interface ORMAdapter {
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
583
|
+
/** Run operations in a database transaction. */
|
|
584
|
+
transaction?<TResult>(callback: (trx: unknown) => Promise<TResult>): Promise<TResult>;
|
|
585
|
+
/** Find multiple records with optional filtering, ordering, and pagination. */
|
|
586
|
+
findMany<TRecord extends PlainObject = PlainObject>(table: unknown, options?: FindManyOptions<TRecord>, queryOptions?: AdapterQueryOptions): Promise<TRecord[]>;
|
|
587
|
+
/** Find a single record by unique criteria. */
|
|
588
|
+
findUnique<TRecord extends PlainObject = PlainObject>(table: unknown, where: WhereClause<TRecord>, queryOptions?: AdapterQueryOptions): Promise<TRecord | null>;
|
|
589
|
+
/** Create a new record. */
|
|
590
|
+
create<TRecord extends PlainObject = PlainObject>(table: unknown, data: PlainObject, writeOptions?: AdapterQueryOptions): Promise<TRecord>;
|
|
591
|
+
/** Update records matching criteria. */
|
|
592
|
+
update?<TRecord extends PlainObject = PlainObject>(table: unknown, where: WhereClause<TRecord>, data: PlainObject, writeOptions?: AdapterQueryOptions): Promise<TRecord>;
|
|
593
|
+
/** Delete records matching criteria. */
|
|
594
|
+
delete?<TRecord extends PlainObject = PlainObject>(table: unknown, where: WhereClause<TRecord>, writeOptions?: AdapterQueryOptions): Promise<number | PlainObject | void>;
|
|
595
|
+
/** Count records matching criteria. */
|
|
596
|
+
count?<TRecord extends PlainObject = PlainObject>(table: unknown, where?: WhereClause<TRecord>, queryOptions?: AdapterQueryOptions): Promise<number>;
|
|
597
597
|
}
|
|
598
598
|
interface AdapterQueryOptions {
|
|
599
|
-
|
|
599
|
+
trx?: unknown;
|
|
600
600
|
}
|
|
601
601
|
type ModelWriteOptions = AdapterQueryOptions;
|
|
602
602
|
type ModelQueryOptions = AdapterQueryOptions;
|
|
603
603
|
type TransactionHandle = NonNullable<AdapterQueryOptions['trx']>;
|
|
604
604
|
type SelectFrom<TDatabase> = TDatabase extends {
|
|
605
|
-
|
|
605
|
+
select: (...args: any[]) => infer TSelect;
|
|
606
606
|
} ? TSelect extends {
|
|
607
|
-
|
|
607
|
+
from: (...args: any[]) => infer TResult;
|
|
608
608
|
} ? TResult : never : never;
|
|
609
609
|
/**
|
|
610
610
|
* ActiveRecord-style base class for database models.
|
|
@@ -639,849 +639,846 @@ type SelectFrom<TDatabase> = TDatabase extends {
|
|
|
639
639
|
* const usersWithPosts = await User.with('posts')
|
|
640
640
|
*/
|
|
641
641
|
declare abstract class Model<TRecord extends PlainObject = PlainObject> {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
642
|
+
/** The ORM adapter used for database operations. */
|
|
643
|
+
protected static ormAdapter: ORMAdapter;
|
|
644
|
+
/** The database table (e.g., Drizzle table schema). */
|
|
645
|
+
protected static table: unknown;
|
|
646
|
+
/** Type marker for TypeScript inference. Set by `defineModel()`; when extending `Model` directly, redeclare as `declare static readonly recordType: YourRecordType`. */
|
|
647
|
+
static readonly recordType: unknown;
|
|
648
|
+
/** Type marker for insert/update payload inference. Set by `defineModel()`; when extending `Model` directly, redeclare as `declare static readonly createType: YourCreateType`. */
|
|
649
|
+
static readonly createType: unknown;
|
|
650
|
+
protected static relationDefinitions?: Map<string, RelationDefinition>;
|
|
651
|
+
/** Type marker for relation types. Define relation types here for type inference. */
|
|
652
|
+
static relationTypes: RelationShape;
|
|
653
|
+
/**
|
|
654
|
+
* Named query scopes for reusable query constraints.
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* class Post extends Model<PostRecord> {
|
|
658
|
+
* static scopes = {
|
|
659
|
+
* published: (q: QueryBuilder<PostRecord>) => q.where('status', 'published'),
|
|
660
|
+
* popular: (q: QueryBuilder<PostRecord>) => q.where('views', '>', 1000),
|
|
661
|
+
* }
|
|
662
|
+
* }
|
|
663
|
+
* // Usage: Post.scope('published').scope('popular').get()
|
|
664
|
+
*/
|
|
665
|
+
static scopes?: Record<string, (q: QueryBuilder<any>) => QueryBuilder<any>>;
|
|
666
|
+
/**
|
|
667
|
+
* Default scope applied to all queries on this model.
|
|
668
|
+
* Override this to automatically filter queries (e.g., soft deletes).
|
|
669
|
+
*
|
|
670
|
+
* @example
|
|
671
|
+
* static defaultScope = (q: QueryBuilder<any>) => q.whereNull('deletedAt')
|
|
672
|
+
*/
|
|
673
|
+
static defaultScope?: (q: QueryBuilder<any>) => QueryBuilder<any>;
|
|
674
|
+
/**
|
|
675
|
+
* Model lifecycle hooks that fire during create, update, and delete operations.
|
|
676
|
+
*
|
|
677
|
+
* @example
|
|
678
|
+
* class User extends Model<UserRecord> {
|
|
679
|
+
* static hooks: ModelHooks = {
|
|
680
|
+
* creating: async (data) => { data.password = await hash(data.password as string) },
|
|
681
|
+
* created: async (data) => { console.log('User created:', data) },
|
|
682
|
+
* }
|
|
683
|
+
* }
|
|
684
|
+
*/
|
|
685
|
+
static hooks?: ModelHooks;
|
|
686
|
+
/**
|
|
687
|
+
* Attribute casting definitions for automatic type conversion.
|
|
688
|
+
*
|
|
689
|
+
* Casts are applied when reading records from the database and when
|
|
690
|
+
* writing records to the database.
|
|
691
|
+
*
|
|
692
|
+
* @example
|
|
693
|
+
* class Post extends Model<PostRecord> {
|
|
694
|
+
* static casts = {
|
|
695
|
+
* metadata: 'json',
|
|
696
|
+
* publishedAt: 'date',
|
|
697
|
+
* isActive: 'boolean',
|
|
698
|
+
* viewCount: 'number',
|
|
699
|
+
* }
|
|
700
|
+
* }
|
|
701
|
+
*/
|
|
702
|
+
static casts?: Record<string, CastType>;
|
|
703
|
+
/**
|
|
704
|
+
* Whitelist of fields allowed for mass assignment.
|
|
705
|
+
* If set, `create()` and `update()` accept only these fields — input
|
|
706
|
+
* containing any other key throws a MassAssignmentException so bugs
|
|
707
|
+
* (and injection attempts) surface immediately instead of being
|
|
708
|
+
* silently discarded. Use `forceCreate()` / `forceUpdate()` for
|
|
709
|
+
* trusted server-side data.
|
|
710
|
+
*
|
|
711
|
+
* The primary key (`id`) is always silently stripped from mass-assignment
|
|
712
|
+
* input, and fields returned by `deniedFields()` always throw — neither
|
|
713
|
+
* depends on `fillable`.
|
|
714
|
+
*
|
|
715
|
+
* @example
|
|
716
|
+
* class User extends Model<UserRecord> {
|
|
717
|
+
* static fillable = ['name', 'email', 'password']
|
|
718
|
+
* }
|
|
719
|
+
*/
|
|
720
|
+
static fillable?: string[];
|
|
721
|
+
/**
|
|
722
|
+
* Fields that can never be mass-assigned, resolved at call time so
|
|
723
|
+
* subclasses can derive them from their own configuration (e.g.
|
|
724
|
+
* `AuthenticatableModel` contributes its resolved password-hash and
|
|
725
|
+
* remember-token columns). Input containing any of these throws a
|
|
726
|
+
* MassAssignmentException regardless of `fillable` — use
|
|
727
|
+
* `forceCreate()` / `forceUpdate()` for trusted server-side values.
|
|
728
|
+
*/
|
|
729
|
+
protected static deniedFields(): string[];
|
|
730
|
+
/**
|
|
731
|
+
* Accessor functions for computed/virtual attributes.
|
|
732
|
+
* Applied after reading records from the database.
|
|
733
|
+
*
|
|
734
|
+
* @example
|
|
735
|
+
* class User extends Model<UserRecord> {
|
|
736
|
+
* static accessors = {
|
|
737
|
+
* fullName: (record) => `${record.firstName} ${record.lastName}`,
|
|
738
|
+
* }
|
|
739
|
+
* }
|
|
740
|
+
*/
|
|
741
|
+
static accessors?: AccessorDefinitions;
|
|
742
|
+
/**
|
|
743
|
+
* Mutator functions for transforming attributes before persistence.
|
|
744
|
+
*
|
|
745
|
+
* @example
|
|
746
|
+
* class User extends Model<UserRecord> {
|
|
747
|
+
* static mutators = {
|
|
748
|
+
* email: (value) => String(value).toLowerCase(),
|
|
749
|
+
* }
|
|
750
|
+
* }
|
|
751
|
+
*/
|
|
752
|
+
static mutators?: MutatorDefinitions;
|
|
753
|
+
/**
|
|
754
|
+
* Fields to exclude from serialization output.
|
|
755
|
+
*
|
|
756
|
+
* @example
|
|
757
|
+
* class User extends Model<UserRecord> {
|
|
758
|
+
* static hidden = ['passwordHash', 'rememberToken']
|
|
759
|
+
* }
|
|
760
|
+
*/
|
|
761
|
+
static hidden?: string[];
|
|
762
|
+
/**
|
|
763
|
+
* Whitelist of fields to include in serialization output.
|
|
764
|
+
* When set, only these fields appear. Takes precedence over `hidden`.
|
|
765
|
+
*/
|
|
766
|
+
static visible?: string[];
|
|
767
|
+
/**
|
|
768
|
+
* Virtual accessor attributes to include in serialization output.
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* class User extends Model<UserRecord> {
|
|
772
|
+
* static appends = ['fullName']
|
|
773
|
+
* static accessors = { fullName: (r) => `${r.firstName} ${r.lastName}` }
|
|
774
|
+
* }
|
|
775
|
+
*/
|
|
776
|
+
static appends?: string[];
|
|
777
|
+
/** Registered model observers. */
|
|
778
|
+
protected static observers?: ModelObserver[];
|
|
779
|
+
/** Named global scopes registry. */
|
|
780
|
+
protected static globalScopeRegistry?: GlobalScopeRegistry;
|
|
781
|
+
/**
|
|
782
|
+
* Set a custom ORM adapter for this model.
|
|
783
|
+
* @param adapter - The adapter to use
|
|
784
|
+
*/
|
|
785
|
+
static useAdapter(adapter: ORMAdapter): void;
|
|
786
|
+
/**
|
|
787
|
+
* Get the current ORM adapter.
|
|
788
|
+
* @returns The configured adapter
|
|
789
|
+
*/
|
|
790
|
+
static getAdapter(): ORMAdapter;
|
|
791
|
+
/**
|
|
792
|
+
* Run operations inside a database transaction.
|
|
793
|
+
*
|
|
794
|
+
* @example
|
|
795
|
+
* await User.transaction(async (trx) => {
|
|
796
|
+
* const user = await User.create({ name: 'John' }, { trx })
|
|
797
|
+
* await Profile.create({ userId: user.id }, { trx })
|
|
798
|
+
* })
|
|
799
|
+
*/
|
|
800
|
+
static transaction<T extends typeof Model, TResult>(this: T, callback: (trx: TransactionHandle, scope: TransactionModelScope<T>) => Promise<TResult>): Promise<TResult>;
|
|
801
|
+
/**
|
|
802
|
+
* Create a transaction-bound model scope that automatically forwards `trx`
|
|
803
|
+
* to query and write operations.
|
|
804
|
+
*
|
|
805
|
+
* @example
|
|
806
|
+
* await User.transaction(async (trx, txUser) => {
|
|
807
|
+
* await txUser.create({ name: 'Shinji' })
|
|
808
|
+
* await txUser.update({ id: 1 }, { name: 'Ikari' })
|
|
809
|
+
* })
|
|
810
|
+
*/
|
|
811
|
+
static inTransaction<T extends typeof Model>(this: T, trx: TransactionHandle): TransactionModelScope<T>;
|
|
812
|
+
/**
|
|
813
|
+
* Register a model observer class.
|
|
814
|
+
*
|
|
815
|
+
* @example
|
|
816
|
+
* User.observe(UserObserver)
|
|
817
|
+
*/
|
|
818
|
+
static observe(ObserverClass: ModelObserverConstructor): void;
|
|
819
|
+
/** Clear all registered observers. */
|
|
820
|
+
static clearObservers(): void;
|
|
821
|
+
/**
|
|
822
|
+
* The registry this class may mutate, creating it on first write.
|
|
823
|
+
*
|
|
824
|
+
* The new registry is seeded from the inherited one: the read paths
|
|
825
|
+
* (`newQuery()`, `hasScopes()`) resolve `globalScopeRegistry` through the
|
|
826
|
+
* prototype chain, so a subclass that started from an empty registry would
|
|
827
|
+
* shadow — and silently drop — every scope it inherited. That is exactly the
|
|
828
|
+
* `class Post extends SoftDeletes(Base)` then `Post.addGlobalScope('tenant')`
|
|
829
|
+
* case, which would otherwise lose the `softDelete` filter.
|
|
830
|
+
*
|
|
831
|
+
* The copy is a snapshot: scopes added to the parent *after* a subclass first
|
|
832
|
+
* registers or removes one do not reach that subclass. Nothing in the
|
|
833
|
+
* framework relies on later propagation — the `SoftDeletes` mixin registers
|
|
834
|
+
* `softDelete` synchronously while building the class, before any user code
|
|
835
|
+
* can touch it.
|
|
836
|
+
*/
|
|
837
|
+
protected static getGlobalScopes(): GlobalScopeRegistry;
|
|
838
|
+
/**
|
|
839
|
+
* Register a named global scope.
|
|
840
|
+
*
|
|
841
|
+
* @example
|
|
842
|
+
* User.addGlobalScope('active', (q) => q.where('active', true))
|
|
843
|
+
*/
|
|
844
|
+
static addGlobalScope(name: string, fn: ScopeFunction): void;
|
|
845
|
+
/** Remove a named global scope. */
|
|
846
|
+
static removeGlobalScope(name: string): void;
|
|
847
|
+
/**
|
|
848
|
+
* Start a query excluding specific global scope(s).
|
|
849
|
+
*
|
|
850
|
+
* Only *named* scopes can be excluded: `defaultScope` is re-applied here
|
|
851
|
+
* whatever this is asked to drop. A mixin whose filter users must be able to
|
|
852
|
+
* opt out of therefore has to register a named scope and nothing else —
|
|
853
|
+
* registering as both makes the filter unremovable, which is how `SoftDeletes`
|
|
854
|
+
* once made `withoutGlobalScope('softDelete')` a no-op.
|
|
855
|
+
*
|
|
856
|
+
* @example
|
|
857
|
+
* const all = await User.withoutGlobalScope('active').get()
|
|
858
|
+
*/
|
|
859
|
+
static withoutGlobalScope<T extends typeof Model>(this: T, ...names: string[]): QueryBuilder<TRecordFor<T>>;
|
|
860
|
+
/**
|
|
861
|
+
* Start a query with no global scopes applied (also skips defaultScope).
|
|
862
|
+
*
|
|
863
|
+
* @example
|
|
864
|
+
* const all = await User.withoutGlobalScopes().get()
|
|
865
|
+
*/
|
|
866
|
+
static withoutGlobalScopes<T extends typeof Model>(this: T): QueryBuilder<TRecordFor<T>>;
|
|
867
|
+
/**
|
|
868
|
+
* Serialize a record for API/JSON output.
|
|
869
|
+
* Applies hidden/visible filtering, accessors, and appends.
|
|
870
|
+
*
|
|
871
|
+
* @example
|
|
872
|
+
* const json = User.serialize(user)
|
|
873
|
+
*/
|
|
874
|
+
static serialize<T extends typeof Model>(this: T, record: TRecordFor<T>): PlainObject;
|
|
875
|
+
/**
|
|
876
|
+
* Serialize an array of records.
|
|
877
|
+
*
|
|
878
|
+
* @example
|
|
879
|
+
* const json = User.serializeMany(users)
|
|
880
|
+
*/
|
|
881
|
+
static serializeMany<T extends typeof Model>(this: T, records: TRecordFor<T>[]): PlainObject[];
|
|
882
|
+
/**
|
|
883
|
+
* Apply attribute casts to a record read from the database.
|
|
884
|
+
*
|
|
885
|
+
* @param record - The raw record from the database
|
|
886
|
+
* @returns The record with cast attributes applied
|
|
887
|
+
*/
|
|
888
|
+
static applyCasts<T extends PlainObject>(record: T): T;
|
|
889
|
+
/**
|
|
890
|
+
* Apply all read-time transforms: casts then accessors.
|
|
891
|
+
* Used internally after fetching records from the database.
|
|
892
|
+
*/
|
|
893
|
+
protected static applyReadTransforms<T extends PlainObject>(record: T): T;
|
|
894
|
+
/**
|
|
895
|
+
* Filter input data based on mass assignment protection rules.
|
|
896
|
+
*
|
|
897
|
+
* Applied in order: fields returned by `deniedFields()` throw a
|
|
898
|
+
* MassAssignmentException (checked on the raw input, so no other rule
|
|
899
|
+
* can silently swallow them); the primary key (`id`) is silently
|
|
900
|
+
* stripped; when `fillable` is defined, any remaining field outside it
|
|
901
|
+
* throws and only listed fields are kept.
|
|
902
|
+
*
|
|
903
|
+
* @param data - The input data to filter
|
|
904
|
+
* @returns Filtered data safe for mass assignment
|
|
905
|
+
*/
|
|
906
|
+
static filterFillable(data: PlainObject): PlainObject;
|
|
907
|
+
protected static preparePersistencePayload(data: PlainObject): Promise<PlainObject>;
|
|
908
|
+
/** @internal Lets QueryBuilder bulk updates run the same payload preparation as `Model.update()`. */
|
|
909
|
+
static prepareBulkPersistencePayload(data: PlainObject): Promise<PlainObject>;
|
|
910
|
+
protected static getRelationDefinitions(): Map<string, RelationDefinition>;
|
|
911
|
+
/** @internal */
|
|
912
|
+
static getRelationDefinition(name: string): RelationDefinition | undefined;
|
|
913
|
+
static resolveTable(): unknown;
|
|
914
|
+
/**
|
|
915
|
+
* Retrieve all records from the table.
|
|
916
|
+
*
|
|
917
|
+
* @returns Array of all records
|
|
918
|
+
*
|
|
919
|
+
* @example
|
|
920
|
+
* const users = await User.all()
|
|
921
|
+
*/
|
|
922
|
+
static all<T extends typeof Model>(this: T, queryOptions?: ModelQueryOptions): Promise<Array<TRecordFor<T>>>;
|
|
923
|
+
/**
|
|
924
|
+
* Find a record by its primary key.
|
|
925
|
+
*
|
|
926
|
+
* @param id - The primary key value
|
|
927
|
+
* @param key - The primary key column name (default: 'id')
|
|
928
|
+
* @returns The record or null if not found
|
|
929
|
+
*
|
|
930
|
+
* @example
|
|
931
|
+
* const user = await User.find(1)
|
|
932
|
+
* const userByEmail = await User.find('john@example.com', 'email')
|
|
933
|
+
*/
|
|
934
|
+
static find<T extends typeof Model>(this: T, id: TRecordFor<T>[keyof TRecordFor<T> & string], key?: keyof TRecordFor<T> & string, queryOptions?: ModelQueryOptions): Promise<TRecordFor<T> | null>;
|
|
935
|
+
/**
|
|
936
|
+
* Find a record by primary key or throw an error.
|
|
937
|
+
*
|
|
938
|
+
* @param id - The primary key value
|
|
939
|
+
* @param key - The primary key column name (default: 'id')
|
|
940
|
+
* @returns The record
|
|
941
|
+
* @throws Error if record not found
|
|
942
|
+
*
|
|
943
|
+
* @example
|
|
944
|
+
* const user = await User.findOrFail(1) // Throws if not found
|
|
945
|
+
*/
|
|
946
|
+
static findOrFail<T extends typeof Model>(this: T, id: TRecordFor<T>[keyof TRecordFor<T> & string], key?: keyof TRecordFor<T> & string, queryOptions?: ModelQueryOptions): Promise<TRecordFor<T>>;
|
|
947
|
+
/**
|
|
948
|
+
* Find a record by primary key with eager-loaded relations.
|
|
949
|
+
*
|
|
950
|
+
* @param id - The primary key value
|
|
951
|
+
* @param relations - Relation name(s) to eager load
|
|
952
|
+
* @param key - The primary key column name (default: 'id')
|
|
953
|
+
* @returns The record with loaded relations, or null if not found
|
|
954
|
+
*
|
|
955
|
+
* @example
|
|
956
|
+
* const post = await Post.findWith(1, 'author')
|
|
957
|
+
* const post = await Post.findWith(1, ['author', 'tags'])
|
|
958
|
+
*/
|
|
959
|
+
static findWith<T extends typeof Model, K extends RelationPath<T>>(this: T, id: TRecordFor<T>[keyof TRecordFor<T> & string], relations: K | readonly K[], key?: keyof TRecordFor<T> & string, queryOptions?: ModelQueryOptions): Promise<(TRecordFor<T> & RelationTypePick<T, K | readonly K[]>) | null>;
|
|
960
|
+
/**
|
|
961
|
+
* Find a record by primary key with eager-loaded relations, or throw.
|
|
962
|
+
*
|
|
963
|
+
* @param id - The primary key value
|
|
964
|
+
* @param relations - Relation name(s) to eager load
|
|
965
|
+
* @param key - The primary key column name (default: 'id')
|
|
966
|
+
* @returns The record with loaded relations
|
|
967
|
+
* @throws ModelNotFoundException if record not found
|
|
968
|
+
*
|
|
969
|
+
* @example
|
|
970
|
+
* const post = await Post.findWithOrFail(1, 'author')
|
|
971
|
+
* const post = await Post.findWithOrFail(1, ['author', 'tags'])
|
|
972
|
+
* const post = await Post.findWithOrFail(1, 'comments.author') // nested
|
|
973
|
+
*/
|
|
974
|
+
static findWithOrFail<T extends typeof Model, K extends RelationPath<T>>(this: T, id: TRecordFor<T>[keyof TRecordFor<T> & string], relations: K | readonly K[], key?: keyof TRecordFor<T> & string, queryOptions?: ModelQueryOptions): Promise<TRecordFor<T> & RelationTypePick<T, K | readonly K[]>>;
|
|
975
|
+
/**
|
|
976
|
+
* Get the first record matching the conditions.
|
|
977
|
+
*
|
|
978
|
+
* @param where - Optional filter conditions
|
|
979
|
+
* @returns The first matching record or null
|
|
980
|
+
*
|
|
981
|
+
* @example
|
|
982
|
+
* const admin = await User.first({ role: 'admin' })
|
|
983
|
+
*/
|
|
984
|
+
static first<T extends typeof Model>(this: T, where?: WhereClauseFor<T>, queryOptions?: ModelQueryOptions): Promise<TRecordFor<T> | null>;
|
|
985
|
+
/**
|
|
986
|
+
* Start a fluent query with where conditions.
|
|
987
|
+
*
|
|
988
|
+
* Returns a QueryBuilder that is thenable, so it can be directly awaited.
|
|
989
|
+
*
|
|
990
|
+
* @example
|
|
991
|
+
* // Object form - multiple equality conditions
|
|
992
|
+
* const activeUsers = await User.where({ status: 'active' })
|
|
993
|
+
*
|
|
994
|
+
* // Field + value - equality
|
|
995
|
+
* const admins = await User.where('role', 'admin')
|
|
996
|
+
*
|
|
997
|
+
* // Field + operator + value - comparison
|
|
998
|
+
* const recent = await Post.where('views', '>', 100)
|
|
999
|
+
*
|
|
1000
|
+
* // Fluent chaining
|
|
1001
|
+
* const posts = await Post.where('status', 'published')
|
|
1002
|
+
* .where('views', '>', 100)
|
|
1003
|
+
* .orderBy('createdAt', 'desc')
|
|
1004
|
+
* .limit(10)
|
|
1005
|
+
* .get()
|
|
1006
|
+
*/
|
|
1007
|
+
static where<T extends typeof Model>(this: T, callback: WhereGroupCallback<TRecordFor<T>>): QueryBuilder<TRecordFor<T>>;
|
|
1008
|
+
static where<T extends typeof Model>(this: T, conditions: WhereClauseFor<T>): QueryBuilder<TRecordFor<T>>;
|
|
1009
|
+
static where<T extends typeof Model>(this: T, field: keyof TRecordFor<T> & string, value: unknown): QueryBuilder<TRecordFor<T>>;
|
|
1010
|
+
static where<T extends typeof Model>(this: T, field: keyof TRecordFor<T> & string, operator: WhereOperator, value: unknown): QueryBuilder<TRecordFor<T>>;
|
|
1011
|
+
/**
|
|
1012
|
+
* Start a fluent query with a WHERE NULL condition.
|
|
1013
|
+
* @param field - Column to check for NULL
|
|
1014
|
+
*/
|
|
1015
|
+
static whereNull<T extends typeof Model>(this: T, field: keyof TRecordFor<T> & string): QueryBuilder<TRecordFor<T>>;
|
|
1016
|
+
/**
|
|
1017
|
+
* Start a fluent query with a WHERE NOT NULL condition.
|
|
1018
|
+
* @param field - Column to check for NOT NULL
|
|
1019
|
+
*/
|
|
1020
|
+
static whereNotNull<T extends typeof Model>(this: T, field: keyof TRecordFor<T> & string): QueryBuilder<TRecordFor<T>>;
|
|
1021
|
+
/**
|
|
1022
|
+
* Start a fluent query with a WHERE IN condition.
|
|
1023
|
+
* @param field - Column to check
|
|
1024
|
+
* @param values - Array of values to match against
|
|
1025
|
+
*/
|
|
1026
|
+
static whereIn<T extends typeof Model>(this: T, field: keyof TRecordFor<T> & string, values: readonly TRecordFor<T>[keyof TRecordFor<T> & string][]): QueryBuilder<TRecordFor<T>>;
|
|
1027
|
+
/**
|
|
1028
|
+
* Start a fluent query with a WHERE NOT IN condition.
|
|
1029
|
+
* @param field - Column to check
|
|
1030
|
+
* @param values - Array of values to exclude
|
|
1031
|
+
*/
|
|
1032
|
+
static whereNotIn<T extends typeof Model>(this: T, field: keyof TRecordFor<T> & string, values: readonly TRecordFor<T>[keyof TRecordFor<T> & string][]): QueryBuilder<TRecordFor<T>>;
|
|
1033
|
+
/**
|
|
1034
|
+
* Start a fluent query with a typed field selection.
|
|
1035
|
+
*
|
|
1036
|
+
* @example
|
|
1037
|
+
* const rows = await User.select('id', 'name')
|
|
1038
|
+
* const first = await User.select('id').first()
|
|
1039
|
+
*/
|
|
1040
|
+
static select<T extends typeof Model, Keys extends keyof TRecordFor<T> & string>(this: T, ...fields: readonly Keys[]): QueryBuilder<TRecordFor<T>, Pick<TRecordFor<T>, Keys>>;
|
|
1041
|
+
/**
|
|
1042
|
+
* Start a new QueryBuilder for fluent query construction.
|
|
1043
|
+
*
|
|
1044
|
+
* @returns A fresh QueryBuilder instance
|
|
1045
|
+
*
|
|
1046
|
+
* @example
|
|
1047
|
+
* const results = await User.newQuery()
|
|
1048
|
+
* .where('status', 'active')
|
|
1049
|
+
* .orderBy('name')
|
|
1050
|
+
* .limit(10)
|
|
1051
|
+
* .get()
|
|
1052
|
+
*/
|
|
1053
|
+
static newQuery<T extends typeof Model>(this: T, queryOptions?: ModelQueryOptions): QueryBuilder<TRecordFor<T>>;
|
|
1054
|
+
/**
|
|
1055
|
+
* Create a new QueryBuilder without applying any default scopes.
|
|
1056
|
+
* Useful for querying soft-deleted records or bypassing global filters.
|
|
1057
|
+
*
|
|
1058
|
+
* @returns A fresh QueryBuilder instance with no scopes applied
|
|
1059
|
+
*/
|
|
1060
|
+
static newQueryWithoutScopes<T extends typeof Model>(this: T, queryOptions?: ModelQueryOptions): QueryBuilder<TRecordFor<T>>;
|
|
1061
|
+
/**
|
|
1062
|
+
* Whether this model carries a filter that every query must apply.
|
|
1063
|
+
*
|
|
1064
|
+
* The entry points that talk to the adapter directly check this to decide
|
|
1065
|
+
* whether they can take the fast path; anything that returns a QueryBuilder
|
|
1066
|
+
* goes through `newQuery()` unconditionally.
|
|
1067
|
+
*/
|
|
1068
|
+
protected static hasScopes(): boolean;
|
|
1069
|
+
/**
|
|
1070
|
+
* Apply a named query scope.
|
|
1071
|
+
*
|
|
1072
|
+
* @param name - The scope name defined in `static scopes`
|
|
1073
|
+
* @returns A QueryBuilder with the scope applied
|
|
1074
|
+
*
|
|
1075
|
+
* @example
|
|
1076
|
+
* const published = await Post.scope('published').get()
|
|
1077
|
+
* const popularPublished = await Post.scope('published').scope('popular').get()
|
|
1078
|
+
*/
|
|
1079
|
+
static scope<T extends typeof Model>(this: T, name: string): QueryBuilder<TRecordFor<T>>;
|
|
1080
|
+
/**
|
|
1081
|
+
* Define a one-to-many relationship.
|
|
1082
|
+
*
|
|
1083
|
+
* @param name - Relation name (used in `with()` calls)
|
|
1084
|
+
* @param related - The related model class
|
|
1085
|
+
* @param foreignKey - Foreign key column on the related model
|
|
1086
|
+
* @param localKey - Local key column on this model
|
|
1087
|
+
*
|
|
1088
|
+
* @example
|
|
1089
|
+
* class User extends Model<UserRecord> {
|
|
1090
|
+
* static {
|
|
1091
|
+
* this.hasMany('posts', Post, 'userId', 'id')
|
|
1092
|
+
* }
|
|
1093
|
+
* }
|
|
1094
|
+
* // Later: User.with('posts')
|
|
1095
|
+
*/
|
|
1096
|
+
static hasMany<This extends typeof Model, Related extends typeof Model, ForeignKey extends keyof TRecordFor<Related> & string, LocalKey extends keyof TRecordFor<This> & string, Name extends RelationKeyOrString<This>>(this: This, name: Name, related: Related | (() => Related | Promise<Related>), foreignKey: ForeignKey, localKey: LocalKey): void;
|
|
1097
|
+
/**
|
|
1098
|
+
* Define a many-to-one (inverse) relationship.
|
|
1099
|
+
*
|
|
1100
|
+
* @param name - Relation name (used in `with()` calls)
|
|
1101
|
+
* @param related - The related model class
|
|
1102
|
+
* @param foreignKey - Foreign key column on this model
|
|
1103
|
+
* @param ownerKey - Primary key column on the related model
|
|
1104
|
+
*
|
|
1105
|
+
* @example
|
|
1106
|
+
* class Post extends Model<PostRecord> {
|
|
1107
|
+
* static {
|
|
1108
|
+
* this.belongsTo('author', User, 'userId', 'id')
|
|
1109
|
+
* }
|
|
1110
|
+
* }
|
|
1111
|
+
* // Later: Post.with('author')
|
|
1112
|
+
*/
|
|
1113
|
+
static belongsTo<This extends typeof Model, Related extends typeof Model, ForeignKey extends keyof TRecordFor<This> & string, OwnerKey extends keyof TRecordFor<Related> & string, Name extends RelationKeyOrString<This>>(this: This, name: Name, related: Related | (() => Related | Promise<Related>), foreignKey: ForeignKey, ownerKey: OwnerKey): void;
|
|
1114
|
+
/**
|
|
1115
|
+
* Define a one-to-one relationship.
|
|
1116
|
+
*
|
|
1117
|
+
* @param name - Relation name (used in `with()` calls)
|
|
1118
|
+
* @param related - The related model class
|
|
1119
|
+
* @param foreignKey - Foreign key column on the related model
|
|
1120
|
+
* @param localKey - Local key column on this model
|
|
1121
|
+
*
|
|
1122
|
+
* @example
|
|
1123
|
+
* class User extends Model<UserRecord> {
|
|
1124
|
+
* static {
|
|
1125
|
+
* this.hasOne('profile', Profile, 'userId', 'id')
|
|
1126
|
+
* }
|
|
1127
|
+
* }
|
|
1128
|
+
* // Later: User.with('profile')
|
|
1129
|
+
*/
|
|
1130
|
+
static hasOne<This extends typeof Model, Related extends typeof Model, ForeignKey extends keyof TRecordFor<Related> & string, LocalKey extends keyof TRecordFor<This> & string, Name extends RelationKeyOrString<This>>(this: This, name: Name, related: Related | (() => Related | Promise<Related>), foreignKey: ForeignKey, localKey: LocalKey): void;
|
|
1131
|
+
/**
|
|
1132
|
+
* Define a many-to-many relationship via a pivot table.
|
|
1133
|
+
*
|
|
1134
|
+
* @param name - Relation name (used in `with()` calls)
|
|
1135
|
+
* @param related - The related model class
|
|
1136
|
+
* @param pivotTable - The pivot/junction table (e.g., Drizzle table schema)
|
|
1137
|
+
* @param foreignPivotKey - Column on pivot table referencing this model
|
|
1138
|
+
* @param relatedPivotKey - Column on pivot table referencing the related model
|
|
1139
|
+
* @param parentKey - Local key on this model (default: 'id')
|
|
1140
|
+
* @param relatedKey - Local key on the related model (default: 'id')
|
|
1141
|
+
*
|
|
1142
|
+
* @example
|
|
1143
|
+
* class User extends Model<UserRecord> {
|
|
1144
|
+
* static {
|
|
1145
|
+
* this.belongsToMany('roles', Role, userRoles, 'userId', 'roleId', 'id', 'id')
|
|
1146
|
+
* }
|
|
1147
|
+
* }
|
|
1148
|
+
* // Later: User.with('roles')
|
|
1149
|
+
*/
|
|
1150
|
+
static belongsToMany<This extends typeof Model, Related extends typeof Model, Name extends RelationKeyOrString<This>>(this: This, name: Name, related: Related | (() => Related | Promise<Related>), pivotTable: unknown, foreignPivotKey: string, relatedPivotKey: string, parentKey?: string, relatedKey?: string): void;
|
|
1151
|
+
/**
|
|
1152
|
+
* Define a has-many-through relationship.
|
|
1153
|
+
*
|
|
1154
|
+
* @param name - Relation name (used in `with()` calls)
|
|
1155
|
+
* @param related - The final related model class
|
|
1156
|
+
* @param through - The intermediate model class
|
|
1157
|
+
* @param firstKey - Foreign key on the intermediate model referencing this model
|
|
1158
|
+
* @param secondKey - Foreign key on the related model referencing the intermediate model
|
|
1159
|
+
* @param localKey - Local key on this model (default: 'id')
|
|
1160
|
+
* @param secondLocalKey - Local key on the intermediate model (default: 'id')
|
|
1161
|
+
*
|
|
1162
|
+
* @example
|
|
1163
|
+
* class Country extends Model<CountryRecord> {
|
|
1164
|
+
* static {
|
|
1165
|
+
* this.hasManyThrough('posts', Post, User, 'countryId', 'userId', 'id', 'id')
|
|
1166
|
+
* }
|
|
1167
|
+
* }
|
|
1168
|
+
* // Later: Country.with('posts')
|
|
1169
|
+
*/
|
|
1170
|
+
static hasManyThrough<This extends typeof Model, Related extends typeof Model, Through extends typeof Model, Name extends RelationKeyOrString<This>>(this: This, name: Name, related: Related | (() => Related | Promise<Related>), through: Through | (() => Through | Promise<Through>), firstKey: string, secondKey: string, localKey?: string, secondLocalKey?: string): void;
|
|
1171
|
+
/**
|
|
1172
|
+
* Map of type strings to model classes for polymorphic relationships.
|
|
1173
|
+
*
|
|
1174
|
+
* @example
|
|
1175
|
+
* Model.morphMap = { Post, Video }
|
|
1176
|
+
*/
|
|
1177
|
+
static morphMap?: Record<string, typeof Model>;
|
|
1178
|
+
/**
|
|
1179
|
+
* Define a one-to-many polymorphic relationship.
|
|
1180
|
+
*
|
|
1181
|
+
* @param name - Relation name
|
|
1182
|
+
* @param related - The related model class
|
|
1183
|
+
* @param morphName - Base name for the type/id columns (e.g. 'commentable' → commentableType + commentableId)
|
|
1184
|
+
* @param localKey - Local key on this model (default: 'id')
|
|
1185
|
+
*
|
|
1186
|
+
* @example
|
|
1187
|
+
* Post.morphMany('comments', Comment, 'commentable', 'id')
|
|
1188
|
+
*/
|
|
1189
|
+
static morphMany<This extends typeof Model, Related extends typeof Model, Name extends RelationKeyOrString<This>>(this: This, name: Name, related: Related | (() => Related | Promise<Related>), morphName: string, localKey?: string): void;
|
|
1190
|
+
/**
|
|
1191
|
+
* Define the inverse of a polymorphic relationship.
|
|
1192
|
+
*
|
|
1193
|
+
* @param name - Relation name
|
|
1194
|
+
* @param morphName - Base name for the type/id columns
|
|
1195
|
+
*
|
|
1196
|
+
* @example
|
|
1197
|
+
* Comment.morphTo('commentable', 'commentable')
|
|
1198
|
+
*/
|
|
1199
|
+
static morphTo<This extends typeof Model, Name extends RelationKeyOrString<This>>(this: This, name: Name, morphName: string): void;
|
|
1200
|
+
/**
|
|
1201
|
+
* Get records sorted by the specified order.
|
|
1202
|
+
*
|
|
1203
|
+
* @param order - Order expression(s)
|
|
1204
|
+
* @param where - Optional filter conditions
|
|
1205
|
+
* @returns Sorted array of records
|
|
1206
|
+
*
|
|
1207
|
+
* @example
|
|
1208
|
+
* // Single column ascending
|
|
1209
|
+
* await User.orderBy('createdAt')
|
|
1210
|
+
*
|
|
1211
|
+
* // Single column descending
|
|
1212
|
+
* await User.orderBy(['createdAt', 'desc'])
|
|
1213
|
+
*
|
|
1214
|
+
* // Multiple columns
|
|
1215
|
+
* await User.orderBy([['lastName', 'asc'], ['firstName', 'asc']])
|
|
1216
|
+
*
|
|
1217
|
+
* // With where clause
|
|
1218
|
+
* await User.orderBy('name', { status: 'active' })
|
|
1219
|
+
*/
|
|
1220
|
+
static orderBy<T extends typeof Model>(this: T, order: OrderByInput<TRecordFor<T>>, where?: WhereClauseFor<T>, queryOptions?: ModelQueryOptions): Promise<TRecordFor<T>[]>;
|
|
1221
|
+
/**
|
|
1222
|
+
* Get paginated records.
|
|
1223
|
+
*
|
|
1224
|
+
* @param options - Pagination options
|
|
1225
|
+
* @returns Paginated result with data and metadata
|
|
1226
|
+
*
|
|
1227
|
+
* @example
|
|
1228
|
+
* const result = await User.paginate({ page: 1, perPage: 10 })
|
|
1229
|
+
* // result.data - Array of users
|
|
1230
|
+
* // result.meta.total - Total count
|
|
1231
|
+
* // result.meta.hasMore - Whether there are more pages
|
|
1232
|
+
*
|
|
1233
|
+
* // With filtering and ordering
|
|
1234
|
+
* await User.paginate({
|
|
1235
|
+
* page: 2,
|
|
1236
|
+
* perPage: 20,
|
|
1237
|
+
* where: { status: 'active' },
|
|
1238
|
+
* orderBy: ['createdAt', 'desc']
|
|
1239
|
+
* })
|
|
1240
|
+
*/
|
|
1241
|
+
static paginate<T extends typeof Model>(this: T, options?: PaginateOptions<TRecordFor<T>>, queryOptions?: ModelQueryOptions): Promise<PaginatedResult<TRecordFor<T>>>;
|
|
1242
|
+
/**
|
|
1243
|
+
* Paginate records with eager-loaded relationships.
|
|
1244
|
+
*
|
|
1245
|
+
* @param relations - Relation name(s) to load
|
|
1246
|
+
* @param options - Pagination options
|
|
1247
|
+
* @returns Paginated result with loaded relationships
|
|
1248
|
+
*
|
|
1249
|
+
* @example
|
|
1250
|
+
* const result = await User.withPaginate('posts', { page: 1, perPage: 10 })
|
|
1251
|
+
* // result.data - Users with their posts loaded
|
|
1252
|
+
* // result.meta - Pagination metadata
|
|
1253
|
+
*/
|
|
1254
|
+
static withPaginate<T extends typeof Model, K extends RelationPath<T>>(this: T, relations: K | readonly K[], options?: PaginateOptions<TRecordFor<T>>, queryOptions?: ModelQueryOptions): Promise<PaginatedResult<TRecordFor<T> & RelationTypePick<T, K | readonly K[]>>>;
|
|
1255
|
+
/**
|
|
1256
|
+
* Create a new record.
|
|
1257
|
+
*
|
|
1258
|
+
* @param data - Record data to insert
|
|
1259
|
+
* @returns The created record
|
|
1260
|
+
*
|
|
1261
|
+
* @example
|
|
1262
|
+
* const user = await User.create({
|
|
1263
|
+
* name: 'John Doe',
|
|
1264
|
+
* email: 'john@example.com'
|
|
1265
|
+
* })
|
|
1266
|
+
*/
|
|
1267
|
+
static create<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1268
|
+
/**
|
|
1269
|
+
* Create a record bypassing mass-assignment protection. Use for trusted,
|
|
1270
|
+
* server-side-assembled data (OAuth account linking, seeders, system
|
|
1271
|
+
* records) — never for raw request input.
|
|
1272
|
+
*
|
|
1273
|
+
* @example
|
|
1274
|
+
* const user = await User.forceCreate({
|
|
1275
|
+
* name: profile.name,
|
|
1276
|
+
* email: profile.email,
|
|
1277
|
+
* passwordHash: `oauth:${provider}:${profile.id}`,
|
|
1278
|
+
* })
|
|
1279
|
+
*/
|
|
1280
|
+
static forceCreate<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1281
|
+
protected static runCreate<T extends typeof Model>(this: T, data: TCreateFor<T>, writeOptions: ModelWriteOptions | undefined, applyFillable: boolean): Promise<TRecordFor<T>>;
|
|
1282
|
+
/**
|
|
1283
|
+
* Update records matching the conditions.
|
|
1284
|
+
*
|
|
1285
|
+
* @param where - Filter conditions to identify records
|
|
1286
|
+
* @param data - Data to update
|
|
1287
|
+
* @returns The updated record
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* await User.update({ id: 1 }, { name: 'Jane Doe' })
|
|
1291
|
+
*/
|
|
1292
|
+
static update<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1293
|
+
/**
|
|
1294
|
+
* Update records bypassing mass-assignment protection. Use for trusted,
|
|
1295
|
+
* server-side-assembled data — never for raw request input.
|
|
1296
|
+
*/
|
|
1297
|
+
static forceUpdate<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions?: ModelWriteOptions): Promise<TRecordFor<T>>;
|
|
1298
|
+
protected static runUpdate<T extends typeof Model>(this: T, where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>, writeOptions: ModelWriteOptions | undefined, applyFillable: boolean): Promise<TRecordFor<T>>;
|
|
1299
|
+
/**
|
|
1300
|
+
* Delete records matching the conditions.
|
|
1301
|
+
*
|
|
1302
|
+
* @param where - Filter conditions to identify records
|
|
1303
|
+
* @returns Number of deleted records or void depending on adapter
|
|
1304
|
+
*
|
|
1305
|
+
* @example
|
|
1306
|
+
* await User.delete({ id: 1 })
|
|
1307
|
+
* await User.delete({ status: 'inactive' })
|
|
1308
|
+
*/
|
|
1309
|
+
static delete<T extends typeof Model>(this: T, where: WhereClauseFor<T>, writeOptions?: ModelWriteOptions): Promise<number | PlainObject | void>;
|
|
1310
|
+
/**
|
|
1311
|
+
* Get a raw Drizzle query builder for complex queries.
|
|
1312
|
+
*
|
|
1313
|
+
* @param db - Optional Drizzle database instance
|
|
1314
|
+
* @returns Drizzle query builder starting with `select().from(table)`
|
|
1315
|
+
*
|
|
1316
|
+
* @example
|
|
1317
|
+
* // Simple query
|
|
1318
|
+
* const users = await User.query(db)
|
|
1319
|
+
* .where(eq(users.status, 'active'))
|
|
1320
|
+
* .limit(10)
|
|
1321
|
+
*
|
|
1322
|
+
* // With joins
|
|
1323
|
+
* const usersWithPosts = await User.query(db)
|
|
1324
|
+
* .leftJoin(posts, eq(users.id, posts.userId))
|
|
1325
|
+
*/
|
|
1326
|
+
static query<TDatabase extends {
|
|
1327
|
+
select: (...args: any[]) => any;
|
|
1328
|
+
} = {
|
|
1329
|
+
select: (...args: any[]) => any;
|
|
1330
|
+
}>(
|
|
1331
|
+
// eslint-disable-line @typescript-eslint/no-explicit-any
|
|
1332
|
+
this: typeof Model, db?: TDatabase): SelectFrom<TDatabase>;
|
|
1333
|
+
/**
|
|
1334
|
+
* Eager-load relationships on records.
|
|
1335
|
+
*
|
|
1336
|
+
* @param relations - Relation name(s) to load
|
|
1337
|
+
* @param where - Optional filter conditions
|
|
1338
|
+
* @returns Records with loaded relationships
|
|
1339
|
+
*
|
|
1340
|
+
* @example
|
|
1341
|
+
* // Single relation
|
|
1342
|
+
* const usersWithPosts = await User.with('posts')
|
|
1343
|
+
*
|
|
1344
|
+
* // Multiple relations
|
|
1345
|
+
* const usersWithAll = await User.with(['posts', 'comments'])
|
|
1346
|
+
*
|
|
1347
|
+
* // With filtering
|
|
1348
|
+
* const activeUsersWithPosts = await User.with('posts', { status: 'active' })
|
|
1349
|
+
*
|
|
1350
|
+
* // Nested relations via dot notation
|
|
1351
|
+
* const usersWithPostComments = await User.with('posts.comments')
|
|
1352
|
+
*/
|
|
1353
|
+
static with<T extends typeof Model, K extends RelationPath<T>>(this: T, relations: K | readonly K[], where?: WhereClauseFor<T>, queryOptions?: ModelQueryOptions): Promise<Array<TRecordFor<T> & RelationTypePick<T, K | readonly K[]>>>;
|
|
1354
|
+
/**
|
|
1355
|
+
* Fetch records with related record counts attached as `${name}Count`.
|
|
1356
|
+
* Counts hasMany/hasOne/morphMany children per record without attaching
|
|
1357
|
+
* the related rows themselves; belongsTo yields 0 or 1.
|
|
1358
|
+
*
|
|
1359
|
+
* @example
|
|
1360
|
+
* const users = await User.withCount('posts')
|
|
1361
|
+
* users[0].postsCount // number
|
|
1362
|
+
*/
|
|
1363
|
+
static withCount<T extends typeof Model, K extends RelationKey<T>>(this: T, relations: K | readonly K[], where?: WhereClauseFor<T>, queryOptions?: ModelQueryOptions): Promise<Array<TRecordFor<T> & RelationCountPick<K | readonly K[]>>>;
|
|
1364
|
+
/** @internal Attaches a `${name}Count` field for one relation. */
|
|
1365
|
+
protected static loadRelationCountInto<T extends typeof Model>(this: T, records: Array<PlainObject>, relationName: string, queryOptions?: ModelQueryOptions): Promise<void>;
|
|
1366
|
+
/**
|
|
1367
|
+
* @internal Used by QueryBuilder for eager loading.
|
|
1368
|
+
* Supports nested paths with dot notation, e.g. `posts.comments`.
|
|
1369
|
+
*
|
|
1370
|
+
* Paths are grouped by their head segment so a relation shared by several
|
|
1371
|
+
* paths is loaded exactly once. Walking `posts.comments` and `posts.tags`
|
|
1372
|
+
* independently would load `posts` twice, and the loaders assign fresh
|
|
1373
|
+
* spread copies — so the second pass would replace the very row objects the
|
|
1374
|
+
* first had already attached children to, and only the last path would
|
|
1375
|
+
* survive.
|
|
1376
|
+
*/
|
|
1377
|
+
static loadRelationsInto<T extends typeof Model>(this: T, records: Array<PlainObject>, relationNames: readonly string[], queryOptions?: ModelQueryOptions, constraints?: EagerLoadConstraints, pathPrefix?: string): Promise<void>;
|
|
1378
|
+
/**
|
|
1379
|
+
* @internal Public since it is reachable from generated code; kept for
|
|
1380
|
+
* backwards compatibility now that the eager loader walks whole path lists.
|
|
1381
|
+
* Internal callers should use {@link loadRelationsInto} instead.
|
|
1382
|
+
*/
|
|
1383
|
+
static loadRelationInto<T extends typeof Model>(this: T, records: Array<PlainObject>, relationName: string, queryOptions?: ModelQueryOptions, constraints?: EagerLoadConstraints, pathPrefix?: string): Promise<void>;
|
|
1384
|
+
/**
|
|
1385
|
+
* Load one relation level and recurse into the tails beneath it.
|
|
1386
|
+
*
|
|
1387
|
+
* `constraints` are keyed by the full path of the level each one constrains,
|
|
1388
|
+
* so `pathPrefix` accumulates the path walked so far as the recursion
|
|
1389
|
+
* descends: at `posts.comments` the leaf looks itself up under that whole
|
|
1390
|
+
* key, not under `comments`.
|
|
1391
|
+
*/
|
|
1392
|
+
protected static loadRelationLevel<T extends typeof Model>(this: T, records: Array<PlainObject>, head: string, tails: readonly string[], queryOptions?: ModelQueryOptions, constraints?: EagerLoadConstraints, pathPrefix?: string): Promise<void>;
|
|
1393
|
+
protected static loadHasMany(records: Array<PlainObject>, definition: HasManyRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1394
|
+
protected static loadHasOne(records: Array<PlainObject>, definition: HasOneRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1395
|
+
protected static loadBelongsTo(records: Array<PlainObject>, definition: BelongsToRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1396
|
+
protected static loadBelongsToMany(records: Array<PlainObject>, definition: BelongsToManyRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1397
|
+
protected static loadHasManyThrough(records: Array<PlainObject>, definition: HasManyThroughRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1398
|
+
protected static loadMorphMany(records: Array<PlainObject>, definition: MorphManyRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1399
|
+
protected static loadMorphTo(records: Array<PlainObject>, definition: MorphToRelationDefinition, queryOptions?: ModelQueryOptions, constraint?: EagerLoadConstraint): Promise<void>;
|
|
1399
1400
|
}
|
|
1400
1401
|
type TRecordFor<T extends typeof Model> = T extends {
|
|
1401
|
-
|
|
1402
|
+
recordType: infer R;
|
|
1402
1403
|
} ? R extends PlainObject ? R : PlainObject : PlainObject;
|
|
1403
1404
|
type TCreateFor<T extends typeof Model> = T extends {
|
|
1404
|
-
|
|
1405
|
+
createType: infer R;
|
|
1405
1406
|
} ? R extends PlainObject ? R : PlainObject : PlainObject;
|
|
1406
1407
|
type WhereClauseFor<T extends typeof Model> = WhereClause<TRecordFor<T>>;
|
|
1407
1408
|
type FieldFor<T extends typeof Model> = keyof TRecordFor<T> & string;
|
|
1408
1409
|
type RelationPath<T extends typeof Model> = RelationKey<T> | `${RelationKey<T>}.${string}`;
|
|
1409
1410
|
type RelationHead<Name> = Name extends `${infer Head}.${string}` ? Head : Name;
|
|
1410
1411
|
type RelationTypesFor<T extends typeof Model> = T extends {
|
|
1411
|
-
|
|
1412
|
+
relationTypes: infer R;
|
|
1412
1413
|
} ? R extends RelationShape ? R : {} : {};
|
|
1413
1414
|
type RelationKey<T extends typeof Model> = keyof RelationTypesFor<T> & string;
|
|
1414
1415
|
type RelationKeyOrString<T extends typeof Model> = RelationKey<T> extends never ? string : RelationKey<T>;
|
|
1415
1416
|
type RelationNameUnion<Names> = Names extends readonly (infer Items)[] ? Items : Names;
|
|
1416
|
-
type RelationTypePick<T extends typeof Model, Names> = {
|
|
1417
|
-
|
|
1418
|
-
};
|
|
1419
|
-
type RelationCountPick<Names> = {
|
|
1420
|
-
[K in RelationNameUnion<Names> & string as `${K}Count`]: number;
|
|
1421
|
-
};
|
|
1417
|
+
type RelationTypePick<T extends typeof Model, Names> = { [K in RelationHead<RelationNameUnion<Names>> & string & keyof RelationTypesFor<T>]: RelationTypesFor<T>[K]; };
|
|
1418
|
+
type RelationCountPick<Names> = { [K in RelationNameUnion<Names> & string as `${K}Count`]: number; };
|
|
1422
1419
|
interface TransactionModelScope<T extends typeof Model> {
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1420
|
+
readonly trx: TransactionHandle;
|
|
1421
|
+
all(): Promise<TRecordFor<T>[]>;
|
|
1422
|
+
find(id: unknown): Promise<TRecordFor<T> | null>;
|
|
1423
|
+
findOrFail(id: unknown): Promise<TRecordFor<T>>;
|
|
1424
|
+
first(where?: WhereClauseFor<T>): Promise<TRecordFor<T> | null>;
|
|
1425
|
+
where(callback: WhereGroupCallback<TRecordFor<T>>): QueryBuilder<TRecordFor<T>>;
|
|
1426
|
+
where(conditions: WhereClauseFor<T>): QueryBuilder<TRecordFor<T>>;
|
|
1427
|
+
where(field: FieldFor<T>, value: unknown): QueryBuilder<TRecordFor<T>>;
|
|
1428
|
+
where(field: FieldFor<T>, operator: WhereOperator, value: unknown): QueryBuilder<TRecordFor<T>>;
|
|
1429
|
+
newQuery(): QueryBuilder<TRecordFor<T>>;
|
|
1430
|
+
create(data: TCreateFor<T>): Promise<TRecordFor<T>>;
|
|
1431
|
+
update(where: WhereClauseFor<T>, data: Partial<TCreateFor<T>>): Promise<TRecordFor<T>>;
|
|
1432
|
+
delete(where: WhereClauseFor<T>): Promise<number | PlainObject | void>;
|
|
1433
|
+
paginate(options?: PaginateOptions<TRecordFor<T>>): Promise<PaginatedResult<TRecordFor<T>>>;
|
|
1437
1434
|
}
|
|
1438
1435
|
interface BaseRelationDefinition {
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1436
|
+
type: 'hasMany' | 'hasOne' | 'belongsTo' | 'belongsToMany' | 'hasManyThrough' | 'morphMany' | 'morphTo';
|
|
1437
|
+
name: string;
|
|
1438
|
+
related: typeof Model | (() => typeof Model | Promise<typeof Model>);
|
|
1442
1439
|
}
|
|
1443
1440
|
interface HasManyRelationDefinition extends BaseRelationDefinition {
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1441
|
+
type: 'hasMany';
|
|
1442
|
+
foreignKey: string;
|
|
1443
|
+
localKey: string;
|
|
1447
1444
|
}
|
|
1448
1445
|
interface HasOneRelationDefinition extends BaseRelationDefinition {
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1446
|
+
type: 'hasOne';
|
|
1447
|
+
foreignKey: string;
|
|
1448
|
+
localKey: string;
|
|
1452
1449
|
}
|
|
1453
1450
|
interface BelongsToRelationDefinition extends BaseRelationDefinition {
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1451
|
+
type: 'belongsTo';
|
|
1452
|
+
foreignKey: string;
|
|
1453
|
+
ownerKey: string;
|
|
1457
1454
|
}
|
|
1458
1455
|
interface BelongsToManyRelationDefinition extends BaseRelationDefinition {
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1456
|
+
type: 'belongsToMany';
|
|
1457
|
+
pivotTable: unknown;
|
|
1458
|
+
foreignPivotKey: string;
|
|
1459
|
+
relatedPivotKey: string;
|
|
1460
|
+
parentKey: string;
|
|
1461
|
+
relatedKey: string;
|
|
1465
1462
|
}
|
|
1466
1463
|
interface HasManyThroughRelationDefinition extends BaseRelationDefinition {
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1464
|
+
type: 'hasManyThrough';
|
|
1465
|
+
through: typeof Model | (() => typeof Model | Promise<typeof Model>);
|
|
1466
|
+
firstKey: string;
|
|
1467
|
+
secondKey: string;
|
|
1468
|
+
localKey: string;
|
|
1469
|
+
secondLocalKey: string;
|
|
1473
1470
|
}
|
|
1474
1471
|
type RelationDefinition = HasManyRelationDefinition | HasOneRelationDefinition | BelongsToRelationDefinition | BelongsToManyRelationDefinition | HasManyThroughRelationDefinition | MorphManyRelationDefinition | MorphToRelationDefinition;
|
|
1475
1472
|
interface MorphManyRelationDefinition extends BaseRelationDefinition {
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1473
|
+
type: 'morphMany';
|
|
1474
|
+
morphName: string;
|
|
1475
|
+
localKey: string;
|
|
1479
1476
|
}
|
|
1480
1477
|
interface MorphToRelationDefinition {
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1478
|
+
type: 'morphTo';
|
|
1479
|
+
name: string;
|
|
1480
|
+
related: undefined;
|
|
1481
|
+
morphName: string;
|
|
1485
1482
|
}
|
|
1486
1483
|
/** Type for hasMany relation results (array of related records). */
|
|
1487
1484
|
type HasManyRelationResult<T extends typeof Model> = Array<TRecordFor<T>>;
|
|
@@ -1523,9 +1520,9 @@ type MorphToRecord = PlainObject | null;
|
|
|
1523
1520
|
/** Utility type: model record with specified relations merged. */
|
|
1524
1521
|
type WithRelations<T extends typeof Model, K extends RelationPath<T> | readonly RelationPath<T>[]> = TRecordFor<T> & RelationTypePick<T, K>;
|
|
1525
1522
|
type ModelClassWithTable<TTable extends TableShape, TBase extends typeof Model, TCreate extends PlainObject> = TBase & {
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1523
|
+
readonly table: TTable;
|
|
1524
|
+
readonly recordType: InferModelRecord<TTable>;
|
|
1525
|
+
readonly createType: TCreate;
|
|
1529
1526
|
};
|
|
1530
1527
|
/** A key that can appear in a model's create payload: a table column, or a named field its base contributes. */
|
|
1531
1528
|
type CreateKey<TTable extends TableShape, TBase extends typeof Model> = keyof InferModelInsert<TTable> | keyof TCreateFor<TBase>;
|
|
@@ -1547,9 +1544,7 @@ type RecordKey<TTable extends TableShape> = keyof InferModelRecord<TTable> & str
|
|
|
1547
1544
|
* here: the accessor functions are context-sensitive, which defers inference
|
|
1548
1545
|
* past the point where the key type parameter falls back to its default.
|
|
1549
1546
|
*/
|
|
1550
|
-
type AccessorsShape<TTable extends TableShape, TKey extends string> = {
|
|
1551
|
-
[K in TKey]: (record: InferModelRecord<TTable>) => unknown;
|
|
1552
|
-
};
|
|
1547
|
+
type AccessorsShape<TTable extends TableShape, TKey extends string> = { [K in TKey]: (record: InferModelRecord<TTable>) => unknown; };
|
|
1553
1548
|
type CreateShape<TTable extends TableShape, TBase extends typeof Model, TOptional extends keyof InferModelInsert<TTable>, TRequire extends CreateKey<TTable, TBase>> = Partial<Pick<InferModelInsert<TTable>, TOptional>> & Omit<InferModelInsert<TTable>, TOptional> & TCreateFor<TBase> & Required<Pick<InferModelInsert<TTable> & TCreateFor<TBase>, TRequire & keyof (InferModelInsert<TTable> & TCreateFor<TBase>)>>;
|
|
1554
1549
|
/**
|
|
1555
1550
|
* Create a table-backed model base class from a Drizzle table.
|
|
@@ -1582,48 +1577,49 @@ type CreateShape<TTable extends TableShape, TBase extends typeof Model, TOptiona
|
|
|
1582
1577
|
* }) {}
|
|
1583
1578
|
*/
|
|
1584
1579
|
declare function defineModel<TTable extends TableShape, TBase extends typeof Model = typeof Model, const TOptional extends keyof InferModelInsert<TTable> = never, const TRequire extends CreateKey<TTable, TBase> = never, TAccessorKey extends string = never>(table: TTable, options?: {
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1580
|
+
base?: TBase;
|
|
1581
|
+
/**
|
|
1582
|
+
* Columns the model fills in itself, so callers need not pass them.
|
|
1583
|
+
* Type-level only.
|
|
1584
|
+
*/
|
|
1585
|
+
optionalOnCreate?: readonly TOptional[];
|
|
1586
|
+
/**
|
|
1587
|
+
* Fields to make required on the create payload. Accepts table columns
|
|
1588
|
+
* (Drizzle marks defaulted ones optional) and named fields contributed by
|
|
1589
|
+
* `base`, such as `AuthenticatableModel`'s virtual `password`.
|
|
1590
|
+
* Type-level only.
|
|
1591
|
+
*/
|
|
1592
|
+
requireOnCreate?: readonly TRequire[];
|
|
1593
|
+
/**
|
|
1594
|
+
* Typed form of `static fillable`: mass-assignment allowlist checked
|
|
1595
|
+
* against insertable columns and base-contributed fields.
|
|
1596
|
+
*/
|
|
1597
|
+
fillable?: readonly FillableKey<TTable, TBase>[];
|
|
1598
|
+
/**
|
|
1599
|
+
* Typed form of `static hidden`: fields excluded from serialization,
|
|
1600
|
+
* checked against record columns and declared accessors.
|
|
1601
|
+
*/
|
|
1602
|
+
hidden?: readonly (RecordKey<TTable> | NoInfer<TAccessorKey>)[];
|
|
1603
|
+
/**
|
|
1604
|
+
* Typed form of `static visible`: serialization allowlist, checked
|
|
1605
|
+
* against record columns and declared accessors.
|
|
1606
|
+
*/
|
|
1607
|
+
visible?: readonly (RecordKey<TTable> | NoInfer<TAccessorKey>)[];
|
|
1608
|
+
/**
|
|
1609
|
+
* Typed form of `static accessors`: each function receives the table's
|
|
1610
|
+
* inferred record. Names declared here are what `appends` may reference.
|
|
1611
|
+
* The Record intersection rejects non-object values — with no keys to
|
|
1612
|
+
* infer, the mapped type alone collapses to `{}`, which admits anything.
|
|
1613
|
+
*/
|
|
1614
|
+
accessors?: AccessorsShape<TTable, TAccessorKey> & Record<string, (record: InferModelRecord<TTable>) => unknown>;
|
|
1615
|
+
/**
|
|
1616
|
+
* Typed form of `static appends`: virtual attributes to serialize,
|
|
1617
|
+
* checked against the names declared in `accessors`.
|
|
1618
|
+
*/
|
|
1619
|
+
appends?: readonly NoInfer<TAccessorKey>[];
|
|
1625
1620
|
}): ModelClassWithTable<TTable, TBase, CreateShape<TTable, TBase, TOptional, TRequire>>;
|
|
1626
|
-
|
|
1621
|
+
//#endregion
|
|
1622
|
+
//#region src/ModelNotFoundException.d.ts
|
|
1627
1623
|
/**
|
|
1628
1624
|
* Exception thrown when a model is not found via findOrFail or firstOrFail.
|
|
1629
1625
|
*
|
|
@@ -1631,12 +1627,13 @@ declare function defineModel<TTable extends TableShape, TBase extends typeof Mod
|
|
|
1631
1627
|
* can render it as a proper HTTP 404 without coupling to the ORM package.
|
|
1632
1628
|
*/
|
|
1633
1629
|
declare class ModelNotFoundException extends Error {
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1630
|
+
readonly statusCode = 404;
|
|
1631
|
+
readonly modelName: string;
|
|
1632
|
+
readonly modelId: unknown;
|
|
1633
|
+
constructor(modelName: string, id?: unknown, key?: string);
|
|
1638
1634
|
}
|
|
1639
|
-
|
|
1635
|
+
//#endregion
|
|
1636
|
+
//#region src/MassAssignmentException.d.ts
|
|
1640
1637
|
/**
|
|
1641
1638
|
* Thrown when mass assignment receives fields it must not write. Silently
|
|
1642
1639
|
* discarding them hides bugs (a typo'd fillable entry surfaces as a NOT NULL
|
|
@@ -1650,28 +1647,29 @@ declare class ModelNotFoundException extends Error {
|
|
|
1650
1647
|
* mass-assigned, `fillable` notwithstanding.
|
|
1651
1648
|
*/
|
|
1652
1649
|
declare class MassAssignmentException extends Error {
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1650
|
+
readonly model: string;
|
|
1651
|
+
readonly fields: string[];
|
|
1652
|
+
readonly reason: 'not-fillable' | 'denied';
|
|
1653
|
+
constructor(model: string, fields: string[], options?: {
|
|
1654
|
+
reason?: 'not-fillable' | 'denied';
|
|
1655
|
+
});
|
|
1659
1656
|
}
|
|
1660
|
-
|
|
1657
|
+
//#endregion
|
|
1658
|
+
//#region src/SoftDeletes.d.ts
|
|
1661
1659
|
/**
|
|
1662
1660
|
* Interface describing the static methods added by the SoftDeletes mixin.
|
|
1663
1661
|
*/
|
|
1664
1662
|
interface SoftDeletesStatic {
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1663
|
+
/** Column name used for tracking soft deletion timestamp. */
|
|
1664
|
+
deletedAtColumn: string;
|
|
1665
|
+
/** Start a query that includes soft-deleted records. */
|
|
1666
|
+
withTrashed(): QueryBuilder<PlainObject>;
|
|
1667
|
+
/** Start a query that returns only soft-deleted records. */
|
|
1668
|
+
onlyTrashed(): QueryBuilder<PlainObject>;
|
|
1669
|
+
/** Restore soft-deleted records by setting deletedAt back to null. */
|
|
1670
|
+
restore(where: Partial<Record<string, unknown>>): Promise<PlainObject>;
|
|
1671
|
+
/** Permanently delete records from the database, bypassing soft delete. */
|
|
1672
|
+
forceDelete(where: Partial<Record<string, unknown>>): Promise<number | PlainObject | void>;
|
|
1675
1673
|
}
|
|
1676
1674
|
/**
|
|
1677
1675
|
* Mixin that adds soft delete behavior to a Model.
|
|
@@ -1702,47 +1700,49 @@ interface SoftDeletesStatic {
|
|
|
1702
1700
|
* await Post.forceDelete({ id: 1 })
|
|
1703
1701
|
*/
|
|
1704
1702
|
declare function SoftDeletes<TBase extends typeof Model>(Base: TBase): TBase & SoftDeletesStatic;
|
|
1705
|
-
|
|
1703
|
+
//#endregion
|
|
1704
|
+
//#region src/adapters/drizzle-adapter.d.ts
|
|
1706
1705
|
type DrizzleLikeSelect = {
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1706
|
+
where?: (clause: unknown) => DrizzleLikeSelect;
|
|
1707
|
+
orderBy?: (...clauses: unknown[]) => DrizzleLikeSelect;
|
|
1708
|
+
limit?: (value: number) => DrizzleLikeSelect;
|
|
1709
|
+
offset?: (value: number) => DrizzleLikeSelect;
|
|
1710
|
+
all?: () => Promise<unknown[]>;
|
|
1711
|
+
get?: () => Promise<unknown>;
|
|
1713
1712
|
};
|
|
1714
1713
|
type DrizzleSelectBuilder = DrizzleLikeSelect & {
|
|
1715
|
-
|
|
1714
|
+
from(table: unknown): DrizzleLikeSelect;
|
|
1716
1715
|
};
|
|
1717
1716
|
type DrizzleLikeInsert = {
|
|
1718
|
-
|
|
1717
|
+
values: (record: PlainObject) => DrizzleLikeInsertResult;
|
|
1719
1718
|
};
|
|
1720
1719
|
type DrizzleLikeInsertResult = {
|
|
1721
|
-
|
|
1722
|
-
|
|
1720
|
+
returning?: () => Promise<unknown[]>;
|
|
1721
|
+
run?: () => Promise<unknown>;
|
|
1723
1722
|
};
|
|
1724
1723
|
type DrizzleLikeUpdate = {
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1724
|
+
set: (record: PlainObject) => DrizzleLikeUpdate;
|
|
1725
|
+
where: (clause: unknown) => DrizzleLikeUpdate;
|
|
1726
|
+
returning?: () => Promise<unknown[]>;
|
|
1728
1727
|
};
|
|
1729
1728
|
type DrizzleLikeDelete = {
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1729
|
+
where: (clause: unknown) => DrizzleLikeDelete;
|
|
1730
|
+
returning?: () => Promise<unknown[]>;
|
|
1731
|
+
run?: () => Promise<unknown>;
|
|
1733
1732
|
};
|
|
1734
1733
|
type DrizzleDatabase = {
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1734
|
+
select(selection?: Record<string, unknown>): DrizzleSelectBuilder;
|
|
1735
|
+
insert(table: unknown): DrizzleLikeInsert;
|
|
1736
|
+
update?(table: unknown): DrizzleLikeUpdate;
|
|
1737
|
+
delete?(table: unknown): DrizzleLikeDelete;
|
|
1738
|
+
transaction?<TResult>(callback: (trx: unknown) => Promise<TResult>): Promise<TResult>;
|
|
1740
1739
|
};
|
|
1741
1740
|
declare const DrizzleAdapter: ORMAdapterAdvanced & {
|
|
1742
|
-
|
|
1743
|
-
|
|
1741
|
+
configure(db: DrizzleDatabase): void;
|
|
1742
|
+
getDatabase<TDatabase extends DrizzleDatabase = DrizzleDatabase>(): TDatabase;
|
|
1744
1743
|
};
|
|
1745
|
-
|
|
1744
|
+
//#endregion
|
|
1745
|
+
//#region src/adapters/drizzle-conditions.d.ts
|
|
1746
1746
|
/**
|
|
1747
1747
|
* Translates an array of QueryBuilder WhereConditions into a single
|
|
1748
1748
|
* Drizzle ORM condition expression.
|
|
@@ -1759,7 +1759,8 @@ declare const DrizzleAdapter: ORMAdapterAdvanced & {
|
|
|
1759
1759
|
* @returns A Drizzle SQL condition or undefined if no conditions
|
|
1760
1760
|
*/
|
|
1761
1761
|
declare function buildDrizzleConditions(table: unknown, conditions: WhereCondition[]): SQL | undefined;
|
|
1762
|
-
|
|
1762
|
+
//#endregion
|
|
1763
|
+
//#region src/migration-utils.d.ts
|
|
1763
1764
|
/**
|
|
1764
1765
|
* What one `migrateDatabase()` call had to work with. `db:migrate` reports
|
|
1765
1766
|
* success off this rather than off the call returning: a folder with nothing
|
|
@@ -1772,24 +1773,25 @@ declare function buildDrizzleConditions(table: unknown, conditions: WhereConditi
|
|
|
1772
1773
|
* behaviour, faithfully reported.
|
|
1773
1774
|
*/
|
|
1774
1775
|
interface MigrationRunSummary {
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1776
|
+
/** The folder the migrator read, absolute as the driver resolved it. */
|
|
1777
|
+
migrationsFolder: string;
|
|
1778
|
+
/** drizzle-kit migrations found there (one folder each, holding migration.sql). */
|
|
1779
|
+
migrationsFound: number;
|
|
1780
|
+
/**
|
|
1781
|
+
* Loose .sql files in that folder, which the drizzle migrator never runs.
|
|
1782
|
+
* Non-zero alongside `migrationsFound: 0` means the folder is not empty but
|
|
1783
|
+
* holds nothing to apply, which is a different problem from having generated
|
|
1784
|
+
* no migrations yet.
|
|
1785
|
+
*/
|
|
1786
|
+
looseSqlFiles: number;
|
|
1786
1787
|
}
|
|
1787
1788
|
interface MigrationStatusEntry {
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1789
|
+
name: string;
|
|
1790
|
+
applied: boolean;
|
|
1791
|
+
appliedAt: Date | null;
|
|
1791
1792
|
}
|
|
1792
|
-
|
|
1793
|
+
//#endregion
|
|
1794
|
+
//#region src/seeder.d.ts
|
|
1793
1795
|
/**
|
|
1794
1796
|
* The context a seeder receives. `db` is the drizzle database the app
|
|
1795
1797
|
* configured, so its type depends on the dialect: annotate the seeder with the
|
|
@@ -1800,7 +1802,7 @@ interface MigrationStatusEntry {
|
|
|
1800
1802
|
* compatibility — seeders written before this was generic keep compiling.
|
|
1801
1803
|
*/
|
|
1802
1804
|
interface SeederContext<TDatabase = PostgresJsDatabase> {
|
|
1803
|
-
|
|
1805
|
+
db: TDatabase;
|
|
1804
1806
|
}
|
|
1805
1807
|
type SeederHandler<TDatabase = PostgresJsDatabase> = (context: SeederContext<TDatabase>) => unknown;
|
|
1806
1808
|
/** Seeder context for apps created with `createPostgresDatabase()`. */
|
|
@@ -1810,7 +1812,7 @@ type MySqlSeederContext = SeederContext<MySql2Database>;
|
|
|
1810
1812
|
/** Seeder context for apps created with `createSqliteDatabase()`. */
|
|
1811
1813
|
type SqliteSeederContext = SeederContext<SQLiteBunDatabase>;
|
|
1812
1814
|
/** Seeder context for apps created with `createAwsDataApiDatabase()`. */
|
|
1813
|
-
type AwsDataApiSeederContext = SeederContext<AwsDataApiPgDatabase
|
|
1815
|
+
type AwsDataApiSeederContext = SeederContext<AwsDataApiPgDatabase>;
|
|
1814
1816
|
/**
|
|
1815
1817
|
* What one `runSeeders()` call had to work with. `db:seed` reports success off
|
|
1816
1818
|
* this rather than off the call returning: a folder with no seeders in it seeds
|
|
@@ -1818,17 +1820,17 @@ type AwsDataApiSeederContext = SeederContext<AwsDataApiPgDatabase$1>;
|
|
|
1818
1820
|
* rows the seeders would have written.
|
|
1819
1821
|
*/
|
|
1820
1822
|
interface SeederRunSummary {
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1823
|
+
/** The folder that was read, absolute as the driver resolved it. */
|
|
1824
|
+
seedersFolder: string;
|
|
1825
|
+
/** How many seeders ran. They run in the order their files sorted. */
|
|
1826
|
+
seedersRan: number;
|
|
1827
|
+
/**
|
|
1828
|
+
* Files with a seeder extension that exported nothing runnable, so they were
|
|
1829
|
+
* skipped. Non-zero alongside `seedersRan: 0` means the folder is not empty
|
|
1830
|
+
* but holds nothing to run — a different problem from having written no
|
|
1831
|
+
* seeder yet, and one `make:seeder` would not solve.
|
|
1832
|
+
*/
|
|
1833
|
+
filesWithoutSeeder: number;
|
|
1832
1834
|
}
|
|
1833
1835
|
/**
|
|
1834
1836
|
* Seeders are loaded as modules, so the dialect they were written against is
|
|
@@ -1839,129 +1841,133 @@ declare function loadSeeders<TDatabase = PostgresJsDatabase>(directory: string |
|
|
|
1839
1841
|
/** Runs every seeder in `directory` and reports what the folder held. */
|
|
1840
1842
|
declare function runSeeders<TDatabase>(db: TDatabase, directory: string | URL): Promise<SeederRunSummary>;
|
|
1841
1843
|
declare function defineSeeder<TDatabase = PostgresJsDatabase>(handler: SeederHandler<TDatabase>): SeederHandler<TDatabase>;
|
|
1842
|
-
|
|
1844
|
+
//#endregion
|
|
1845
|
+
//#region src/postgres.d.ts
|
|
1843
1846
|
type ConnectionResolver$3 = string | (() => string | undefined);
|
|
1844
1847
|
interface PostgresDatabaseOptions {
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1848
|
+
migrationsFolder: string | URL;
|
|
1849
|
+
connectionString?: ConnectionResolver$3;
|
|
1850
|
+
clientOptions?: postgres.Options<Record<string, never>>;
|
|
1851
|
+
seedersFolder?: string | URL;
|
|
1852
|
+
/**
|
|
1853
|
+
* Drizzle relations for RQB v2 (`db.query.*`).
|
|
1854
|
+
* Build with `defineRelations(schema, ...)` from `drizzle-orm`,
|
|
1855
|
+
* or with `relations()` from `drizzle-orm/_relations` for the RQB v1 partial-upgrade path.
|
|
1856
|
+
*/
|
|
1857
|
+
relations?: Record<string, unknown>;
|
|
1855
1858
|
}
|
|
1856
1859
|
interface PostgresDatabase {
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1860
|
+
getDatabase(): Promise<PostgresJsDatabase>;
|
|
1861
|
+
/** Applies pending drizzle-kit migrations and reports what the folder held. */
|
|
1862
|
+
migrateDatabase(): Promise<MigrationRunSummary>;
|
|
1863
|
+
closeDatabase(): Promise<void>;
|
|
1864
|
+
configureOrm(): Promise<void>;
|
|
1865
|
+
/** Runs every seeder in the configured folder and reports what it held. */
|
|
1866
|
+
seedDatabase(): Promise<SeederRunSummary>;
|
|
1867
|
+
/** Drops the public schema (and the drizzle tracker schema), then re-applies migrations — same end state as `guren db:reset`. */
|
|
1868
|
+
resetDatabase(): Promise<MigrationRunSummary>;
|
|
1869
|
+
/** Per-migration applied state derived from the drizzle-kit journal and drizzle.__drizzle_migrations. */
|
|
1870
|
+
migrationStatus(): Promise<MigrationStatusEntry[]>;
|
|
1868
1871
|
}
|
|
1869
1872
|
declare function createPostgresDatabase(options: PostgresDatabaseOptions): PostgresDatabase;
|
|
1870
|
-
|
|
1873
|
+
//#endregion
|
|
1874
|
+
//#region src/mysql.d.ts
|
|
1871
1875
|
type ConnectionResolver$2 = string | (() => string | undefined);
|
|
1872
1876
|
type MySqlConnectionOptions = Record<string, unknown>;
|
|
1873
1877
|
interface MySqlDatabaseOptions {
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1878
|
+
migrationsFolder: string | URL;
|
|
1879
|
+
connectionString?: ConnectionResolver$2;
|
|
1880
|
+
clientOptions?: MySqlConnectionOptions;
|
|
1881
|
+
seedersFolder?: string | URL;
|
|
1882
|
+
/**
|
|
1883
|
+
* Drizzle relations for RQB v2 (`db.query.*`).
|
|
1884
|
+
* Build with `defineRelations(schema, ...)` from `drizzle-orm`,
|
|
1885
|
+
* or with `relations()` from `drizzle-orm/_relations` for the RQB v1 partial-upgrade path.
|
|
1886
|
+
*/
|
|
1887
|
+
relations?: Record<string, unknown>;
|
|
1884
1888
|
}
|
|
1885
1889
|
interface MySqlDatabase {
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1890
|
+
getDatabase(): Promise<MySql2Database>;
|
|
1891
|
+
/** Applies pending drizzle-kit migrations and reports what the folder held. */
|
|
1892
|
+
migrateDatabase(): Promise<MigrationRunSummary>;
|
|
1893
|
+
closeDatabase(): Promise<void>;
|
|
1894
|
+
configureOrm(): Promise<void>;
|
|
1895
|
+
/** Runs every seeder in the configured folder and reports what it held. */
|
|
1896
|
+
seedDatabase(): Promise<SeederRunSummary>;
|
|
1897
|
+
/** Drops every table and view (including the drizzle migration tracker), then re-applies migrations — same end state as `guren db:reset`. */
|
|
1898
|
+
resetDatabase(): Promise<MigrationRunSummary>;
|
|
1899
|
+
/** Per-migration applied state derived from the drizzle-kit journal and the __drizzle_migrations table. */
|
|
1900
|
+
migrationStatus(): Promise<MigrationStatusEntry[]>;
|
|
1897
1901
|
}
|
|
1898
1902
|
declare function createMySqlDatabase(options: MySqlDatabaseOptions): MySqlDatabase;
|
|
1899
|
-
|
|
1903
|
+
//#endregion
|
|
1904
|
+
//#region src/sqlite.d.ts
|
|
1900
1905
|
type ConnectionResolver$1 = string | (() => string | undefined);
|
|
1901
1906
|
interface SqliteDatabaseOptions {
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1907
|
+
migrationsFolder: string | URL;
|
|
1908
|
+
/** Path to the SQLite database file. Defaults to `./data/guren.db`. */
|
|
1909
|
+
filename?: ConnectionResolver$1;
|
|
1910
|
+
seedersFolder?: string | URL;
|
|
1911
|
+
/**
|
|
1912
|
+
* Drizzle relations for RQB v2 (`db.query.*`).
|
|
1913
|
+
* Build with `defineRelations(schema, ...)` from `drizzle-orm`,
|
|
1914
|
+
* or with `relations()` from `drizzle-orm/_relations` for the RQB v1 partial-upgrade path.
|
|
1915
|
+
*/
|
|
1916
|
+
relations?: Record<string, unknown>;
|
|
1912
1917
|
}
|
|
1913
1918
|
interface SqliteDatabase {
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1919
|
+
getDatabase(): Promise<unknown>;
|
|
1920
|
+
/** Applies pending drizzle-kit migrations and reports what the folder held. */
|
|
1921
|
+
migrateDatabase(): Promise<MigrationRunSummary>;
|
|
1922
|
+
closeDatabase(): Promise<void>;
|
|
1923
|
+
configureOrm(): Promise<void>;
|
|
1924
|
+
/** Runs every seeder in the configured folder and reports what it held. */
|
|
1925
|
+
seedDatabase(): Promise<SeederRunSummary>;
|
|
1926
|
+
/**
|
|
1927
|
+
* Drops every table and view (including the drizzle migration tracker), then
|
|
1928
|
+
* re-applies migrations — same end state as `guren db:reset`. Resolves
|
|
1929
|
+
* undefined only when a concurrent `closeDatabase()` left nothing to drop, so
|
|
1930
|
+
* there was no migration run to report.
|
|
1931
|
+
*/
|
|
1932
|
+
resetDatabase(): Promise<MigrationRunSummary | undefined>;
|
|
1933
|
+
/** Per-migration applied state derived from the drizzle-kit journal and the __drizzle_migrations table. */
|
|
1934
|
+
migrationStatus(): Promise<MigrationStatusEntry[]>;
|
|
1930
1935
|
}
|
|
1931
1936
|
declare function createSqliteDatabase(options: SqliteDatabaseOptions): SqliteDatabase;
|
|
1932
|
-
|
|
1937
|
+
//#endregion
|
|
1938
|
+
//#region src/d1.d.ts
|
|
1933
1939
|
interface D1DatabaseOptions {
|
|
1934
|
-
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
1940
|
+
/**
|
|
1941
|
+
* Resolver returning the Cloudflare D1 binding. Bindings arrive with the
|
|
1942
|
+
* first request on Workers, so this must be a deferred closure, e.g.
|
|
1943
|
+
* `binding: () => getWorkersEnv<Env>().DB`.
|
|
1944
|
+
*/
|
|
1945
|
+
binding: () => unknown;
|
|
1946
|
+
/**
|
|
1947
|
+
* Drizzle-kit migrations folder. D1 migrations are applied out-of-band via
|
|
1948
|
+
* `wrangler d1 migrations apply` (point `d1_databases[].migrations_dir` at
|
|
1949
|
+
* this folder); the factory only references it in error guidance.
|
|
1950
|
+
*/
|
|
1951
|
+
migrationsFolder?: string | URL;
|
|
1952
|
+
/**
|
|
1953
|
+
* Drizzle relations for RQB v2 (`db.query.*`).
|
|
1954
|
+
* Build with `defineRelations(schema, ...)` from `drizzle-orm`,
|
|
1955
|
+
* or with `relations()` from `drizzle-orm/_relations` for the RQB v1 partial-upgrade path.
|
|
1956
|
+
*/
|
|
1957
|
+
relations?: Record<string, unknown>;
|
|
1952
1958
|
}
|
|
1953
1959
|
interface D1DatabaseHandle {
|
|
1954
|
-
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
|
|
1961
|
-
|
|
1962
|
-
|
|
1963
|
-
|
|
1964
|
-
|
|
1960
|
+
getDatabase(): Promise<unknown>;
|
|
1961
|
+
/** Always throws: D1 migrations are applied with `wrangler d1 migrations apply`, never at runtime. */
|
|
1962
|
+
migrateDatabase(): Promise<never>;
|
|
1963
|
+
closeDatabase(): Promise<void>;
|
|
1964
|
+
configureOrm(): Promise<void>;
|
|
1965
|
+
/** Always throws: seed with SQL through `wrangler d1 execute`. */
|
|
1966
|
+
seedDatabase(): Promise<never>;
|
|
1967
|
+
/** Always throws: recreate the database with wrangler instead. */
|
|
1968
|
+
resetDatabase(): Promise<never>;
|
|
1969
|
+
/** Always throws: wrangler's tracker is authoritative — use `wrangler d1 migrations list`. */
|
|
1970
|
+
migrationStatus(): Promise<never>;
|
|
1965
1971
|
}
|
|
1966
1972
|
/**
|
|
1967
1973
|
* Cloudflare D1 database factory. D1 speaks the SQLite dialect, so schemas
|
|
@@ -1969,62 +1975,64 @@ interface D1DatabaseHandle {
|
|
|
1969
1975
|
* the migration workflow differ (wrangler owns migrations, see RFC 0003).
|
|
1970
1976
|
*/
|
|
1971
1977
|
declare function createD1Database(options: D1DatabaseOptions): D1DatabaseHandle;
|
|
1972
|
-
|
|
1978
|
+
//#endregion
|
|
1979
|
+
//#region src/aws-data-api.d.ts
|
|
1973
1980
|
type ConnectionResolver = string | (() => string | undefined);
|
|
1974
|
-
type AwsDataApiPgDatabase =
|
|
1981
|
+
type AwsDataApiPgDatabase$1 = import('drizzle-orm/aws-data-api/pg').AwsDataApiPgDatabase;
|
|
1975
1982
|
interface AwsDataApiDatabaseOptions {
|
|
1976
|
-
|
|
1977
|
-
|
|
1978
|
-
|
|
1979
|
-
|
|
1980
|
-
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
1983
|
+
migrationsFolder: string | URL;
|
|
1984
|
+
/** Database name. Falls back to the DATABASE_NAME environment variable. */
|
|
1985
|
+
database?: ConnectionResolver;
|
|
1986
|
+
/** Aurora cluster ARN. Falls back to the DATABASE_RESOURCE_ARN environment variable. */
|
|
1987
|
+
resourceArn?: ConnectionResolver;
|
|
1988
|
+
/** Secrets Manager secret ARN holding the credentials. Falls back to the DATABASE_SECRET_ARN environment variable. */
|
|
1989
|
+
secretArn?: ConnectionResolver;
|
|
1990
|
+
/** Extra RDSDataClient configuration (region, credentials, ...). */
|
|
1991
|
+
clientOptions?: RDSDataClientConfig;
|
|
1992
|
+
/**
|
|
1993
|
+
* Run pending migrations on the first `getDatabase()` call, like the other
|
|
1994
|
+
* database factories do. Off by default: on Lambda that check runs on every
|
|
1995
|
+
* cold start and costs several serialized Data API round trips before the
|
|
1996
|
+
* first query. Run migrations out of band instead — `db:migrate` locally,
|
|
1997
|
+
* or the console handler (`{"command": "db:migrate"}`) once deployed.
|
|
1998
|
+
*/
|
|
1999
|
+
migrateOnStart?: boolean;
|
|
2000
|
+
seedersFolder?: string | URL;
|
|
2001
|
+
/**
|
|
2002
|
+
* Drizzle relations for RQB v2 (`db.query.*`).
|
|
2003
|
+
* Build with `defineRelations(schema, ...)` from `drizzle-orm`,
|
|
2004
|
+
* or with `relations()` from `drizzle-orm/_relations` for the RQB v1 partial-upgrade path.
|
|
2005
|
+
*/
|
|
2006
|
+
relations?: Record<string, unknown>;
|
|
2000
2007
|
}
|
|
2001
2008
|
interface AwsDataApiDatabase {
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2012
|
-
|
|
2009
|
+
getDatabase(): Promise<AwsDataApiPgDatabase$1>;
|
|
2010
|
+
/** Applies pending drizzle-kit migrations and reports what the folder held. */
|
|
2011
|
+
migrateDatabase(): Promise<MigrationRunSummary>;
|
|
2012
|
+
closeDatabase(): Promise<void>;
|
|
2013
|
+
configureOrm(): Promise<void>;
|
|
2014
|
+
/** Runs every seeder in the configured folder and reports what it held. */
|
|
2015
|
+
seedDatabase(): Promise<SeederRunSummary>;
|
|
2016
|
+
/** Drops the public schema (and the drizzle tracker schema), then re-applies migrations — same end state as `guren db:reset`. */
|
|
2017
|
+
resetDatabase(): Promise<MigrationRunSummary>;
|
|
2018
|
+
/** Per-migration applied state derived from the drizzle-kit journal and drizzle.__drizzle_migrations. */
|
|
2019
|
+
migrationStatus(): Promise<MigrationStatusEntry[]>;
|
|
2013
2020
|
}
|
|
2014
2021
|
declare function createAwsDataApiDatabase(options: AwsDataApiDatabaseOptions): AwsDataApiDatabase;
|
|
2015
|
-
|
|
2022
|
+
//#endregion
|
|
2023
|
+
//#region src/serialization.d.ts
|
|
2016
2024
|
/**
|
|
2017
2025
|
* Serialize a model record for API/JSON output.
|
|
2018
2026
|
*
|
|
2019
2027
|
* Applies hidden/visible filtering, casts, accessors, and appends.
|
|
2020
2028
|
*/
|
|
2021
2029
|
declare function serializeRecord(record: PlainObject, options: {
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2030
|
+
hidden?: string[];
|
|
2031
|
+
visible?: string[];
|
|
2032
|
+
appends?: string[];
|
|
2033
|
+
accessors?: AccessorDefinitions;
|
|
2026
2034
|
}): PlainObject;
|
|
2027
2035
|
/** Serialize an array of records. */
|
|
2028
2036
|
declare function serializeRecords(records: PlainObject[], options: Parameters<typeof serializeRecord>[1]): PlainObject[];
|
|
2029
|
-
|
|
2030
|
-
export { type AccessorDefinitions, type AccessorFn, type AwsDataApiDatabase, type AwsDataApiDatabaseOptions, type AwsDataApiSeederContext, type BelongsToManyRecord, type BelongsToManyRelationResult, type BelongsToRecord, type BelongsToRelationResult, type BelongsToRequiredRecord, type CastType, type D1DatabaseHandle, type D1DatabaseOptions, DrizzleAdapter, type EagerLoadConstraint, type FindManyOptions, GlobalScopeRegistry, type GroupCondition, type HasManyRecord, type HasManyRelationResult, type HasManyThroughRecord, type HasManyThroughRelationResult, type HasOneRecord, type HasOneRelationResult, type HookCallback, type HookName, type InferModelInsert, type InferModelRecord, MassAssignmentException, type MigrationRunSummary, type MigrationStatusEntry, Model, type ModelHooks, ModelNotFoundException, type ModelObserver, type ModelObserverConstructor, type ModelPaginationMeta, type ModelQueryOptions, type MorphManyRecord, type MorphManyRelationResult, type MorphToRecord, type MorphToRelationResult, type MutatorDefinitions, type MutatorFn, type MySqlDatabase, type MySqlDatabaseOptions, type MySqlSeederContext, type ORMAdapter, type ORMAdapterAdvanced, type OrderByClause, type OrderByInput, type OrderDefinition, type OrderDirection, type PaginateOptions, type PaginatedResult, type PlainObject, type PostgresDatabase, type PostgresDatabaseOptions, type PostgresSeederContext, QueryBuilder, type QueryBuilderOptions, type ScopeFunction, type SeederContext, type SeederHandler, type SeederRunSummary, type SimpleCondition, SoftDeletes, type SoftDeletesStatic, type SqliteDatabase, type SqliteDatabaseOptions, type SqliteSeederContext, type TransactionHandle, type TransactionModelScope, type WhereClause, type WhereCondition, type WhereGroupCallback, type WhereOperator, type WithRelations, applyAccessors, applyMutators, buildDrizzleConditions, createAwsDataApiDatabase, createD1Database, createMySqlDatabase, createPostgresDatabase, createSqliteDatabase, defineModel, defineSeeder, executeHook, executeObservers, loadSeeders, runSeeders, serializeRecord, serializeRecords };
|
|
2037
|
+
//#endregion
|
|
2038
|
+
export { type AccessorDefinitions, type AccessorFn, type AwsDataApiDatabase, type AwsDataApiDatabaseOptions, type AwsDataApiSeederContext, type BelongsToManyRecord, type BelongsToManyRelationResult, type BelongsToRecord, type BelongsToRelationResult, type BelongsToRequiredRecord, type CastType, type D1DatabaseHandle, type D1DatabaseOptions, DrizzleAdapter, type EagerLoadConstraint, type FindManyOptions, GlobalScopeRegistry, type GroupCondition, type HasManyRecord, type HasManyRelationResult, type HasManyThroughRecord, type HasManyThroughRelationResult, type HasOneRecord, type HasOneRelationResult, type HookCallback, type HookName, type InferModelInsert, type InferModelRecord, MassAssignmentException, type MigrationRunSummary, type MigrationStatusEntry, Model, type ModelHooks, ModelNotFoundException, type ModelObserver, type ModelObserverConstructor, type ModelPaginationMeta, type ModelQueryOptions, type MorphManyRecord, type MorphManyRelationResult, type MorphToRecord, type MorphToRelationResult, type MutatorDefinitions, type MutatorFn, type MySqlDatabase, type MySqlDatabaseOptions, type MySqlSeederContext, type ORMAdapter, type ORMAdapterAdvanced, type OrderByClause, type OrderByInput, type OrderDefinition, type OrderDirection, type PaginateOptions, type PaginatedResult, type PlainObject, type PostgresDatabase, type PostgresDatabaseOptions, type PostgresSeederContext, QueryBuilder, type QueryBuilderOptions, type ScopeFunction, type SeederContext, type SeederHandler, type SeederRunSummary, type SimpleCondition, SoftDeletes, type SoftDeletesStatic, type SqliteDatabase, type SqliteDatabaseOptions, type SqliteSeederContext, type TransactionHandle, type TransactionModelScope, type WhereClause, type WhereCondition, type WhereGroupCallback, type WhereOperator, type WithRelations, applyAccessors, applyMutators, buildDrizzleConditions, createAwsDataApiDatabase, createD1Database, createMySqlDatabase, createPostgresDatabase, createSqliteDatabase, defineModel, defineSeeder, executeHook, executeObservers, loadSeeders, runSeeders, serializeRecord, serializeRecords };
|