UNPKG

3.86 kBTypeScriptView Raw
1/// <reference path="../../adonis-typings/index.d.ts" />
2/// <reference types="@adonisjs/logger/build/adonis-typings/logger" />
3/// <reference types="node" />
4import { Pool } from 'tarn';
5import { Knex } from 'knex';
6import { EventEmitter } from 'events';
7import { LoggerContract } from '@ioc:Adonis/Core/Logger';
8import { ConnectionConfig, ConnectionContract, ReportNode } from '@ioc:Adonis/Lucid/Database';
9/**
10 * Connection class manages a given database connection. Internally it uses
11 * knex to build the database connection with appropriate database
12 * driver.
13 */
14export declare class Connection extends EventEmitter implements ConnectionContract {
15 readonly name: string;
16 config: ConnectionConfig;
17 private logger;
18 /**
19 * Reference to knex. The instance is created once the `open`
20 * method is invoked
21 */
22 client?: Knex;
23 /**
24 * Read client when read/write replicas are defined in the config, otherwise
25 * it is a reference to the `client`.
26 */
27 readClient?: Knex;
28 /**
29 * Connection dialect name
30 */
31 dialectName: ConnectionContract['dialectName'];
32 /**
33 * A boolean to know if connection operates on read/write
34 * replicas
35 */
36 hasReadWriteReplicas: boolean;
37 /**
38 * Config for one or more read replicas. Only exists, when replicas are
39 * defined
40 */
41 private readReplicas;
42 /**
43 * The round robin counter for reading config
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}