@spinajs/orm 2.0.180 → 2.0.181

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.
Files changed (91) hide show
  1. package/lib/cjs/builders.d.ts +643 -643
  2. package/lib/cjs/builders.js +1602 -1602
  3. package/lib/cjs/builders.js.map +1 -1
  4. package/lib/cjs/converters.d.ts +34 -34
  5. package/lib/cjs/converters.js +104 -104
  6. package/lib/cjs/decorators.d.ts +152 -152
  7. package/lib/cjs/decorators.js +449 -449
  8. package/lib/cjs/dehydrators.d.ts +10 -10
  9. package/lib/cjs/dehydrators.js +47 -47
  10. package/lib/cjs/driver.d.ts +82 -82
  11. package/lib/cjs/driver.js +102 -102
  12. package/lib/cjs/driver.js.map +1 -1
  13. package/lib/cjs/enums.d.ts +116 -116
  14. package/lib/cjs/enums.js +126 -126
  15. package/lib/cjs/enums.js.map +1 -1
  16. package/lib/cjs/exceptions.d.ts +6 -6
  17. package/lib/cjs/exceptions.js +10 -10
  18. package/lib/cjs/hydrators.d.ts +19 -19
  19. package/lib/cjs/hydrators.js +132 -132
  20. package/lib/cjs/hydrators.js.map +1 -1
  21. package/lib/cjs/index.d.ts +17 -17
  22. package/lib/cjs/index.js +33 -33
  23. package/lib/cjs/interfaces.d.ts +919 -919
  24. package/lib/cjs/interfaces.js +279 -279
  25. package/lib/cjs/interfaces.js.map +1 -1
  26. package/lib/cjs/middlewares.d.ts +62 -62
  27. package/lib/cjs/middlewares.js +258 -258
  28. package/lib/cjs/model.d.ts +284 -284
  29. package/lib/cjs/model.js +810 -810
  30. package/lib/cjs/orm.d.ts +61 -61
  31. package/lib/cjs/orm.js +333 -333
  32. package/lib/cjs/orm.js.map +1 -1
  33. package/lib/cjs/relation-objects.d.ts +108 -108
  34. package/lib/cjs/relation-objects.js +221 -221
  35. package/lib/cjs/relations.d.ts +61 -61
  36. package/lib/cjs/relations.js +194 -194
  37. package/lib/cjs/relations.js.map +1 -1
  38. package/lib/cjs/statements.d.ts +143 -143
  39. package/lib/cjs/statements.js +309 -309
  40. package/lib/cjs/statements.js.map +1 -1
  41. package/lib/cjs/types.d.ts +32 -32
  42. package/lib/cjs/types.js +2 -2
  43. package/lib/cjs/wrappers.d.ts +5 -5
  44. package/lib/cjs/wrappers.js +12 -12
  45. package/lib/mjs/builders.d.ts +643 -643
  46. package/lib/mjs/builders.js +1594 -1594
  47. package/lib/mjs/builders.js.map +1 -1
  48. package/lib/mjs/converters.d.ts +34 -34
  49. package/lib/mjs/converters.js +96 -96
  50. package/lib/mjs/decorators.d.ts +152 -152
  51. package/lib/mjs/decorators.js +422 -422
  52. package/lib/mjs/dehydrators.d.ts +10 -10
  53. package/lib/mjs/dehydrators.js +41 -41
  54. package/lib/mjs/driver.d.ts +82 -82
  55. package/lib/mjs/driver.js +98 -98
  56. package/lib/mjs/driver.js.map +1 -1
  57. package/lib/mjs/enums.d.ts +116 -116
  58. package/lib/mjs/enums.js +123 -123
  59. package/lib/mjs/enums.js.map +1 -1
  60. package/lib/mjs/exceptions.d.ts +6 -6
  61. package/lib/mjs/exceptions.js +6 -6
  62. package/lib/mjs/hydrators.d.ts +19 -19
  63. package/lib/mjs/hydrators.js +128 -128
  64. package/lib/mjs/hydrators.js.map +1 -1
  65. package/lib/mjs/index.d.ts +17 -17
  66. package/lib/mjs/index.js +17 -17
  67. package/lib/mjs/interfaces.d.ts +919 -919
  68. package/lib/mjs/interfaces.js +267 -267
  69. package/lib/mjs/interfaces.js.map +1 -1
  70. package/lib/mjs/middlewares.d.ts +62 -62
  71. package/lib/mjs/middlewares.js +249 -249
  72. package/lib/mjs/model.d.ts +284 -284
  73. package/lib/mjs/model.js +800 -800
  74. package/lib/mjs/orm.d.ts +61 -61
  75. package/lib/mjs/orm.js +326 -326
  76. package/lib/mjs/orm.js.map +1 -1
  77. package/lib/mjs/relation-objects.d.ts +108 -108
  78. package/lib/mjs/relation-objects.js +211 -211
  79. package/lib/mjs/relations.d.ts +61 -61
  80. package/lib/mjs/relations.js +191 -191
  81. package/lib/mjs/relations.js.map +1 -1
  82. package/lib/mjs/statements.d.ts +143 -143
  83. package/lib/mjs/statements.js +301 -301
  84. package/lib/mjs/statements.js.map +1 -1
  85. package/lib/mjs/types.d.ts +32 -32
  86. package/lib/mjs/types.js +1 -1
  87. package/lib/mjs/wrappers.d.ts +5 -5
  88. package/lib/mjs/wrappers.js +9 -9
  89. package/lib/tsconfig.cjs.tsbuildinfo +1 -1
  90. package/lib/tsconfig.mjs.tsbuildinfo +1 -1
  91. package/package.json +5 -5
@@ -1,920 +1,920 @@
1
- import { Op } from './enums.js';
2
- import { QueryBuilder, RawQuery } from './builders.js';
3
- import { SortOrder, WhereBoolean } from './enums.js';
4
- import { IQueryStatement, Wrap } from './statements.js';
5
- import { ModelData, ModelDataWithRelationData, ModelDataWithRelationDataSearchable, PartialArray, PickRelations, Unbox, WhereFunction } from './types.js';
6
- import { IOrmRelation } from './relations.js';
7
- import { OrmDriver } from './driver.js';
8
- import { Constructor, IContainer } from '@spinajs/di';
9
- import { ModelBase } from './model.js';
10
- import { DateTime } from 'luxon';
11
- import { Relation } from './relation-objects.js';
12
- export declare enum QueryContext {
13
- Insert = 0,
14
- Select = 1,
15
- Update = 2,
16
- Delete = 3,
17
- Schema = 4,
18
- Transaction = 5
19
- }
20
- export declare enum ColumnAlterationType {
21
- Add = 0,
22
- Modify = 1,
23
- Rename = 2
24
- }
25
- export interface IRelation {
26
- TargetModelDescriptor: IModelDescriptor;
27
- /**
28
- * Indicates if data was fetched from db
29
- */
30
- Populated: boolean;
31
- }
32
- export declare abstract class DefaultValueBuilder<T> {
33
- Query: RawQuery;
34
- Value: string | number;
35
- /**
36
- * fills by default with current date
37
- */
38
- abstract date(): T;
39
- /**
40
- * fills by default with current datetime
41
- */
42
- abstract dateTime(): T;
43
- /**
44
- * Fills column with default value
45
- *
46
- * @param val - value to fill
47
- */
48
- abstract value(val: string | number): T;
49
- /**
50
- * Fills column with result of query provided
51
- *
52
- * @param query - raw query instance
53
- */
54
- abstract raw(query: RawQuery): T;
55
- }
56
- export declare enum InsertBehaviour {
57
- /**
58
- * Ignores if primary key exists in db
59
- */
60
- InsertOrIgnore = 0,
61
- /**
62
- * Updates entry if pk exists
63
- */
64
- InsertOrUpdate = 1,
65
- /**
66
- * Replaces entry if pk exists
67
- */
68
- InsertOrReplace = 2,
69
- None = 3
70
- }
71
- /**
72
- * Foreign key referential actions
73
- */
74
- export declare enum ReferentialAction {
75
- Cascade = "CASCADE",
76
- SetNull = "SET NULL",
77
- Restrict = "RESTRICT",
78
- NoAction = "NO ACTION",
79
- SetDefault = "SET DEFAULT"
80
- }
81
- /**
82
- * Transaction mode when migration DB
83
- */
84
- export declare enum MigrationTransactionMode {
85
- /**
86
- * Migration is run whithout transaction
87
- */
88
- None = 0,
89
- /**
90
- * On transaction for one migration - every migration has its own
91
- */
92
- PerMigration = 1
93
- }
94
- /**
95
- * Configuration options to set in configuration file and used in OrmDriver
96
- */
97
- export interface IDriverOptions {
98
- /**
99
- * Max connections limit
100
- */
101
- PoolLimit?: number;
102
- /**
103
- * Database name associated with this connection
104
- */
105
- Database?: string;
106
- /**
107
- * User associatet with this connection
108
- */
109
- User?: string;
110
- /**
111
- * Password to database
112
- */
113
- Password?: string;
114
- /**
115
- * DB Host
116
- */
117
- Host?: string;
118
- /**
119
- * Connection port
120
- */
121
- Port?: number;
122
- /**
123
- * Connection encoding eg. utf-8
124
- */
125
- Encoding?: string;
126
- /**
127
- * Database filename eg. for Sqlite driver
128
- */
129
- Filename?: string;
130
- /**
131
- * Driver name eg. mysql, sqlite, mssql etc.
132
- */
133
- Driver: string;
134
- /**
135
- * Connection name for identification
136
- */
137
- Name: string;
138
- /**
139
- * Additional driver-specific options
140
- */
141
- Options?: any;
142
- /**
143
- * If this is set, database connection will be made by ssh tunnel
144
- */
145
- SSH?: {
146
- Host: string;
147
- Port: number;
148
- PrivateKey: string;
149
- User: string;
150
- };
151
- Migration?: {
152
- /**
153
- * Should run migration on startup
154
- */
155
- OnStartup?: boolean;
156
- /**
157
- * Migration table name, if not set default is spinajs_orm_migrations
158
- */
159
- Table?: string;
160
- /**
161
- * Migration transaction options
162
- */
163
- Transaction?: {
164
- /**
165
- * How to run migration - with or without transaction
166
- */
167
- Mode?: MigrationTransactionMode;
168
- };
169
- };
170
- /**
171
- * When building queries with auto generated tables & fields
172
- * we wrap them in special caharacter eg. $
173
- * Different sql engines allows different characters,
174
- * SQLITE & MYSQL allow to use $ in queries, but MSSQL its special characted used to create pseudocolumn
175
- *
176
- * Example: SELECT $users$.Name FROM users as $users$
177
- */
178
- AliasSeparator?: string;
179
- /**
180
- * Is this connection default. Later can be referenced under 'default' name
181
- * eg. @Connection('default')
182
- */
183
- DefaultConnection?: boolean;
184
- }
185
- export interface IMigrationDescriptor {
186
- /**
187
- * Whitch connection migration will be executed
188
- */
189
- Connection: string;
190
- }
191
- export interface IValueConverterDescriptor {
192
- Class: Constructor<ValueConverter>;
193
- Options?: any;
194
- }
195
- /**
196
- * Describes model, used internally
197
- */
198
- export interface IModelDescriptor {
199
- /**
200
- * Primary key name
201
- */
202
- PrimaryKey: string;
203
- /**
204
- * Connection name, must be avaible in db config
205
- */
206
- Connection: string;
207
- /**
208
- * Table name in database for this model
209
- */
210
- TableName: string;
211
- /**
212
- * Optional, describes timestamps in model
213
- */
214
- Timestamps: IModelTimestampDescriptor;
215
- /**
216
- * Optional, describes soft delete
217
- */
218
- SoftDelete: IModelSoftDeleteDescriptor;
219
- /**
220
- * Optional, is archive mode enabled
221
- */
222
- Archived: IModelArchivedDescriptor;
223
- /**
224
- * Column / fields list in model
225
- */
226
- Columns: IColumnDescriptor[];
227
- /**
228
- * Converters attached to fields
229
- */
230
- Converters: Map<string, IValueConverterDescriptor>;
231
- /**
232
- * List of unique columns ( UNIQUE constraint )
233
- */
234
- JunctionModelProperties: IJunctionProperty[];
235
- /**
236
- * List of relations in model
237
- */
238
- Relations: Map<string, IRelationDescriptor>;
239
- /** Name of model */
240
- Name: string;
241
- /**
242
- * Model discrimination map that allows to create different models based on db field value
243
- */
244
- DiscriminationMap: IDiscriminationMap;
245
- /**
246
- * Orm driver that this model
247
- */
248
- Driver: OrmDriver;
249
- /**
250
- * Json schema for validation
251
- */
252
- Schema: any;
253
- }
254
- export interface IDiscriminationMap {
255
- /**
256
- * DB field that holds inheritance value
257
- */
258
- Field: string;
259
- /**
260
- * Field values mapped for proper models
261
- */
262
- Models: Map<string, Constructor<ModelBase>>;
263
- }
264
- export interface IDiscriminationEntry {
265
- Key: string;
266
- Value: Constructor<ModelBase>;
267
- }
268
- export declare enum RelationType {
269
- One = 0,
270
- Many = 1,
271
- ManyToMany = 2
272
- }
273
- export type ForwardRefFunction = () => Constructor<ModelBase>;
274
- /**
275
- * Returns result of last insert or affected rows ( eg. rows affected would be 0 if insert is ignored )
276
- */
277
- export interface IUpdateResult {
278
- RowsAffected: number;
279
- LastInsertId: number;
280
- }
281
- export interface IRelationDescriptor {
282
- /**
283
- * Name of relations, defaults for property name in model that owns relation
284
- */
285
- Name: string;
286
- /**
287
- * Is it one-to-one, one-to-many or many-to-many
288
- */
289
- Type: RelationType;
290
- TargetModelType: Constructor<ModelBase> | ForwardRefFunction | string;
291
- /**
292
- * Relation model ( foreign )
293
- */
294
- TargetModel: Constructor<ModelBase> & IModelStatic;
295
- /**
296
- * Relation owner
297
- */
298
- SourceModel: Constructor<ModelBase> & IModelStatic;
299
- /**
300
- * Relation foreign key (one to one, one to many)
301
- */
302
- ForeignKey: string;
303
- /**
304
- * Relation primary key (one to one, one to many)
305
- */
306
- PrimaryKey: string;
307
- /**
308
- * Used in many to many relations, model for join table
309
- */
310
- JunctionModel?: Constructor<ModelBase>;
311
- /**
312
- * Join table foreign keys, defaults to auto generated field names. Can be override.
313
- */
314
- JunctionModelTargetModelFKey_Name?: string;
315
- JunctionModelSourceModelFKey_Name?: string;
316
- /**
317
- * Is this relation recursive ? Used for hierarchical / paren one-to-one relations
318
- */
319
- Recursive: boolean;
320
- /**
321
- * Relation factory, sometimes we dont want to create standard relation object
322
- */
323
- Factory?: (model: ModelBase<unknown>, relation: IRelationDescriptor, container: IContainer) => Relation<ModelBase<unknown>, ModelBase<unknown>>;
324
- /**
325
- * sometimes we dont want to create standard relation object, so we create type
326
- * that is passed in this property
327
- */
328
- RelationClass?: Constructor<Relation<ModelBase<unknown>, ModelBase<unknown>>>;
329
- }
330
- export interface IModelStatic extends Constructor<ModelBase<unknown>> {
331
- where<T extends typeof ModelBase>(val: boolean): ISelectQueryBuilder<Array<InstanceType<T>>>;
332
- where<T extends typeof ModelBase>(val: PartialArray<InstanceType<T>> | PickRelations<T>): ISelectQueryBuilder<Array<InstanceType<T>>>;
333
- where<T extends typeof ModelBase>(func: WhereFunction<InstanceType<T>>): ISelectQueryBuilder<Array<InstanceType<T>>>;
334
- where<T extends typeof ModelBase>(column: string, operator: Op, value: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
335
- where<T extends typeof ModelBase>(column: string, value: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
336
- where<T extends typeof ModelBase>(statement: Wrap): ISelectQueryBuilder<Array<InstanceType<T>>>;
337
- where<T extends typeof ModelBase>(column: string | boolean | WhereFunction<InstanceType<T>> | RawQuery | PartialArray<InstanceType<T>> | Wrap | PickRelations<T>, operator?: Op | any, value?: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
338
- where<T extends typeof ModelBase>(column: string | boolean | WhereFunction<InstanceType<T>> | RawQuery | PartialArray<InstanceType<T>> | Wrap | PickRelations<T>, _operator?: Op | any, _value?: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
339
- destroy<T extends typeof ModelBase>(pk?: any | any[]): IDeleteQueryBuilder<InstanceType<T>> & Promise<IUpdateResult>;
340
- create<T extends typeof ModelBase>(data: Partial<InstanceType<T>>): Promise<InstanceType<T>>;
341
- query<T extends typeof ModelBase>(): ISelectQueryBuilder<Array<InstanceType<T>>>;
342
- count<T extends typeof ModelBase>(callback?: (builder: IWhereBuilder<T>) => void): Promise<number>;
343
- count(): Promise<number>;
344
- update<T extends typeof ModelBase>(data: Partial<InstanceType<T>>): IUpdateQueryBuilder<InstanceType<T>>;
345
- exists(pk: any): Promise<boolean>;
346
- get<T extends typeof ModelBase>(pk: any): Promise<InstanceType<T>>;
347
- insert<T extends typeof ModelBase>(data: InstanceType<T> | Partial<InstanceType<T>> | Array<InstanceType<T>> | Array<Partial<InstanceType<T>>>, insertBehaviour?: InsertBehaviour): Promise<IUpdateResult>;
348
- getModelDescriptor(): IModelDescriptor;
349
- getRelationDescriptor(relation: string): IRelationDescriptor;
350
- }
351
- export interface IModelBase {
352
- ModelDescriptor: IModelDescriptor;
353
- Container: IContainer;
354
- PrimaryKeyName: string;
355
- PrimaryKeyValue: any;
356
- getFlattenRelationModels(): IModelBase[];
357
- /**
358
- * Fills model with data. It only fills properties that exists in database
359
- *
360
- * @param data - data to fill
361
- */
362
- hydrate(data: Partial<this>): void;
363
- /**
364
- *
365
- * Attachess model to proper relation an sets foreign key
366
- *
367
- * @param data - model to attach
368
- */
369
- attach(data: ModelBase): void;
370
- /**
371
- * Extracts all data from model. It takes only properties that exists in DB. Does not dehydrate related data.
372
- *
373
- * @param omit - fields to omit
374
- */
375
- dehydrate(omit?: string[]): ModelData<this>;
376
- /**
377
- *
378
- * Extracts all data from model with relation data. Relation data are dehydrated recursively.
379
- *
380
- * @param omit - fields to omit
381
- */
382
- dehydrateWithRelations(omit?: string[]): ModelDataWithRelationData<this>;
383
- /**
384
- * deletes enitt from db. If model have SoftDelete decorator, model is marked as deleted
385
- */
386
- destroy(): Promise<void>;
387
- /**
388
- * If model can be in achived state - sets archived at date and saves it to db
389
- */
390
- archive(): Promise<void>;
391
- /**
392
- * Updates model to db
393
- */
394
- update(): Promise<void>;
395
- /**
396
- * Save all changes to db. It creates new entry id db or updates existing one if
397
- * primary key exists
398
- */
399
- insert(insertBehaviour: InsertBehaviour): Promise<IUpdateResult>;
400
- /**
401
- * Gets model data from database and returns as fresh instance.
402
- *
403
- * If primary key is not fetched, tries to load by columns with unique constraint.
404
- * If there is no unique columns or primary key, throws error
405
- */
406
- fresh(): Promise<this>;
407
- /**
408
- * Refresh model from database.
409
- *
410
- * If no primary key is set, tries to fetch data base on columns
411
- * with unique constraints. If none exists, throws exception
412
- */
413
- refresh(): Promise<void>;
414
- /**
415
- * Used for JSON serialization
416
- */
417
- toJSON(): any;
418
- driver(): OrmDriver;
419
- }
420
- export interface IJunctionProperty {
421
- Name: string;
422
- Model: Constructor<ModelBase>;
423
- }
424
- /**
425
- * Table column description, used in models to build schema, validate & other stuff
426
- */
427
- export interface IColumnDescriptor {
428
- /**
429
- * Columnt type eg int, varchar, text
430
- */
431
- Type: string;
432
- /**
433
- * Max character lenght handled in column
434
- */
435
- MaxLength: number;
436
- /**
437
- * Column comment, use it for documentation purposes.
438
- */
439
- Comment: string;
440
- /**
441
- * Default column value
442
- */
443
- DefaultValue: any;
444
- /**
445
- * Full database type with size/length info & sign eg. int(10) unsigned if avaible
446
- */
447
- NativeType: string;
448
- /**
449
- * Numeric types sign
450
- */
451
- Unsigned: boolean;
452
- /**
453
- * Is column nullable (can be null)
454
- */
455
- Nullable: boolean;
456
- /**
457
- * Is column primary key
458
- */
459
- PrimaryKey: boolean;
460
- /**
461
- * Is column auto increment
462
- */
463
- AutoIncrement: boolean;
464
- /**
465
- * Column name
466
- */
467
- Name: string;
468
- /**
469
- * Value converter between database & model
470
- */
471
- Converter: IValueConverter;
472
- /**
473
- * JSON schema definition build for this column. Used to automate data validation
474
- */
475
- Schema: any;
476
- /**
477
- * Does have unique constraint
478
- */
479
- Unique: boolean;
480
- /**
481
- * Is uuid generated column
482
- */
483
- Uuid: boolean;
484
- Ignore: boolean;
485
- IsForeignKey: boolean;
486
- ForeignKeyDescription: {
487
- From: string;
488
- To: string;
489
- Table: string;
490
- };
491
- }
492
- /**
493
- * Value converter between model & database data types
494
- */
495
- export interface IValueConverter {
496
- /**
497
- * Converts value to database type
498
- *
499
- * @param value - value to convert
500
- */
501
- toDB(value: any, model: ModelBase, options: any): any;
502
- /**
503
- * Converts value from database type eg. mysql timestamp to DateTime
504
- *
505
- * @param value - value to convert
506
- */
507
- fromDB(value: any, rawData: any, options: any): any;
508
- }
509
- /**
510
- * Model timestamps description
511
- */
512
- export interface IModelTimestampDescriptor {
513
- /**
514
- * Created at column name
515
- */
516
- CreatedAt: string;
517
- /**
518
- * Updated at column name
519
- */
520
- UpdatedAt: string;
521
- }
522
- /**
523
- * Model soft delete description
524
- */
525
- export interface IModelSoftDeleteDescriptor {
526
- /**
527
- * Deleted at column name
528
- */
529
- DeletedAt: string;
530
- }
531
- export declare abstract class OrmMigration {
532
- /**
533
- *
534
- * Migrate up - create tables, indices etc.
535
- * Be aware that model function are not avaible yet. To fill tables with
536
- * data use fill function
537
- */
538
- abstract up(connection: OrmDriver): Promise<void>;
539
- /**
540
- * Migrate down - undo changes made in up
541
- */
542
- abstract down(connection: OrmDriver): Promise<void>;
543
- }
544
- /**
545
- * Model archived description
546
- */
547
- export interface IModelArchivedDescriptor {
548
- /**
549
- * Archived at column name
550
- */
551
- ArchivedAt: string;
552
- }
553
- export interface IQueryLimit {
554
- limit?: number;
555
- offset?: number;
556
- }
557
- export interface ISort {
558
- column: string;
559
- order: SortOrder;
560
- }
561
- export interface IQueryBuilder {
562
- Table: string;
563
- TableAlias: string;
564
- Database: string;
565
- database(database: string): IQueryBuilder;
566
- from(table: string, alias?: string): this;
567
- setAlias(alias: string): this;
568
- Driver: OrmDriver;
569
- Container: IContainer;
570
- }
571
- export interface ILimitBuilder<T> {
572
- take(count: number): this;
573
- skip(count: number): this;
574
- first(): Promise<Unbox<T>>;
575
- takeFirst(): this;
576
- firstOrFail(): Promise<Unbox<T>>;
577
- firstOrThrow(error: Error): Promise<Unbox<T>>;
578
- orThrow(error: Error): Promise<Unbox<T>>;
579
- getLimits(): IQueryLimit;
580
- }
581
- export interface IOrderByBuilder {
582
- orderBy(column: string): this;
583
- orderByDescending(column: string): this;
584
- order(column: string, direction: 'ASC' | 'DESC'): this;
585
- getSort(): ISort;
586
- }
587
- export interface IColumnsBuilder {
588
- /**
589
- * clears selected columns
590
- */
591
- clearColumns(): this;
592
- /**
593
- *
594
- * Select columns from db result ( multiple at once )
595
- *
596
- * @param names - column names to select
597
- */
598
- columns(names: string[]): this;
599
- /**
600
- * Return selected columns in this query
601
- */
602
- getColumns(): IQueryStatement[];
603
- /**
604
- * Selects single column from DB result with optional alias
605
- * Can be used multiple times
606
- *
607
- * @param column - column to select
608
- * @param alias - column alias ( optional )
609
- */
610
- select(column: string, alias?: string): this;
611
- /**
612
- * Selects custom values from DB. eg. Count(*)
613
- *
614
- * @param rawQuery - raw query to be executed
615
- */
616
- select(rawQuery: RawQuery): this;
617
- /**
618
- * Selects multiple columns at once with aliases. Map key property is column name, value is its alias
619
- *
620
- * @param columns - column list with aliases
621
- */
622
- select(columns: Map<string, string>): this;
623
- }
624
- export interface IWhereBuilder<T> {
625
- Statements: IQueryStatement[];
626
- Op: WhereBoolean;
627
- when(condition: boolean, callback?: WhereFunction<T>, callbackElse?: WhereFunction<T>): this;
628
- where(val: boolean): this;
629
- where(val: Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>): this;
630
- where(func: WhereFunction<T>): this;
631
- where(column: string, operator: Op, value: any): this;
632
- where(column: string, value: any): this;
633
- where(statement: Wrap): this;
634
- where(column: string | boolean | WhereFunction<T> | RawQuery | Partial<ModelDataWithRelationDataSearchable<Unbox<T>>> | Wrap, operator?: Op | any, value?: any): this;
635
- orWhere(val: boolean): this;
636
- orWhere(val: Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>): this;
637
- orWhere(func: WhereFunction<T>): this;
638
- orWhere(column: string, operator: Op, value: any): this;
639
- orWhere(column: string, value: any): this;
640
- orWhere(statement: Wrap): this;
641
- orWhere(column: string | boolean | WhereFunction<T> | RawQuery | Wrap | Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>, operator?: Op | any, value?: any): this;
642
- andWhere(val: boolean): this;
643
- andWhere(val: Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>): this;
644
- andWhere(func: WhereFunction<T>): this;
645
- andWhere(column: string, operator: Op, value: any): this;
646
- andWhere(column: string, value: any): this;
647
- andWhere(statement: Wrap): this;
648
- andWhere(column: string | boolean | WhereFunction<T> | RawQuery | Wrap | Partial<ModelData<Unbox<T>>>, operator?: Op | any, value?: any): this;
649
- whereObject(obj: Partial<ModelData<Unbox<T>>>): this;
650
- whereNotNull(column: string): this;
651
- whereNull(column: string): this;
652
- whereNot(column: string, val: unknown): this;
653
- whereIn(column: string, val: unknown[]): this;
654
- whereNotIn(column: string, val: unknown[]): this;
655
- whereExist(query: ISelectQueryBuilder<T>): this;
656
- whereNotExists(query: ISelectQueryBuilder<T>): this;
657
- whereBetween(column: string, val: unknown[]): this;
658
- whereNotBetween(column: string, val: unknown[]): this;
659
- whereInSet(column: string, val: unknown[]): this;
660
- whereNotInSet(column: string, val: unknown[]): this;
661
- clearWhere(): this;
662
- }
663
- export interface IWithRecursiveBuilder {
664
- CteRecursive: IQueryStatement;
665
- withRecursive(recKeyName: string, pkKeyName: string): this;
666
- }
667
- export interface IGroupByBuilder {
668
- GroupStatements: IQueryStatement[];
669
- clearGroupBy(): this;
670
- groupBy(expression: RawQuery | string): this;
671
- }
672
- /**
673
- * Dummy abstract class for allowing to add extensions for builder via declaration merging & mixins
674
- */
675
- export interface ISelectBuilderExtensions<T> {
676
- }
677
- export interface IJoinBuilder {
678
- JoinStatements: IQueryStatement[];
679
- clearJoins(): this;
680
- innerJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
681
- innerJoin(query: RawQuery): this;
682
- innerJoin(table: string, foreignKey: string, primaryKey: string): this;
683
- innerJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string): this;
684
- leftJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
685
- leftJoin(query: RawQuery): this;
686
- leftJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
687
- leftJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
688
- leftOuterJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
689
- leftOuterJoin(query: RawQuery): this;
690
- leftOuterJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
691
- leftOuterJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
692
- rightJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
693
- rightJoin(query: RawQuery): this;
694
- rightJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
695
- rightJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
696
- rightOuterJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
697
- rightOuterJoin(query: RawQuery): this;
698
- rightOuterJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
699
- rightOuterJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
700
- fullOuterJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
701
- fullOuterJoin(query: RawQuery): this;
702
- fullOuterJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
703
- fullOuterJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
704
- crossJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
705
- crossJoin(query: RawQuery): this;
706
- crossJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
707
- crossJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
708
- }
709
- export interface IBuilder<T> extends PromiseLike<T> {
710
- middleware(middleware: IBuilderMiddleware<T>): this;
711
- toDB(): ICompilerOutput | ICompilerOutput[];
712
- }
713
- export interface IUpdateQueryBuilder<T> extends IColumnsBuilder, IWhereBuilder<T> {
714
- }
715
- export interface IDeleteQueryBuilder<T> extends IWhereBuilder<T>, ILimitBuilder<T> {
716
- }
717
- export interface ISelectQueryBuilder<T = unknown> extends IColumnsBuilder, IOrderByBuilder, ILimitBuilder<T>, IWhereBuilder<T>, IJoinBuilder, IWithRecursiveBuilder, IGroupByBuilder, IQueryBuilder, IBuilder<T> {
718
- min(column: string, as?: string): this;
719
- max(column: string, as?: string): this;
720
- count(column: string, as?: string): this;
721
- sum(column: string, as?: string): this;
722
- avg(column: string, as?: string): this;
723
- setAlias(alias: string): this;
724
- setTable(table: string, alias?: string): this;
725
- distinct(): this;
726
- clone(): this;
727
- populate<R = this>(relation: {} | null, callback?: (this: ISelectQueryBuilder<R>, relation: IOrmRelation) => void): this;
728
- populate<R = this>(relation: string, callback?: (this: ISelectQueryBuilder<R>, relation: IOrmRelation) => void): this;
729
- asRaw<T>(): Promise<T>;
730
- /**
731
- * Returns all records. Its for type castin when using with scopes mostly.
732
- */
733
- all(): Promise<T[]>;
734
- }
735
- export interface ICompilerOutput {
736
- expression: string;
737
- bindings: any[];
738
- }
739
- export interface IQueryCompiler {
740
- compile(): ICompilerOutput | ICompilerOutput[];
741
- }
742
- export interface ILimitCompiler {
743
- limit(builder: ILimitBuilder<any>): ICompilerOutput;
744
- }
745
- export interface IGroupByCompiler {
746
- group(builder: IGroupByBuilder): ICompilerOutput;
747
- }
748
- export interface IRecursiveCompiler {
749
- recursive(builder: IWithRecursiveBuilder): ICompilerOutput;
750
- }
751
- export interface IColumnsCompiler {
752
- columns(builder: IColumnsBuilder): ICompilerOutput;
753
- }
754
- export interface IWhereCompiler {
755
- where(builder: IWhereBuilder<any>): ICompilerOutput;
756
- }
757
- export interface IJoinCompiler {
758
- join(builder: IJoinBuilder): ICompilerOutput;
759
- }
760
- /**
761
- * Definitions of query compiler are needed for DI resolving
762
- * ==========================================================
763
- */
764
- export declare abstract class RecursiveQueryCompiler implements IQueryCompiler {
765
- abstract compile(): ICompilerOutput;
766
- }
767
- export declare abstract class SelectQueryCompiler implements IQueryCompiler {
768
- abstract compile(): ICompilerOutput;
769
- }
770
- export declare abstract class JoinQueryCompiler implements IQueryCompiler {
771
- abstract compile(): ICompilerOutput;
772
- }
773
- export declare abstract class IndexQueryCompiler implements IQueryCompiler {
774
- abstract compile(): ICompilerOutput;
775
- }
776
- export declare abstract class LimitQueryCompiler implements IQueryCompiler {
777
- abstract compile(): ICompilerOutput;
778
- }
779
- export declare abstract class ForeignKeyQueryCompiler implements IQueryCompiler {
780
- abstract compile(): ICompilerOutput;
781
- }
782
- export declare abstract class DeleteQueryCompiler implements IQueryCompiler {
783
- abstract compile(): ICompilerOutput;
784
- }
785
- export declare abstract class UpdateQueryCompiler implements IQueryCompiler {
786
- abstract compile(): ICompilerOutput;
787
- }
788
- export declare abstract class InsertQueryCompiler implements IQueryCompiler {
789
- abstract compile(): ICompilerOutput;
790
- }
791
- export declare abstract class OnDuplicateQueryCompiler implements IQueryCompiler {
792
- abstract compile(): ICompilerOutput;
793
- }
794
- export declare abstract class TableQueryCompiler implements IQueryCompiler {
795
- abstract compile(): ICompilerOutput[] | ICompilerOutput;
796
- }
797
- export declare abstract class TableHistoryQueryCompiler implements IQueryCompiler {
798
- abstract compile(): ICompilerOutput[];
799
- }
800
- export declare abstract class TruncateTableQueryCompiler implements IQueryCompiler {
801
- abstract compile(): ICompilerOutput;
802
- }
803
- export declare abstract class TableCloneQueryCompiler implements IQueryCompiler {
804
- abstract compile(): ICompilerOutput[];
805
- }
806
- export declare abstract class EventQueryCompiler implements IQueryCompiler {
807
- abstract compile(): ICompilerOutput[];
808
- }
809
- export declare abstract class DropEventQueryCompiler implements IQueryCompiler {
810
- abstract compile(): ICompilerOutput[];
811
- }
812
- export declare abstract class AlterTableQueryCompiler implements IQueryCompiler {
813
- abstract compile(): ICompilerOutput[];
814
- }
815
- export declare abstract class TableExistsCompiler implements IQueryCompiler {
816
- abstract compile(): ICompilerOutput;
817
- }
818
- export declare abstract class DropTableCompiler implements IQueryCompiler {
819
- abstract compile(): ICompilerOutput;
820
- }
821
- export declare abstract class ColumnQueryCompiler implements IQueryCompiler {
822
- abstract compile(): ICompilerOutput;
823
- }
824
- export declare abstract class AlterColumnQueryCompiler implements IQueryCompiler {
825
- abstract compile(): ICompilerOutput;
826
- }
827
- export declare abstract class OrderByQueryCompiler implements IQueryCompiler {
828
- abstract compile(): ICompilerOutput;
829
- }
830
- export declare abstract class GroupByQueryCompiler implements IQueryCompiler {
831
- abstract compile(): ICompilerOutput;
832
- }
833
- /**
834
- * ==========================================================
835
- */
836
- /**
837
- * Middlewares for query builders
838
- */
839
- export interface IBuilderMiddleware<T = any[]> {
840
- /**
841
- *
842
- * Executed AFTER query is executed in DB and raw data is fetched
843
- * Use it to transform DB data before everything else
844
- *
845
- * @param data - raw data fetched from DB
846
- */
847
- afterQuery(data: T): T;
848
- /**
849
- * Executed when model is about to create. Use it to
850
- * override model creation logic. If null is returned, default model
851
- * is executed
852
- *
853
- * @param data - raw data to create
854
- */
855
- modelCreation(data: any): ModelBase;
856
- /**
857
- * executed after model was created ( all returned data by query is executed)
858
- *
859
- * @param data - hydrated data. Models are created and hydrated with data
860
- */
861
- afterHydration(data: ModelBase[]): Promise<any[] | void>;
862
- }
863
- export declare abstract class QueryMiddleware {
864
- abstract afterQueryCreation(query: QueryBuilder): void;
865
- abstract beforeQueryExecution(query: QueryBuilder): void;
866
- }
867
- export declare abstract class ModelMiddleware {
868
- abstract onDelete(model: ModelBase): Promise<void>;
869
- abstract onUpdate(model: ModelBase): Promise<void>;
870
- abstract onInsert(model: ModelBase): Promise<void>;
871
- abstract onSelect(model: ModelBase): Promise<void>;
872
- }
873
- export declare class ValueConverter implements IValueConverter {
874
- /**
875
- * Converts value to database type
876
- *
877
- * @param value - value to convert
878
- */
879
- toDB(_value: any, _model: ModelBase<any>, _options: any): any;
880
- /**
881
- * Converts value from database type eg. mysql timestamp to DateTime
882
- *
883
- * @param value - value to convert
884
- */
885
- fromDB(_value: any, _rawData: any, _options: any): any;
886
- }
887
- /**
888
- * Converter for DATETIME field (eg. mysql datetime)
889
- */
890
- export declare class DatetimeValueConverter extends ValueConverter {
891
- }
892
- /**
893
- * Converter for set field (eg. mysql SET)
894
- */
895
- export declare class SetValueConverter extends ValueConverter {
896
- }
897
- export declare abstract class TableAliasCompiler {
898
- abstract compile(builder: QueryBuilder, tbl?: string): string;
899
- }
900
- export interface IUniversalConverterOptions {
901
- TypeColumn: string;
902
- }
903
- /**
904
- * base class for select & where builder for defining scopes
905
- */
906
- export declare abstract class QueryScope {
907
- }
908
- export interface IHistoricalModel {
909
- readonly __action__: 'insert' | 'update' | 'delete';
910
- readonly __revision__: number;
911
- readonly __start__: DateTime;
912
- readonly __end__: DateTime;
913
- }
914
- export declare abstract class ModelToSqlConverter {
915
- abstract toSql(model: ModelBase<unknown>): unknown;
916
- }
917
- export declare abstract class ObjectToSqlConverter {
918
- abstract toSql(model: unknown): unknown;
919
- }
1
+ import { Op } from './enums.js';
2
+ import { QueryBuilder, RawQuery } from './builders.js';
3
+ import { SortOrder, WhereBoolean } from './enums.js';
4
+ import { IQueryStatement, Wrap } from './statements.js';
5
+ import { ModelData, ModelDataWithRelationData, ModelDataWithRelationDataSearchable, PartialArray, PickRelations, Unbox, WhereFunction } from './types.js';
6
+ import { IOrmRelation } from './relations.js';
7
+ import { OrmDriver } from './driver.js';
8
+ import { Constructor, IContainer } from '@spinajs/di';
9
+ import { ModelBase } from './model.js';
10
+ import { DateTime } from 'luxon';
11
+ import { Relation } from './relation-objects.js';
12
+ export declare enum QueryContext {
13
+ Insert = 0,
14
+ Select = 1,
15
+ Update = 2,
16
+ Delete = 3,
17
+ Schema = 4,
18
+ Transaction = 5
19
+ }
20
+ export declare enum ColumnAlterationType {
21
+ Add = 0,
22
+ Modify = 1,
23
+ Rename = 2
24
+ }
25
+ export interface IRelation {
26
+ TargetModelDescriptor: IModelDescriptor;
27
+ /**
28
+ * Indicates if data was fetched from db
29
+ */
30
+ Populated: boolean;
31
+ }
32
+ export declare abstract class DefaultValueBuilder<T> {
33
+ Query: RawQuery;
34
+ Value: string | number;
35
+ /**
36
+ * fills by default with current date
37
+ */
38
+ abstract date(): T;
39
+ /**
40
+ * fills by default with current datetime
41
+ */
42
+ abstract dateTime(): T;
43
+ /**
44
+ * Fills column with default value
45
+ *
46
+ * @param val - value to fill
47
+ */
48
+ abstract value(val: string | number): T;
49
+ /**
50
+ * Fills column with result of query provided
51
+ *
52
+ * @param query - raw query instance
53
+ */
54
+ abstract raw(query: RawQuery): T;
55
+ }
56
+ export declare enum InsertBehaviour {
57
+ /**
58
+ * Ignores if primary key exists in db
59
+ */
60
+ InsertOrIgnore = 0,
61
+ /**
62
+ * Updates entry if pk exists
63
+ */
64
+ InsertOrUpdate = 1,
65
+ /**
66
+ * Replaces entry if pk exists
67
+ */
68
+ InsertOrReplace = 2,
69
+ None = 3
70
+ }
71
+ /**
72
+ * Foreign key referential actions
73
+ */
74
+ export declare enum ReferentialAction {
75
+ Cascade = "CASCADE",
76
+ SetNull = "SET NULL",
77
+ Restrict = "RESTRICT",
78
+ NoAction = "NO ACTION",
79
+ SetDefault = "SET DEFAULT"
80
+ }
81
+ /**
82
+ * Transaction mode when migration DB
83
+ */
84
+ export declare enum MigrationTransactionMode {
85
+ /**
86
+ * Migration is run whithout transaction
87
+ */
88
+ None = 0,
89
+ /**
90
+ * On transaction for one migration - every migration has its own
91
+ */
92
+ PerMigration = 1
93
+ }
94
+ /**
95
+ * Configuration options to set in configuration file and used in OrmDriver
96
+ */
97
+ export interface IDriverOptions {
98
+ /**
99
+ * Max connections limit
100
+ */
101
+ PoolLimit?: number;
102
+ /**
103
+ * Database name associated with this connection
104
+ */
105
+ Database?: string;
106
+ /**
107
+ * User associatet with this connection
108
+ */
109
+ User?: string;
110
+ /**
111
+ * Password to database
112
+ */
113
+ Password?: string;
114
+ /**
115
+ * DB Host
116
+ */
117
+ Host?: string;
118
+ /**
119
+ * Connection port
120
+ */
121
+ Port?: number;
122
+ /**
123
+ * Connection encoding eg. utf-8
124
+ */
125
+ Encoding?: string;
126
+ /**
127
+ * Database filename eg. for Sqlite driver
128
+ */
129
+ Filename?: string;
130
+ /**
131
+ * Driver name eg. mysql, sqlite, mssql etc.
132
+ */
133
+ Driver: string;
134
+ /**
135
+ * Connection name for identification
136
+ */
137
+ Name: string;
138
+ /**
139
+ * Additional driver-specific options
140
+ */
141
+ Options?: any;
142
+ /**
143
+ * If this is set, database connection will be made by ssh tunnel
144
+ */
145
+ SSH?: {
146
+ Host: string;
147
+ Port: number;
148
+ PrivateKey: string;
149
+ User: string;
150
+ };
151
+ Migration?: {
152
+ /**
153
+ * Should run migration on startup
154
+ */
155
+ OnStartup?: boolean;
156
+ /**
157
+ * Migration table name, if not set default is spinajs_orm_migrations
158
+ */
159
+ Table?: string;
160
+ /**
161
+ * Migration transaction options
162
+ */
163
+ Transaction?: {
164
+ /**
165
+ * How to run migration - with or without transaction
166
+ */
167
+ Mode?: MigrationTransactionMode;
168
+ };
169
+ };
170
+ /**
171
+ * When building queries with auto generated tables & fields
172
+ * we wrap them in special caharacter eg. $
173
+ * Different sql engines allows different characters,
174
+ * SQLITE & MYSQL allow to use $ in queries, but MSSQL its special characted used to create pseudocolumn
175
+ *
176
+ * Example: SELECT $users$.Name FROM users as $users$
177
+ */
178
+ AliasSeparator?: string;
179
+ /**
180
+ * Is this connection default. Later can be referenced under 'default' name
181
+ * eg. @Connection('default')
182
+ */
183
+ DefaultConnection?: boolean;
184
+ }
185
+ export interface IMigrationDescriptor {
186
+ /**
187
+ * Whitch connection migration will be executed
188
+ */
189
+ Connection: string;
190
+ }
191
+ export interface IValueConverterDescriptor {
192
+ Class: Constructor<ValueConverter>;
193
+ Options?: any;
194
+ }
195
+ /**
196
+ * Describes model, used internally
197
+ */
198
+ export interface IModelDescriptor {
199
+ /**
200
+ * Primary key name
201
+ */
202
+ PrimaryKey: string;
203
+ /**
204
+ * Connection name, must be avaible in db config
205
+ */
206
+ Connection: string;
207
+ /**
208
+ * Table name in database for this model
209
+ */
210
+ TableName: string;
211
+ /**
212
+ * Optional, describes timestamps in model
213
+ */
214
+ Timestamps: IModelTimestampDescriptor;
215
+ /**
216
+ * Optional, describes soft delete
217
+ */
218
+ SoftDelete: IModelSoftDeleteDescriptor;
219
+ /**
220
+ * Optional, is archive mode enabled
221
+ */
222
+ Archived: IModelArchivedDescriptor;
223
+ /**
224
+ * Column / fields list in model
225
+ */
226
+ Columns: IColumnDescriptor[];
227
+ /**
228
+ * Converters attached to fields
229
+ */
230
+ Converters: Map<string, IValueConverterDescriptor>;
231
+ /**
232
+ * List of unique columns ( UNIQUE constraint )
233
+ */
234
+ JunctionModelProperties: IJunctionProperty[];
235
+ /**
236
+ * List of relations in model
237
+ */
238
+ Relations: Map<string, IRelationDescriptor>;
239
+ /** Name of model */
240
+ Name: string;
241
+ /**
242
+ * Model discrimination map that allows to create different models based on db field value
243
+ */
244
+ DiscriminationMap: IDiscriminationMap;
245
+ /**
246
+ * Orm driver that this model
247
+ */
248
+ Driver: OrmDriver;
249
+ /**
250
+ * Json schema for validation
251
+ */
252
+ Schema: any;
253
+ }
254
+ export interface IDiscriminationMap {
255
+ /**
256
+ * DB field that holds inheritance value
257
+ */
258
+ Field: string;
259
+ /**
260
+ * Field values mapped for proper models
261
+ */
262
+ Models: Map<string, Constructor<ModelBase>>;
263
+ }
264
+ export interface IDiscriminationEntry {
265
+ Key: string;
266
+ Value: Constructor<ModelBase>;
267
+ }
268
+ export declare enum RelationType {
269
+ One = 0,
270
+ Many = 1,
271
+ ManyToMany = 2
272
+ }
273
+ export type ForwardRefFunction = () => Constructor<ModelBase>;
274
+ /**
275
+ * Returns result of last insert or affected rows ( eg. rows affected would be 0 if insert is ignored )
276
+ */
277
+ export interface IUpdateResult {
278
+ RowsAffected: number;
279
+ LastInsertId: number;
280
+ }
281
+ export interface IRelationDescriptor {
282
+ /**
283
+ * Name of relations, defaults for property name in model that owns relation
284
+ */
285
+ Name: string;
286
+ /**
287
+ * Is it one-to-one, one-to-many or many-to-many
288
+ */
289
+ Type: RelationType;
290
+ TargetModelType: Constructor<ModelBase> | ForwardRefFunction | string;
291
+ /**
292
+ * Relation model ( foreign )
293
+ */
294
+ TargetModel: Constructor<ModelBase> & IModelStatic;
295
+ /**
296
+ * Relation owner
297
+ */
298
+ SourceModel: Constructor<ModelBase> & IModelStatic;
299
+ /**
300
+ * Relation foreign key (one to one, one to many)
301
+ */
302
+ ForeignKey: string;
303
+ /**
304
+ * Relation primary key (one to one, one to many)
305
+ */
306
+ PrimaryKey: string;
307
+ /**
308
+ * Used in many to many relations, model for join table
309
+ */
310
+ JunctionModel?: Constructor<ModelBase>;
311
+ /**
312
+ * Join table foreign keys, defaults to auto generated field names. Can be override.
313
+ */
314
+ JunctionModelTargetModelFKey_Name?: string;
315
+ JunctionModelSourceModelFKey_Name?: string;
316
+ /**
317
+ * Is this relation recursive ? Used for hierarchical / paren one-to-one relations
318
+ */
319
+ Recursive: boolean;
320
+ /**
321
+ * Relation factory, sometimes we dont want to create standard relation object
322
+ */
323
+ Factory?: (model: ModelBase<unknown>, relation: IRelationDescriptor, container: IContainer) => Relation<ModelBase<unknown>, ModelBase<unknown>>;
324
+ /**
325
+ * sometimes we dont want to create standard relation object, so we create type
326
+ * that is passed in this property
327
+ */
328
+ RelationClass?: Constructor<Relation<ModelBase<unknown>, ModelBase<unknown>>>;
329
+ }
330
+ export interface IModelStatic extends Constructor<ModelBase<unknown>> {
331
+ where<T extends typeof ModelBase>(val: boolean): ISelectQueryBuilder<Array<InstanceType<T>>>;
332
+ where<T extends typeof ModelBase>(val: PartialArray<InstanceType<T>> | PickRelations<T>): ISelectQueryBuilder<Array<InstanceType<T>>>;
333
+ where<T extends typeof ModelBase>(func: WhereFunction<InstanceType<T>>): ISelectQueryBuilder<Array<InstanceType<T>>>;
334
+ where<T extends typeof ModelBase>(column: string, operator: Op, value: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
335
+ where<T extends typeof ModelBase>(column: string, value: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
336
+ where<T extends typeof ModelBase>(statement: Wrap): ISelectQueryBuilder<Array<InstanceType<T>>>;
337
+ where<T extends typeof ModelBase>(column: string | boolean | WhereFunction<InstanceType<T>> | RawQuery | PartialArray<InstanceType<T>> | Wrap | PickRelations<T>, operator?: Op | any, value?: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
338
+ where<T extends typeof ModelBase>(column: string | boolean | WhereFunction<InstanceType<T>> | RawQuery | PartialArray<InstanceType<T>> | Wrap | PickRelations<T>, _operator?: Op | any, _value?: any): ISelectQueryBuilder<Array<InstanceType<T>>>;
339
+ destroy<T extends typeof ModelBase>(pk?: any | any[]): IDeleteQueryBuilder<InstanceType<T>> & Promise<IUpdateResult>;
340
+ create<T extends typeof ModelBase>(data: Partial<InstanceType<T>>): Promise<InstanceType<T>>;
341
+ query<T extends typeof ModelBase>(): ISelectQueryBuilder<Array<InstanceType<T>>>;
342
+ count<T extends typeof ModelBase>(callback?: (builder: IWhereBuilder<T>) => void): Promise<number>;
343
+ count(): Promise<number>;
344
+ update<T extends typeof ModelBase>(data: Partial<InstanceType<T>>): IUpdateQueryBuilder<InstanceType<T>>;
345
+ exists(pk: any): Promise<boolean>;
346
+ get<T extends typeof ModelBase>(pk: any): Promise<InstanceType<T>>;
347
+ insert<T extends typeof ModelBase>(data: InstanceType<T> | Partial<InstanceType<T>> | Array<InstanceType<T>> | Array<Partial<InstanceType<T>>>, insertBehaviour?: InsertBehaviour): Promise<IUpdateResult>;
348
+ getModelDescriptor(): IModelDescriptor;
349
+ getRelationDescriptor(relation: string): IRelationDescriptor;
350
+ }
351
+ export interface IModelBase {
352
+ ModelDescriptor: IModelDescriptor;
353
+ Container: IContainer;
354
+ PrimaryKeyName: string;
355
+ PrimaryKeyValue: any;
356
+ getFlattenRelationModels(): IModelBase[];
357
+ /**
358
+ * Fills model with data. It only fills properties that exists in database
359
+ *
360
+ * @param data - data to fill
361
+ */
362
+ hydrate(data: Partial<this>): void;
363
+ /**
364
+ *
365
+ * Attachess model to proper relation an sets foreign key
366
+ *
367
+ * @param data - model to attach
368
+ */
369
+ attach(data: ModelBase): void;
370
+ /**
371
+ * Extracts all data from model. It takes only properties that exists in DB. Does not dehydrate related data.
372
+ *
373
+ * @param omit - fields to omit
374
+ */
375
+ dehydrate(omit?: string[]): ModelData<this>;
376
+ /**
377
+ *
378
+ * Extracts all data from model with relation data. Relation data are dehydrated recursively.
379
+ *
380
+ * @param omit - fields to omit
381
+ */
382
+ dehydrateWithRelations(omit?: string[]): ModelDataWithRelationData<this>;
383
+ /**
384
+ * deletes enitt from db. If model have SoftDelete decorator, model is marked as deleted
385
+ */
386
+ destroy(): Promise<void>;
387
+ /**
388
+ * If model can be in achived state - sets archived at date and saves it to db
389
+ */
390
+ archive(): Promise<void>;
391
+ /**
392
+ * Updates model to db
393
+ */
394
+ update(): Promise<void>;
395
+ /**
396
+ * Save all changes to db. It creates new entry id db or updates existing one if
397
+ * primary key exists
398
+ */
399
+ insert(insertBehaviour: InsertBehaviour): Promise<IUpdateResult>;
400
+ /**
401
+ * Gets model data from database and returns as fresh instance.
402
+ *
403
+ * If primary key is not fetched, tries to load by columns with unique constraint.
404
+ * If there is no unique columns or primary key, throws error
405
+ */
406
+ fresh(): Promise<this>;
407
+ /**
408
+ * Refresh model from database.
409
+ *
410
+ * If no primary key is set, tries to fetch data base on columns
411
+ * with unique constraints. If none exists, throws exception
412
+ */
413
+ refresh(): Promise<void>;
414
+ /**
415
+ * Used for JSON serialization
416
+ */
417
+ toJSON(): any;
418
+ driver(): OrmDriver;
419
+ }
420
+ export interface IJunctionProperty {
421
+ Name: string;
422
+ Model: Constructor<ModelBase>;
423
+ }
424
+ /**
425
+ * Table column description, used in models to build schema, validate & other stuff
426
+ */
427
+ export interface IColumnDescriptor {
428
+ /**
429
+ * Columnt type eg int, varchar, text
430
+ */
431
+ Type: string;
432
+ /**
433
+ * Max character lenght handled in column
434
+ */
435
+ MaxLength: number;
436
+ /**
437
+ * Column comment, use it for documentation purposes.
438
+ */
439
+ Comment: string;
440
+ /**
441
+ * Default column value
442
+ */
443
+ DefaultValue: any;
444
+ /**
445
+ * Full database type with size/length info & sign eg. int(10) unsigned if avaible
446
+ */
447
+ NativeType: string;
448
+ /**
449
+ * Numeric types sign
450
+ */
451
+ Unsigned: boolean;
452
+ /**
453
+ * Is column nullable (can be null)
454
+ */
455
+ Nullable: boolean;
456
+ /**
457
+ * Is column primary key
458
+ */
459
+ PrimaryKey: boolean;
460
+ /**
461
+ * Is column auto increment
462
+ */
463
+ AutoIncrement: boolean;
464
+ /**
465
+ * Column name
466
+ */
467
+ Name: string;
468
+ /**
469
+ * Value converter between database & model
470
+ */
471
+ Converter: IValueConverter;
472
+ /**
473
+ * JSON schema definition build for this column. Used to automate data validation
474
+ */
475
+ Schema: any;
476
+ /**
477
+ * Does have unique constraint
478
+ */
479
+ Unique: boolean;
480
+ /**
481
+ * Is uuid generated column
482
+ */
483
+ Uuid: boolean;
484
+ Ignore: boolean;
485
+ IsForeignKey: boolean;
486
+ ForeignKeyDescription: {
487
+ From: string;
488
+ To: string;
489
+ Table: string;
490
+ };
491
+ }
492
+ /**
493
+ * Value converter between model & database data types
494
+ */
495
+ export interface IValueConverter {
496
+ /**
497
+ * Converts value to database type
498
+ *
499
+ * @param value - value to convert
500
+ */
501
+ toDB(value: any, model: ModelBase, options: any): any;
502
+ /**
503
+ * Converts value from database type eg. mysql timestamp to DateTime
504
+ *
505
+ * @param value - value to convert
506
+ */
507
+ fromDB(value: any, rawData: any, options: any): any;
508
+ }
509
+ /**
510
+ * Model timestamps description
511
+ */
512
+ export interface IModelTimestampDescriptor {
513
+ /**
514
+ * Created at column name
515
+ */
516
+ CreatedAt: string;
517
+ /**
518
+ * Updated at column name
519
+ */
520
+ UpdatedAt: string;
521
+ }
522
+ /**
523
+ * Model soft delete description
524
+ */
525
+ export interface IModelSoftDeleteDescriptor {
526
+ /**
527
+ * Deleted at column name
528
+ */
529
+ DeletedAt: string;
530
+ }
531
+ export declare abstract class OrmMigration {
532
+ /**
533
+ *
534
+ * Migrate up - create tables, indices etc.
535
+ * Be aware that model function are not avaible yet. To fill tables with
536
+ * data use fill function
537
+ */
538
+ abstract up(connection: OrmDriver): Promise<void>;
539
+ /**
540
+ * Migrate down - undo changes made in up
541
+ */
542
+ abstract down(connection: OrmDriver): Promise<void>;
543
+ }
544
+ /**
545
+ * Model archived description
546
+ */
547
+ export interface IModelArchivedDescriptor {
548
+ /**
549
+ * Archived at column name
550
+ */
551
+ ArchivedAt: string;
552
+ }
553
+ export interface IQueryLimit {
554
+ limit?: number;
555
+ offset?: number;
556
+ }
557
+ export interface ISort {
558
+ column: string;
559
+ order: SortOrder;
560
+ }
561
+ export interface IQueryBuilder {
562
+ Table: string;
563
+ TableAlias: string;
564
+ Database: string;
565
+ database(database: string): IQueryBuilder;
566
+ from(table: string, alias?: string): this;
567
+ setAlias(alias: string): this;
568
+ Driver: OrmDriver;
569
+ Container: IContainer;
570
+ }
571
+ export interface ILimitBuilder<T> {
572
+ take(count: number): this;
573
+ skip(count: number): this;
574
+ first(): Promise<Unbox<T>>;
575
+ takeFirst(): this;
576
+ firstOrFail(): Promise<Unbox<T>>;
577
+ firstOrThrow(error: Error): Promise<Unbox<T>>;
578
+ orThrow(error: Error): Promise<Unbox<T>>;
579
+ getLimits(): IQueryLimit;
580
+ }
581
+ export interface IOrderByBuilder {
582
+ orderBy(column: string): this;
583
+ orderByDescending(column: string): this;
584
+ order(column: string, direction: 'ASC' | 'DESC'): this;
585
+ getSort(): ISort;
586
+ }
587
+ export interface IColumnsBuilder {
588
+ /**
589
+ * clears selected columns
590
+ */
591
+ clearColumns(): this;
592
+ /**
593
+ *
594
+ * Select columns from db result ( multiple at once )
595
+ *
596
+ * @param names - column names to select
597
+ */
598
+ columns(names: string[]): this;
599
+ /**
600
+ * Return selected columns in this query
601
+ */
602
+ getColumns(): IQueryStatement[];
603
+ /**
604
+ * Selects single column from DB result with optional alias
605
+ * Can be used multiple times
606
+ *
607
+ * @param column - column to select
608
+ * @param alias - column alias ( optional )
609
+ */
610
+ select(column: string, alias?: string): this;
611
+ /**
612
+ * Selects custom values from DB. eg. Count(*)
613
+ *
614
+ * @param rawQuery - raw query to be executed
615
+ */
616
+ select(rawQuery: RawQuery): this;
617
+ /**
618
+ * Selects multiple columns at once with aliases. Map key property is column name, value is its alias
619
+ *
620
+ * @param columns - column list with aliases
621
+ */
622
+ select(columns: Map<string, string>): this;
623
+ }
624
+ export interface IWhereBuilder<T> {
625
+ Statements: IQueryStatement[];
626
+ Op: WhereBoolean;
627
+ when(condition: boolean, callback?: WhereFunction<T>, callbackElse?: WhereFunction<T>): this;
628
+ where(val: boolean): this;
629
+ where(val: Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>): this;
630
+ where(func: WhereFunction<T>): this;
631
+ where(column: string, operator: Op, value: any): this;
632
+ where(column: string, value: any): this;
633
+ where(statement: Wrap): this;
634
+ where(column: string | boolean | WhereFunction<T> | RawQuery | Partial<ModelDataWithRelationDataSearchable<Unbox<T>>> | Wrap, operator?: Op | any, value?: any): this;
635
+ orWhere(val: boolean): this;
636
+ orWhere(val: Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>): this;
637
+ orWhere(func: WhereFunction<T>): this;
638
+ orWhere(column: string, operator: Op, value: any): this;
639
+ orWhere(column: string, value: any): this;
640
+ orWhere(statement: Wrap): this;
641
+ orWhere(column: string | boolean | WhereFunction<T> | RawQuery | Wrap | Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>, operator?: Op | any, value?: any): this;
642
+ andWhere(val: boolean): this;
643
+ andWhere(val: Partial<ModelDataWithRelationDataSearchable<Unbox<T>>>): this;
644
+ andWhere(func: WhereFunction<T>): this;
645
+ andWhere(column: string, operator: Op, value: any): this;
646
+ andWhere(column: string, value: any): this;
647
+ andWhere(statement: Wrap): this;
648
+ andWhere(column: string | boolean | WhereFunction<T> | RawQuery | Wrap | Partial<ModelData<Unbox<T>>>, operator?: Op | any, value?: any): this;
649
+ whereObject(obj: Partial<ModelData<Unbox<T>>>): this;
650
+ whereNotNull(column: string): this;
651
+ whereNull(column: string): this;
652
+ whereNot(column: string, val: unknown): this;
653
+ whereIn(column: string, val: unknown[]): this;
654
+ whereNotIn(column: string, val: unknown[]): this;
655
+ whereExist(query: ISelectQueryBuilder<T>): this;
656
+ whereNotExists(query: ISelectQueryBuilder<T>): this;
657
+ whereBetween(column: string, val: unknown[]): this;
658
+ whereNotBetween(column: string, val: unknown[]): this;
659
+ whereInSet(column: string, val: unknown[]): this;
660
+ whereNotInSet(column: string, val: unknown[]): this;
661
+ clearWhere(): this;
662
+ }
663
+ export interface IWithRecursiveBuilder {
664
+ CteRecursive: IQueryStatement;
665
+ withRecursive(recKeyName: string, pkKeyName: string): this;
666
+ }
667
+ export interface IGroupByBuilder {
668
+ GroupStatements: IQueryStatement[];
669
+ clearGroupBy(): this;
670
+ groupBy(expression: RawQuery | string): this;
671
+ }
672
+ /**
673
+ * Dummy abstract class for allowing to add extensions for builder via declaration merging & mixins
674
+ */
675
+ export interface ISelectBuilderExtensions<T> {
676
+ }
677
+ export interface IJoinBuilder {
678
+ JoinStatements: IQueryStatement[];
679
+ clearJoins(): this;
680
+ innerJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
681
+ innerJoin(query: RawQuery): this;
682
+ innerJoin(table: string, foreignKey: string, primaryKey: string): this;
683
+ innerJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string): this;
684
+ leftJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
685
+ leftJoin(query: RawQuery): this;
686
+ leftJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
687
+ leftJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
688
+ leftOuterJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
689
+ leftOuterJoin(query: RawQuery): this;
690
+ leftOuterJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
691
+ leftOuterJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
692
+ rightJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
693
+ rightJoin(query: RawQuery): this;
694
+ rightJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
695
+ rightJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
696
+ rightOuterJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
697
+ rightOuterJoin(query: RawQuery): this;
698
+ rightOuterJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
699
+ rightOuterJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
700
+ fullOuterJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
701
+ fullOuterJoin(query: RawQuery): this;
702
+ fullOuterJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
703
+ fullOuterJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
704
+ crossJoin<M extends ModelBase>(model: Constructor<M>, where?: (this: ISelectQueryBuilder<M>) => void): this;
705
+ crossJoin(query: RawQuery): this;
706
+ crossJoin(table: string, foreignKey: string, primaryKey: string, database?: string): this;
707
+ crossJoin(table: string, tableAlias: string, foreignKey: string, primaryKey: string, database?: string): this;
708
+ }
709
+ export interface IBuilder<T> extends PromiseLike<T> {
710
+ middleware(middleware: IBuilderMiddleware<T>): this;
711
+ toDB(): ICompilerOutput | ICompilerOutput[];
712
+ }
713
+ export interface IUpdateQueryBuilder<T> extends IColumnsBuilder, IWhereBuilder<T> {
714
+ }
715
+ export interface IDeleteQueryBuilder<T> extends IWhereBuilder<T>, ILimitBuilder<T> {
716
+ }
717
+ export interface ISelectQueryBuilder<T = unknown> extends IColumnsBuilder, IOrderByBuilder, ILimitBuilder<T>, IWhereBuilder<T>, IJoinBuilder, IWithRecursiveBuilder, IGroupByBuilder, IQueryBuilder, IBuilder<T> {
718
+ min(column: string, as?: string): this;
719
+ max(column: string, as?: string): this;
720
+ count(column: string, as?: string): this;
721
+ sum(column: string, as?: string): this;
722
+ avg(column: string, as?: string): this;
723
+ setAlias(alias: string): this;
724
+ setTable(table: string, alias?: string): this;
725
+ distinct(): this;
726
+ clone(): this;
727
+ populate<R = this>(relation: {} | null, callback?: (this: ISelectQueryBuilder<R>, relation: IOrmRelation) => void): this;
728
+ populate<R = this>(relation: string, callback?: (this: ISelectQueryBuilder<R>, relation: IOrmRelation) => void): this;
729
+ asRaw<T>(): Promise<T>;
730
+ /**
731
+ * Returns all records. Its for type castin when using with scopes mostly.
732
+ */
733
+ all(): Promise<T[]>;
734
+ }
735
+ export interface ICompilerOutput {
736
+ expression: string;
737
+ bindings: any[];
738
+ }
739
+ export interface IQueryCompiler {
740
+ compile(): ICompilerOutput | ICompilerOutput[];
741
+ }
742
+ export interface ILimitCompiler {
743
+ limit(builder: ILimitBuilder<any>): ICompilerOutput;
744
+ }
745
+ export interface IGroupByCompiler {
746
+ group(builder: IGroupByBuilder): ICompilerOutput;
747
+ }
748
+ export interface IRecursiveCompiler {
749
+ recursive(builder: IWithRecursiveBuilder): ICompilerOutput;
750
+ }
751
+ export interface IColumnsCompiler {
752
+ columns(builder: IColumnsBuilder): ICompilerOutput;
753
+ }
754
+ export interface IWhereCompiler {
755
+ where(builder: IWhereBuilder<any>): ICompilerOutput;
756
+ }
757
+ export interface IJoinCompiler {
758
+ join(builder: IJoinBuilder): ICompilerOutput;
759
+ }
760
+ /**
761
+ * Definitions of query compiler are needed for DI resolving
762
+ * ==========================================================
763
+ */
764
+ export declare abstract class RecursiveQueryCompiler implements IQueryCompiler {
765
+ abstract compile(): ICompilerOutput;
766
+ }
767
+ export declare abstract class SelectQueryCompiler implements IQueryCompiler {
768
+ abstract compile(): ICompilerOutput;
769
+ }
770
+ export declare abstract class JoinQueryCompiler implements IQueryCompiler {
771
+ abstract compile(): ICompilerOutput;
772
+ }
773
+ export declare abstract class IndexQueryCompiler implements IQueryCompiler {
774
+ abstract compile(): ICompilerOutput;
775
+ }
776
+ export declare abstract class LimitQueryCompiler implements IQueryCompiler {
777
+ abstract compile(): ICompilerOutput;
778
+ }
779
+ export declare abstract class ForeignKeyQueryCompiler implements IQueryCompiler {
780
+ abstract compile(): ICompilerOutput;
781
+ }
782
+ export declare abstract class DeleteQueryCompiler implements IQueryCompiler {
783
+ abstract compile(): ICompilerOutput;
784
+ }
785
+ export declare abstract class UpdateQueryCompiler implements IQueryCompiler {
786
+ abstract compile(): ICompilerOutput;
787
+ }
788
+ export declare abstract class InsertQueryCompiler implements IQueryCompiler {
789
+ abstract compile(): ICompilerOutput;
790
+ }
791
+ export declare abstract class OnDuplicateQueryCompiler implements IQueryCompiler {
792
+ abstract compile(): ICompilerOutput;
793
+ }
794
+ export declare abstract class TableQueryCompiler implements IQueryCompiler {
795
+ abstract compile(): ICompilerOutput[] | ICompilerOutput;
796
+ }
797
+ export declare abstract class TableHistoryQueryCompiler implements IQueryCompiler {
798
+ abstract compile(): ICompilerOutput[];
799
+ }
800
+ export declare abstract class TruncateTableQueryCompiler implements IQueryCompiler {
801
+ abstract compile(): ICompilerOutput;
802
+ }
803
+ export declare abstract class TableCloneQueryCompiler implements IQueryCompiler {
804
+ abstract compile(): ICompilerOutput[];
805
+ }
806
+ export declare abstract class EventQueryCompiler implements IQueryCompiler {
807
+ abstract compile(): ICompilerOutput[];
808
+ }
809
+ export declare abstract class DropEventQueryCompiler implements IQueryCompiler {
810
+ abstract compile(): ICompilerOutput[];
811
+ }
812
+ export declare abstract class AlterTableQueryCompiler implements IQueryCompiler {
813
+ abstract compile(): ICompilerOutput[];
814
+ }
815
+ export declare abstract class TableExistsCompiler implements IQueryCompiler {
816
+ abstract compile(): ICompilerOutput;
817
+ }
818
+ export declare abstract class DropTableCompiler implements IQueryCompiler {
819
+ abstract compile(): ICompilerOutput;
820
+ }
821
+ export declare abstract class ColumnQueryCompiler implements IQueryCompiler {
822
+ abstract compile(): ICompilerOutput;
823
+ }
824
+ export declare abstract class AlterColumnQueryCompiler implements IQueryCompiler {
825
+ abstract compile(): ICompilerOutput;
826
+ }
827
+ export declare abstract class OrderByQueryCompiler implements IQueryCompiler {
828
+ abstract compile(): ICompilerOutput;
829
+ }
830
+ export declare abstract class GroupByQueryCompiler implements IQueryCompiler {
831
+ abstract compile(): ICompilerOutput;
832
+ }
833
+ /**
834
+ * ==========================================================
835
+ */
836
+ /**
837
+ * Middlewares for query builders
838
+ */
839
+ export interface IBuilderMiddleware<T = any[]> {
840
+ /**
841
+ *
842
+ * Executed AFTER query is executed in DB and raw data is fetched
843
+ * Use it to transform DB data before everything else
844
+ *
845
+ * @param data - raw data fetched from DB
846
+ */
847
+ afterQuery(data: T): T;
848
+ /**
849
+ * Executed when model is about to create. Use it to
850
+ * override model creation logic. If null is returned, default model
851
+ * is executed
852
+ *
853
+ * @param data - raw data to create
854
+ */
855
+ modelCreation(data: any): ModelBase;
856
+ /**
857
+ * executed after model was created ( all returned data by query is executed)
858
+ *
859
+ * @param data - hydrated data. Models are created and hydrated with data
860
+ */
861
+ afterHydration(data: ModelBase[]): Promise<any[] | void>;
862
+ }
863
+ export declare abstract class QueryMiddleware {
864
+ abstract afterQueryCreation(query: QueryBuilder): void;
865
+ abstract beforeQueryExecution(query: QueryBuilder): void;
866
+ }
867
+ export declare abstract class ModelMiddleware {
868
+ abstract onDelete(model: ModelBase): Promise<void>;
869
+ abstract onUpdate(model: ModelBase): Promise<void>;
870
+ abstract onInsert(model: ModelBase): Promise<void>;
871
+ abstract onSelect(model: ModelBase): Promise<void>;
872
+ }
873
+ export declare class ValueConverter implements IValueConverter {
874
+ /**
875
+ * Converts value to database type
876
+ *
877
+ * @param value - value to convert
878
+ */
879
+ toDB(_value: any, _model: ModelBase<any>, _options: any): any;
880
+ /**
881
+ * Converts value from database type eg. mysql timestamp to DateTime
882
+ *
883
+ * @param value - value to convert
884
+ */
885
+ fromDB(_value: any, _rawData: any, _options: any): any;
886
+ }
887
+ /**
888
+ * Converter for DATETIME field (eg. mysql datetime)
889
+ */
890
+ export declare class DatetimeValueConverter extends ValueConverter {
891
+ }
892
+ /**
893
+ * Converter for set field (eg. mysql SET)
894
+ */
895
+ export declare class SetValueConverter extends ValueConverter {
896
+ }
897
+ export declare abstract class TableAliasCompiler {
898
+ abstract compile(builder: QueryBuilder, tbl?: string): string;
899
+ }
900
+ export interface IUniversalConverterOptions {
901
+ TypeColumn: string;
902
+ }
903
+ /**
904
+ * base class for select & where builder for defining scopes
905
+ */
906
+ export declare abstract class QueryScope {
907
+ }
908
+ export interface IHistoricalModel {
909
+ readonly __action__: 'insert' | 'update' | 'delete';
910
+ readonly __revision__: number;
911
+ readonly __start__: DateTime;
912
+ readonly __end__: DateTime;
913
+ }
914
+ export declare abstract class ModelToSqlConverter {
915
+ abstract toSql(model: ModelBase<unknown>): unknown;
916
+ }
917
+ export declare abstract class ObjectToSqlConverter {
918
+ abstract toSql(model: unknown): unknown;
919
+ }
920
920
  //# sourceMappingURL=interfaces.d.ts.map