1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | exports.QueryClient = void 0;
|
12 | const utils_1 = require("@poppinss/utils");
|
13 | const Dialects_1 = require("../Dialects");
|
14 | const QueryBuilder_1 = require("../Orm/QueryBuilder");
|
15 | const TransactionClient_1 = require("../TransactionClient");
|
16 | const Raw_1 = require("../Database/StaticBuilder/Raw");
|
17 | const Raw_2 = require("../Database/QueryBuilder/Raw");
|
18 | const Insert_1 = require("../Database/QueryBuilder/Insert");
|
19 | const Reference_1 = require("../Database/StaticBuilder/Reference");
|
20 | const Database_1 = require("../Database/QueryBuilder/Database");
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | class 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 |
|
47 |
|
48 | Object.defineProperty(this, "isTransaction", {
|
49 | enumerable: true,
|
50 | configurable: true,
|
51 | writable: true,
|
52 | value: false
|
53 | });
|
54 | |
55 |
|
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 |
|
65 |
|
66 | Object.defineProperty(this, "profiler", {
|
67 | enumerable: true,
|
68 | configurable: true,
|
69 | writable: true,
|
70 | value: void 0
|
71 | });
|
72 | |
73 |
|
74 |
|
75 | Object.defineProperty(this, "connectionName", {
|
76 | enumerable: true,
|
77 | configurable: true,
|
78 | writable: true,
|
79 | value: this.connection.name
|
80 | });
|
81 | |
82 |
|
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 |
|
93 |
|
94 | get schema() {
|
95 | return this.getWriteClient().schema;
|
96 | }
|
97 | |
98 |
|
99 |
|
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 |
|
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 |
|
118 |
|
119 | async truncate(table, cascade = false) {
|
120 | await this.dialect.truncate(table, cascade);
|
121 | }
|
122 | |
123 |
|
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 |
|
133 |
|
134 | async getAllTables(schemas) {
|
135 | return this.dialect.getAllTables(schemas);
|
136 | }
|
137 | |
138 |
|
139 |
|
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 |
|
146 |
|
147 |
|
148 | transaction.profiler = this.profiler?.create('trx:begin', { state: 'begin' });
|
149 | |
150 |
|
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 |
|
167 |
|
168 |
|
169 |
|
170 | knexQuery() {
|
171 | return this.connection.client.queryBuilder();
|
172 | }
|
173 | |
174 |
|
175 |
|
176 |
|
177 |
|
178 | knexRawQuery(sql, bindings) {
|
179 | return bindings ? this.connection.client.raw(sql, bindings) : this.connection.client.raw(sql);
|
180 | }
|
181 | |
182 |
|
183 |
|
184 | modelQuery(model) {
|
185 | return new QueryBuilder_1.ModelQueryBuilder(this.knexQuery(), model, this);
|
186 | }
|
187 | |
188 |
|
189 |
|
190 |
|
191 | query() {
|
192 | return new Database_1.DatabaseQueryBuilder(this.knexQuery(), this);
|
193 | }
|
194 | |
195 |
|
196 |
|
197 | insertQuery() {
|
198 | return new Insert_1.InsertQueryBuilder(this.getWriteClient().queryBuilder(), this);
|
199 | }
|
200 | |
201 |
|
202 |
|
203 | rawQuery(sql, bindings) {
|
204 | return new Raw_2.RawQueryBuilder(this.connection.client.raw(sql, bindings), this);
|
205 | }
|
206 | |
207 |
|
208 |
|
209 |
|
210 |
|
211 | raw(sql, bindings) {
|
212 | return new Raw_1.RawBuilder(sql, bindings);
|
213 | }
|
214 | |
215 |
|
216 |
|
217 | ref(reference) {
|
218 | return new Reference_1.ReferenceBuilder(reference, this.getReadClient().client);
|
219 | }
|
220 | |
221 |
|
222 |
|
223 | from(table) {
|
224 | return this.query().from(table);
|
225 | }
|
226 | |
227 |
|
228 |
|
229 |
|
230 | table(table) {
|
231 | return this.insertQuery().table(table);
|
232 | }
|
233 | |
234 |
|
235 |
|
236 | getAdvisoryLock(key, timeout) {
|
237 | return this.dialect.getAdvisoryLock(key, timeout);
|
238 | }
|
239 | |
240 |
|
241 |
|
242 | releaseAdvisoryLock(key) {
|
243 | return this.dialect.releaseAdvisoryLock(key);
|
244 | }
|
245 | }
|
246 | exports.QueryClient = QueryClient;
|