@veap/database 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +177 -2
- package/dist/casts.d.ts +9 -0
- package/dist/casts.js +78 -0
- package/dist/composition.d.ts +15 -0
- package/dist/composition.js +26 -0
- package/dist/connection.d.ts +49 -0
- package/dist/connection.js +193 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.js +17 -0
- package/dist/events.d.ts +5 -0
- package/dist/events.js +7 -0
- package/dist/factory.d.ts +68 -0
- package/dist/factory.js +161 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/logger.d.ts +11 -0
- package/dist/logger.js +21 -0
- package/dist/migration-provider.d.ts +7 -0
- package/dist/migration-provider.js +36 -0
- package/dist/migrations/generator.d.ts +4 -0
- package/dist/migrations/generator.js +58 -0
- package/dist/migrations/index.d.ts +3 -0
- package/dist/migrations/index.js +3 -0
- package/dist/migrations/runner.d.ts +12 -0
- package/dist/migrations/runner.js +179 -0
- package/dist/migrations/schema-builder.d.ts +84 -0
- package/dist/migrations/schema-builder.js +409 -0
- package/dist/model.d.ts +379 -0
- package/dist/model.js +913 -0
- package/dist/provider.d.ts +13 -0
- package/dist/provider.js +112 -0
- package/dist/query-builder.d.ts +197 -0
- package/dist/query-builder.js +749 -0
- package/dist/relations/belongs-to-many.d.ts +15 -0
- package/dist/relations/belongs-to-many.js +80 -0
- package/dist/relations/belongs-to.d.ts +8 -0
- package/dist/relations/belongs-to.js +37 -0
- package/dist/relations/eager-loader.d.ts +5 -0
- package/dist/relations/eager-loader.js +68 -0
- package/dist/relations/has-many.d.ts +9 -0
- package/dist/relations/has-many.js +48 -0
- package/dist/relations/has-one.d.ts +8 -0
- package/dist/relations/has-one.js +37 -0
- package/dist/relations/index.d.ts +11 -0
- package/dist/relations/index.js +11 -0
- package/dist/relations/morph-many.d.ts +14 -0
- package/dist/relations/morph-many.js +65 -0
- package/dist/relations/morph-map.d.ts +32 -0
- package/dist/relations/morph-map.js +73 -0
- package/dist/relations/morph-one.d.ts +14 -0
- package/dist/relations/morph-one.js +65 -0
- package/dist/relations/morph-to-many.d.ts +44 -0
- package/dist/relations/morph-to-many.js +215 -0
- package/dist/relations/morph-to.d.ts +19 -0
- package/dist/relations/morph-to.js +90 -0
- package/dist/relations/relation.d.ts +22 -0
- package/dist/relations/relation.js +13 -0
- package/dist/scopes.d.ts +16 -0
- package/dist/scopes.js +1 -0
- package/dist/seeder.d.ts +25 -0
- package/dist/seeder.js +49 -0
- package/dist/tokens.d.ts +10 -0
- package/dist/tokens.js +8 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +20 -0
- package/package.json +58 -4
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
import { type CastType } from "./casts.js";
|
|
2
|
+
import { ModelQueryBuilder } from "./query-builder.js";
|
|
3
|
+
import { BelongsTo, BelongsToMany, HasMany, HasOne, MorphedByMany, MorphMany, MorphOne, MorphTo, MorphToMany } from "./relations/index.js";
|
|
4
|
+
import type { GlobalScope } from "./scopes.js";
|
|
5
|
+
export declare abstract class Model<Attributes extends Record<string, any> = Record<string, any>> {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
/**
|
|
8
|
+
* Database table name.
|
|
9
|
+
*/
|
|
10
|
+
static table: string;
|
|
11
|
+
/**
|
|
12
|
+
* Optional polymorphic morph alias (e.g. 'post', 'category', 'user').
|
|
13
|
+
*/
|
|
14
|
+
static morphAlias?: string;
|
|
15
|
+
/**
|
|
16
|
+
* Primary key column name.
|
|
17
|
+
*/
|
|
18
|
+
static primaryKey: string;
|
|
19
|
+
/**
|
|
20
|
+
* Whether to automatically generate UUID v4 for the primary key on creation.
|
|
21
|
+
*/
|
|
22
|
+
static autoUuid: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Whether the table has timestamp columns.
|
|
25
|
+
* Can be true (both created_at and updated_at), false (none),
|
|
26
|
+
* or an array of specific timestamp columns like ["created_at"].
|
|
27
|
+
*/
|
|
28
|
+
static timestamps: boolean | string[];
|
|
29
|
+
/**
|
|
30
|
+
* Attribute type casts dictionary.
|
|
31
|
+
*/
|
|
32
|
+
static casts: Record<string, CastType>;
|
|
33
|
+
/**
|
|
34
|
+
* Whether this model uses soft deletes.
|
|
35
|
+
*/
|
|
36
|
+
static softDeletes: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Column name used for soft deletes.
|
|
39
|
+
*/
|
|
40
|
+
static deletedAtColumn: string;
|
|
41
|
+
/**
|
|
42
|
+
* Hidden attributes that should not be included in toJSON serialization.
|
|
43
|
+
*/
|
|
44
|
+
static hidden: string[];
|
|
45
|
+
/**
|
|
46
|
+
* Attributes that are mass assignable.
|
|
47
|
+
* If not empty, only attributes in this array can be mass-assigned.
|
|
48
|
+
*/
|
|
49
|
+
static fillable: string[];
|
|
50
|
+
/**
|
|
51
|
+
* Attributes that are guarded from mass assignment.
|
|
52
|
+
* If '*' or non-empty, attributes in this array cannot be mass-assigned.
|
|
53
|
+
*/
|
|
54
|
+
static guarded: string[];
|
|
55
|
+
/**
|
|
56
|
+
* Registered global scopes dictionary.
|
|
57
|
+
*/
|
|
58
|
+
static globalScopes: Record<string, GlobalScope>;
|
|
59
|
+
/**
|
|
60
|
+
* Registers a new global scope on this model.
|
|
61
|
+
*/
|
|
62
|
+
static addGlobalScope(name: string, scope: GlobalScope): void;
|
|
63
|
+
/**
|
|
64
|
+
* Checks if a global scope is registered on this model.
|
|
65
|
+
*/
|
|
66
|
+
static hasGlobalScope(name: string): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Model attributes stored in state.
|
|
69
|
+
*/
|
|
70
|
+
protected _attributes: Record<string, any>;
|
|
71
|
+
/**
|
|
72
|
+
* Original attributes as loaded from database (for dirty tracking).
|
|
73
|
+
*/
|
|
74
|
+
protected _original: Record<string, any>;
|
|
75
|
+
/**
|
|
76
|
+
* Eager-loaded relations.
|
|
77
|
+
*/
|
|
78
|
+
protected _relations: Record<string, any>;
|
|
79
|
+
/**
|
|
80
|
+
* Indicates if the model exists in the database.
|
|
81
|
+
*/
|
|
82
|
+
isNew: boolean;
|
|
83
|
+
constructor(attributes?: Partial<Attributes>, isNew?: boolean);
|
|
84
|
+
get id(): any;
|
|
85
|
+
set id(value: any);
|
|
86
|
+
/**
|
|
87
|
+
* Static Query Builder entrypoint.
|
|
88
|
+
*/
|
|
89
|
+
static query<M extends Model>(this: {
|
|
90
|
+
new (...args: any[]): M;
|
|
91
|
+
table: string;
|
|
92
|
+
}): ModelQueryBuilder<M>;
|
|
93
|
+
/**
|
|
94
|
+
* Starts a query builder including soft-deleted models.
|
|
95
|
+
*/
|
|
96
|
+
static withTrashed<M extends Model>(this: {
|
|
97
|
+
new (...args: any[]): M;
|
|
98
|
+
table: string;
|
|
99
|
+
}): ModelQueryBuilder<M>;
|
|
100
|
+
/**
|
|
101
|
+
* Starts a query builder restricting to only soft-deleted models.
|
|
102
|
+
*/
|
|
103
|
+
static onlyTrashed<M extends Model>(this: {
|
|
104
|
+
new (...args: any[]): M;
|
|
105
|
+
table: string;
|
|
106
|
+
}): ModelQueryBuilder<M>;
|
|
107
|
+
/**
|
|
108
|
+
* Finds a model by primary key.
|
|
109
|
+
*/
|
|
110
|
+
static find<M extends Model>(this: {
|
|
111
|
+
new (...args: any[]): M;
|
|
112
|
+
table: string;
|
|
113
|
+
}, id: any): Promise<M | null>;
|
|
114
|
+
/**
|
|
115
|
+
* Finds a model by primary key or throws.
|
|
116
|
+
*/
|
|
117
|
+
static findOrFail<M extends Model>(this: {
|
|
118
|
+
new (...args: any[]): M;
|
|
119
|
+
table: string;
|
|
120
|
+
}, id: any): Promise<M>;
|
|
121
|
+
/**
|
|
122
|
+
* Gets all records from table.
|
|
123
|
+
*/
|
|
124
|
+
static all<M extends Model>(this: {
|
|
125
|
+
new (...args: any[]): M;
|
|
126
|
+
table: string;
|
|
127
|
+
}): Promise<M[]>;
|
|
128
|
+
/**
|
|
129
|
+
* Counts rows in table.
|
|
130
|
+
*/
|
|
131
|
+
static count<M extends Model>(this: {
|
|
132
|
+
new (...args: any[]): M;
|
|
133
|
+
table: string;
|
|
134
|
+
}, column?: string): Promise<number>;
|
|
135
|
+
/**
|
|
136
|
+
* Starts a WHERE query.
|
|
137
|
+
*/
|
|
138
|
+
static where<M extends Model>(this: {
|
|
139
|
+
new (...args: any[]): M;
|
|
140
|
+
table: string;
|
|
141
|
+
}, column: string | Record<string, any>, operator?: any, value?: any): ModelQueryBuilder<M>;
|
|
142
|
+
/**
|
|
143
|
+
* Creates and saves a new model instance.
|
|
144
|
+
*/
|
|
145
|
+
static create<M extends Model>(this: {
|
|
146
|
+
new (...args: any[]): M;
|
|
147
|
+
table: string;
|
|
148
|
+
}, attributes: Record<string, any>): Promise<M>;
|
|
149
|
+
/**
|
|
150
|
+
* Creates and saves a new model instance without mass assignment restrictions.
|
|
151
|
+
*/
|
|
152
|
+
static forceCreate<M extends Model>(this: {
|
|
153
|
+
new (...args: any[]): M;
|
|
154
|
+
table: string;
|
|
155
|
+
}, attributes: Record<string, any>): Promise<M>;
|
|
156
|
+
/**
|
|
157
|
+
* Finds the first record matching attributes or creates and saves a new instance.
|
|
158
|
+
*/
|
|
159
|
+
static firstOrCreate<M extends Model>(this: {
|
|
160
|
+
new (...args: any[]): M;
|
|
161
|
+
table: string;
|
|
162
|
+
}, attributes: Record<string, any>, values?: Record<string, any>): Promise<M>;
|
|
163
|
+
/**
|
|
164
|
+
* Finds the first record matching attributes or instantiates a new instance (unsaved).
|
|
165
|
+
*/
|
|
166
|
+
static firstOrNew<M extends Model>(this: {
|
|
167
|
+
new (...args: any[]): M;
|
|
168
|
+
table: string;
|
|
169
|
+
}, attributes: Record<string, any>, values?: Record<string, any>): Promise<M>;
|
|
170
|
+
/**
|
|
171
|
+
* Creates or updates a record matching attributes, filling it with values.
|
|
172
|
+
*/
|
|
173
|
+
static updateOrCreate<M extends Model>(this: {
|
|
174
|
+
new (...args: any[]): M;
|
|
175
|
+
table: string;
|
|
176
|
+
}, attributes: Record<string, any>, values?: Record<string, any>): Promise<M>;
|
|
177
|
+
/**
|
|
178
|
+
* Begins querying a relation existence on the model.
|
|
179
|
+
*/
|
|
180
|
+
static has<M extends Model>(this: {
|
|
181
|
+
new (...args: any[]): M;
|
|
182
|
+
table: string;
|
|
183
|
+
}, relation: string, operator?: string, count?: number): ModelQueryBuilder<M>;
|
|
184
|
+
/**
|
|
185
|
+
* Begins querying a relation with a callback on the model.
|
|
186
|
+
*/
|
|
187
|
+
static whereHas<M extends Model>(this: {
|
|
188
|
+
new (...args: any[]): M;
|
|
189
|
+
table: string;
|
|
190
|
+
}, relation: string, callback?: (query: ModelQueryBuilder) => void): ModelQueryBuilder<M>;
|
|
191
|
+
/**
|
|
192
|
+
* Begins querying a relation absence on the model.
|
|
193
|
+
*/
|
|
194
|
+
static whereDoesntHave<M extends Model>(this: {
|
|
195
|
+
new (...args: any[]): M;
|
|
196
|
+
table: string;
|
|
197
|
+
}, relation: string, callback?: (query: ModelQueryBuilder) => void): ModelQueryBuilder<M>;
|
|
198
|
+
/**
|
|
199
|
+
* Begins querying with related models count.
|
|
200
|
+
*/
|
|
201
|
+
static withCount<M extends Model>(this: {
|
|
202
|
+
new (...args: any[]): M;
|
|
203
|
+
table: string;
|
|
204
|
+
}, ...relations: any[]): ModelQueryBuilder<M>;
|
|
205
|
+
/**
|
|
206
|
+
* Executes a database transaction.
|
|
207
|
+
*/
|
|
208
|
+
static transaction<T>(callback: (trx: any) => Promise<T>): Promise<T>;
|
|
209
|
+
/**
|
|
210
|
+
* Applies a local scope by name on a new query builder.
|
|
211
|
+
*/
|
|
212
|
+
static scope<M extends Model>(this: {
|
|
213
|
+
new (...args: any[]): M;
|
|
214
|
+
table: string;
|
|
215
|
+
}, name: string, ...args: any[]): ModelQueryBuilder<M>;
|
|
216
|
+
/**
|
|
217
|
+
* Excludes a specific global scope on a new query builder.
|
|
218
|
+
*/
|
|
219
|
+
static withoutGlobalScope<M extends Model>(this: {
|
|
220
|
+
new (...args: any[]): M;
|
|
221
|
+
table: string;
|
|
222
|
+
}, scope: string): ModelQueryBuilder<M>;
|
|
223
|
+
/**
|
|
224
|
+
* Excludes multiple or all global scopes on a new query builder.
|
|
225
|
+
*/
|
|
226
|
+
static withoutGlobalScopes<M extends Model>(this: {
|
|
227
|
+
new (...args: any[]): M;
|
|
228
|
+
table: string;
|
|
229
|
+
}, scopes?: string[]): ModelQueryBuilder<M>;
|
|
230
|
+
/**
|
|
231
|
+
* Starts a new Factory for this model.
|
|
232
|
+
* Uses static newFactory() method on the model class.
|
|
233
|
+
*/
|
|
234
|
+
static factory<F = any>(this: any, count?: number): F;
|
|
235
|
+
/**
|
|
236
|
+
* Deletes a model by primary key (soft-deletes if softDeletes is enabled).
|
|
237
|
+
*/
|
|
238
|
+
static destroy(this: typeof Model, id: any): Promise<boolean>;
|
|
239
|
+
/**
|
|
240
|
+
* Permanently deletes a model by primary key regardless of softDeletes setting.
|
|
241
|
+
*/
|
|
242
|
+
static forceDestroy(this: typeof Model, id: any): Promise<boolean>;
|
|
243
|
+
/**
|
|
244
|
+
* Restores a soft-deleted model by primary key.
|
|
245
|
+
*/
|
|
246
|
+
static restore(this: typeof Model, id: any): Promise<boolean>;
|
|
247
|
+
/**
|
|
248
|
+
* Determines if the given attribute may be mass assigned.
|
|
249
|
+
*/
|
|
250
|
+
isFillable(key: string): boolean;
|
|
251
|
+
/**
|
|
252
|
+
* Fills model with mass-assignable attributes.
|
|
253
|
+
*/
|
|
254
|
+
fill(attributes: Partial<Attributes>): this;
|
|
255
|
+
/**
|
|
256
|
+
* Force fills model with attributes, bypassing fillable/guarded protection.
|
|
257
|
+
*/
|
|
258
|
+
forceFill(attributes: Partial<Attributes>): this;
|
|
259
|
+
/**
|
|
260
|
+
* Fires a model lifecycle event.
|
|
261
|
+
* Calls instance hook methods (e.g. beforeSave/saving) and publishes to eventBus.
|
|
262
|
+
* If any handler returns false, the operation is canceled.
|
|
263
|
+
*/
|
|
264
|
+
protected fireModelEvent(event: string): Promise<boolean>;
|
|
265
|
+
/**
|
|
266
|
+
* Hydrates attributes from raw database row, applying casts.
|
|
267
|
+
*/
|
|
268
|
+
hydrate(rawRow: Record<string, any>): this;
|
|
269
|
+
getAttribute(key: string): any;
|
|
270
|
+
setAttribute(key: string, value: any): this;
|
|
271
|
+
isDirty(key?: string): boolean;
|
|
272
|
+
getRelation<R = any>(name: string): R | undefined;
|
|
273
|
+
setRelation(name: string, value: any): this;
|
|
274
|
+
/**
|
|
275
|
+
* Invokes and returns the Relation instance for a given relation name,
|
|
276
|
+
* bypassing any cached/loaded relation data in `_relations`.
|
|
277
|
+
*/
|
|
278
|
+
getRelationDefinition(name: string): any;
|
|
279
|
+
/**
|
|
280
|
+
* Saves model to database (INSERT if isNew, UPDATE if exists).
|
|
281
|
+
*/
|
|
282
|
+
save(): Promise<this>;
|
|
283
|
+
/**
|
|
284
|
+
* Updates attributes and saves immediately.
|
|
285
|
+
*/
|
|
286
|
+
update(attributes: Partial<Attributes>): Promise<this>;
|
|
287
|
+
/**
|
|
288
|
+
* Deletes model from database (soft-deletes if softDeletes is enabled).
|
|
289
|
+
*/
|
|
290
|
+
delete(): Promise<boolean>;
|
|
291
|
+
/**
|
|
292
|
+
* Permanently deletes model from database, bypassing soft delete.
|
|
293
|
+
*/
|
|
294
|
+
forceDelete(): Promise<boolean>;
|
|
295
|
+
/**
|
|
296
|
+
* Restores a soft-deleted model.
|
|
297
|
+
*/
|
|
298
|
+
restore(): Promise<boolean>;
|
|
299
|
+
/**
|
|
300
|
+
* Checks if the model has been soft-deleted.
|
|
301
|
+
*/
|
|
302
|
+
trashed(): boolean;
|
|
303
|
+
/**
|
|
304
|
+
* Alias for trashed().
|
|
305
|
+
*/
|
|
306
|
+
isTrashed(): boolean;
|
|
307
|
+
/**
|
|
308
|
+
* Refreshes model attributes from database.
|
|
309
|
+
*/
|
|
310
|
+
refresh(): Promise<this>;
|
|
311
|
+
/**
|
|
312
|
+
* Converts model to JSON object including loaded relations.
|
|
313
|
+
* Provides both original and camelCase aliases for seamless frontend DX.
|
|
314
|
+
*/
|
|
315
|
+
toJSON(): Record<string, any>;
|
|
316
|
+
hasOne<R extends Model>(related: {
|
|
317
|
+
new (...args: any[]): R;
|
|
318
|
+
table: string;
|
|
319
|
+
}, foreignKey?: string, localKey?: string): HasOne<this, R>;
|
|
320
|
+
hasMany<R extends Model>(related: {
|
|
321
|
+
new (...args: any[]): R;
|
|
322
|
+
table: string;
|
|
323
|
+
}, foreignKey?: string, localKey?: string): HasMany<this, R>;
|
|
324
|
+
belongsTo<R extends Model>(related: {
|
|
325
|
+
new (...args: any[]): R;
|
|
326
|
+
table: string;
|
|
327
|
+
primaryKey?: string;
|
|
328
|
+
name?: string;
|
|
329
|
+
}, foreignKey?: string, ownerKey?: string): BelongsTo<this, R>;
|
|
330
|
+
belongsToMany<R extends Model>(related: {
|
|
331
|
+
new (...args: any[]): R;
|
|
332
|
+
table: string;
|
|
333
|
+
primaryKey?: string;
|
|
334
|
+
name?: string;
|
|
335
|
+
}, pivotTable: string, foreignPivotKey?: string, relatedPivotKey?: string, localKey?: string, relatedKey?: string): BelongsToMany<this, R>;
|
|
336
|
+
/**
|
|
337
|
+
* Defines a polymorphic, inverse one-to-one or one-to-many relation.
|
|
338
|
+
* E.g. comment.morphTo('commentable') -> resolves type from commentable_type and id from commentable_id
|
|
339
|
+
*/
|
|
340
|
+
morphTo<R extends Model = any>(name?: string, type?: string, id?: string): MorphTo<this, R>;
|
|
341
|
+
/**
|
|
342
|
+
* Defines a polymorphic one-to-one relation.
|
|
343
|
+
* E.g. user.morphOne(Image, 'imageable')
|
|
344
|
+
*/
|
|
345
|
+
morphOne<R extends Model>(related: {
|
|
346
|
+
new (...args: any[]): R;
|
|
347
|
+
table: string;
|
|
348
|
+
primaryKey?: string;
|
|
349
|
+
}, name: string, type?: string, id?: string, localKey?: string): MorphOne<this, R>;
|
|
350
|
+
/**
|
|
351
|
+
* Defines a polymorphic one-to-many relation.
|
|
352
|
+
* E.g. post.morphMany(Comment, 'commentable')
|
|
353
|
+
*/
|
|
354
|
+
morphMany<R extends Model>(related: {
|
|
355
|
+
new (...args: any[]): R;
|
|
356
|
+
table: string;
|
|
357
|
+
primaryKey?: string;
|
|
358
|
+
}, name: string, type?: string, id?: string, localKey?: string): MorphMany<this, R>;
|
|
359
|
+
/**
|
|
360
|
+
* Defines a polymorphic many-to-many relation where the parent is the polymorphic entity.
|
|
361
|
+
* E.g. post.morphToMany(Tag, 'taggable', 'taggables')
|
|
362
|
+
*/
|
|
363
|
+
morphToMany<R extends Model>(related: {
|
|
364
|
+
new (...args: any[]): R;
|
|
365
|
+
table: string;
|
|
366
|
+
primaryKey?: string;
|
|
367
|
+
name?: string;
|
|
368
|
+
}, name: string, pivotTable?: string, foreignPivotKey?: string, relatedPivotKey?: string, parentKey?: string, relatedKey?: string): MorphToMany<this, R>;
|
|
369
|
+
/**
|
|
370
|
+
* Defines the inverse of a polymorphic many-to-many relation.
|
|
371
|
+
* E.g. tag.morphedByMany(BlogPost, 'taggable', 'taggables')
|
|
372
|
+
*/
|
|
373
|
+
morphedByMany<R extends Model>(related: {
|
|
374
|
+
new (...args: any[]): R;
|
|
375
|
+
table: string;
|
|
376
|
+
primaryKey?: string;
|
|
377
|
+
name?: string;
|
|
378
|
+
}, name: string, pivotTable?: string, foreignPivotKey?: string, relatedPivotKey?: string, parentKey?: string, relatedKey?: string): MorphedByMany<this, R>;
|
|
379
|
+
}
|