1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | declare module '@ioc:Adonis/Lucid/Database' {
|
7 | import { Knex } from 'knex';
|
8 | import { Pool } from 'tarn';
|
9 | import { EventEmitter } from 'events';
|
10 | import { ConnectionOptions } from 'tls';
|
11 | import { EmitterContract } from '@ioc:Adonis/Core/Event';
|
12 | import { MacroableConstructorContract } from 'macroable';
|
13 | import { LoggerContract } from '@ioc:Adonis/Core/Logger';
|
14 | import { HealthReportEntry } from '@ioc:Adonis/Core/HealthCheck';
|
15 | import { LucidModel, ModelQueryBuilderContract } from '@ioc:Adonis/Lucid/Orm';
|
16 | import { ProfilerRowContract, ProfilerContract } from '@ioc:Adonis/Core/Profiler';
|
17 | |
18 |
|
19 |
|
20 |
|
21 | export type IsolationLevels = 'read uncommitted' | 'read committed' | 'snapshot' | 'repeatable read' | 'serializable';
|
22 | |
23 |
|
24 |
|
25 |
|
26 | export type FileNode<T> = {
|
27 | absPath: string;
|
28 | name: string;
|
29 | getSource: () => T | Promise<T>;
|
30 | };
|
31 | |
32 |
|
33 |
|
34 | export interface DialectContract {
|
35 | readonly name: 'mssql' | 'mysql' | 'oracledb' | 'postgres' | 'redshift' | 'sqlite3' | 'better-sqlite3';
|
36 | readonly dateTimeFormat: string;
|
37 | readonly version?: string;
|
38 | readonly supportsAdvisoryLocks: boolean;
|
39 | readonly supportsViews: boolean;
|
40 | readonly supportsTypes: boolean;
|
41 | readonly supportsReturningStatement: boolean;
|
42 | getAllTables(schemas?: string[]): Promise<string[]>;
|
43 | dropAllTables(schemas?: string[]): Promise<void>;
|
44 | getAllViews(schemas?: string[]): Promise<string[]>;
|
45 | dropAllViews(schemas?: string[]): Promise<void>;
|
46 | getAllTypes(schemas?: string[]): Promise<string[]>;
|
47 | dropAllTypes(schemas?: string[]): Promise<void>;
|
48 | truncate(table: string, cascade?: boolean): Promise<void>;
|
49 | getAdvisoryLock(key: string | number, timeout?: number): Promise<boolean>;
|
50 | releaseAdvisoryLock(key: string | number): Promise<boolean>;
|
51 | }
|
52 | |
53 |
|
54 |
|
55 | export interface TransactionFn {
|
56 | <T>(callback: (trx: TransactionClientContract) => Promise<T>, options?: {
|
57 | isolationLevel?: IsolationLevels;
|
58 | }): Promise<T>;
|
59 | (options?: {
|
60 | isolationLevel?: IsolationLevels;
|
61 | }): Promise<TransactionClientContract>;
|
62 | }
|
63 | /**
|
64 | * Shape of the query client, that is used to retrive instances
|
65 | * of query builder
|
66 | */
|
67 | export interface QueryClientContract {
|
68 | emitter: EmitterContract;
|
69 | /**
|
70 | * Custom profiler to time queries
|
71 | */
|
72 | profiler?: ProfilerRowContract | ProfilerContract;
|
73 | /**
|
74 | * Tells if client is a transaction client or not
|
75 | */
|
76 | readonly isTransaction: boolean;
|
77 | /**
|
78 | * The database dialect in use
|
79 | */
|
80 | readonly dialect: DialectContract;
|
81 | /**
|
82 | * The client mode in which it is execute queries
|
83 | */
|
84 | readonly mode: 'dual' | 'write' | 'read';
|
85 | /**
|
86 | * The name of the connnection from which the client
|
87 | * was originated
|
88 | */
|
89 | readonly connectionName: string;
|
90 | /**
|
91 | * Is debug enabled on the connnection or not. Also opens up the API to
|
92 | * disable debug for a given client
|
93 | */
|
94 | debug: boolean;
|
95 | /**
|
96 | * Returns schema instance for the write client
|
97 | */
|
98 | schema: Knex.SchemaBuilder;
|
99 | /**
|
100 | * Returns the read and write clients
|
101 | */
|
102 | getReadClient(): Knex<any, any>;
|
103 | getWriteClient(): Knex<any, any>;
|
104 | /**
|
105 | * Returns the query builder for a given model
|
106 | */
|
107 | modelQuery<T extends LucidModel, Result = T>(model: T): ModelQueryBuilderContract<T, Result>;
|
108 | /**
|
109 | * Returns the knex query builder instance
|
110 | */
|
111 | knexQuery(): Knex.QueryBuilder;
|
112 | /**
|
113 | * Returns the knex raw query builder instance
|
114 | */
|
115 | knexRawQuery(sql: string, bindings?: RawQueryBindings): Knex.Raw;
|
116 | /**
|
117 | * Get new query builder instance for select, update and
|
118 | * delete calls
|
119 | */
|
120 | query<Result = any>(): DatabaseQueryBuilderContract<Result>;
|
121 | /**
|
122 | * Get new query builder instance inserts
|
123 | */
|
124 | insertQuery<ReturnColumns = any>(): InsertQueryBuilderContract<ReturnColumns[]>;
|
125 | /**
|
126 | * Get raw query builder instance
|
127 | */
|
128 | rawQuery<Result = any>(sql: string, bindings?: RawQueryBindings): RawQueryBuilderContract<Result>;
|
129 | /**
|
130 | * Returns instance of reference builder
|
131 | */
|
132 | ref(reference: string): ReferenceBuilderContract;
|
133 | /**
|
134 | * Returns instance of raw builder
|
135 | */
|
136 | raw(sql: string, bindings?: RawQueryBindings): RawBuilderContract;
|
137 | /**
|
138 | * Truncate a given table
|
139 | */
|
140 | truncate(table: string, cascade?: boolean): Promise<void>;
|
141 | /**
|
142 | * Returns columns info for a given table
|
143 | */
|
144 | columnsInfo(table: string): Promise<{
|
145 | [column: string]: Knex.ColumnInfo;
|
146 | }>;
|
147 | columnsInfo(table: string, column: string): Promise<Knex.ColumnInfo>;
|
148 | /**
|
149 | * Get all tables of the database
|
150 | */
|
151 | getAllTables(schemas?: string[]): Promise<string[]>;
|
152 | /**
|
153 | * Returns an array of all views names for one or many schemas
|
154 | */
|
155 | getAllViews(schemas?: string[]): Promise<string[]>;
|
156 | /**
|
157 | * Returns an array of all types names
|
158 | */
|
159 | getAllTypes(schemas?: string[]): Promise<string[]>;
|
160 | /**
|
161 | * Drop all tables inside database
|
162 | */
|
163 | dropAllTables(schemas?: string[]): Promise<void>;
|
164 | /**
|
165 | * Drop all views inside the database
|
166 | */
|
167 | dropAllViews(schemas?: string[]): Promise<void>;
|
168 | /**
|
169 | * Drop all types inside the database
|
170 | */
|
171 | dropAllTypes(schemas?: string[]): Promise<void>;
|
172 | /**
|
173 | * Same as `query()`, but also selects the table for the query. The `from` method
|
174 | * doesn't allow defining the return type and one must use `query` to define
|
175 | * that.
|
176 | */
|
177 | from: FromTable<DatabaseQueryBuilderContract<any>>;
|
178 | /**
|
179 | * Same as `insertQuery()`, but also selects the table for the query.
|
180 | * The `table` method doesn't allow defining the return type and
|
181 | * one must use `insertQuery` to define that.
|
182 | */
|
183 | table: <ReturnColumns = any>(table: string) => InsertQueryBuilderContract<ReturnColumns[]>;
|
184 | |
185 |
|
186 |
|
187 | transaction: TransactionFn;
|
188 | |
189 |
|
190 |
|
191 | getAdvisoryLock(key: string | number, timeout?: number): Promise<boolean>;
|
192 | releaseAdvisoryLock(key: string | number): Promise<boolean>;
|
193 | }
|
194 | |
195 |
|
196 |
|
197 |
|
198 | export interface TransactionClientContract extends QueryClientContract, EventEmitter {
|
199 | knexClient: Knex.Transaction;
|
200 | |
201 |
|
202 |
|
203 | profiler?: ProfilerRowContract;
|
204 | |
205 |
|
206 |
|
207 | isCompleted: boolean;
|
208 | |
209 |
|
210 |
|
211 | commit(): Promise<void>;
|
212 | |
213 |
|
214 |
|
215 | rollback(): Promise<void>;
|
216 | |
217 |
|
218 |
|
219 | getReadClient(): Knex.Transaction<any, any>;
|
220 | getWriteClient(): Knex.Transaction<any, any>;
|
221 | |
222 |
|
223 |
|
224 | on(event: 'commit', handler: (client: this) => void): this;
|
225 | on(event: 'rollback', handler: (client: this) => void): this;
|
226 | once(event: 'commit', handler: (client: this) => void): this;
|
227 | once(event: 'rollback', handler: (client: this) => void): this;
|
228 | after(event: 'rollback' | 'commit', handler: () => void | Promise<void>): this;
|
229 | }
|
230 | |
231 |
|
232 |
|
233 |
|
234 | type SharedConnectionNode = {
|
235 | host?: string;
|
236 | user?: string;
|
237 | password?: string;
|
238 | database?: string;
|
239 | port?: number;
|
240 | };
|
241 | |
242 |
|
243 |
|
244 | export type ReportNode = {
|
245 | connection: string;
|
246 | message: string;
|
247 | error: any;
|
248 | };
|
249 | |
250 |
|
251 |
|
252 | export type MigratorConfig = {
|
253 | disableTransactions?: boolean;
|
254 | paths?: string[];
|
255 | tableName?: string;
|
256 | disableRollbacksInProduction?: boolean;
|
257 | naturalSort?: boolean;
|
258 | };
|
259 | |
260 |
|
261 |
|
262 | export type SeedersConfig = {
|
263 | paths: string[];
|
264 | };
|
265 | |
266 |
|
267 |
|
268 | type SharedConfigNode = {
|
269 | useNullAsDefault?: boolean;
|
270 | debug?: boolean;
|
271 | asyncStackTraces?: boolean;
|
272 | revision?: number;
|
273 | healthCheck?: boolean;
|
274 | migrations?: MigratorConfig;
|
275 | seeders?: SeedersConfig;
|
276 | pool?: {
|
277 | afterCreate?: (conn: any, done: any) => void;
|
278 | min?: number;
|
279 | max?: number;
|
280 | acquireTimeoutMillis?: number;
|
281 | createTimeoutMillis?: number;
|
282 | idleTimeoutMillis?: number;
|
283 | createRetryIntervalMillis?: number;
|
284 | reapIntervalMillis?: number;
|
285 | log?: (msg: string) => any;
|
286 | validate?: (resource: any) => boolean;
|
287 | propagateCreateError?: boolean;
|
288 | };
|
289 | };
|
290 | |
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | export type SqliteConfig = SharedConfigNode & {
|
298 | client: 'sqlite' | 'sqlite3' | 'better-sqlite3';
|
299 | connection: {
|
300 | filename: string;
|
301 | flags?: string[];
|
302 | debug?: boolean;
|
303 | mode?: any;
|
304 | };
|
305 | replicas?: never;
|
306 | };
|
307 | |
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | type MysqlConnectionNode = {
|
315 | socketPath?: string;
|
316 | localAddress?: string;
|
317 | charset?: string;
|
318 | timezone?: string;
|
319 | stringifyObjects?: boolean;
|
320 | insecureAuth?: boolean;
|
321 | typeCast?: boolean;
|
322 | supportBigNumbers?: boolean;
|
323 | bigNumberStrings?: boolean;
|
324 | dateStrings?: boolean | string[];
|
325 | flags?: string;
|
326 | ssl?: any;
|
327 | };
|
328 | export type MysqlConfig = SharedConfigNode & {
|
329 | client: 'mysql' | 'mysql2';
|
330 | version?: string;
|
331 | connection?: SharedConnectionNode & MysqlConnectionNode;
|
332 | replicas?: {
|
333 | write: {
|
334 | connection: MysqlConfig['connection'];
|
335 | pool?: MysqlConfig['pool'];
|
336 | };
|
337 | read: {
|
338 | connection: MysqlConfig['connection'][];
|
339 | pool?: MysqlConfig['pool'];
|
340 | };
|
341 | };
|
342 | };
|
343 | |
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | type PostgresConnectionNode = {
|
354 | ssl?: boolean | ConnectionOptions;
|
355 | };
|
356 | export type PostgreConfig = SharedConfigNode & {
|
357 | client: 'pg' | 'postgres' | 'postgresql';
|
358 | version?: string;
|
359 | returning?: string;
|
360 | connection?: string | (SharedConnectionNode & PostgresConnectionNode);
|
361 | replicas?: {
|
362 | write: {
|
363 | connection: PostgreConfig['connection'];
|
364 | pool?: PostgreConfig['pool'];
|
365 | };
|
366 | read: {
|
367 | connection: PostgreConfig['connection'][];
|
368 | pool?: PostgreConfig['pool'];
|
369 | };
|
370 | };
|
371 | searchPath?: string[];
|
372 | wrapIdentifier?: (value: string) => string;
|
373 | };
|
374 | |
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 | export type RedshiftConfig = PostgreConfig & {
|
382 | client: 'redshift';
|
383 | };
|
384 | |
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 | type OracleConnectionNode = {
|
393 | autoCommit?: boolean;
|
394 | connectionClass?: string;
|
395 | edition?: string;
|
396 | externalAuth?: boolean;
|
397 | fetchArraySize?: number;
|
398 | fetchAsBuffer?: any[];
|
399 | lobPrefetchSize?: number;
|
400 | maxRows?: number;
|
401 | oracleClientVersion?: number;
|
402 | };
|
403 | export type OracleConfig = SharedConfigNode & {
|
404 | client: 'oracledb';
|
405 | connection?: SharedConnectionNode & OracleConnectionNode;
|
406 | replicas?: {
|
407 | write: {
|
408 | connection: OracleConfig['connection'];
|
409 | pool?: OracleConfig['pool'];
|
410 | };
|
411 | read: {
|
412 | connection: OracleConfig['connection'][];
|
413 | pool?: OracleConfig['pool'];
|
414 | };
|
415 | };
|
416 | fetchAsString?: any[];
|
417 | };
|
418 | |
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | type MssqlConnectionNode = {
|
426 | server: string;
|
427 | domain?: string;
|
428 | connectionTimeout?: number;
|
429 | requestTimeout?: number;
|
430 | parseJSON?: boolean;
|
431 | options?: {
|
432 | encrypt?: boolean;
|
433 | useUTC?: boolean;
|
434 | tdsVersion?: string;
|
435 | appName?: string;
|
436 | abortTransactionOnError?: boolean;
|
437 | trustedConnection?: boolean;
|
438 | enableArithAbort?: boolean;
|
439 | isolationLevel?: 'READ_UNCOMMITTED' | 'READ_COMMITTED' | 'REPEATABLE_READ' | 'SERIALIZABLE' | 'SNAPSHOT';
|
440 | maxRetriesOnTransientErrors?: number;
|
441 | multiSubnetFailover?: boolean;
|
442 | packetSize?: number;
|
443 | trustServerCertificate?: boolean;
|
444 | };
|
445 | };
|
446 | export type MssqlConfig = SharedConfigNode & {
|
447 | client: 'mssql';
|
448 | version?: string;
|
449 | connection?: SharedConnectionNode & MssqlConnectionNode;
|
450 | replicas?: {
|
451 | write: {
|
452 | connection: MssqlConfig['connection'];
|
453 | pool?: MssqlConfig['pool'];
|
454 | };
|
455 | read: {
|
456 | connection: MssqlConfig['connection'][];
|
457 | pool?: MssqlConfig['pool'];
|
458 | };
|
459 | };
|
460 | };
|
461 | |
462 |
|
463 |
|
464 |
|
465 | export type ConnectionConfig = SqliteConfig | MysqlConfig | PostgreConfig | OracleConfig | RedshiftConfig | MssqlConfig;
|
466 | |
467 |
|
468 |
|
469 | export type DatabaseConfig = {
|
470 | connection: string;
|
471 | connections: {
|
472 | [key: string]: ConnectionConfig;
|
473 | };
|
474 | };
|
475 | |
476 |
|
477 |
|
478 | export type ConnectionNode = {
|
479 | name: string;
|
480 | config: ConnectionConfig;
|
481 | connection?: ConnectionContract;
|
482 | state: 'registered' | 'migrating' | 'open' | 'closing' | 'closed';
|
483 | };
|
484 | |
485 |
|
486 |
|
487 |
|
488 | export interface ConnectionManagerContract {
|
489 | |
490 |
|
491 |
|
492 |
|
493 | connections: Map<string, ConnectionNode>;
|
494 | |
495 |
|
496 |
|
497 |
|
498 | add(connectionName: string, config: ConnectionConfig): void;
|
499 | |
500 |
|
501 |
|
502 |
|
503 | connect(connectionName: string): void;
|
504 | |
505 |
|
506 |
|
507 | get(connectionName: string): ConnectionNode | undefined;
|
508 | |
509 |
|
510 |
|
511 | has(connectionName: string): boolean;
|
512 | |
513 |
|
514 |
|
515 |
|
516 | patch(connectionName: string, config: ConnectionConfig): void;
|
517 | |
518 |
|
519 |
|
520 | isConnected(connectionName: string): boolean;
|
521 | |
522 |
|
523 |
|
524 |
|
525 | close(connectionName: string, release?: boolean): Promise<void>;
|
526 | |
527 |
|
528 |
|
529 | closeAll(release?: boolean): Promise<void>;
|
530 | |
531 |
|
532 |
|
533 |
|
534 | release(connectionName: string): Promise<void>;
|
535 | |
536 |
|
537 |
|
538 | report(): Promise<HealthReportEntry & {
|
539 | meta: ReportNode[];
|
540 | }>;
|
541 | }
|
542 | |
543 |
|
544 |
|
545 |
|
546 | export interface ConnectionContract extends EventEmitter {
|
547 | client?: Knex;
|
548 | readClient?: Knex;
|
549 | readonly dialectName: 'mssql' | 'mysql' | 'mysql2' | 'oracledb' | 'postgres' | 'redshift' | 'sqlite3';
|
550 | |
551 |
|
552 |
|
553 | readonly hasReadWriteReplicas: boolean;
|
554 | |
555 |
|
556 |
|
557 | pool: null | Pool<any>;
|
558 | readPool: null | Pool<any>;
|
559 | |
560 |
|
561 |
|
562 | readonly name: string;
|
563 | |
564 |
|
565 |
|
566 | readonly ready: boolean;
|
567 | |
568 |
|
569 |
|
570 | config: ConnectionConfig;
|
571 | |
572 |
|
573 |
|
574 | on(event: 'connect', callback: (connection: ConnectionContract) => void): this;
|
575 | on(event: 'error', callback: (error: Error, connection: ConnectionContract) => void): this;
|
576 | on(event: 'disconnect', callback: (connection: ConnectionContract) => void): this;
|
577 | on(event: 'disconnect:error', callback: (error: Error, connection: ConnectionContract) => void): this;
|
578 | |
579 |
|
580 |
|
581 | connect(): void;
|
582 | |
583 |
|
584 |
|
585 | disconnect(): Promise<void>;
|
586 | |
587 |
|
588 |
|
589 | getReport(): Promise<ReportNode>;
|
590 | }
|
591 | |
592 |
|
593 |
|
594 |
|
595 | export type DatabaseClientOptions = Partial<{
|
596 | mode: 'read' | 'write';
|
597 | profiler: ProfilerRowContract | ProfilerContract;
|
598 | }>;
|
599 | |
600 |
|
601 |
|
602 | export type DbQueryEventNode = {
|
603 | connection: string;
|
604 | model?: string;
|
605 | ddl?: boolean;
|
606 | duration?: [number, number];
|
607 | method: string;
|
608 | sql: string;
|
609 | bindings?: any[];
|
610 | inTransaction?: boolean;
|
611 | };
|
612 | |
613 |
|
614 |
|
615 |
|
616 | export interface DatabaseContract {
|
617 | Database: MacroableConstructorContract<DatabaseContract> & {
|
618 | new (config: DatabaseConfig, logger: LoggerContract, profiler: ProfilerContract, emitter: EmitterContract): DatabaseContract;
|
619 | };
|
620 | DatabaseQueryBuilder: MacroableConstructorContract<DatabaseQueryBuilderContract>;
|
621 | InsertQueryBuilder: MacroableConstructorContract<InsertQueryBuilderContract>;
|
622 | ModelQueryBuilder: MacroableConstructorContract<ModelQueryBuilderContract<any, any>>;
|
623 | SimplePaginator: {
|
624 | namingStrategy: {
|
625 | paginationMetaKeys(): SimplePaginatorMetaKeys;
|
626 | };
|
627 | new <Row>(total: number, perPage: number, currentPage: number, ...rows: Row[]): SimplePaginatorContract<Row>;
|
628 | };
|
629 | hasHealthChecksEnabled: boolean;
|
630 | |
631 |
|
632 |
|
633 | prettyPrint: (queryLog: DbQueryEventNode) => void;
|
634 | |
635 |
|
636 |
|
637 |
|
638 | primaryConnectionName: string;
|
639 | |
640 |
|
641 |
|
642 | manager: ConnectionManagerContract;
|
643 | |
644 |
|
645 |
|
646 | getRawConnection: ConnectionManagerContract['get'];
|
647 | |
648 |
|
649 |
|
650 |
|
651 | connection(connectionName?: string, options?: DatabaseClientOptions): QueryClientContract;
|
652 | |
653 |
|
654 |
|
655 | knexQuery(): Knex.QueryBuilder;
|
656 | |
657 |
|
658 |
|
659 | knexRawQuery(sql: string, bindings?: RawQueryBindings): Knex.Raw;
|
660 | |
661 |
|
662 |
|
663 | modelQuery<T extends LucidModel, Result = T>(model: T, options?: DatabaseClientOptions): ModelQueryBuilderContract<T, Result>;
|
664 | |
665 |
|
666 |
|
667 | query<Result = any>(options?: DatabaseClientOptions): DatabaseQueryBuilderContract<Result>;
|
668 | |
669 |
|
670 |
|
671 | insertQuery<ReturnColumns = any>(options?: DatabaseClientOptions): InsertQueryBuilderContract<ReturnColumns[]>;
|
672 | |
673 |
|
674 |
|
675 | rawQuery<Result = any>(sql: string, bindings?: RawQueryBindings, options?: DatabaseClientOptions): RawQueryBuilderContract<Result>;
|
676 | |
677 |
|
678 |
|
679 | ref(reference: string): ReferenceBuilderContract;
|
680 | |
681 |
|
682 |
|
683 | raw(sql: string, bindings?: RawQueryBindings): RawBuilderContract;
|
684 | |
685 |
|
686 |
|
687 |
|
688 |
|
689 | from: QueryClientContract['from'];
|
690 | |
691 |
|
692 |
|
693 |
|
694 |
|
695 | table: QueryClientContract['table'];
|
696 | |
697 |
|
698 |
|
699 | transaction: TransactionFn;
|
700 | |
701 |
|
702 |
|
703 | report(): Promise<HealthReportEntry & {
|
704 | meta: ReportNode[];
|
705 | }>;
|
706 | |
707 |
|
708 |
|
709 |
|
710 | beginGlobalTransaction(connectionName?: string, options?: Exclude<DatabaseClientOptions, 'mode'>): Promise<TransactionClientContract>;
|
711 | |
712 |
|
713 |
|
714 | commitGlobalTransaction(connectionName?: string): Promise<void>;
|
715 | |
716 |
|
717 |
|
718 | rollbackGlobalTransaction(connectionName?: string): Promise<void>;
|
719 | }
|
720 | const Database: DatabaseContract;
|
721 | export default Database;
|
722 | }
|