outlet-orm 2.5.1 → 3.2.0

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/types/index.d.ts CHANGED
@@ -14,9 +14,30 @@ declare module 'outlet-orm' {
14
14
  connectionLimit?: number;
15
15
  }
16
16
 
17
+ export interface QueryLogEntry {
18
+ sql: string;
19
+ params: any[];
20
+ duration: number;
21
+ timestamp: Date;
22
+ }
23
+
17
24
  export class DatabaseConnection {
18
25
  constructor(config?: Partial<DatabaseConfig>);
19
26
  connect(): Promise<void>;
27
+
28
+ // Transaction support
29
+ beginTransaction(): Promise<void>;
30
+ commit(): Promise<void>;
31
+ rollback(): Promise<void>;
32
+ transaction<T>(callback: (connection: DatabaseConnection) => Promise<T>): Promise<T>;
33
+
34
+ // Query logging
35
+ static enableQueryLog(): void;
36
+ static disableQueryLog(): void;
37
+ static getQueryLog(): QueryLogEntry[];
38
+ static flushQueryLog(): void;
39
+ static isLogging(): boolean;
40
+
20
41
  select(table: string, query: QueryObject): Promise<any[]>;
21
42
  insert(table: string, data: Record<string, any>): Promise<{ insertId: any; affectedRows: number }>;
22
43
  insertMany(table: string, data: Record<string, any>[]): Promise<{ affectedRows: number }>;
@@ -107,11 +128,11 @@ declare module 'outlet-orm' {
107
128
  whereLike(column: string, value: string): this;
108
129
  /** Filter parents where a relation has matches */
109
130
  whereHas(relationName: string, callback?: (qb: QueryBuilder<any>) => void): this;
110
- /** Filter parents where relation count matches */
111
- has(relationName: string, count: number): this;
112
- has(relationName: string, operator: string, count: number): this;
113
- /** Filter parents without related rows */
114
- whereDoesntHave(relationName: string): this;
131
+ /** Filter parents where relation count matches */
132
+ has(relationName: string, count: number): this;
133
+ has(relationName: string, operator: string, count: number): this;
134
+ /** Filter parents without related rows */
135
+ whereDoesntHave(relationName: string): this;
115
136
  orderBy(column: string, direction?: 'asc' | 'desc'): this;
116
137
  /** Typo-friendly alias for orderBy */
117
138
  ordrer(column: string, direction?: 'asc' | 'desc'): this;
@@ -119,15 +140,23 @@ declare module 'outlet-orm' {
119
140
  offset(value: number): this;
120
141
  skip(value: number): this;
121
142
  take(value: number): this;
122
- with(...relations: string[] | [Record<string, (qb: QueryBuilder<any>) => void> | string[]]): this;
123
- withCount(relations: string | string[]): this;
124
- groupBy(...columns: string[]): this;
125
- having(column: string, operator: string, value: any): this;
143
+ with(...relations: string[] | [Record<string, (qb: QueryBuilder<any>) => void> | string[]]): this;
144
+ withCount(relations: string | string[]): this;
145
+ groupBy(...columns: string[]): this;
146
+ having(column: string, operator: string, value: any): this;
126
147
  join(table: string, first: string, second: string): this;
127
148
  join(table: string, first: string, operator: string, second: string): this;
128
149
  leftJoin(table: string, first: string, second: string): this;
129
150
  leftJoin(table: string, first: string, operator: string, second: string): this;
130
151
 
152
+ // Soft delete methods
153
+ withTrashed(): this;
154
+ onlyTrashed(): this;
155
+
156
+ // Scope methods
157
+ withoutGlobalScope(name: string): this;
158
+ withoutGlobalScopes(): this;
159
+
131
160
  get(): Promise<T[]>;
132
161
  first(): Promise<T | null>;
133
162
  firstOrFail(): Promise<T>;
@@ -149,6 +178,15 @@ declare module 'outlet-orm' {
149
178
 
150
179
  export type CastType = 'int' | 'integer' | 'float' | 'double' | 'string' | 'bool' | 'boolean' | 'array' | 'json' | 'date' | 'datetime' | 'timestamp';
151
180
 
181
+ export type ValidationRule = 'required' | 'string' | 'number' | 'numeric' | 'email' | 'boolean' | 'date' | `min:${number}` | `max:${number}` | `in:${string}` | `regex:${string}`;
182
+
183
+ export interface ValidationResult {
184
+ valid: boolean;
185
+ errors: Record<string, string[]>;
186
+ }
187
+
188
+ export type EventCallback<T extends Model = Model> = (model: T) => boolean | void | Promise<boolean | void>;
189
+
152
190
  export class Model {
153
191
  static table: string;
154
192
  static primaryKey: string;
@@ -158,6 +196,19 @@ declare module 'outlet-orm' {
158
196
  static casts: Record<string, CastType>;
159
197
  static connection: DatabaseConnection | null;
160
198
 
199
+ // Soft Deletes
200
+ static softDeletes: boolean;
201
+ static DELETED_AT: string;
202
+
203
+ // Scopes
204
+ static globalScopes: Record<string, (qb: QueryBuilder<any>) => void>;
205
+
206
+ // Events
207
+ static eventListeners: Record<string, EventCallback[]>;
208
+
209
+ // Validation
210
+ static rules: Record<string, string | ValidationRule[]>;
211
+
161
212
  attributes: Record<string, any>;
162
213
  original: Record<string, any>;
163
214
  relations: Record<string, any>;
@@ -165,8 +216,34 @@ declare module 'outlet-orm' {
165
216
 
166
217
  constructor(attributes?: Record<string, any>);
167
218
 
219
+ // Event registration
220
+ static on(event: string, callback: EventCallback): void;
221
+ static fireEvent(event: string, model: Model): Promise<boolean>;
222
+ static creating(callback: EventCallback): void;
223
+ static created(callback: EventCallback): void;
224
+ static updating(callback: EventCallback): void;
225
+ static updated(callback: EventCallback): void;
226
+ static saving(callback: EventCallback): void;
227
+ static saved(callback: EventCallback): void;
228
+ static deleting(callback: EventCallback): void;
229
+ static deleted(callback: EventCallback): void;
230
+ static restoring(callback: EventCallback): void;
231
+ static restored(callback: EventCallback): void;
232
+
233
+ // Scope methods
234
+ static addGlobalScope(name: string, callback: (qb: QueryBuilder<any>) => void): void;
235
+ static removeGlobalScope(name: string): void;
236
+ static withoutGlobalScope<T extends Model>(this: new () => T, name: string): QueryBuilder<T>;
237
+ static withoutGlobalScopes<T extends Model>(this: new () => T): QueryBuilder<T>;
238
+
239
+ // Soft delete methods
240
+ static withTrashed<T extends Model>(this: new () => T): QueryBuilder<T>;
241
+ static onlyTrashed<T extends Model>(this: new () => T): QueryBuilder<T>;
242
+
168
243
  // Static methods
169
244
  static setConnection(connection: DatabaseConnection): void;
245
+ /** Get the active database connection (v3.0.0+) */
246
+ static getConnection(): DatabaseConnection;
170
247
  static query<T extends Model>(this: new () => T): QueryBuilder<T>;
171
248
  static all<T extends Model>(this: new () => T): Promise<T[]>;
172
249
  static find<T extends Model>(this: new () => T, id: any): Promise<T | null>;
@@ -176,10 +253,10 @@ declare module 'outlet-orm' {
176
253
  static create<T extends Model>(this: new () => T, attributes: Record<string, any>): Promise<T>;
177
254
  static insert(data: Record<string, any> | Record<string, any>[]): Promise<any>;
178
255
  static update(attributes: Record<string, any>): Promise<any>;
179
- /** Update by primary key and return the updated model, optionally eager-loading relations */
180
- static updateAndFetchById<T extends Model>(this: new () => T, id: any, attributes: Record<string, any>, relations?: string[]): Promise<T | null>;
181
- /** Update by primary key */
182
- static updateById<T extends Model>(this: new () => T, id: any, attributes: Record<string, any>): Promise<any>;
256
+ /** Update by primary key and return the updated model, optionally eager-loading relations */
257
+ static updateAndFetchById<T extends Model>(this: new () => T, id: any, attributes: Record<string, any>, relations?: string[]): Promise<T | null>;
258
+ /** Update by primary key */
259
+ static updateById<T extends Model>(this: new () => T, id: any, attributes: Record<string, any>): Promise<any>;
183
260
  static delete(): Promise<any>;
184
261
  static first<T extends Model>(this: new () => T): Promise<T | null>;
185
262
  static orderBy<T extends Model>(this: new () => T, column: string, direction?: 'asc' | 'desc'): QueryBuilder<T>;
@@ -190,11 +267,11 @@ declare module 'outlet-orm' {
190
267
  static whereNull<T extends Model>(this: new () => T, column: string): QueryBuilder<T>;
191
268
  static whereNotNull<T extends Model>(this: new () => T, column: string): QueryBuilder<T>;
192
269
  static count(): Promise<number>;
193
- static with<T extends Model>(this: new () => T, ...relations: string[] | [Record<string, (qb: QueryBuilder<any>) => void> | string[]]): QueryBuilder<T>;
194
- /** Include hidden attributes in query results */
195
- static withHidden<T extends Model>(this: new () => T): QueryBuilder<T>;
196
- /** Control visibility of hidden attributes (false = hide, true = show) */
197
- static withoutHidden<T extends Model>(this: new () => T, show?: boolean): QueryBuilder<T>;
270
+ static with<T extends Model>(this: new () => T, ...relations: string[] | [Record<string, (qb: QueryBuilder<any>) => void> | string[]]): QueryBuilder<T>;
271
+ /** Include hidden attributes in query results */
272
+ static withHidden<T extends Model>(this: new () => T): QueryBuilder<T>;
273
+ /** Control visibility of hidden attributes (false = hide, true = show) */
274
+ static withoutHidden<T extends Model>(this: new () => T, show?: boolean): QueryBuilder<T>;
198
275
 
199
276
  // Instance methods
200
277
  fill(attributes: Record<string, any>): this;
@@ -206,8 +283,17 @@ declare module 'outlet-orm' {
206
283
  getDirty(): Record<string, any>;
207
284
  isDirty(): boolean;
208
285
  toJSON(): Record<string, any>;
209
- /** Load relations on an existing instance. Supports dot-notation and arrays. */
210
- load(...relations: string[] | [string[]]): Promise<this>;
286
+ /** Load relations on an existing instance. Supports dot-notation and arrays. */
287
+ load(...relations: string[] | [string[]]): Promise<this>;
288
+
289
+ // Soft delete instance methods
290
+ trashed(): boolean;
291
+ restore(): Promise<this>;
292
+ forceDelete(): Promise<boolean>;
293
+
294
+ // Validation
295
+ validate(): ValidationResult;
296
+ validateOrFail(): void;
211
297
 
212
298
  // Relationships
213
299
  hasOne<T extends Model>(related: new () => T, foreignKey?: string, localKey?: string): HasOneRelation<T>;
@@ -273,4 +359,24 @@ declare module 'outlet-orm' {
273
359
  get(): Promise<T[]>;
274
360
  eagerLoad(models: Model[], relationName: string, constraint?: (qb: QueryBuilder<T>) => void): Promise<void>;
275
361
  }
362
+
363
+ export class HasOneThroughRelation<T extends Model> extends Relation<T> {
364
+ get(): Promise<T | null>;
365
+ eagerLoad(models: Model[], relationName: string, constraint?: (qb: QueryBuilder<T>) => void): Promise<void>;
366
+ }
367
+
368
+ export class MorphOneRelation<T extends Model> extends Relation<T> {
369
+ get(): Promise<T | null>;
370
+ eagerLoad(models: Model[], relationName: string, constraint?: (qb: QueryBuilder<T>) => void): Promise<void>;
371
+ }
372
+
373
+ export class MorphManyRelation<T extends Model> extends Relation<T> {
374
+ get(): Promise<T[]>;
375
+ eagerLoad(models: Model[], relationName: string, constraint?: (qb: QueryBuilder<T>) => void): Promise<void>;
376
+ }
377
+
378
+ export class MorphToRelation<T extends Model> extends Relation<T> {
379
+ get(): Promise<T | null>;
380
+ eagerLoad(models: Model[], relationName: string, constraint?: (qb: QueryBuilder<T>) => void): Promise<void>;
381
+ }
276
382
  }