UNPKG

7.82 kBJavaScriptView Raw
1"use strict";
2/*
3 * @adonisjs/lucid
4 *
5 * (c) Harminder Virk <virk@adonisjs.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10Object.defineProperty(exports, "__esModule", { value: true });
11exports.QueryClient = void 0;
12const utils_1 = require("@poppinss/utils");
13const Dialects_1 = require("../Dialects");
14const QueryBuilder_1 = require("../Orm/QueryBuilder");
15const TransactionClient_1 = require("../TransactionClient");
16const Raw_1 = require("../Database/StaticBuilder/Raw");
17const Raw_2 = require("../Database/QueryBuilder/Raw");
18const Insert_1 = require("../Database/QueryBuilder/Insert");
19const Reference_1 = require("../Database/StaticBuilder/Reference");
20const Database_1 = require("../Database/QueryBuilder/Database");
21/**
22 * Query client exposes the API to fetch instance of different query builders
23 * to perform queries on a selecte connection.
24 */
25class QueryClient {
26 constructor(mode, connection, emitter) {
27 Object.defineProperty(this, "mode", {
28 enumerable: true,
29 configurable: true,
30 writable: true,
31 value: mode
32 });
33 Object.defineProperty(this, "connection", {
34 enumerable: true,
35 configurable: true,
36 writable: true,
37 value: connection
38 });
39 Object.defineProperty(this, "emitter", {
40 enumerable: true,
41 configurable: true,
42 writable: true,
43 value: emitter
44 });
45 /**
46 * Not a transaction client
47 */
48 Object.defineProperty(this, "isTransaction", {
49 enumerable: true,
50 configurable: true,
51 writable: true,
52 value: false
53 });
54 /**
55 * The dialect in use
56 */
57 Object.defineProperty(this, "dialect", {
58 enumerable: true,
59 configurable: true,
60 writable: true,
61 value: new Dialects_1.dialects[this.connection.dialectName](this)
62 });
63 /**
64 * The profiler to be used for profiling queries
65 */
66 Object.defineProperty(this, "profiler", {
67 enumerable: true,
68 configurable: true,
69 writable: true,
70 value: void 0
71 });
72 /**
73 * Name of the connection in use
74 */
75 Object.defineProperty(this, "connectionName", {
76 enumerable: true,
77 configurable: true,
78 writable: true,
79 value: this.connection.name
80 });
81 /**
82 * Is debugging enabled
83 */
84 Object.defineProperty(this, "debug", {
85 enumerable: true,
86 configurable: true,
87 writable: true,
88 value: !!this.connection.config.debug
89 });
90 }
91 /**
92 * Returns schema instance for the write client
93 */
94 get schema() {
95 return this.getWriteClient().schema;
96 }
97 /**
98 * Returns the read client. The readClient is optional, since we can get
99 * an instance of [[QueryClient]] with a sticky write client.
100 */
101 getReadClient() {
102 if (this.mode === 'read' || this.mode === 'dual') {
103 return this.connection.readClient;
104 }
105 return this.connection.client;
106 }
107 /**
108 * Returns the write client
109 */
110 getWriteClient() {
111 if (this.mode === 'write' || this.mode === 'dual') {
112 return this.connection.client;
113 }
114 throw new utils_1.Exception('Write client is not available for query client instantiated in read mode', 500, 'E_RUNTIME_EXCEPTION');
115 }
116 /**
117 * Truncate table
118 */
119 async truncate(table, cascade = false) {
120 await this.dialect.truncate(table, cascade);
121 }
122 /**
123 * Get information for a table columns
124 */
125 async columnsInfo(table, column) {
126 const result = await this.getWriteClient()
127 .table(table)
128 .columnInfo(column ? column : undefined);
129 return result;
130 }
131 /**
132 * Returns an array of table names
133 */
134 async getAllTables(schemas) {
135 return this.dialect.getAllTables(schemas);
136 }
137 /**
138 * Returns an instance of a transaction. Each transaction will
139 * query and hold a single connection for all queries.
140 */
141 async transaction(callback, options) {
142 const trx = await this.getWriteClient().transaction(options);
143 const transaction = new TransactionClient_1.TransactionClient(trx, this.dialect, this.connectionName, this.debug, this.emitter);
144 /**
145 * Always make sure to pass the profiler and emitter down to the transaction
146 * client as well
147 */
148 transaction.profiler = this.profiler?.create('trx:begin', { state: 'begin' });
149 /**
150 * Self managed transaction
151 */
152 if (typeof callback === 'function') {
153 try {
154 const response = await callback(transaction);
155 !transaction.isCompleted && (await transaction.commit());
156 return response;
157 }
158 catch (error) {
159 await transaction.rollback();
160 throw error;
161 }
162 }
163 return transaction;
164 }
165 /**
166 * Returns the knex query builder instance. The query builder is always
167 * created from the `write` client, so before executing the query, you
168 * may want to decide which client to use.
169 */
170 knexQuery() {
171 return this.connection.client.queryBuilder();
172 }
173 /**
174 * Returns the knex raw query builder instance. The query builder is always
175 * created from the `write` client, so before executing the query, you
176 * may want to decide which client to use.
177 */
178 knexRawQuery(sql, bindings) {
179 return bindings ? this.connection.client.raw(sql, bindings) : this.connection.client.raw(sql);
180 }
181 /**
182 * Returns a query builder instance for a given model.
183 */
184 modelQuery(model) {
185 return new QueryBuilder_1.ModelQueryBuilder(this.knexQuery(), model, this);
186 }
187 /**
188 * Returns instance of a query builder for selecting, updating
189 * or deleting rows
190 */
191 query() {
192 return new Database_1.DatabaseQueryBuilder(this.knexQuery(), this);
193 }
194 /**
195 * Returns instance of a query builder for inserting rows
196 */
197 insertQuery() {
198 return new Insert_1.InsertQueryBuilder(this.getWriteClient().queryBuilder(), this);
199 }
200 /**
201 * Returns instance of raw query builder
202 */
203 rawQuery(sql, bindings) {
204 return new Raw_2.RawQueryBuilder(this.connection.client.raw(sql, bindings), this);
205 }
206 /**
207 * Returns an instance of raw builder. This raw builder queries
208 * cannot be executed. Use `rawQuery`, if you want to execute
209 * queries raw queries.
210 */
211 raw(sql, bindings) {
212 return new Raw_1.RawBuilder(sql, bindings);
213 }
214 /**
215 * Returns reference builder.
216 */
217 ref(reference) {
218 return new Reference_1.ReferenceBuilder(reference, this.getReadClient().client);
219 }
220 /**
221 * Returns instance of a query builder and selects the table
222 */
223 from(table) {
224 return this.query().from(table);
225 }
226 /**
227 * Returns instance of a query builder and selects the table
228 * for an insert query
229 */
230 table(table) {
231 return this.insertQuery().table(table);
232 }
233 /**
234 * Get advisory lock on the selected connection
235 */
236 getAdvisoryLock(key, timeout) {
237 return this.dialect.getAdvisoryLock(key, timeout);
238 }
239 /**
240 * Release advisory lock
241 */
242 releaseAdvisoryLock(key) {
243 return this.dialect.releaseAdvisoryLock(key);
244 }
245}
246exports.QueryClient = QueryClient;