1 |
|
2 |
|
3 |
|
4 | import { Pool } from 'tarn';
|
5 | import { Knex } from 'knex';
|
6 | import { EventEmitter } from 'events';
|
7 | import { LoggerContract } from '@ioc:Adonis/Core/Logger';
|
8 | import { ConnectionConfig, ConnectionContract, ReportNode } from '@ioc:Adonis/Lucid/Database';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export declare class Connection extends EventEmitter implements ConnectionContract {
|
15 | readonly name: string;
|
16 | config: ConnectionConfig;
|
17 | private logger;
|
18 | |
19 |
|
20 |
|
21 |
|
22 | client?: Knex;
|
23 | |
24 |
|
25 |
|
26 |
|
27 | readClient?: Knex;
|
28 | |
29 |
|
30 |
|
31 | dialectName: ConnectionContract['dialectName'];
|
32 | |
33 |
|
34 |
|
35 |
|
36 | hasReadWriteReplicas: boolean;
|
37 | |
38 |
|
39 |
|
40 |
|
41 | private readReplicas;
|
42 | |
43 |
|
44 |
|
45 | private roundRobinCounter;
|
46 | constructor(name: string, config: ConnectionConfig, logger: LoggerContract);
|
47 | /**
|
48 | * Validates the config to ensure that read/write replicas are defined
|
49 | * properly.
|
50 | */
|
51 | private validateConfig;
|
52 | /**
|
53 | * Cleanup references
|
54 | */
|
55 | private cleanup;
|
56 | /**
|
57 | * Does cleanup by removing knex reference and removing all listeners.
|
58 | * For the same of simplicity, we get rid of both read and write
|
59 | * clients, when anyone of them disconnects.
|
60 | */
|
61 | private monitorPoolResources;
|
62 | /**
|
63 | * Returns normalized config object for write replica to be
|
64 | * used by knex
|
65 | */
|
66 | private getWriteConfig;
|
67 | /**
|
68 | * Returns the config for read replicas.
|
69 | */
|
70 | private getReadConfig;
|
71 | /**
|
72 | * Resolves connection config for the writer connection
|
73 | */
|
74 | private writeConfigResolver;
|
75 | /**
|
76 | * Resolves connection config for the reader connection
|
77 | */
|
78 | private readConfigResolver;
|
79 | /**
|
80 | * Creates the write connection.
|
81 | */
|
82 | private setupWriteConnection;
|
83 | /**
|
84 | * Creates the read connection. If there aren't any replicas in use, then
|
85 | * it will use the write client instead.
|
86 | */
|
87 | private setupReadConnection;
|
88 | /**
|
89 | * Checks all the read hosts by running a query on them. Stops
|
90 | * after first error.
|
91 | */
|
92 | private checkReadHosts;
|
93 | /**
|
94 | * Checks for the write host
|
95 | */
|
96 | private checkWriteHost;
|
97 | /**
|
98 | * Returns the pool instance for the given connection
|
99 | */
|
100 | get pool(): null | Pool<any>;
|
101 | /**
|
102 | * Returns the pool instance for the read connection. When replicas are
|
103 | * not in use, then read/write pools are same.
|
104 | */
|
105 | get readPool(): null | Pool<any>;
|
106 | /**
|
107 | * Returns a boolean indicating if the connection is ready for making
|
108 | * database queries. If not, one must call `connect`.
|
109 | */
|
110 | get ready(): boolean;
|
111 | /**
|
112 | * Opens the connection by creating knex instance
|
113 | */
|
114 | connect(): void;
|
115 | /**
|
116 | * Closes DB connection by destroying knex instance. The `connection`
|
117 | * object must be free for garbage collection.
|
118 | *
|
119 | * In case of error this method will emit `close:error` event followed
|
120 | * by the `close` event.
|
121 | */
|
122 | disconnect(): Promise<void>;
|
123 | /**
|
124 | * Returns the healthcheck report for the connection
|
125 | */
|
126 | getReport(): Promise<ReportNode>;
|
127 | }
|