/
index.ts
300 lines (260 loc) · 7.76 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/// <reference path="../../adonis-typings/index.ts" />
import { Knex } from 'knex'
import { Exception } from '@poppinss/utils'
import { EmitterContract } from '@ioc:Adonis/Core/Event'
import { ProfilerRowContract, ProfilerContract } from '@ioc:Adonis/Core/Profiler'
import {
IsolationLevels,
DialectContract,
ConnectionContract,
QueryClientContract,
TransactionClientContract,
} from '@ioc:Adonis/Lucid/Database'
import { dialects } from '../Dialects'
import { ModelQueryBuilder } from '../Orm/QueryBuilder'
import { TransactionClient } from '../TransactionClient'
import { RawBuilder } from '../Database/StaticBuilder/Raw'
import { RawQueryBuilder } from '../Database/QueryBuilder/Raw'
import { InsertQueryBuilder } from '../Database/QueryBuilder/Insert'
import { ReferenceBuilder } from '../Database/StaticBuilder/Reference'
import { DatabaseQueryBuilder } from '../Database/QueryBuilder/Database'
/**
* Query client exposes the API to fetch instance of different query builders
* to perform queries on a selecte connection.
*/
export class QueryClient implements QueryClientContract {
/**
* Not a transaction client
*/
public readonly isTransaction = false
/**
* The dialect in use
*/
public dialect: DialectContract = new dialects[this.connection.dialectName](
this,
this.connection.config
)
/**
* The profiler to be used for profiling queries
*/
public profiler?: ProfilerRowContract | ProfilerContract
/**
* Name of the connection in use
*/
public readonly connectionName = this.connection.name
/**
* Is debugging enabled
*/
public debug = !!this.connection.config.debug
constructor(
public readonly mode: 'dual' | 'write' | 'read',
private connection: ConnectionContract,
public emitter: EmitterContract
) {}
/**
* Returns schema instance for the write client
*/
public get schema() {
return this.getWriteClient().schema
}
/**
* Returns the read client. The readClient is optional, since we can get
* an instance of [[QueryClient]] with a sticky write client.
*/
public getReadClient(): Knex {
if (this.mode === 'read' || this.mode === 'dual') {
return this.connection.readClient!
}
return this.connection.client!
}
/**
* Returns the write client
*/
public getWriteClient(): Knex {
if (this.mode === 'write' || this.mode === 'dual') {
return this.connection.client!
}
throw new Exception(
'Write client is not available for query client instantiated in read mode',
500,
'E_RUNTIME_EXCEPTION'
)
}
/**
* Truncate table
*/
public async truncate(table: string, cascade: boolean = false): Promise<void> {
await this.dialect.truncate(table, cascade)
}
/**
* Get information for a table columns
*/
public async columnsInfo(table: string, column?: string): Promise<any> {
const result = await this.getWriteClient()
.table(table)
.columnInfo(column ? (column as any) : undefined)
return result
}
/**
* Returns an array of table names
*/
public async getAllTables(schemas?: string[]): Promise<string[]> {
return this.dialect.getAllTables(schemas)
}
/**
* Returns an array of all views names
*/
public async getAllViews(schemas?: string[]): Promise<string[]> {
return this.dialect.getAllViews(schemas)
}
/**
* Returns an array of all types names
*/
public async getAllTypes(schemas?: string[]): Promise<string[]> {
return this.dialect.getAllTypes(schemas)
}
/**
* Drop all tables inside database
*/
public async dropAllTables(schemas?: string[]): Promise<void> {
return this.dialect.dropAllTables(schemas || ['public'])
}
/**
* Drop all views inside the database
*/
public async dropAllViews(schemas?: string[]): Promise<void> {
return this.dialect.dropAllViews(schemas || ['public'])
}
/**
* Drop all custom types inside the database
*/
public async dropAllTypes(schemas?: string[]): Promise<void> {
return this.dialect.dropAllTypes(schemas || ['public'])
}
/**
* Returns an instance of a transaction. Each transaction will
* query and hold a single connection for all queries.
*/
public async transaction(
callback?:
| { isolationLevel?: IsolationLevels }
| ((trx: TransactionClientContract) => Promise<any>),
options?: { isolationLevel?: IsolationLevels }
): Promise<any> {
const trx = await this.getWriteClient().transaction(options)
const transaction = new TransactionClient(
trx,
this.dialect,
this.connectionName,
this.debug,
this.emitter
)
/**
* Always make sure to pass the profiler and emitter down to the transaction
* client as well
*/
transaction.profiler = this.profiler?.create('trx:begin', { state: 'begin' })
/**
* Self managed transaction
*/
if (typeof callback === 'function') {
try {
const response = await callback(transaction)
!transaction.isCompleted && (await transaction.commit())
return response
} catch (error) {
await transaction.rollback()
throw error
}
}
return transaction
}
/**
* Returns the knex query builder instance. The query builder is always
* created from the `write` client, so before executing the query, you
* may want to decide which client to use.
*/
public knexQuery(): Knex.QueryBuilder {
return this.connection.client!.queryBuilder()
}
/**
* Returns the knex raw query builder instance. The query builder is always
* created from the `write` client, so before executing the query, you
* may want to decide which client to use.
*/
public knexRawQuery(sql: string, bindings?: any): Knex.Raw {
return bindings ? this.connection.client!.raw(sql, bindings) : this.connection.client!.raw(sql)
}
/**
* Returns a query builder instance for a given model.
*/
public modelQuery(model: any): any {
return new ModelQueryBuilder(this.knexQuery(), model, this)
}
/**
* Returns instance of a query builder for selecting, updating
* or deleting rows
*/
public query(): any {
return new DatabaseQueryBuilder(this.knexQuery(), this)
}
/**
* Returns instance of a query builder for inserting rows
*/
public insertQuery(): any {
return new InsertQueryBuilder(this.getWriteClient().queryBuilder(), this)
}
/**
* Returns instance of raw query builder
*/
public rawQuery(sql: any, bindings?: any): any {
return new RawQueryBuilder(this.connection.client!.raw(sql, bindings), this)
}
/**
* Returns an instance of raw builder. This raw builder queries
* cannot be executed. Use `rawQuery`, if you want to execute
* queries raw queries.
*/
public raw(sql: string, bindings?: any) {
return new RawBuilder(sql, bindings)
}
/**
* Returns reference builder.
*/
public ref(reference: string) {
return new ReferenceBuilder(reference, this.getReadClient().client)
}
/**
* Returns instance of a query builder and selects the table
*/
public from(table: any): any {
return this.query().from(table)
}
/**
* Returns instance of a query builder and selects the table
* for an insert query
*/
public table(table: any): any {
return this.insertQuery().table(table)
}
/**
* Get advisory lock on the selected connection
*/
public getAdvisoryLock(key: string, timeout?: number): any {
return this.dialect.getAdvisoryLock(key, timeout)
}
/**
* Release advisory lock
*/
public releaseAdvisoryLock(key: string): any {
return this.dialect.releaseAdvisoryLock(key)
}
}