azure-mock 2.20.0 → 2.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -201
- package/dist/better-sqlite3-CtLaF97z-DWR9uC3b.js +129 -0
- package/dist/chunk-2X2C5D2I-dJHtPFe9-DxsO-1YF.js +4026 -0
- package/dist/d1-pK2J-_yx-BoQTXsTa.js +185 -0
- package/dist/dialect-pBb6Cg_F-xgpUtfmk.js +570 -0
- package/dist/dist-C481edUb-CHbOkeXH.js +10131 -0
- package/dist/dist-DUzzleZr-DxHmeYjp.js +2 -0
- package/dist/index.d.ts +21 -21
- package/dist/index.js +20396 -18921
- package/dist/libsql-Nq-S0r5x-d2dAzhgO.js +240 -0
- package/dist/logger-TIsK7375-CLNNa25D.js +652 -0
- package/dist/migrator-B9kYwLqO-4g9JX3_v.js +8 -0
- package/dist/migrator-BfTfJctM-DrNlb1BS.js +8 -0
- package/dist/migrator-BjoW0_3j-CD-XdZ5a.js +8 -0
- package/dist/migrator-CWWlo_Jk-C4Hw4PPN.js +8 -0
- package/dist/migrator-CpiXRkls-COYfhljd.js +24 -0
- package/dist/migrator-CtqJkLVI-D1lU6ZV0.js +8 -0
- package/dist/migrator-CxcBjmET-Dcd21VIf.js +8 -0
- package/dist/migrator-Cyhbhxo_-Bh7sF0R3.js +21 -0
- package/dist/migrator-DG1mWUoR-DVfkqn_v.js +31 -0
- package/dist/migrator-DrWmHwY2-YjAfotd7.js +8 -0
- package/dist/migrator-WAPonQxA-CRvJJhKG.js +8 -0
- package/dist/migrator-Zdh8WMMx-gsmNXspd.js +8 -0
- package/dist/migrator-xRJ6NOTS-SsAuB-Gi.js +24 -0
- package/dist/migrator-xbWwvFYF-BjyVdQS8.js +8 -0
- package/dist/mysql2-DkoPEsRu-BMwVSCmY.js +251 -0
- package/dist/neon-serverless-BEVFA7yv-DGSPAG1z.js +205 -0
- package/dist/node-postgres-DQA7bEhW-CkM_cVLD.js +216 -0
- package/dist/nodefs-Bc8b83o_-DetP9qUa.js +24 -0
- package/dist/opfs-ahp-DbstDvx--CHskKtzG.js +365 -0
- package/dist/pg-ylxXyvKj-Hm8vcZoi.js +279 -0
- package/dist/pglite-CPs4w-D9-DHOg0D8W.js +179 -0
- package/dist/pglite-DMWgTUE6-BOIRUWUT.js +2 -0
- package/dist/planetscale-serverless-DUMheN-f-Dyc_W6V_.js +172 -0
- package/dist/query-builder-CLJAKedv-DTZiP7B6.js +1715 -0
- package/dist/query-builder-CT3_liD0-hhg5kRTk.js +1347 -0
- package/dist/session-BOEirggu-DTmpyU_x.js +2485 -0
- package/dist/session-CAUQtT0A-BoVK2x7A.js +745 -0
- package/dist/session-Cjeygn2Z-BO0mi6pq.js +989 -0
- package/dist/singlestore-Cdlo23hW-BuFJ4Zqb.js +1647 -0
- package/dist/sql-CNZp2yLp-Bwugq384.js +611 -0
- package/dist/sqlite-proxy-BgUfVEbZ-CsSkc-_K.js +190 -0
- package/dist/src-LcyXhCXE-C_vKJiLK.js +1920 -0
- package/dist/vercel-postgres-BYmFKsTS-CpV2usun.js +203 -0
- package/package.json +7 -8
|
@@ -0,0 +1,1647 @@
|
|
|
1
|
+
import { C as sql, T as version, _ as fillPlaceholders, b as is, d as Table, g as entityKind, h as WithSubquery, r as Param, s as SQL, t as Column, u as Subquery } from "./sql-CNZp2yLp-Bwugq384.js";
|
|
2
|
+
import { D as haveSameKeys, E as hashQuery, F as orderSelectedFields, M as mapResultRow, N as mapUpdateSet, O as isConfig, T as getTableLikeName, f as SelectionProxyHandler, i as DrizzleQueryError, m as TypedQueryBuilder, n as DefaultLogger, o as NoopCache, p as TransactionRollbackError, s as NoopLogger, u as QueryPromise, v as applyMixins, w as getTableColumns, x as extractTablesRelationalConfig, y as createTableRelationsHelpers } from "./logger-TIsK7375-CLNNa25D.js";
|
|
3
|
+
import { r as extractUsedTable, t as SingleStoreDialect } from "./dialect-pBb6Cg_F-xgpUtfmk.js";
|
|
4
|
+
import { createPool } from "mysql2";
|
|
5
|
+
import { once } from "node:events";
|
|
6
|
+
//#region ../db/dist/singlestore-Cdlo23hW.js
|
|
7
|
+
var SingleStoreCountBuilder = class SingleStoreCountBuilder extends SQL {
|
|
8
|
+
constructor(params) {
|
|
9
|
+
super(SingleStoreCountBuilder.buildEmbeddedCount(params.source, params.filters).queryChunks);
|
|
10
|
+
this.params = params;
|
|
11
|
+
this.mapWith(Number);
|
|
12
|
+
this.session = params.session;
|
|
13
|
+
this.sql = SingleStoreCountBuilder.buildCount(params.source, params.filters);
|
|
14
|
+
}
|
|
15
|
+
sql;
|
|
16
|
+
static [entityKind] = "SingleStoreCountBuilder";
|
|
17
|
+
[Symbol.toStringTag] = "SingleStoreCountBuilder";
|
|
18
|
+
session;
|
|
19
|
+
static buildEmbeddedCount(source, filters) {
|
|
20
|
+
return sql`(select count(*) from ${source}${sql.raw(" where ").if(filters)}${filters})`;
|
|
21
|
+
}
|
|
22
|
+
static buildCount(source, filters) {
|
|
23
|
+
return sql`select count(*) as count from ${source}${sql.raw(" where ").if(filters)}${filters}`;
|
|
24
|
+
}
|
|
25
|
+
then(onfulfilled, onrejected) {
|
|
26
|
+
return Promise.resolve(this.session.count(this.sql)).then(onfulfilled, onrejected);
|
|
27
|
+
}
|
|
28
|
+
catch(onRejected) {
|
|
29
|
+
return this.then(void 0, onRejected);
|
|
30
|
+
}
|
|
31
|
+
finally(onFinally) {
|
|
32
|
+
return this.then((value) => {
|
|
33
|
+
onFinally?.();
|
|
34
|
+
return value;
|
|
35
|
+
}, (reason) => {
|
|
36
|
+
onFinally?.();
|
|
37
|
+
throw reason;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var SingleStoreDeleteBase = class extends QueryPromise {
|
|
42
|
+
constructor(table, session, dialect, withList) {
|
|
43
|
+
super();
|
|
44
|
+
this.table = table;
|
|
45
|
+
this.session = session;
|
|
46
|
+
this.dialect = dialect;
|
|
47
|
+
this.config = {
|
|
48
|
+
table,
|
|
49
|
+
withList
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
static [entityKind] = "SingleStoreDelete";
|
|
53
|
+
config;
|
|
54
|
+
/**
|
|
55
|
+
* Adds a `where` clause to the query.
|
|
56
|
+
*
|
|
57
|
+
* Calling this method will delete only those rows that fulfill a specified condition.
|
|
58
|
+
*
|
|
59
|
+
* See docs: {@link https://orm.drizzle.team/docs/delete}
|
|
60
|
+
*
|
|
61
|
+
* @param where the `where` clause.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* You can use conditional operators and `sql function` to filter the rows to be deleted.
|
|
65
|
+
*
|
|
66
|
+
* ```ts
|
|
67
|
+
* // Delete all cars with green color
|
|
68
|
+
* db.delete(cars).where(eq(cars.color, 'green'));
|
|
69
|
+
* // or
|
|
70
|
+
* db.delete(cars).where(sql`${cars.color} = 'green'`)
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* // Delete all BMW cars with a green color
|
|
77
|
+
* db.delete(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
78
|
+
*
|
|
79
|
+
* // Delete all cars with the green or blue color
|
|
80
|
+
* db.delete(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
where(where) {
|
|
84
|
+
this.config.where = where;
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
orderBy(...columns) {
|
|
88
|
+
if (typeof columns[0] === "function") {
|
|
89
|
+
const orderBy = columns[0](new Proxy(this.config.table[Table.Symbol.Columns], new SelectionProxyHandler({
|
|
90
|
+
sqlAliasedBehavior: "alias",
|
|
91
|
+
sqlBehavior: "sql"
|
|
92
|
+
})));
|
|
93
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
94
|
+
this.config.orderBy = orderByArray;
|
|
95
|
+
} else {
|
|
96
|
+
const orderByArray = columns;
|
|
97
|
+
this.config.orderBy = orderByArray;
|
|
98
|
+
}
|
|
99
|
+
return this;
|
|
100
|
+
}
|
|
101
|
+
limit(limit) {
|
|
102
|
+
this.config.limit = limit;
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
/** @internal */
|
|
106
|
+
getSQL() {
|
|
107
|
+
return this.dialect.buildDeleteQuery(this.config);
|
|
108
|
+
}
|
|
109
|
+
toSQL() {
|
|
110
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
111
|
+
return rest;
|
|
112
|
+
}
|
|
113
|
+
prepare() {
|
|
114
|
+
return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, void 0, void 0, void 0, {
|
|
115
|
+
type: "delete",
|
|
116
|
+
tables: extractUsedTable(this.config.table)
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
execute = (placeholderValues) => {
|
|
120
|
+
return this.prepare().execute(placeholderValues);
|
|
121
|
+
};
|
|
122
|
+
createIterator = () => {
|
|
123
|
+
const self = this;
|
|
124
|
+
return async function* (placeholderValues) {
|
|
125
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
iterator = this.createIterator();
|
|
129
|
+
$dynamic() {
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
var SingleStoreInsertBuilder = class {
|
|
134
|
+
constructor(table, session, dialect) {
|
|
135
|
+
this.table = table;
|
|
136
|
+
this.session = session;
|
|
137
|
+
this.dialect = dialect;
|
|
138
|
+
}
|
|
139
|
+
static [entityKind] = "SingleStoreInsertBuilder";
|
|
140
|
+
shouldIgnore = false;
|
|
141
|
+
ignore() {
|
|
142
|
+
this.shouldIgnore = true;
|
|
143
|
+
return this;
|
|
144
|
+
}
|
|
145
|
+
values(values) {
|
|
146
|
+
values = Array.isArray(values) ? values : [values];
|
|
147
|
+
if (values.length === 0) throw new Error("values() must be called with at least one value");
|
|
148
|
+
const mappedValues = values.map((entry) => {
|
|
149
|
+
const result = {};
|
|
150
|
+
const cols = this.table[Table.Symbol.Columns];
|
|
151
|
+
for (const colKey of Object.keys(entry)) {
|
|
152
|
+
const colValue = entry[colKey];
|
|
153
|
+
result[colKey] = is(colValue, SQL) ? colValue : new Param(colValue, cols[colKey]);
|
|
154
|
+
}
|
|
155
|
+
return result;
|
|
156
|
+
});
|
|
157
|
+
return new SingleStoreInsertBase(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
var SingleStoreInsertBase = class extends QueryPromise {
|
|
161
|
+
constructor(table, values, ignore, session, dialect) {
|
|
162
|
+
super();
|
|
163
|
+
this.session = session;
|
|
164
|
+
this.dialect = dialect;
|
|
165
|
+
this.config = {
|
|
166
|
+
table,
|
|
167
|
+
values,
|
|
168
|
+
ignore
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
static [entityKind] = "SingleStoreInsert";
|
|
172
|
+
config;
|
|
173
|
+
/**
|
|
174
|
+
* Adds an `on duplicate key update` clause to the query.
|
|
175
|
+
*
|
|
176
|
+
* Calling this method will update update the row if any unique index conflicts. MySQL will automatically determine the conflict target based on the primary key and unique indexes.
|
|
177
|
+
*
|
|
178
|
+
* See docs: {@link https://orm.drizzle.team/docs/insert#on-duplicate-key-update}
|
|
179
|
+
*
|
|
180
|
+
* @param config The `set` clause
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* await db.insert(cars)
|
|
185
|
+
* .values({ id: 1, brand: 'BMW'})
|
|
186
|
+
* .onDuplicateKeyUpdate({ set: { brand: 'Porsche' }});
|
|
187
|
+
* ```
|
|
188
|
+
*
|
|
189
|
+
* While MySQL does not directly support doing nothing on conflict, you can perform a no-op by setting any column's value to itself and achieve the same effect:
|
|
190
|
+
*
|
|
191
|
+
* ```ts
|
|
192
|
+
* import { sql } from 'drizzle-orm';
|
|
193
|
+
*
|
|
194
|
+
* await db.insert(cars)
|
|
195
|
+
* .values({ id: 1, brand: 'BMW' })
|
|
196
|
+
* .onDuplicateKeyUpdate({ set: { id: sql`id` } });
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
onDuplicateKeyUpdate(config) {
|
|
200
|
+
const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
|
|
201
|
+
this.config.onConflict = sql`update ${setSql}`;
|
|
202
|
+
return this;
|
|
203
|
+
}
|
|
204
|
+
$returningId() {
|
|
205
|
+
const returning = [];
|
|
206
|
+
for (const [key, value] of Object.entries(this.config.table[Table.Symbol.Columns])) if (value.primary) returning.push({
|
|
207
|
+
field: value,
|
|
208
|
+
path: [key]
|
|
209
|
+
});
|
|
210
|
+
this.config.returning = orderSelectedFields(this.config.table[Table.Symbol.Columns]);
|
|
211
|
+
return this;
|
|
212
|
+
}
|
|
213
|
+
/** @internal */
|
|
214
|
+
getSQL() {
|
|
215
|
+
return this.dialect.buildInsertQuery(this.config).sql;
|
|
216
|
+
}
|
|
217
|
+
toSQL() {
|
|
218
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
219
|
+
return rest;
|
|
220
|
+
}
|
|
221
|
+
prepare() {
|
|
222
|
+
const { sql: sql2, generatedIds } = this.dialect.buildInsertQuery(this.config);
|
|
223
|
+
return this.session.prepareQuery(this.dialect.sqlToQuery(sql2), void 0, void 0, generatedIds, this.config.returning, {
|
|
224
|
+
type: "delete",
|
|
225
|
+
tables: extractUsedTable(this.config.table)
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
execute = (placeholderValues) => {
|
|
229
|
+
return this.prepare().execute(placeholderValues);
|
|
230
|
+
};
|
|
231
|
+
createIterator = () => {
|
|
232
|
+
const self = this;
|
|
233
|
+
return async function* (placeholderValues) {
|
|
234
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
iterator = this.createIterator();
|
|
238
|
+
$dynamic() {
|
|
239
|
+
return this;
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
var SingleStoreSelectBuilder = class {
|
|
243
|
+
static [entityKind] = "SingleStoreSelectBuilder";
|
|
244
|
+
fields;
|
|
245
|
+
session;
|
|
246
|
+
dialect;
|
|
247
|
+
withList = [];
|
|
248
|
+
distinct;
|
|
249
|
+
constructor(config) {
|
|
250
|
+
this.fields = config.fields;
|
|
251
|
+
this.session = config.session;
|
|
252
|
+
this.dialect = config.dialect;
|
|
253
|
+
if (config.withList) this.withList = config.withList;
|
|
254
|
+
this.distinct = config.distinct;
|
|
255
|
+
}
|
|
256
|
+
from(source) {
|
|
257
|
+
const isPartialSelect = !!this.fields;
|
|
258
|
+
let fields;
|
|
259
|
+
if (this.fields) fields = this.fields;
|
|
260
|
+
else if (is(source, Subquery)) fields = Object.fromEntries(Object.keys(source._.selectedFields).map((key) => [key, source[key]]));
|
|
261
|
+
else if (is(source, SQL)) fields = {};
|
|
262
|
+
else fields = getTableColumns(source);
|
|
263
|
+
return new SingleStoreSelectBase({
|
|
264
|
+
table: source,
|
|
265
|
+
fields,
|
|
266
|
+
isPartialSelect,
|
|
267
|
+
session: this.session,
|
|
268
|
+
dialect: this.dialect,
|
|
269
|
+
withList: this.withList,
|
|
270
|
+
distinct: this.distinct
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
};
|
|
274
|
+
var SingleStoreSelectQueryBuilderBase = class extends TypedQueryBuilder {
|
|
275
|
+
static [entityKind] = "SingleStoreSelectQueryBuilder";
|
|
276
|
+
_;
|
|
277
|
+
config;
|
|
278
|
+
joinsNotNullableMap;
|
|
279
|
+
tableName;
|
|
280
|
+
isPartialSelect;
|
|
281
|
+
/** @internal */
|
|
282
|
+
session;
|
|
283
|
+
dialect;
|
|
284
|
+
cacheConfig = void 0;
|
|
285
|
+
usedTables = /* @__PURE__ */ new Set();
|
|
286
|
+
constructor({ table, fields, isPartialSelect, session, dialect, withList, distinct }) {
|
|
287
|
+
super();
|
|
288
|
+
this.config = {
|
|
289
|
+
withList,
|
|
290
|
+
table,
|
|
291
|
+
fields: { ...fields },
|
|
292
|
+
distinct,
|
|
293
|
+
setOperators: []
|
|
294
|
+
};
|
|
295
|
+
this.isPartialSelect = isPartialSelect;
|
|
296
|
+
this.session = session;
|
|
297
|
+
this.dialect = dialect;
|
|
298
|
+
this._ = {
|
|
299
|
+
selectedFields: fields,
|
|
300
|
+
config: this.config
|
|
301
|
+
};
|
|
302
|
+
this.tableName = getTableLikeName(table);
|
|
303
|
+
this.joinsNotNullableMap = typeof this.tableName === "string" ? { [this.tableName]: true } : {};
|
|
304
|
+
for (const item of extractUsedTable(table)) this.usedTables.add(item);
|
|
305
|
+
}
|
|
306
|
+
/** @internal */
|
|
307
|
+
getUsedTables() {
|
|
308
|
+
return [...this.usedTables];
|
|
309
|
+
}
|
|
310
|
+
createJoin(joinType, lateral) {
|
|
311
|
+
return (table, on) => {
|
|
312
|
+
const baseTableName = this.tableName;
|
|
313
|
+
const tableName = getTableLikeName(table);
|
|
314
|
+
for (const item of extractUsedTable(table)) this.usedTables.add(item);
|
|
315
|
+
if (typeof tableName === "string" && this.config.joins?.some((join) => join.alias === tableName)) throw new Error(`Alias "${tableName}" is already used in this query`);
|
|
316
|
+
if (!this.isPartialSelect) {
|
|
317
|
+
if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === "string") this.config.fields = { [baseTableName]: this.config.fields };
|
|
318
|
+
if (typeof tableName === "string" && !is(table, SQL)) {
|
|
319
|
+
const selection = is(table, Subquery) ? table._.selectedFields : table[Table.Symbol.Columns];
|
|
320
|
+
this.config.fields[tableName] = selection;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
if (typeof on === "function") on = on(new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
324
|
+
sqlAliasedBehavior: "sql",
|
|
325
|
+
sqlBehavior: "sql"
|
|
326
|
+
})));
|
|
327
|
+
if (!this.config.joins) this.config.joins = [];
|
|
328
|
+
this.config.joins.push({
|
|
329
|
+
on,
|
|
330
|
+
table,
|
|
331
|
+
joinType,
|
|
332
|
+
alias: tableName,
|
|
333
|
+
lateral
|
|
334
|
+
});
|
|
335
|
+
if (typeof tableName === "string") switch (joinType) {
|
|
336
|
+
case "left":
|
|
337
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
338
|
+
break;
|
|
339
|
+
case "right":
|
|
340
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
341
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
342
|
+
break;
|
|
343
|
+
case "cross":
|
|
344
|
+
case "inner":
|
|
345
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
346
|
+
break;
|
|
347
|
+
case "full":
|
|
348
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
349
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
return this;
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Executes a `left join` operation by adding another table to the current query.
|
|
357
|
+
*
|
|
358
|
+
* Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
|
|
359
|
+
*
|
|
360
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#left-join}
|
|
361
|
+
*
|
|
362
|
+
* @param table the table to join.
|
|
363
|
+
* @param on the `on` clause.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
*
|
|
367
|
+
* ```ts
|
|
368
|
+
* // Select all users and their pets
|
|
369
|
+
* const usersWithPets: { user: User; pets: Pet | null; }[] = await db.select()
|
|
370
|
+
* .from(users)
|
|
371
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId))
|
|
372
|
+
*
|
|
373
|
+
* // Select userId and petId
|
|
374
|
+
* const usersIdsAndPetIds: { userId: number; petId: number | null; }[] = await db.select({
|
|
375
|
+
* userId: users.id,
|
|
376
|
+
* petId: pets.id,
|
|
377
|
+
* })
|
|
378
|
+
* .from(users)
|
|
379
|
+
* .leftJoin(pets, eq(users.id, pets.ownerId))
|
|
380
|
+
* ```
|
|
381
|
+
*/
|
|
382
|
+
leftJoin = this.createJoin("left", false);
|
|
383
|
+
/**
|
|
384
|
+
* Executes a `left join lateral` operation by adding subquery to the current query.
|
|
385
|
+
*
|
|
386
|
+
* A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
|
|
387
|
+
*
|
|
388
|
+
* Calling this method associates each row of the table with the corresponding row from the joined table, if a match is found. If no matching row exists, it sets all columns of the joined table to null.
|
|
389
|
+
*
|
|
390
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#left-join-lateral}
|
|
391
|
+
*
|
|
392
|
+
* @param table the subquery to join.
|
|
393
|
+
* @param on the `on` clause.
|
|
394
|
+
*/
|
|
395
|
+
leftJoinLateral = this.createJoin("left", true);
|
|
396
|
+
/**
|
|
397
|
+
* Executes a `right join` operation by adding another table to the current query.
|
|
398
|
+
*
|
|
399
|
+
* Calling this method associates each row of the joined table with the corresponding row from the main table, if a match is found. If no matching row exists, it sets all columns of the main table to null.
|
|
400
|
+
*
|
|
401
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#right-join}
|
|
402
|
+
*
|
|
403
|
+
* @param table the table to join.
|
|
404
|
+
* @param on the `on` clause.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
*
|
|
408
|
+
* ```ts
|
|
409
|
+
* // Select all users and their pets
|
|
410
|
+
* const usersWithPets: { user: User | null; pets: Pet; }[] = await db.select()
|
|
411
|
+
* .from(users)
|
|
412
|
+
* .rightJoin(pets, eq(users.id, pets.ownerId))
|
|
413
|
+
*
|
|
414
|
+
* // Select userId and petId
|
|
415
|
+
* const usersIdsAndPetIds: { userId: number | null; petId: number; }[] = await db.select({
|
|
416
|
+
* userId: users.id,
|
|
417
|
+
* petId: pets.id,
|
|
418
|
+
* })
|
|
419
|
+
* .from(users)
|
|
420
|
+
* .rightJoin(pets, eq(users.id, pets.ownerId))
|
|
421
|
+
* ```
|
|
422
|
+
*/
|
|
423
|
+
rightJoin = this.createJoin("right", false);
|
|
424
|
+
/**
|
|
425
|
+
* Executes an `inner join` operation, creating a new table by combining rows from two tables that have matching values.
|
|
426
|
+
*
|
|
427
|
+
* Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
|
|
428
|
+
*
|
|
429
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#inner-join}
|
|
430
|
+
*
|
|
431
|
+
* @param table the table to join.
|
|
432
|
+
* @param on the `on` clause.
|
|
433
|
+
*
|
|
434
|
+
* @example
|
|
435
|
+
*
|
|
436
|
+
* ```ts
|
|
437
|
+
* // Select all users and their pets
|
|
438
|
+
* const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
|
|
439
|
+
* .from(users)
|
|
440
|
+
* .innerJoin(pets, eq(users.id, pets.ownerId))
|
|
441
|
+
*
|
|
442
|
+
* // Select userId and petId
|
|
443
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
444
|
+
* userId: users.id,
|
|
445
|
+
* petId: pets.id,
|
|
446
|
+
* })
|
|
447
|
+
* .from(users)
|
|
448
|
+
* .innerJoin(pets, eq(users.id, pets.ownerId))
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
innerJoin = this.createJoin("inner", false);
|
|
452
|
+
/**
|
|
453
|
+
* Executes an `inner join lateral` operation, creating a new table by combining rows from two queries that have matching values.
|
|
454
|
+
*
|
|
455
|
+
* A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
|
|
456
|
+
*
|
|
457
|
+
* Calling this method retrieves rows that have corresponding entries in both joined tables. Rows without matching entries in either table are excluded, resulting in a table that includes only matching pairs.
|
|
458
|
+
*
|
|
459
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#inner-join-lateral}
|
|
460
|
+
*
|
|
461
|
+
* @param table the subquery to join.
|
|
462
|
+
* @param on the `on` clause.
|
|
463
|
+
*/
|
|
464
|
+
innerJoinLateral = this.createJoin("inner", true);
|
|
465
|
+
/**
|
|
466
|
+
* Executes a `full join` operation by combining rows from two tables into a new table.
|
|
467
|
+
*
|
|
468
|
+
* Calling this method retrieves all rows from both main and joined tables, merging rows with matching values and filling in `null` for non-matching columns.
|
|
469
|
+
*
|
|
470
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#full-join}
|
|
471
|
+
*
|
|
472
|
+
* @param table the table to join.
|
|
473
|
+
* @param on the `on` clause.
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
*
|
|
477
|
+
* ```ts
|
|
478
|
+
* // Select all users and their pets
|
|
479
|
+
* const usersWithPets: { user: User | null; pets: Pet | null; }[] = await db.select()
|
|
480
|
+
* .from(users)
|
|
481
|
+
* .fullJoin(pets, eq(users.id, pets.ownerId))
|
|
482
|
+
*
|
|
483
|
+
* // Select userId and petId
|
|
484
|
+
* const usersIdsAndPetIds: { userId: number | null; petId: number | null; }[] = await db.select({
|
|
485
|
+
* userId: users.id,
|
|
486
|
+
* petId: pets.id,
|
|
487
|
+
* })
|
|
488
|
+
* .from(users)
|
|
489
|
+
* .fullJoin(pets, eq(users.id, pets.ownerId))
|
|
490
|
+
* ```
|
|
491
|
+
*/
|
|
492
|
+
fullJoin = this.createJoin("full", false);
|
|
493
|
+
/**
|
|
494
|
+
* Executes a `cross join` operation by combining rows from two tables into a new table.
|
|
495
|
+
*
|
|
496
|
+
* Calling this method retrieves all rows from both main and joined tables, merging all rows from each table.
|
|
497
|
+
*
|
|
498
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#cross-join}
|
|
499
|
+
*
|
|
500
|
+
* @param table the table to join.
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
*
|
|
504
|
+
* ```ts
|
|
505
|
+
* // Select all users, each user with every pet
|
|
506
|
+
* const usersWithPets: { user: User; pets: Pet; }[] = await db.select()
|
|
507
|
+
* .from(users)
|
|
508
|
+
* .crossJoin(pets)
|
|
509
|
+
*
|
|
510
|
+
* // Select userId and petId
|
|
511
|
+
* const usersIdsAndPetIds: { userId: number; petId: number; }[] = await db.select({
|
|
512
|
+
* userId: users.id,
|
|
513
|
+
* petId: pets.id,
|
|
514
|
+
* })
|
|
515
|
+
* .from(users)
|
|
516
|
+
* .crossJoin(pets)
|
|
517
|
+
* ```
|
|
518
|
+
*/
|
|
519
|
+
crossJoin = this.createJoin("cross", false);
|
|
520
|
+
/**
|
|
521
|
+
* Executes a `cross join lateral` operation by combining rows from two queries into a new table.
|
|
522
|
+
*
|
|
523
|
+
* A `lateral` join allows the right-hand expression to refer to columns from the left-hand side.
|
|
524
|
+
*
|
|
525
|
+
* Calling this method retrieves all rows from both main and joined queries, merging all rows from each query.
|
|
526
|
+
*
|
|
527
|
+
* See docs: {@link https://orm.drizzle.team/docs/joins#cross-join-lateral}
|
|
528
|
+
*
|
|
529
|
+
* @param table the query to join.
|
|
530
|
+
*/
|
|
531
|
+
crossJoinLateral = this.createJoin("cross", true);
|
|
532
|
+
createSetOperator(type, isAll) {
|
|
533
|
+
return (rightSelection) => {
|
|
534
|
+
const rightSelect = typeof rightSelection === "function" ? rightSelection(getSingleStoreSetOperators()) : rightSelection;
|
|
535
|
+
if (!haveSameKeys(this.getSelectedFields(), rightSelect.getSelectedFields())) throw new Error("Set operator error (union / intersect / except): selected fields are not the same or are in a different order");
|
|
536
|
+
this.config.setOperators.push({
|
|
537
|
+
type,
|
|
538
|
+
isAll,
|
|
539
|
+
rightSelect
|
|
540
|
+
});
|
|
541
|
+
return this;
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Adds `union` set operator to the query.
|
|
546
|
+
*
|
|
547
|
+
* Calling this method will combine the result sets of the `select` statements and remove any duplicate rows that appear across them.
|
|
548
|
+
*
|
|
549
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#union}
|
|
550
|
+
*
|
|
551
|
+
* @example
|
|
552
|
+
*
|
|
553
|
+
* ```ts
|
|
554
|
+
* // Select all unique names from customers and users tables
|
|
555
|
+
* await db.select({ name: users.name })
|
|
556
|
+
* .from(users)
|
|
557
|
+
* .union(
|
|
558
|
+
* db.select({ name: customers.name }).from(customers)
|
|
559
|
+
* );
|
|
560
|
+
* // or
|
|
561
|
+
* import { union } from 'drizzle-orm/singlestore-core'
|
|
562
|
+
*
|
|
563
|
+
* await union(
|
|
564
|
+
* db.select({ name: users.name }).from(users),
|
|
565
|
+
* db.select({ name: customers.name }).from(customers)
|
|
566
|
+
* );
|
|
567
|
+
* ```
|
|
568
|
+
*/
|
|
569
|
+
union = this.createSetOperator("union", false);
|
|
570
|
+
/**
|
|
571
|
+
* Adds `union all` set operator to the query.
|
|
572
|
+
*
|
|
573
|
+
* Calling this method will combine the result-set of the `select` statements and keep all duplicate rows that appear across them.
|
|
574
|
+
*
|
|
575
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#union-all}
|
|
576
|
+
*
|
|
577
|
+
* @example
|
|
578
|
+
*
|
|
579
|
+
* ```ts
|
|
580
|
+
* // Select all transaction ids from both online and in-store sales
|
|
581
|
+
* await db.select({ transaction: onlineSales.transactionId })
|
|
582
|
+
* .from(onlineSales)
|
|
583
|
+
* .unionAll(
|
|
584
|
+
* db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
|
|
585
|
+
* );
|
|
586
|
+
* // or
|
|
587
|
+
* import { unionAll } from 'drizzle-orm/singlestore-core'
|
|
588
|
+
*
|
|
589
|
+
* await unionAll(
|
|
590
|
+
* db.select({ transaction: onlineSales.transactionId }).from(onlineSales),
|
|
591
|
+
* db.select({ transaction: inStoreSales.transactionId }).from(inStoreSales)
|
|
592
|
+
* );
|
|
593
|
+
* ```
|
|
594
|
+
*/
|
|
595
|
+
unionAll = this.createSetOperator("union", true);
|
|
596
|
+
/**
|
|
597
|
+
* Adds `intersect` set operator to the query.
|
|
598
|
+
*
|
|
599
|
+
* Calling this method will retain only the rows that are present in both result sets and eliminate duplicates.
|
|
600
|
+
*
|
|
601
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#intersect}
|
|
602
|
+
*
|
|
603
|
+
* @example
|
|
604
|
+
*
|
|
605
|
+
* ```ts
|
|
606
|
+
* // Select course names that are offered in both departments A and B
|
|
607
|
+
* await db.select({ courseName: depA.courseName })
|
|
608
|
+
* .from(depA)
|
|
609
|
+
* .intersect(
|
|
610
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
611
|
+
* );
|
|
612
|
+
* // or
|
|
613
|
+
* import { intersect } from 'drizzle-orm/singlestore-core'
|
|
614
|
+
*
|
|
615
|
+
* await intersect(
|
|
616
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
617
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
618
|
+
* );
|
|
619
|
+
* ```
|
|
620
|
+
*/
|
|
621
|
+
intersect = this.createSetOperator("intersect", false);
|
|
622
|
+
/**
|
|
623
|
+
* Adds `except` set operator to the query.
|
|
624
|
+
*
|
|
625
|
+
* Calling this method will retrieve all unique rows from the left query, except for the rows that are present in the result set of the right query.
|
|
626
|
+
*
|
|
627
|
+
* See docs: {@link https://orm.drizzle.team/docs/set-operations#except}
|
|
628
|
+
*
|
|
629
|
+
* @example
|
|
630
|
+
*
|
|
631
|
+
* ```ts
|
|
632
|
+
* // Select all courses offered in department A but not in department B
|
|
633
|
+
* await db.select({ courseName: depA.courseName })
|
|
634
|
+
* .from(depA)
|
|
635
|
+
* .except(
|
|
636
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
637
|
+
* );
|
|
638
|
+
* // or
|
|
639
|
+
* import { except } from 'drizzle-orm/singlestore-core'
|
|
640
|
+
*
|
|
641
|
+
* await except(
|
|
642
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
643
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
644
|
+
* );
|
|
645
|
+
* ```
|
|
646
|
+
*/
|
|
647
|
+
except = this.createSetOperator("except", false);
|
|
648
|
+
/**
|
|
649
|
+
* Adds `minus` set operator to the query.
|
|
650
|
+
*
|
|
651
|
+
* This is an alias of `except` supported by SingleStore.
|
|
652
|
+
*
|
|
653
|
+
* @example
|
|
654
|
+
*
|
|
655
|
+
* ```ts
|
|
656
|
+
* // Select all courses offered in department A but not in department B
|
|
657
|
+
* await db.select({ courseName: depA.courseName })
|
|
658
|
+
* .from(depA)
|
|
659
|
+
* .minus(
|
|
660
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
661
|
+
* );
|
|
662
|
+
* // or
|
|
663
|
+
* import { minus } from 'drizzle-orm/singlestore-core'
|
|
664
|
+
*
|
|
665
|
+
* await minus(
|
|
666
|
+
* db.select({ courseName: depA.courseName }).from(depA),
|
|
667
|
+
* db.select({ courseName: depB.courseName }).from(depB)
|
|
668
|
+
* );
|
|
669
|
+
* ```
|
|
670
|
+
*/
|
|
671
|
+
minus = this.createSetOperator("except", false);
|
|
672
|
+
/** @internal */
|
|
673
|
+
addSetOperators(setOperators) {
|
|
674
|
+
this.config.setOperators.push(...setOperators);
|
|
675
|
+
return this;
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* Adds a `where` clause to the query.
|
|
679
|
+
*
|
|
680
|
+
* Calling this method will select only those rows that fulfill a specified condition.
|
|
681
|
+
*
|
|
682
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#filtering}
|
|
683
|
+
*
|
|
684
|
+
* @param where the `where` clause.
|
|
685
|
+
*
|
|
686
|
+
* @example
|
|
687
|
+
* You can use conditional operators and `sql function` to filter the rows to be selected.
|
|
688
|
+
*
|
|
689
|
+
* ```ts
|
|
690
|
+
* // Select all cars with green color
|
|
691
|
+
* await db.select().from(cars).where(eq(cars.color, 'green'));
|
|
692
|
+
* // or
|
|
693
|
+
* await db.select().from(cars).where(sql`${cars.color} = 'green'`)
|
|
694
|
+
* ```
|
|
695
|
+
*
|
|
696
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
697
|
+
*
|
|
698
|
+
* ```ts
|
|
699
|
+
* // Select all BMW cars with a green color
|
|
700
|
+
* await db.select().from(cars).where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
701
|
+
*
|
|
702
|
+
* // Select all cars with the green or blue color
|
|
703
|
+
* await db.select().from(cars).where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
704
|
+
* ```
|
|
705
|
+
*/
|
|
706
|
+
where(where) {
|
|
707
|
+
if (typeof where === "function") where = where(new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
708
|
+
sqlAliasedBehavior: "sql",
|
|
709
|
+
sqlBehavior: "sql"
|
|
710
|
+
})));
|
|
711
|
+
this.config.where = where;
|
|
712
|
+
return this;
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* Adds a `having` clause to the query.
|
|
716
|
+
*
|
|
717
|
+
* Calling this method will select only those rows that fulfill a specified condition. It is typically used with aggregate functions to filter the aggregated data based on a specified condition.
|
|
718
|
+
*
|
|
719
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#aggregations}
|
|
720
|
+
*
|
|
721
|
+
* @param having the `having` clause.
|
|
722
|
+
*
|
|
723
|
+
* @example
|
|
724
|
+
*
|
|
725
|
+
* ```ts
|
|
726
|
+
* // Select all brands with more than one car
|
|
727
|
+
* await db.select({
|
|
728
|
+
* brand: cars.brand,
|
|
729
|
+
* count: sql<number>`cast(count(${cars.id}) as int)`,
|
|
730
|
+
* })
|
|
731
|
+
* .from(cars)
|
|
732
|
+
* .groupBy(cars.brand)
|
|
733
|
+
* .having(({ count }) => gt(count, 1));
|
|
734
|
+
* ```
|
|
735
|
+
*/
|
|
736
|
+
having(having) {
|
|
737
|
+
if (typeof having === "function") having = having(new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
738
|
+
sqlAliasedBehavior: "sql",
|
|
739
|
+
sqlBehavior: "sql"
|
|
740
|
+
})));
|
|
741
|
+
this.config.having = having;
|
|
742
|
+
return this;
|
|
743
|
+
}
|
|
744
|
+
groupBy(...columns) {
|
|
745
|
+
if (typeof columns[0] === "function") {
|
|
746
|
+
const groupBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
747
|
+
sqlAliasedBehavior: "alias",
|
|
748
|
+
sqlBehavior: "sql"
|
|
749
|
+
})));
|
|
750
|
+
this.config.groupBy = Array.isArray(groupBy) ? groupBy : [groupBy];
|
|
751
|
+
} else this.config.groupBy = columns;
|
|
752
|
+
return this;
|
|
753
|
+
}
|
|
754
|
+
orderBy(...columns) {
|
|
755
|
+
if (typeof columns[0] === "function") {
|
|
756
|
+
const orderBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
757
|
+
sqlAliasedBehavior: "alias",
|
|
758
|
+
sqlBehavior: "sql"
|
|
759
|
+
})));
|
|
760
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
761
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).orderBy = orderByArray;
|
|
762
|
+
else this.config.orderBy = orderByArray;
|
|
763
|
+
} else {
|
|
764
|
+
const orderByArray = columns;
|
|
765
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).orderBy = orderByArray;
|
|
766
|
+
else this.config.orderBy = orderByArray;
|
|
767
|
+
}
|
|
768
|
+
return this;
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Adds a `limit` clause to the query.
|
|
772
|
+
*
|
|
773
|
+
* Calling this method will set the maximum number of rows that will be returned by this query.
|
|
774
|
+
*
|
|
775
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
|
|
776
|
+
*
|
|
777
|
+
* @param limit the `limit` clause.
|
|
778
|
+
*
|
|
779
|
+
* @example
|
|
780
|
+
*
|
|
781
|
+
* ```ts
|
|
782
|
+
* // Get the first 10 people from this query.
|
|
783
|
+
* await db.select().from(people).limit(10);
|
|
784
|
+
* ```
|
|
785
|
+
*/
|
|
786
|
+
limit(limit) {
|
|
787
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).limit = limit;
|
|
788
|
+
else this.config.limit = limit;
|
|
789
|
+
return this;
|
|
790
|
+
}
|
|
791
|
+
/**
|
|
792
|
+
* Adds an `offset` clause to the query.
|
|
793
|
+
*
|
|
794
|
+
* Calling this method will skip a number of rows when returning results from this query.
|
|
795
|
+
*
|
|
796
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#limit--offset}
|
|
797
|
+
*
|
|
798
|
+
* @param offset the `offset` clause.
|
|
799
|
+
*
|
|
800
|
+
* @example
|
|
801
|
+
*
|
|
802
|
+
* ```ts
|
|
803
|
+
* // Get the 10th-20th people from this query.
|
|
804
|
+
* await db.select().from(people).offset(10).limit(10);
|
|
805
|
+
* ```
|
|
806
|
+
*/
|
|
807
|
+
offset(offset) {
|
|
808
|
+
if (this.config.setOperators.length > 0) this.config.setOperators.at(-1).offset = offset;
|
|
809
|
+
else this.config.offset = offset;
|
|
810
|
+
return this;
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Adds a `for` clause to the query.
|
|
814
|
+
*
|
|
815
|
+
* Calling this method will specify a lock strength for this query that controls how strictly it acquires exclusive access to the rows being queried.
|
|
816
|
+
*
|
|
817
|
+
* @param strength the lock strength.
|
|
818
|
+
* @param config the lock configuration.
|
|
819
|
+
*/
|
|
820
|
+
for(strength, config = {}) {
|
|
821
|
+
this.config.lockingClause = {
|
|
822
|
+
strength,
|
|
823
|
+
config
|
|
824
|
+
};
|
|
825
|
+
return this;
|
|
826
|
+
}
|
|
827
|
+
/** @internal */
|
|
828
|
+
getSQL() {
|
|
829
|
+
return this.dialect.buildSelectQuery(this.config);
|
|
830
|
+
}
|
|
831
|
+
toSQL() {
|
|
832
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
833
|
+
return rest;
|
|
834
|
+
}
|
|
835
|
+
as(alias) {
|
|
836
|
+
const usedTables = [];
|
|
837
|
+
usedTables.push(...extractUsedTable(this.config.table));
|
|
838
|
+
if (this.config.joins) for (const it of this.config.joins) usedTables.push(...extractUsedTable(it.table));
|
|
839
|
+
return new Proxy(new Subquery(this.getSQL(), this.config.fields, alias, false, [...new Set(usedTables)]), new SelectionProxyHandler({
|
|
840
|
+
alias,
|
|
841
|
+
sqlAliasedBehavior: "alias",
|
|
842
|
+
sqlBehavior: "error"
|
|
843
|
+
}));
|
|
844
|
+
}
|
|
845
|
+
/** @internal */
|
|
846
|
+
getSelectedFields() {
|
|
847
|
+
return new Proxy(this.config.fields, new SelectionProxyHandler({
|
|
848
|
+
alias: this.tableName,
|
|
849
|
+
sqlAliasedBehavior: "alias",
|
|
850
|
+
sqlBehavior: "error"
|
|
851
|
+
}));
|
|
852
|
+
}
|
|
853
|
+
$dynamic() {
|
|
854
|
+
return this;
|
|
855
|
+
}
|
|
856
|
+
};
|
|
857
|
+
var SingleStoreSelectBase = class extends SingleStoreSelectQueryBuilderBase {
|
|
858
|
+
static [entityKind] = "SingleStoreSelect";
|
|
859
|
+
prepare() {
|
|
860
|
+
if (!this.session) throw new Error("Cannot execute a query on a query builder. Please use a database instance instead.");
|
|
861
|
+
const fieldsList = orderSelectedFields(this.config.fields);
|
|
862
|
+
const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), fieldsList, void 0, void 0, void 0, {
|
|
863
|
+
type: "select",
|
|
864
|
+
tables: [...this.usedTables]
|
|
865
|
+
}, this.cacheConfig);
|
|
866
|
+
query.joinsNotNullableMap = this.joinsNotNullableMap;
|
|
867
|
+
return query;
|
|
868
|
+
}
|
|
869
|
+
$withCache(config) {
|
|
870
|
+
this.cacheConfig = config === void 0 ? {
|
|
871
|
+
config: {},
|
|
872
|
+
enable: true,
|
|
873
|
+
autoInvalidate: true
|
|
874
|
+
} : config === false ? { enable: false } : {
|
|
875
|
+
enable: true,
|
|
876
|
+
autoInvalidate: true,
|
|
877
|
+
...config
|
|
878
|
+
};
|
|
879
|
+
return this;
|
|
880
|
+
}
|
|
881
|
+
execute = (placeholderValues) => {
|
|
882
|
+
return this.prepare().execute(placeholderValues);
|
|
883
|
+
};
|
|
884
|
+
createIterator = () => {
|
|
885
|
+
const self = this;
|
|
886
|
+
return async function* (placeholderValues) {
|
|
887
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
888
|
+
};
|
|
889
|
+
};
|
|
890
|
+
iterator = this.createIterator();
|
|
891
|
+
};
|
|
892
|
+
applyMixins(SingleStoreSelectBase, [QueryPromise]);
|
|
893
|
+
function createSetOperator(type, isAll) {
|
|
894
|
+
return (leftSelect, rightSelect, ...restSelects) => {
|
|
895
|
+
const setOperators = [rightSelect, ...restSelects].map((select) => ({
|
|
896
|
+
type,
|
|
897
|
+
isAll,
|
|
898
|
+
rightSelect: select
|
|
899
|
+
}));
|
|
900
|
+
for (const setOperator of setOperators) if (!haveSameKeys(leftSelect.getSelectedFields(), setOperator.rightSelect.getSelectedFields())) throw new Error("Set operator error (union / intersect / except): selected fields are not the same or are in a different order");
|
|
901
|
+
return leftSelect.addSetOperators(setOperators);
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
const getSingleStoreSetOperators = () => ({
|
|
905
|
+
union,
|
|
906
|
+
unionAll,
|
|
907
|
+
intersect,
|
|
908
|
+
except,
|
|
909
|
+
minus
|
|
910
|
+
});
|
|
911
|
+
const union = createSetOperator("union", false);
|
|
912
|
+
const unionAll = createSetOperator("union", true);
|
|
913
|
+
const intersect = createSetOperator("intersect", false);
|
|
914
|
+
const except = createSetOperator("except", false);
|
|
915
|
+
const minus = createSetOperator("except", true);
|
|
916
|
+
var QueryBuilder = class {
|
|
917
|
+
static [entityKind] = "SingleStoreQueryBuilder";
|
|
918
|
+
dialect;
|
|
919
|
+
dialectConfig;
|
|
920
|
+
constructor(dialect) {
|
|
921
|
+
this.dialect = is(dialect, SingleStoreDialect) ? dialect : void 0;
|
|
922
|
+
this.dialectConfig = is(dialect, SingleStoreDialect) ? void 0 : dialect;
|
|
923
|
+
}
|
|
924
|
+
$with = (alias, selection) => {
|
|
925
|
+
const queryBuilder = this;
|
|
926
|
+
const as = (qb) => {
|
|
927
|
+
if (typeof qb === "function") qb = qb(queryBuilder);
|
|
928
|
+
return new Proxy(new WithSubquery(qb.getSQL(), selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}), alias, true), new SelectionProxyHandler({
|
|
929
|
+
alias,
|
|
930
|
+
sqlAliasedBehavior: "alias",
|
|
931
|
+
sqlBehavior: "error"
|
|
932
|
+
}));
|
|
933
|
+
};
|
|
934
|
+
return { as };
|
|
935
|
+
};
|
|
936
|
+
with(...queries) {
|
|
937
|
+
const self = this;
|
|
938
|
+
function select(fields) {
|
|
939
|
+
return new SingleStoreSelectBuilder({
|
|
940
|
+
fields: fields ?? void 0,
|
|
941
|
+
session: void 0,
|
|
942
|
+
dialect: self.getDialect(),
|
|
943
|
+
withList: queries
|
|
944
|
+
});
|
|
945
|
+
}
|
|
946
|
+
function selectDistinct(fields) {
|
|
947
|
+
return new SingleStoreSelectBuilder({
|
|
948
|
+
fields: fields ?? void 0,
|
|
949
|
+
session: void 0,
|
|
950
|
+
dialect: self.getDialect(),
|
|
951
|
+
withList: queries,
|
|
952
|
+
distinct: true
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
return {
|
|
956
|
+
select,
|
|
957
|
+
selectDistinct
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
select(fields) {
|
|
961
|
+
return new SingleStoreSelectBuilder({
|
|
962
|
+
fields: fields ?? void 0,
|
|
963
|
+
session: void 0,
|
|
964
|
+
dialect: this.getDialect()
|
|
965
|
+
});
|
|
966
|
+
}
|
|
967
|
+
selectDistinct(fields) {
|
|
968
|
+
return new SingleStoreSelectBuilder({
|
|
969
|
+
fields: fields ?? void 0,
|
|
970
|
+
session: void 0,
|
|
971
|
+
dialect: this.getDialect(),
|
|
972
|
+
distinct: true
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
getDialect() {
|
|
976
|
+
if (!this.dialect) this.dialect = new SingleStoreDialect(this.dialectConfig);
|
|
977
|
+
return this.dialect;
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
var SingleStoreUpdateBuilder = class {
|
|
981
|
+
constructor(table, session, dialect, withList) {
|
|
982
|
+
this.table = table;
|
|
983
|
+
this.session = session;
|
|
984
|
+
this.dialect = dialect;
|
|
985
|
+
this.withList = withList;
|
|
986
|
+
}
|
|
987
|
+
static [entityKind] = "SingleStoreUpdateBuilder";
|
|
988
|
+
set(values) {
|
|
989
|
+
return new SingleStoreUpdateBase(this.table, mapUpdateSet(this.table, values), this.session, this.dialect, this.withList);
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
var SingleStoreUpdateBase = class extends QueryPromise {
|
|
993
|
+
constructor(table, set, session, dialect, withList) {
|
|
994
|
+
super();
|
|
995
|
+
this.session = session;
|
|
996
|
+
this.dialect = dialect;
|
|
997
|
+
this.config = {
|
|
998
|
+
set,
|
|
999
|
+
table,
|
|
1000
|
+
withList
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
static [entityKind] = "SingleStoreUpdate";
|
|
1004
|
+
config;
|
|
1005
|
+
/**
|
|
1006
|
+
* Adds a 'where' clause to the query.
|
|
1007
|
+
*
|
|
1008
|
+
* Calling this method will update only those rows that fulfill a specified condition.
|
|
1009
|
+
*
|
|
1010
|
+
* See docs: {@link https://orm.drizzle.team/docs/update}
|
|
1011
|
+
*
|
|
1012
|
+
* @param where the 'where' clause.
|
|
1013
|
+
*
|
|
1014
|
+
* @example
|
|
1015
|
+
* You can use conditional operators and `sql function` to filter the rows to be updated.
|
|
1016
|
+
*
|
|
1017
|
+
* ```ts
|
|
1018
|
+
* // Update all cars with green color
|
|
1019
|
+
* db.update(cars).set({ color: 'red' })
|
|
1020
|
+
* .where(eq(cars.color, 'green'));
|
|
1021
|
+
* // or
|
|
1022
|
+
* db.update(cars).set({ color: 'red' })
|
|
1023
|
+
* .where(sql`${cars.color} = 'green'`)
|
|
1024
|
+
* ```
|
|
1025
|
+
*
|
|
1026
|
+
* You can logically combine conditional operators with `and()` and `or()` operators:
|
|
1027
|
+
*
|
|
1028
|
+
* ```ts
|
|
1029
|
+
* // Update all BMW cars with a green color
|
|
1030
|
+
* db.update(cars).set({ color: 'red' })
|
|
1031
|
+
* .where(and(eq(cars.color, 'green'), eq(cars.brand, 'BMW')));
|
|
1032
|
+
*
|
|
1033
|
+
* // Update all cars with the green or blue color
|
|
1034
|
+
* db.update(cars).set({ color: 'red' })
|
|
1035
|
+
* .where(or(eq(cars.color, 'green'), eq(cars.color, 'blue')));
|
|
1036
|
+
* ```
|
|
1037
|
+
*/
|
|
1038
|
+
where(where) {
|
|
1039
|
+
this.config.where = where;
|
|
1040
|
+
return this;
|
|
1041
|
+
}
|
|
1042
|
+
orderBy(...columns) {
|
|
1043
|
+
if (typeof columns[0] === "function") {
|
|
1044
|
+
const orderBy = columns[0](new Proxy(this.config.table[Table.Symbol.Columns], new SelectionProxyHandler({
|
|
1045
|
+
sqlAliasedBehavior: "alias",
|
|
1046
|
+
sqlBehavior: "sql"
|
|
1047
|
+
})));
|
|
1048
|
+
const orderByArray = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
1049
|
+
this.config.orderBy = orderByArray;
|
|
1050
|
+
} else {
|
|
1051
|
+
const orderByArray = columns;
|
|
1052
|
+
this.config.orderBy = orderByArray;
|
|
1053
|
+
}
|
|
1054
|
+
return this;
|
|
1055
|
+
}
|
|
1056
|
+
limit(limit) {
|
|
1057
|
+
this.config.limit = limit;
|
|
1058
|
+
return this;
|
|
1059
|
+
}
|
|
1060
|
+
/** @internal */
|
|
1061
|
+
getSQL() {
|
|
1062
|
+
return this.dialect.buildUpdateQuery(this.config);
|
|
1063
|
+
}
|
|
1064
|
+
toSQL() {
|
|
1065
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
1066
|
+
return rest;
|
|
1067
|
+
}
|
|
1068
|
+
prepare() {
|
|
1069
|
+
return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning, void 0, void 0, void 0, {
|
|
1070
|
+
type: "delete",
|
|
1071
|
+
tables: extractUsedTable(this.config.table)
|
|
1072
|
+
});
|
|
1073
|
+
}
|
|
1074
|
+
execute = (placeholderValues) => {
|
|
1075
|
+
return this.prepare().execute(placeholderValues);
|
|
1076
|
+
};
|
|
1077
|
+
createIterator = () => {
|
|
1078
|
+
const self = this;
|
|
1079
|
+
return async function* (placeholderValues) {
|
|
1080
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
1081
|
+
};
|
|
1082
|
+
};
|
|
1083
|
+
iterator = this.createIterator();
|
|
1084
|
+
$dynamic() {
|
|
1085
|
+
return this;
|
|
1086
|
+
}
|
|
1087
|
+
};
|
|
1088
|
+
var SingleStoreDatabase = class {
|
|
1089
|
+
constructor(dialect, session, schema) {
|
|
1090
|
+
this.dialect = dialect;
|
|
1091
|
+
this.session = session;
|
|
1092
|
+
this._ = schema ? {
|
|
1093
|
+
schema: schema.schema,
|
|
1094
|
+
fullSchema: schema.fullSchema,
|
|
1095
|
+
tableNamesMap: schema.tableNamesMap
|
|
1096
|
+
} : {
|
|
1097
|
+
schema: void 0,
|
|
1098
|
+
fullSchema: {},
|
|
1099
|
+
tableNamesMap: {}
|
|
1100
|
+
};
|
|
1101
|
+
this.query = {};
|
|
1102
|
+
this.$cache = { invalidate: async (_params) => {} };
|
|
1103
|
+
}
|
|
1104
|
+
static [entityKind] = "SingleStoreDatabase";
|
|
1105
|
+
/**@inrernal */
|
|
1106
|
+
query;
|
|
1107
|
+
/**
|
|
1108
|
+
* Creates a subquery that defines a temporary named result set as a CTE.
|
|
1109
|
+
*
|
|
1110
|
+
* It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
|
|
1111
|
+
*
|
|
1112
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
|
|
1113
|
+
*
|
|
1114
|
+
* @param alias The alias for the subquery.
|
|
1115
|
+
*
|
|
1116
|
+
* Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
|
|
1117
|
+
*
|
|
1118
|
+
* @example
|
|
1119
|
+
*
|
|
1120
|
+
* ```ts
|
|
1121
|
+
* // Create a subquery with alias 'sq' and use it in the select query
|
|
1122
|
+
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
|
|
1123
|
+
*
|
|
1124
|
+
* const result = await db.with(sq).select().from(sq);
|
|
1125
|
+
* ```
|
|
1126
|
+
*
|
|
1127
|
+
* To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:
|
|
1128
|
+
*
|
|
1129
|
+
* ```ts
|
|
1130
|
+
* // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
|
|
1131
|
+
* const sq = db.$with('sq').as(db.select({
|
|
1132
|
+
* name: sql<string>`upper(${users.name})`.as('name'),
|
|
1133
|
+
* })
|
|
1134
|
+
* .from(users));
|
|
1135
|
+
*
|
|
1136
|
+
* const result = await db.with(sq).select({ name: sq.name }).from(sq);
|
|
1137
|
+
* ```
|
|
1138
|
+
*/
|
|
1139
|
+
$with = (alias, selection) => {
|
|
1140
|
+
const self = this;
|
|
1141
|
+
const as = (qb) => {
|
|
1142
|
+
if (typeof qb === "function") qb = qb(new QueryBuilder(self.dialect));
|
|
1143
|
+
return new Proxy(new WithSubquery(qb.getSQL(), selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}), alias, true), new SelectionProxyHandler({
|
|
1144
|
+
alias,
|
|
1145
|
+
sqlAliasedBehavior: "alias",
|
|
1146
|
+
sqlBehavior: "error"
|
|
1147
|
+
}));
|
|
1148
|
+
};
|
|
1149
|
+
return { as };
|
|
1150
|
+
};
|
|
1151
|
+
$count(source, filters) {
|
|
1152
|
+
return new SingleStoreCountBuilder({
|
|
1153
|
+
source,
|
|
1154
|
+
filters,
|
|
1155
|
+
session: this.session
|
|
1156
|
+
});
|
|
1157
|
+
}
|
|
1158
|
+
/**
|
|
1159
|
+
* Incorporates a previously defined CTE (using `$with`) into the main query.
|
|
1160
|
+
*
|
|
1161
|
+
* This method allows the main query to reference a temporary named result set.
|
|
1162
|
+
*
|
|
1163
|
+
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
|
|
1164
|
+
*
|
|
1165
|
+
* @param queries The CTEs to incorporate into the main query.
|
|
1166
|
+
*
|
|
1167
|
+
* @example
|
|
1168
|
+
*
|
|
1169
|
+
* ```ts
|
|
1170
|
+
* // Define a subquery 'sq' as a CTE using $with
|
|
1171
|
+
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
|
|
1172
|
+
*
|
|
1173
|
+
* // Incorporate the CTE 'sq' into the main query and select from it
|
|
1174
|
+
* const result = await db.with(sq).select().from(sq);
|
|
1175
|
+
* ```
|
|
1176
|
+
*/
|
|
1177
|
+
with(...queries) {
|
|
1178
|
+
const self = this;
|
|
1179
|
+
function select(fields) {
|
|
1180
|
+
return new SingleStoreSelectBuilder({
|
|
1181
|
+
fields: fields ?? void 0,
|
|
1182
|
+
session: self.session,
|
|
1183
|
+
dialect: self.dialect,
|
|
1184
|
+
withList: queries
|
|
1185
|
+
});
|
|
1186
|
+
}
|
|
1187
|
+
function selectDistinct(fields) {
|
|
1188
|
+
return new SingleStoreSelectBuilder({
|
|
1189
|
+
fields: fields ?? void 0,
|
|
1190
|
+
session: self.session,
|
|
1191
|
+
dialect: self.dialect,
|
|
1192
|
+
withList: queries,
|
|
1193
|
+
distinct: true
|
|
1194
|
+
});
|
|
1195
|
+
}
|
|
1196
|
+
function update(table) {
|
|
1197
|
+
return new SingleStoreUpdateBuilder(table, self.session, self.dialect, queries);
|
|
1198
|
+
}
|
|
1199
|
+
function delete_(table) {
|
|
1200
|
+
return new SingleStoreDeleteBase(table, self.session, self.dialect, queries);
|
|
1201
|
+
}
|
|
1202
|
+
return {
|
|
1203
|
+
select,
|
|
1204
|
+
selectDistinct,
|
|
1205
|
+
update,
|
|
1206
|
+
delete: delete_
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
select(fields) {
|
|
1210
|
+
return new SingleStoreSelectBuilder({
|
|
1211
|
+
fields: fields ?? void 0,
|
|
1212
|
+
session: this.session,
|
|
1213
|
+
dialect: this.dialect
|
|
1214
|
+
});
|
|
1215
|
+
}
|
|
1216
|
+
selectDistinct(fields) {
|
|
1217
|
+
return new SingleStoreSelectBuilder({
|
|
1218
|
+
fields: fields ?? void 0,
|
|
1219
|
+
session: this.session,
|
|
1220
|
+
dialect: this.dialect,
|
|
1221
|
+
distinct: true
|
|
1222
|
+
});
|
|
1223
|
+
}
|
|
1224
|
+
/**
|
|
1225
|
+
* Creates an update query.
|
|
1226
|
+
*
|
|
1227
|
+
* Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
|
|
1228
|
+
*
|
|
1229
|
+
* Use `.set()` method to specify which values to update.
|
|
1230
|
+
*
|
|
1231
|
+
* See docs: {@link https://orm.drizzle.team/docs/update}
|
|
1232
|
+
*
|
|
1233
|
+
* @param table The table to update.
|
|
1234
|
+
*
|
|
1235
|
+
* @example
|
|
1236
|
+
*
|
|
1237
|
+
* ```ts
|
|
1238
|
+
* // Update all rows in the 'cars' table
|
|
1239
|
+
* await db.update(cars).set({ color: 'red' });
|
|
1240
|
+
*
|
|
1241
|
+
* // Update rows with filters and conditions
|
|
1242
|
+
* await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
|
|
1243
|
+
* ```
|
|
1244
|
+
*/
|
|
1245
|
+
update(table) {
|
|
1246
|
+
return new SingleStoreUpdateBuilder(table, this.session, this.dialect);
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Creates an insert query.
|
|
1250
|
+
*
|
|
1251
|
+
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
|
|
1252
|
+
*
|
|
1253
|
+
* See docs: {@link https://orm.drizzle.team/docs/insert}
|
|
1254
|
+
*
|
|
1255
|
+
* @param table The table to insert into.
|
|
1256
|
+
*
|
|
1257
|
+
* @example
|
|
1258
|
+
*
|
|
1259
|
+
* ```ts
|
|
1260
|
+
* // Insert one row
|
|
1261
|
+
* await db.insert(cars).values({ brand: 'BMW' });
|
|
1262
|
+
*
|
|
1263
|
+
* // Insert multiple rows
|
|
1264
|
+
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
1265
|
+
* ```
|
|
1266
|
+
*/
|
|
1267
|
+
insert(table) {
|
|
1268
|
+
return new SingleStoreInsertBuilder(table, this.session, this.dialect);
|
|
1269
|
+
}
|
|
1270
|
+
/**
|
|
1271
|
+
* Creates a delete query.
|
|
1272
|
+
*
|
|
1273
|
+
* Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
|
|
1274
|
+
*
|
|
1275
|
+
* See docs: {@link https://orm.drizzle.team/docs/delete}
|
|
1276
|
+
*
|
|
1277
|
+
* @param table The table to delete from.
|
|
1278
|
+
*
|
|
1279
|
+
* @example
|
|
1280
|
+
*
|
|
1281
|
+
* ```ts
|
|
1282
|
+
* // Delete all rows in the 'cars' table
|
|
1283
|
+
* await db.delete(cars);
|
|
1284
|
+
*
|
|
1285
|
+
* // Delete rows with filters and conditions
|
|
1286
|
+
* await db.delete(cars).where(eq(cars.color, 'green'));
|
|
1287
|
+
* ```
|
|
1288
|
+
*/
|
|
1289
|
+
delete(table) {
|
|
1290
|
+
return new SingleStoreDeleteBase(table, this.session, this.dialect);
|
|
1291
|
+
}
|
|
1292
|
+
execute(query) {
|
|
1293
|
+
return this.session.execute(typeof query === "string" ? sql.raw(query) : query.getSQL());
|
|
1294
|
+
}
|
|
1295
|
+
$cache;
|
|
1296
|
+
transaction(transaction, config) {
|
|
1297
|
+
return this.session.transaction(transaction, config);
|
|
1298
|
+
}
|
|
1299
|
+
};
|
|
1300
|
+
var SingleStorePreparedQuery = class {
|
|
1301
|
+
constructor(cache, queryMetadata, cacheConfig) {
|
|
1302
|
+
this.cache = cache;
|
|
1303
|
+
this.queryMetadata = queryMetadata;
|
|
1304
|
+
this.cacheConfig = cacheConfig;
|
|
1305
|
+
if (cache && cache.strategy() === "all" && cacheConfig === void 0) this.cacheConfig = {
|
|
1306
|
+
enable: true,
|
|
1307
|
+
autoInvalidate: true
|
|
1308
|
+
};
|
|
1309
|
+
if (!this.cacheConfig?.enable) this.cacheConfig = void 0;
|
|
1310
|
+
}
|
|
1311
|
+
static [entityKind] = "SingleStorePreparedQuery";
|
|
1312
|
+
/** @internal */
|
|
1313
|
+
async queryWithCache(queryString, params, query) {
|
|
1314
|
+
if (this.cache === void 0 || is(this.cache, NoopCache) || this.queryMetadata === void 0) try {
|
|
1315
|
+
return await query();
|
|
1316
|
+
} catch (e) {
|
|
1317
|
+
throw new DrizzleQueryError(queryString, params, e);
|
|
1318
|
+
}
|
|
1319
|
+
if (this.cacheConfig && !this.cacheConfig.enable) try {
|
|
1320
|
+
return await query();
|
|
1321
|
+
} catch (e) {
|
|
1322
|
+
throw new DrizzleQueryError(queryString, params, e);
|
|
1323
|
+
}
|
|
1324
|
+
if ((this.queryMetadata.type === "insert" || this.queryMetadata.type === "update" || this.queryMetadata.type === "delete") && this.queryMetadata.tables.length > 0) try {
|
|
1325
|
+
const [res] = await Promise.all([query(), this.cache.onMutate({ tables: this.queryMetadata.tables })]);
|
|
1326
|
+
return res;
|
|
1327
|
+
} catch (e) {
|
|
1328
|
+
throw new DrizzleQueryError(queryString, params, e);
|
|
1329
|
+
}
|
|
1330
|
+
if (!this.cacheConfig) try {
|
|
1331
|
+
return await query();
|
|
1332
|
+
} catch (e) {
|
|
1333
|
+
throw new DrizzleQueryError(queryString, params, e);
|
|
1334
|
+
}
|
|
1335
|
+
if (this.queryMetadata.type === "select") {
|
|
1336
|
+
const fromCache = await this.cache.get(this.cacheConfig.tag ?? await hashQuery(queryString, params), this.queryMetadata.tables, this.cacheConfig.tag !== void 0, this.cacheConfig.autoInvalidate);
|
|
1337
|
+
if (fromCache === void 0) {
|
|
1338
|
+
let result;
|
|
1339
|
+
try {
|
|
1340
|
+
result = await query();
|
|
1341
|
+
} catch (e) {
|
|
1342
|
+
throw new DrizzleQueryError(queryString, params, e);
|
|
1343
|
+
}
|
|
1344
|
+
await this.cache.put(this.cacheConfig.tag ?? await hashQuery(queryString, params), result, this.cacheConfig.autoInvalidate ? this.queryMetadata.tables : [], this.cacheConfig.tag !== void 0, this.cacheConfig.config);
|
|
1345
|
+
return result;
|
|
1346
|
+
}
|
|
1347
|
+
return fromCache;
|
|
1348
|
+
}
|
|
1349
|
+
try {
|
|
1350
|
+
return await query();
|
|
1351
|
+
} catch (e) {
|
|
1352
|
+
throw new DrizzleQueryError(queryString, params, e);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
/** @internal */
|
|
1356
|
+
joinsNotNullableMap;
|
|
1357
|
+
};
|
|
1358
|
+
var SingleStoreSession = class {
|
|
1359
|
+
constructor(dialect) {
|
|
1360
|
+
this.dialect = dialect;
|
|
1361
|
+
}
|
|
1362
|
+
static [entityKind] = "SingleStoreSession";
|
|
1363
|
+
execute(query) {
|
|
1364
|
+
return this.prepareQuery(this.dialect.sqlToQuery(query), void 0).execute();
|
|
1365
|
+
}
|
|
1366
|
+
async count(sql2) {
|
|
1367
|
+
const res = await this.execute(sql2);
|
|
1368
|
+
return Number(res[0][0]["count"]);
|
|
1369
|
+
}
|
|
1370
|
+
getSetTransactionSQL(config) {
|
|
1371
|
+
const parts = [];
|
|
1372
|
+
if (config.isolationLevel) parts.push(`isolation level ${config.isolationLevel}`);
|
|
1373
|
+
return parts.length ? sql`set transaction ${sql.raw(parts.join(" "))}` : void 0;
|
|
1374
|
+
}
|
|
1375
|
+
getStartTransactionSQL(config) {
|
|
1376
|
+
const parts = [];
|
|
1377
|
+
if (config.withConsistentSnapshot) parts.push("with consistent snapshot");
|
|
1378
|
+
if (config.accessMode) parts.push(config.accessMode);
|
|
1379
|
+
return parts.length ? sql`start transaction ${sql.raw(parts.join(" "))}` : void 0;
|
|
1380
|
+
}
|
|
1381
|
+
};
|
|
1382
|
+
var SingleStoreTransaction = class extends SingleStoreDatabase {
|
|
1383
|
+
constructor(dialect, session, schema, nestedIndex) {
|
|
1384
|
+
super(dialect, session, schema);
|
|
1385
|
+
this.schema = schema;
|
|
1386
|
+
this.nestedIndex = nestedIndex;
|
|
1387
|
+
}
|
|
1388
|
+
static [entityKind] = "SingleStoreTransaction";
|
|
1389
|
+
rollback() {
|
|
1390
|
+
throw new TransactionRollbackError();
|
|
1391
|
+
}
|
|
1392
|
+
};
|
|
1393
|
+
var SingleStoreDriverPreparedQuery = class extends SingleStorePreparedQuery {
|
|
1394
|
+
constructor(client, queryString, params, logger, cache, queryMetadata, cacheConfig, fields, customResultMapper, generatedIds, returningIds) {
|
|
1395
|
+
super(cache, queryMetadata, cacheConfig);
|
|
1396
|
+
this.client = client;
|
|
1397
|
+
this.params = params;
|
|
1398
|
+
this.logger = logger;
|
|
1399
|
+
this.fields = fields;
|
|
1400
|
+
this.customResultMapper = customResultMapper;
|
|
1401
|
+
this.generatedIds = generatedIds;
|
|
1402
|
+
this.returningIds = returningIds;
|
|
1403
|
+
this.rawQuery = {
|
|
1404
|
+
sql: queryString,
|
|
1405
|
+
typeCast: function(field, next) {
|
|
1406
|
+
if (field.type === "TIMESTAMP" || field.type === "DATETIME" || field.type === "DATE") return field.string();
|
|
1407
|
+
return next();
|
|
1408
|
+
}
|
|
1409
|
+
};
|
|
1410
|
+
this.query = {
|
|
1411
|
+
sql: queryString,
|
|
1412
|
+
rowsAsArray: true,
|
|
1413
|
+
typeCast: function(field, next) {
|
|
1414
|
+
if (field.type === "TIMESTAMP" || field.type === "DATETIME" || field.type === "DATE") return field.string();
|
|
1415
|
+
return next();
|
|
1416
|
+
}
|
|
1417
|
+
};
|
|
1418
|
+
}
|
|
1419
|
+
static [entityKind] = "SingleStoreDriverPreparedQuery";
|
|
1420
|
+
rawQuery;
|
|
1421
|
+
query;
|
|
1422
|
+
async execute(placeholderValues = {}) {
|
|
1423
|
+
const params = fillPlaceholders(this.params, placeholderValues);
|
|
1424
|
+
this.logger.logQuery(this.rawQuery.sql, params);
|
|
1425
|
+
const { fields, client, rawQuery, query, joinsNotNullableMap, customResultMapper, returningIds, generatedIds } = this;
|
|
1426
|
+
if (!fields && !customResultMapper) {
|
|
1427
|
+
const res = await this.queryWithCache(rawQuery.sql, params, async () => {
|
|
1428
|
+
return await client.query(rawQuery, params);
|
|
1429
|
+
});
|
|
1430
|
+
const insertId = res[0].insertId;
|
|
1431
|
+
const affectedRows = res[0].affectedRows;
|
|
1432
|
+
if (returningIds) {
|
|
1433
|
+
const returningResponse = [];
|
|
1434
|
+
let j = 0;
|
|
1435
|
+
for (let i = insertId; i < insertId + affectedRows; i++) {
|
|
1436
|
+
for (const column of returningIds) {
|
|
1437
|
+
const key = returningIds[0].path[0];
|
|
1438
|
+
if (is(column.field, Column)) {
|
|
1439
|
+
if (column.field.primary && column.field.autoIncrement) returningResponse.push({ [key]: i });
|
|
1440
|
+
if (column.field.defaultFn && generatedIds) returningResponse.push({ [key]: generatedIds[j][key] });
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
j++;
|
|
1444
|
+
}
|
|
1445
|
+
return returningResponse;
|
|
1446
|
+
}
|
|
1447
|
+
return res;
|
|
1448
|
+
}
|
|
1449
|
+
const rows = (await this.queryWithCache(query.sql, params, async () => {
|
|
1450
|
+
return await client.query(query, params);
|
|
1451
|
+
}))[0];
|
|
1452
|
+
if (customResultMapper) return customResultMapper(rows);
|
|
1453
|
+
return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
|
|
1454
|
+
}
|
|
1455
|
+
async *iterator(placeholderValues = {}) {
|
|
1456
|
+
const params = fillPlaceholders(this.params, placeholderValues);
|
|
1457
|
+
const conn = (isPool(this.client) ? await this.client.getConnection() : this.client).connection;
|
|
1458
|
+
const { fields, query, rawQuery, joinsNotNullableMap, client, customResultMapper } = this;
|
|
1459
|
+
const hasRowsMapper = Boolean(fields || customResultMapper);
|
|
1460
|
+
const stream = (hasRowsMapper ? conn.query(query, params) : conn.query(rawQuery, params)).stream();
|
|
1461
|
+
function dataListener() {
|
|
1462
|
+
stream.pause();
|
|
1463
|
+
}
|
|
1464
|
+
stream.on("data", dataListener);
|
|
1465
|
+
try {
|
|
1466
|
+
const onEnd = once(stream, "end");
|
|
1467
|
+
const onError = once(stream, "error");
|
|
1468
|
+
while (true) {
|
|
1469
|
+
stream.resume();
|
|
1470
|
+
const row = await Promise.race([
|
|
1471
|
+
onEnd,
|
|
1472
|
+
onError,
|
|
1473
|
+
new Promise((resolve) => stream.once("data", resolve))
|
|
1474
|
+
]);
|
|
1475
|
+
if (row === void 0 || Array.isArray(row) && row.length === 0) break;
|
|
1476
|
+
else if (row instanceof Error) throw row;
|
|
1477
|
+
else if (hasRowsMapper) if (customResultMapper) {
|
|
1478
|
+
const mappedRow = customResultMapper([row]);
|
|
1479
|
+
yield Array.isArray(mappedRow) ? mappedRow[0] : mappedRow;
|
|
1480
|
+
} else yield mapResultRow(fields, row, joinsNotNullableMap);
|
|
1481
|
+
else yield row;
|
|
1482
|
+
}
|
|
1483
|
+
} finally {
|
|
1484
|
+
stream.off("data", dataListener);
|
|
1485
|
+
if (isPool(client)) conn.end();
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
var SingleStoreDriverSession = class SingleStoreDriverSession extends SingleStoreSession {
|
|
1490
|
+
constructor(client, dialect, schema, options) {
|
|
1491
|
+
super(dialect);
|
|
1492
|
+
this.client = client;
|
|
1493
|
+
this.schema = schema;
|
|
1494
|
+
this.options = options;
|
|
1495
|
+
this.logger = options.logger ?? new NoopLogger();
|
|
1496
|
+
this.cache = options.cache ?? new NoopCache();
|
|
1497
|
+
}
|
|
1498
|
+
static [entityKind] = "SingleStoreDriverSession";
|
|
1499
|
+
logger;
|
|
1500
|
+
cache;
|
|
1501
|
+
prepareQuery(query, fields, customResultMapper, generatedIds, returningIds, queryMetadata, cacheConfig) {
|
|
1502
|
+
return new SingleStoreDriverPreparedQuery(this.client, query.sql, query.params, this.logger, this.cache, queryMetadata, cacheConfig, fields, customResultMapper, generatedIds, returningIds);
|
|
1503
|
+
}
|
|
1504
|
+
/**
|
|
1505
|
+
* @internal
|
|
1506
|
+
* What is its purpose?
|
|
1507
|
+
*/
|
|
1508
|
+
async query(query, params) {
|
|
1509
|
+
this.logger.logQuery(query, params);
|
|
1510
|
+
return await this.client.query({
|
|
1511
|
+
sql: query,
|
|
1512
|
+
values: params,
|
|
1513
|
+
rowsAsArray: true,
|
|
1514
|
+
typeCast: function(field, next) {
|
|
1515
|
+
if (field.type === "TIMESTAMP" || field.type === "DATETIME" || field.type === "DATE") return field.string();
|
|
1516
|
+
return next();
|
|
1517
|
+
}
|
|
1518
|
+
});
|
|
1519
|
+
}
|
|
1520
|
+
all(query) {
|
|
1521
|
+
const querySql = this.dialect.sqlToQuery(query);
|
|
1522
|
+
this.logger.logQuery(querySql.sql, querySql.params);
|
|
1523
|
+
return this.client.execute(querySql.sql, querySql.params).then((result) => result[0]);
|
|
1524
|
+
}
|
|
1525
|
+
async transaction(transaction, config) {
|
|
1526
|
+
const session = isPool(this.client) ? new SingleStoreDriverSession(await this.client.getConnection(), this.dialect, this.schema, this.options) : this;
|
|
1527
|
+
const tx = new SingleStoreDriverTransaction(this.dialect, session, this.schema, 0);
|
|
1528
|
+
if (config) {
|
|
1529
|
+
const setTransactionConfigSql = this.getSetTransactionSQL(config);
|
|
1530
|
+
if (setTransactionConfigSql) await tx.execute(setTransactionConfigSql);
|
|
1531
|
+
const startTransactionSql = this.getStartTransactionSQL(config);
|
|
1532
|
+
await (startTransactionSql ? tx.execute(startTransactionSql) : tx.execute(sql`begin`));
|
|
1533
|
+
} else await tx.execute(sql`begin`);
|
|
1534
|
+
try {
|
|
1535
|
+
const result = await transaction(tx);
|
|
1536
|
+
await tx.execute(sql`commit`);
|
|
1537
|
+
return result;
|
|
1538
|
+
} catch (err) {
|
|
1539
|
+
await tx.execute(sql`rollback`);
|
|
1540
|
+
throw err;
|
|
1541
|
+
} finally {
|
|
1542
|
+
if (isPool(this.client)) session.client.release();
|
|
1543
|
+
}
|
|
1544
|
+
}
|
|
1545
|
+
};
|
|
1546
|
+
var SingleStoreDriverTransaction = class SingleStoreDriverTransaction extends SingleStoreTransaction {
|
|
1547
|
+
static [entityKind] = "SingleStoreDriverTransaction";
|
|
1548
|
+
async transaction(transaction) {
|
|
1549
|
+
const savepointName = `sp${this.nestedIndex + 1}`;
|
|
1550
|
+
const tx = new SingleStoreDriverTransaction(this.dialect, this.session, this.schema, this.nestedIndex + 1);
|
|
1551
|
+
await tx.execute(sql.raw(`savepoint ${savepointName}`));
|
|
1552
|
+
try {
|
|
1553
|
+
const result = await transaction(tx);
|
|
1554
|
+
await tx.execute(sql.raw(`release savepoint ${savepointName}`));
|
|
1555
|
+
return result;
|
|
1556
|
+
} catch (err) {
|
|
1557
|
+
await tx.execute(sql.raw(`rollback to savepoint ${savepointName}`));
|
|
1558
|
+
throw err;
|
|
1559
|
+
}
|
|
1560
|
+
}
|
|
1561
|
+
};
|
|
1562
|
+
function isPool(client) {
|
|
1563
|
+
return "getConnection" in client;
|
|
1564
|
+
}
|
|
1565
|
+
var SingleStoreDriverDriver = class {
|
|
1566
|
+
constructor(client, dialect, options = {}) {
|
|
1567
|
+
this.client = client;
|
|
1568
|
+
this.dialect = dialect;
|
|
1569
|
+
this.options = options;
|
|
1570
|
+
}
|
|
1571
|
+
static [entityKind] = "SingleStoreDriverDriver";
|
|
1572
|
+
createSession(schema) {
|
|
1573
|
+
return new SingleStoreDriverSession(this.client, this.dialect, schema, {
|
|
1574
|
+
logger: this.options.logger,
|
|
1575
|
+
cache: this.options.cache
|
|
1576
|
+
});
|
|
1577
|
+
}
|
|
1578
|
+
};
|
|
1579
|
+
var SingleStoreDriverDatabase = class extends SingleStoreDatabase {
|
|
1580
|
+
static [entityKind] = "SingleStoreDriverDatabase";
|
|
1581
|
+
};
|
|
1582
|
+
function construct(client, config = {}) {
|
|
1583
|
+
const dialect = new SingleStoreDialect({ casing: config.casing });
|
|
1584
|
+
let logger;
|
|
1585
|
+
if (config.logger === true) logger = new DefaultLogger();
|
|
1586
|
+
else if (config.logger !== false) logger = config.logger;
|
|
1587
|
+
const clientForInstance = isCallbackClient(client) ? client.promise() : client;
|
|
1588
|
+
let schema;
|
|
1589
|
+
if (config.schema) {
|
|
1590
|
+
const tablesConfig = extractTablesRelationalConfig(config.schema, createTableRelationsHelpers);
|
|
1591
|
+
schema = {
|
|
1592
|
+
fullSchema: config.schema,
|
|
1593
|
+
schema: tablesConfig.tables,
|
|
1594
|
+
tableNamesMap: tablesConfig.tableNamesMap
|
|
1595
|
+
};
|
|
1596
|
+
}
|
|
1597
|
+
const db = new SingleStoreDriverDatabase(dialect, new SingleStoreDriverDriver(clientForInstance, dialect, {
|
|
1598
|
+
logger,
|
|
1599
|
+
cache: config.cache
|
|
1600
|
+
}).createSession(schema), schema);
|
|
1601
|
+
db.$client = client;
|
|
1602
|
+
db.$cache = config.cache;
|
|
1603
|
+
if (db.$cache) db.$cache["invalidate"] = config.cache?.onMutate;
|
|
1604
|
+
return db;
|
|
1605
|
+
}
|
|
1606
|
+
function isCallbackClient(client) {
|
|
1607
|
+
return typeof client.promise === "function";
|
|
1608
|
+
}
|
|
1609
|
+
const CONNECTION_ATTRS = {
|
|
1610
|
+
_connector_name: "SingleStore Drizzle ORM Driver",
|
|
1611
|
+
_connector_version: version
|
|
1612
|
+
};
|
|
1613
|
+
function drizzle(...params) {
|
|
1614
|
+
if (typeof params[0] === "string") {
|
|
1615
|
+
const connectionString = params[0];
|
|
1616
|
+
return construct(createPool({
|
|
1617
|
+
uri: connectionString,
|
|
1618
|
+
connectAttributes: CONNECTION_ATTRS
|
|
1619
|
+
}), params[1]);
|
|
1620
|
+
}
|
|
1621
|
+
if (isConfig(params[0])) {
|
|
1622
|
+
const { connection, client, ...drizzleConfig } = params[0];
|
|
1623
|
+
if (client) return construct(client, drizzleConfig);
|
|
1624
|
+
let opts = {};
|
|
1625
|
+
opts = typeof connection === "string" ? {
|
|
1626
|
+
uri: connection,
|
|
1627
|
+
supportBigNumbers: true,
|
|
1628
|
+
connectAttributes: CONNECTION_ATTRS
|
|
1629
|
+
} : {
|
|
1630
|
+
...connection,
|
|
1631
|
+
connectAttributes: {
|
|
1632
|
+
...connection.connectAttributes,
|
|
1633
|
+
...CONNECTION_ATTRS
|
|
1634
|
+
}
|
|
1635
|
+
};
|
|
1636
|
+
return construct(createPool(opts), drizzleConfig);
|
|
1637
|
+
}
|
|
1638
|
+
return construct(params[0], params[1]);
|
|
1639
|
+
}
|
|
1640
|
+
((drizzle2) => {
|
|
1641
|
+
function mock(config) {
|
|
1642
|
+
return construct({}, config);
|
|
1643
|
+
}
|
|
1644
|
+
drizzle2.mock = mock;
|
|
1645
|
+
})(drizzle || (drizzle = {}));
|
|
1646
|
+
//#endregion
|
|
1647
|
+
export { drizzle };
|