drizzle-orm 0.25.2 → 0.25.3-4cc2d87
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/aws-data-api/pg/index.cjs +234 -1
- package/aws-data-api/pg/index.cjs.map +1 -1
- package/aws-data-api/pg/index.d.ts +5 -6
- package/aws-data-api/pg/index.mjs +228 -1
- package/aws-data-api/pg/index.mjs.map +1 -1
- package/aws-data-api/pg/migrator.cjs +13 -1
- package/aws-data-api/pg/migrator.cjs.map +1 -1
- package/aws-data-api/pg/migrator.d.ts +5 -6
- package/aws-data-api/pg/migrator.mjs +11 -1
- package/aws-data-api/pg/migrator.mjs.map +1 -1
- package/better-sqlite3/index.cjs +119 -1
- package/better-sqlite3/index.cjs.map +1 -1
- package/better-sqlite3/index.d.ts +14 -12
- package/better-sqlite3/index.mjs +114 -1
- package/better-sqlite3/index.mjs.map +1 -1
- package/better-sqlite3/migrator.cjs +13 -1
- package/better-sqlite3/migrator.cjs.map +1 -1
- package/better-sqlite3/migrator.d.ts +5 -5
- package/better-sqlite3/migrator.mjs +11 -1
- package/better-sqlite3/migrator.mjs.map +1 -1
- package/bun-sqlite/index.cjs +126 -1
- package/bun-sqlite/index.cjs.map +1 -1
- package/bun-sqlite/index.d.ts +14 -12
- package/bun-sqlite/index.mjs +121 -1
- package/bun-sqlite/index.mjs.map +1 -1
- package/bun-sqlite/migrator.cjs +13 -1
- package/bun-sqlite/migrator.cjs.map +1 -1
- package/bun-sqlite/migrator.d.ts +5 -5
- package/bun-sqlite/migrator.mjs +11 -1
- package/bun-sqlite/migrator.mjs.map +1 -1
- package/{column.d-8b137277.d.ts → column.d-c31e7ad3.d.ts} +77 -6
- package/d1/index.cjs +116 -1
- package/d1/index.cjs.map +1 -1
- package/d1/index.d.ts +15 -12
- package/d1/index.mjs +111 -1
- package/d1/index.mjs.map +1 -1
- package/d1/migrator.cjs +13 -1
- package/d1/migrator.cjs.map +1 -1
- package/d1/migrator.d.ts +5 -5
- package/d1/migrator.mjs +11 -1
- package/d1/migrator.mjs.map +1 -1
- package/{db.d-ae495c35.d.ts → db.d-a2311092.d.ts} +121 -44
- package/{db.d-66553b9e.d.ts → db.d-bc9a1d6c.d.ts} +193 -16
- package/driver.d-17ca4c15.d.ts +8 -0
- package/driver.d-300ddb0e.d.ts +64 -0
- package/driver.d-45e56643.d.ts +60 -0
- package/driver.d-64f2125c.d.ts +8 -0
- package/driver.d-7fde2e9d.d.ts +55 -0
- package/driver.d-9d703b84.d.ts +8 -0
- package/driver.d-b2b94bf9.d.ts +46 -0
- package/driver.d-b70ee7ee.d.ts +55 -0
- package/driver.d-dea23ee6.d.ts +8 -0
- package/driver.d-e54af17b.d.ts +52 -0
- package/driver.d-eb490c91.d.ts +9 -0
- package/driver.d-ef6fa2df.d.ts +14 -0
- package/errors-bb636d84.mjs +19 -0
- package/errors-bb636d84.mjs.map +1 -0
- package/errors-d0192d62.cjs +22 -0
- package/errors-d0192d62.cjs.map +1 -0
- package/index.cjs +105 -1
- package/index.cjs.map +1 -1
- package/index.d.ts +16 -432
- package/index.mjs +3 -1
- package/index.mjs.map +1 -1
- package/knex/index.cjs +2 -1
- package/knex/index.cjs.map +1 -1
- package/knex/index.d.ts +1 -1
- package/kysely/index.cjs +2 -1
- package/kysely/index.cjs.map +1 -1
- package/kysely/index.d.ts +1 -1
- package/libsql/index.cjs +155 -1
- package/libsql/index.cjs.map +1 -1
- package/libsql/index.d.ts +14 -12
- package/libsql/index.mjs +150 -1
- package/libsql/index.mjs.map +1 -1
- package/libsql/migrator.cjs +13 -1
- package/libsql/migrator.cjs.map +1 -1
- package/libsql/migrator.d.ts +5 -5
- package/libsql/migrator.mjs +11 -1
- package/libsql/migrator.mjs.map +1 -1
- package/logger-caa1ca6e.cjs +34 -0
- package/logger-caa1ca6e.cjs.map +1 -0
- package/logger-caf75bde.mjs +30 -0
- package/logger-caf75bde.mjs.map +1 -0
- package/migrator.cjs +48 -1
- package/migrator.cjs.map +1 -1
- package/migrator.mjs +46 -1
- package/migrator.mjs.map +1 -1
- package/mysql-core/index.cjs +1004 -1
- package/mysql-core/index.cjs.map +1 -1
- package/mysql-core/index.d.ts +347 -347
- package/mysql-core/index.mjs +869 -1
- package/mysql-core/index.mjs.map +1 -1
- package/mysql2/index.cjs +230 -1
- package/mysql2/index.cjs.map +1 -1
- package/mysql2/index.d.ts +5 -6
- package/mysql2/index.mjs +223 -1
- package/mysql2/index.mjs.map +1 -1
- package/mysql2/migrator.cjs +13 -1
- package/mysql2/migrator.cjs.map +1 -1
- package/mysql2/migrator.d.ts +5 -6
- package/mysql2/migrator.mjs +11 -1
- package/mysql2/migrator.mjs.map +1 -1
- package/neon-serverless/index.cjs +155 -1
- package/neon-serverless/index.cjs.map +1 -1
- package/neon-serverless/index.d.ts +5 -6
- package/neon-serverless/index.mjs +149 -1
- package/neon-serverless/index.mjs.map +1 -1
- package/neon-serverless/migrator.cjs +13 -1
- package/neon-serverless/migrator.cjs.map +1 -1
- package/neon-serverless/migrator.d.ts +5 -6
- package/neon-serverless/migrator.mjs +11 -1
- package/neon-serverless/migrator.mjs.map +1 -1
- package/node-postgres/index.cjs +157 -1
- package/node-postgres/index.cjs.map +1 -1
- package/node-postgres/index.d.ts +5 -6
- package/node-postgres/index.mjs +151 -1
- package/node-postgres/index.mjs.map +1 -1
- package/node-postgres/migrator.cjs +13 -1
- package/node-postgres/migrator.cjs.map +1 -1
- package/node-postgres/migrator.d.ts +5 -6
- package/node-postgres/migrator.mjs +11 -1
- package/node-postgres/migrator.mjs.map +1 -1
- package/package.json +97 -34
- package/pg-core/index.cjs +652 -1
- package/pg-core/index.cjs.map +1 -1
- package/pg-core/index.d.ts +13 -7
- package/pg-core/index.mjs +501 -1
- package/pg-core/index.mjs.map +1 -1
- package/planetscale-serverless/index.cjs +111 -1
- package/planetscale-serverless/index.cjs.map +1 -1
- package/planetscale-serverless/index.d.ts +5 -6
- package/planetscale-serverless/index.mjs +106 -1
- package/planetscale-serverless/index.mjs.map +1 -1
- package/planetscale-serverless/migrator.cjs +13 -1
- package/planetscale-serverless/migrator.cjs.map +1 -1
- package/planetscale-serverless/migrator.d.ts +5 -6
- package/planetscale-serverless/migrator.mjs +11 -1
- package/planetscale-serverless/migrator.mjs.map +1 -1
- package/postgres-js/index.cjs +113 -1
- package/postgres-js/index.cjs.map +1 -1
- package/postgres-js/index.d.ts +5 -6
- package/postgres-js/index.mjs +108 -1
- package/postgres-js/index.mjs.map +1 -1
- package/postgres-js/migrator.cjs +13 -1
- package/postgres-js/migrator.cjs.map +1 -1
- package/postgres-js/migrator.d.ts +5 -6
- package/postgres-js/migrator.mjs +11 -1
- package/postgres-js/migrator.mjs.map +1 -1
- package/query-promise.d-e370e0a9.d.ts +617 -0
- package/relations-47eb5c5f.mjs +2954 -0
- package/relations-47eb5c5f.mjs.map +1 -0
- package/relations-5e2d30dd.cjs +3117 -0
- package/relations-5e2d30dd.cjs.map +1 -0
- package/{select.types.d-c3e86d45.d.ts → select.types.d-34d7f74e.d.ts} +1 -1
- package/{select.types.d-adb82002.d.ts → select.types.d-ae2f8e44.d.ts} +58 -14
- package/session-483ed08d.mjs +1263 -0
- package/session-483ed08d.mjs.map +1 -0
- package/session-6bd76405.cjs +362 -0
- package/session-6bd76405.cjs.map +1 -0
- package/session-a90df8a2.cjs +1252 -0
- package/session-a90df8a2.cjs.map +1 -0
- package/session-b99382a2.mjs +1223 -0
- package/session-b99382a2.mjs.map +1 -0
- package/session-c62f6348.cjs +1298 -0
- package/session-c62f6348.cjs.map +1 -0
- package/session-e8745392.mjs +351 -0
- package/session-e8745392.mjs.map +1 -0
- package/sql-js/index.cjs +175 -1
- package/sql-js/index.cjs.map +1 -1
- package/sql-js/index.d.ts +14 -12
- package/sql-js/index.mjs +170 -1
- package/sql-js/index.mjs.map +1 -1
- package/sql-js/migrator.cjs +13 -1
- package/sql-js/migrator.cjs.map +1 -1
- package/sql-js/migrator.d.ts +5 -5
- package/sql-js/migrator.mjs +11 -1
- package/sql-js/migrator.mjs.map +1 -1
- package/sqlite-core/index.cjs +516 -1
- package/sqlite-core/index.cjs.map +1 -1
- package/sqlite-core/index.d.ts +6 -5
- package/sqlite-core/index.mjs +441 -1
- package/sqlite-core/index.mjs.map +1 -1
- package/sqlite-proxy/index.cjs +118 -1
- package/sqlite-proxy/index.cjs.map +1 -1
- package/sqlite-proxy/index.d.ts +12 -11
- package/sqlite-proxy/index.mjs +113 -1
- package/sqlite-proxy/index.mjs.map +1 -1
- package/sqlite-proxy/migrator.cjs +25 -2
- package/sqlite-proxy/migrator.cjs.map +1 -1
- package/sqlite-proxy/migrator.d.ts +5 -5
- package/sqlite-proxy/migrator.mjs +23 -2
- package/sqlite-proxy/migrator.mjs.map +1 -1
- package/version.cjs +9 -1
- package/version.cjs.map +1 -1
- package/version.d.ts +1 -1
- package/version.mjs +6 -1
- package/version.mjs.map +1 -1
- package/README.md +0 -150
- package/column-builder-592f0191.mjs +0 -2
- package/column-builder-592f0191.mjs.map +0 -1
- package/column-builder-b48639f3.cjs +0 -2
- package/column-builder-b48639f3.cjs.map +0 -1
- package/driver.d-0158cd93.d.ts +0 -11
- package/driver.d-1f73a4a9.d.ts +0 -70
- package/driver.d-2e907d12.d.ts +0 -17
- package/driver.d-3781598a.d.ts +0 -11
- package/driver.d-3a8adf2a.d.ts +0 -61
- package/driver.d-59580d08.d.ts +0 -11
- package/driver.d-5f3fc125.d.ts +0 -12
- package/driver.d-693f7f9f.d.ts +0 -56
- package/driver.d-6c43e393.d.ts +0 -60
- package/driver.d-b00fc6ec.d.ts +0 -57
- package/driver.d-e4bd120b.d.ts +0 -47
- package/driver.d-f4b5b390.d.ts +0 -11
- package/index-59b7992d.cjs +0 -2
- package/index-59b7992d.cjs.map +0 -1
- package/index-b71998f1.mjs +0 -2
- package/index-b71998f1.mjs.map +0 -1
- package/logger-04bad527.cjs +0 -2
- package/logger-04bad527.cjs.map +0 -1
- package/logger-2598bf05.mjs +0 -2
- package/logger-2598bf05.mjs.map +0 -1
- package/logger.d-37185354.d.ts +0 -21
- package/query-builder-2f2e8229.cjs +0 -2
- package/query-builder-2f2e8229.cjs.map +0 -1
- package/query-builder-2fcde2f0.mjs +0 -2
- package/query-builder-2fcde2f0.mjs.map +0 -1
- package/query-promise-2c5b43ab.cjs +0 -2
- package/query-promise-2c5b43ab.cjs.map +0 -1
- package/query-promise-a65edd44.mjs +0 -2
- package/query-promise-a65edd44.mjs.map +0 -1
- package/query-promise.d-a8af8583.d.ts +0 -9
- package/session-8a621f09.mjs +0 -8
- package/session-8a621f09.mjs.map +0 -1
- package/session-b6939bab.cjs +0 -14
- package/session-b6939bab.cjs.map +0 -1
- package/session-b977ce56.mjs +0 -14
- package/session-b977ce56.mjs.map +0 -1
- package/session-c891400d.mjs +0 -8
- package/session-c891400d.mjs.map +0 -1
- package/session-e6db6732.cjs +0 -8
- package/session-e6db6732.cjs.map +0 -1
- package/session-ef1ef979.cjs +0 -8
- package/session-ef1ef979.cjs.map +0 -1
- package/utils-9d882195.cjs +0 -2
- package/utils-9d882195.cjs.map +0 -1
- package/utils-e6870670.mjs +0 -2
- package/utils-e6870670.mjs.map +0 -1
|
@@ -0,0 +1,1263 @@
|
|
|
1
|
+
import { T as TransactionRollbackError } from './errors-bb636d84.mjs';
|
|
2
|
+
import { al as Table, f as ColumnBuilder, e as Column, Q as QueryPromise, Z as SQL, a4 as Param, aq as mapUpdateSet, a6 as sql, ad as SelectionProxyHandler, as as getTableColumns, av as View, $ as name, ap as orderSelectedFields, an as getTableName, ab as Subquery, aa as SubqueryConfig, au as ViewBaseConfig, c as aliasedTableColumn, b as aliasedRelation, m as mapColumnsInAliasedSQLToAlias, G as Relation, S as normalizeRelation, i as and, h as eq, o as or, J as orderByOperators, d as mapColumnsInSQLToAlias, a as aliasedTable, I as operators, ar as applyMixins, bH as TypedQueryBuilder, at as getTableLikeName, ac as WithSubquery, V as mapRelationalRow } from './relations-47eb5c5f.mjs';
|
|
3
|
+
|
|
4
|
+
var _a, _b;
|
|
5
|
+
/** @internal */
|
|
6
|
+
const InlineForeignKeys = Symbol('InlineForeignKeys');
|
|
7
|
+
class MySqlTable extends Table {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(...arguments);
|
|
10
|
+
/** @internal */
|
|
11
|
+
this[_a] = [];
|
|
12
|
+
/** @internal */
|
|
13
|
+
this[_b] = undefined;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
Table.Symbol.Columns, _a = InlineForeignKeys, _b = Table.Symbol.ExtraConfigBuilder;
|
|
17
|
+
/** @internal */
|
|
18
|
+
MySqlTable.Symbol = Object.assign({}, Table.Symbol, {
|
|
19
|
+
InlineForeignKeys: InlineForeignKeys,
|
|
20
|
+
});
|
|
21
|
+
function mysqlTableWithSchema(name, columns, extraConfig, schema, baseName = name) {
|
|
22
|
+
const rawTable = new MySqlTable(name, schema, baseName);
|
|
23
|
+
const builtColumns = Object.fromEntries(Object.entries(columns).map(([name, colBuilder]) => {
|
|
24
|
+
const column = colBuilder.build(rawTable);
|
|
25
|
+
rawTable[InlineForeignKeys].push(...colBuilder.buildForeignKeys(column, rawTable));
|
|
26
|
+
return [name, column];
|
|
27
|
+
}));
|
|
28
|
+
const table = Object.assign(rawTable, builtColumns);
|
|
29
|
+
table[Table.Symbol.Columns] = builtColumns;
|
|
30
|
+
if (extraConfig) {
|
|
31
|
+
table[MySqlTable.Symbol.ExtraConfigBuilder] = extraConfig;
|
|
32
|
+
}
|
|
33
|
+
return table;
|
|
34
|
+
}
|
|
35
|
+
const mysqlTable = (name, columns, extraConfig) => {
|
|
36
|
+
return mysqlTableWithSchema(name, columns, extraConfig, undefined, name);
|
|
37
|
+
};
|
|
38
|
+
function mysqlTableCreator(customizeTableName) {
|
|
39
|
+
return (name, columns, extraConfig) => {
|
|
40
|
+
return mysqlTableWithSchema(customizeTableName(name), columns, extraConfig, undefined, name);
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class ForeignKeyBuilder {
|
|
45
|
+
constructor(config, actions) {
|
|
46
|
+
this.reference = () => {
|
|
47
|
+
const { columns, foreignColumns } = config();
|
|
48
|
+
return { columns, foreignTable: foreignColumns[0].table, foreignColumns };
|
|
49
|
+
};
|
|
50
|
+
if (actions) {
|
|
51
|
+
this._onUpdate = actions.onUpdate;
|
|
52
|
+
this._onDelete = actions.onDelete;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
onUpdate(action) {
|
|
56
|
+
this._onUpdate = action;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
onDelete(action) {
|
|
60
|
+
this._onDelete = action;
|
|
61
|
+
return this;
|
|
62
|
+
}
|
|
63
|
+
/** @internal */
|
|
64
|
+
build(table) {
|
|
65
|
+
return new ForeignKey(table, this);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
class ForeignKey {
|
|
69
|
+
constructor(table, builder) {
|
|
70
|
+
this.table = table;
|
|
71
|
+
this.reference = builder.reference;
|
|
72
|
+
this.onUpdate = builder._onUpdate;
|
|
73
|
+
this.onDelete = builder._onDelete;
|
|
74
|
+
}
|
|
75
|
+
getName() {
|
|
76
|
+
const { columns, foreignColumns } = this.reference();
|
|
77
|
+
const columnNames = columns.map((column) => column.name);
|
|
78
|
+
const foreignColumnNames = foreignColumns.map((column) => column.name);
|
|
79
|
+
const chunks = [
|
|
80
|
+
this.table[MySqlTable.Symbol.Name],
|
|
81
|
+
...columnNames,
|
|
82
|
+
foreignColumns[0].table[MySqlTable.Symbol.Name],
|
|
83
|
+
...foreignColumnNames,
|
|
84
|
+
];
|
|
85
|
+
return `${chunks.join('_')}_fk`;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function foreignKey(config) {
|
|
89
|
+
function mappedConfig() {
|
|
90
|
+
const { columns, foreignColumns } = config;
|
|
91
|
+
return {
|
|
92
|
+
columns,
|
|
93
|
+
foreignColumns,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return new ForeignKeyBuilder(mappedConfig);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class MySqlColumnBuilder extends ColumnBuilder {
|
|
100
|
+
constructor() {
|
|
101
|
+
super(...arguments);
|
|
102
|
+
this.foreignKeyConfigs = [];
|
|
103
|
+
}
|
|
104
|
+
references(ref, actions = {}) {
|
|
105
|
+
this.foreignKeyConfigs.push({ ref, actions });
|
|
106
|
+
return this;
|
|
107
|
+
}
|
|
108
|
+
/** @internal */
|
|
109
|
+
buildForeignKeys(column, table) {
|
|
110
|
+
return this.foreignKeyConfigs.map(({ ref, actions }) => {
|
|
111
|
+
return ((ref, actions) => {
|
|
112
|
+
const builder = new ForeignKeyBuilder(() => {
|
|
113
|
+
const foreignColumn = ref();
|
|
114
|
+
return { columns: [column], foreignColumns: [foreignColumn] };
|
|
115
|
+
});
|
|
116
|
+
if (actions.onUpdate) {
|
|
117
|
+
builder.onUpdate(actions.onUpdate);
|
|
118
|
+
}
|
|
119
|
+
if (actions.onDelete) {
|
|
120
|
+
builder.onDelete(actions.onDelete);
|
|
121
|
+
}
|
|
122
|
+
return builder.build(table);
|
|
123
|
+
})(ref, actions);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// To understand how to use `MySqlColumn` and `AnyMySqlColumn`, see `Column` and `AnyColumn` documentation.
|
|
128
|
+
class MySqlColumn extends Column {
|
|
129
|
+
}
|
|
130
|
+
class MySqlColumnBuilderWithAutoIncrement extends MySqlColumnBuilder {
|
|
131
|
+
constructor(name) {
|
|
132
|
+
super(name);
|
|
133
|
+
this.config.autoIncrement = false;
|
|
134
|
+
}
|
|
135
|
+
autoincrement() {
|
|
136
|
+
this.config.autoIncrement = true;
|
|
137
|
+
return this;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
class MySqlColumnWithAutoIncrement extends MySqlColumn {
|
|
141
|
+
constructor() {
|
|
142
|
+
super(...arguments);
|
|
143
|
+
this.autoIncrement = this.config.autoIncrement;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
class MySqlDelete extends QueryPromise {
|
|
148
|
+
constructor(table, session, dialect) {
|
|
149
|
+
super();
|
|
150
|
+
this.table = table;
|
|
151
|
+
this.session = session;
|
|
152
|
+
this.dialect = dialect;
|
|
153
|
+
this.execute = (placeholderValues) => {
|
|
154
|
+
return this.prepare().execute(placeholderValues);
|
|
155
|
+
};
|
|
156
|
+
this.createIterator = () => {
|
|
157
|
+
const self = this;
|
|
158
|
+
return async function* (placeholderValues) {
|
|
159
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
160
|
+
};
|
|
161
|
+
};
|
|
162
|
+
this.iterator = this.createIterator();
|
|
163
|
+
this.config = { table };
|
|
164
|
+
}
|
|
165
|
+
where(where) {
|
|
166
|
+
this.config.where = where;
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
/** @internal */
|
|
170
|
+
getSQL() {
|
|
171
|
+
return this.dialect.buildDeleteQuery(this.config);
|
|
172
|
+
}
|
|
173
|
+
toSQL() {
|
|
174
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
175
|
+
return rest;
|
|
176
|
+
}
|
|
177
|
+
prepare() {
|
|
178
|
+
return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
class MySqlInsertBuilder {
|
|
183
|
+
constructor(table, session, dialect) {
|
|
184
|
+
this.table = table;
|
|
185
|
+
this.session = session;
|
|
186
|
+
this.dialect = dialect;
|
|
187
|
+
this.shouldIgnore = false;
|
|
188
|
+
}
|
|
189
|
+
ignore() {
|
|
190
|
+
this.shouldIgnore = true;
|
|
191
|
+
return this;
|
|
192
|
+
}
|
|
193
|
+
values(values) {
|
|
194
|
+
values = Array.isArray(values) ? values : [values];
|
|
195
|
+
if (values.length === 0) {
|
|
196
|
+
throw new Error('values() must be called with at least one value');
|
|
197
|
+
}
|
|
198
|
+
const mappedValues = values.map((entry) => {
|
|
199
|
+
const result = {};
|
|
200
|
+
const cols = this.table[Table.Symbol.Columns];
|
|
201
|
+
for (const colKey of Object.keys(entry)) {
|
|
202
|
+
const colValue = entry[colKey];
|
|
203
|
+
result[colKey] = colValue instanceof SQL ? colValue : new Param(colValue, cols[colKey]);
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
});
|
|
207
|
+
return new MySqlInsert(this.table, mappedValues, this.shouldIgnore, this.session, this.dialect);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
class MySqlInsert extends QueryPromise {
|
|
211
|
+
constructor(table, values, ignore, session, dialect) {
|
|
212
|
+
super();
|
|
213
|
+
this.session = session;
|
|
214
|
+
this.dialect = dialect;
|
|
215
|
+
this.execute = (placeholderValues) => {
|
|
216
|
+
return this.prepare().execute(placeholderValues);
|
|
217
|
+
};
|
|
218
|
+
this.createIterator = () => {
|
|
219
|
+
const self = this;
|
|
220
|
+
return async function* (placeholderValues) {
|
|
221
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
222
|
+
};
|
|
223
|
+
};
|
|
224
|
+
this.iterator = this.createIterator();
|
|
225
|
+
this.config = { table, values, ignore };
|
|
226
|
+
}
|
|
227
|
+
onDuplicateKeyUpdate(config) {
|
|
228
|
+
const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));
|
|
229
|
+
this.config.onConflict = sql `update ${setSql}`;
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
/** @internal */
|
|
233
|
+
getSQL() {
|
|
234
|
+
return this.dialect.buildInsertQuery(this.config);
|
|
235
|
+
}
|
|
236
|
+
toSQL() {
|
|
237
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
238
|
+
return rest;
|
|
239
|
+
}
|
|
240
|
+
prepare() {
|
|
241
|
+
return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), undefined);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
class ViewBuilderCore {
|
|
246
|
+
constructor(name, schema) {
|
|
247
|
+
this.name = name;
|
|
248
|
+
this.schema = schema;
|
|
249
|
+
this.config = {};
|
|
250
|
+
}
|
|
251
|
+
algorithm(algorithm) {
|
|
252
|
+
this.config.algorithm = algorithm;
|
|
253
|
+
return this;
|
|
254
|
+
}
|
|
255
|
+
definer(definer) {
|
|
256
|
+
this.config.definer = definer;
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
sqlSecurity(sqlSecurity) {
|
|
260
|
+
this.config.sqlSecurity = sqlSecurity;
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
withCheckOption(withCheckOption) {
|
|
264
|
+
this.config.withCheckOption = withCheckOption ?? 'cascaded';
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
class ViewBuilder extends ViewBuilderCore {
|
|
269
|
+
as(qb) {
|
|
270
|
+
if (typeof qb === 'function') {
|
|
271
|
+
qb = qb(new QueryBuilder());
|
|
272
|
+
}
|
|
273
|
+
const selectionProxy = new SelectionProxyHandler({
|
|
274
|
+
alias: this.name,
|
|
275
|
+
sqlBehavior: 'error',
|
|
276
|
+
sqlAliasedBehavior: 'alias',
|
|
277
|
+
replaceOriginalName: true,
|
|
278
|
+
});
|
|
279
|
+
const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
|
|
280
|
+
return new Proxy(new MySqlView({
|
|
281
|
+
mysqlConfig: this.config,
|
|
282
|
+
config: {
|
|
283
|
+
name: this.name,
|
|
284
|
+
schema: this.schema,
|
|
285
|
+
selectedFields: aliasedSelection,
|
|
286
|
+
query: qb.getSQL().inlineParams(),
|
|
287
|
+
},
|
|
288
|
+
}), selectionProxy);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
class ManualViewBuilder extends ViewBuilderCore {
|
|
292
|
+
constructor(name, columns, schema) {
|
|
293
|
+
super(name, schema);
|
|
294
|
+
this.columns = getTableColumns(mysqlTable(name, columns));
|
|
295
|
+
}
|
|
296
|
+
existing() {
|
|
297
|
+
return new Proxy(new MySqlView({
|
|
298
|
+
mysqlConfig: undefined,
|
|
299
|
+
config: {
|
|
300
|
+
name: this.name,
|
|
301
|
+
schema: this.schema,
|
|
302
|
+
selectedFields: this.columns,
|
|
303
|
+
query: undefined,
|
|
304
|
+
},
|
|
305
|
+
}), new SelectionProxyHandler({
|
|
306
|
+
alias: this.name,
|
|
307
|
+
sqlBehavior: 'error',
|
|
308
|
+
sqlAliasedBehavior: 'alias',
|
|
309
|
+
replaceOriginalName: true,
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
as(query) {
|
|
313
|
+
return new Proxy(new MySqlView({
|
|
314
|
+
mysqlConfig: this.config,
|
|
315
|
+
config: {
|
|
316
|
+
name: this.name,
|
|
317
|
+
schema: this.schema,
|
|
318
|
+
selectedFields: this.columns,
|
|
319
|
+
query: query.inlineParams(),
|
|
320
|
+
},
|
|
321
|
+
}), new SelectionProxyHandler({
|
|
322
|
+
alias: this.name,
|
|
323
|
+
sqlBehavior: 'error',
|
|
324
|
+
sqlAliasedBehavior: 'alias',
|
|
325
|
+
replaceOriginalName: true,
|
|
326
|
+
}));
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
class MySqlViewBase extends View {
|
|
330
|
+
}
|
|
331
|
+
const MySqlViewConfig = Symbol('MySqlViewConfig');
|
|
332
|
+
class MySqlView extends MySqlViewBase {
|
|
333
|
+
constructor({ mysqlConfig, config }) {
|
|
334
|
+
super(config);
|
|
335
|
+
this[MySqlViewConfig] = mysqlConfig;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/** @internal */
|
|
339
|
+
function mysqlViewWithSchema(name, selection, schema) {
|
|
340
|
+
if (selection) {
|
|
341
|
+
return new ManualViewBuilder(name, selection, schema);
|
|
342
|
+
}
|
|
343
|
+
return new ViewBuilder(name, schema);
|
|
344
|
+
}
|
|
345
|
+
function mysqlView(name, selection) {
|
|
346
|
+
return mysqlViewWithSchema(name, selection, undefined);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// TODO find out how to use all/values. Seems like I need those functions
|
|
350
|
+
// Build project
|
|
351
|
+
// copy runtime tests to be sure it's working
|
|
352
|
+
// Add mysql to drizzle-kit
|
|
353
|
+
// Add Planetscale Driver and create example repo
|
|
354
|
+
class MySqlDialect {
|
|
355
|
+
async migrate(migrations, session, config) {
|
|
356
|
+
const migrationsTable = config.migrationsTable ?? '__drizzle_migrations';
|
|
357
|
+
const migrationTableCreate = sql `
|
|
358
|
+
create table if not exists ${name(migrationsTable)} (
|
|
359
|
+
id serial primary key,
|
|
360
|
+
hash text not null,
|
|
361
|
+
created_at bigint
|
|
362
|
+
)
|
|
363
|
+
`;
|
|
364
|
+
await session.execute(migrationTableCreate);
|
|
365
|
+
const dbMigrations = await session.all(sql `select id, hash, created_at from ${name(migrationsTable)} order by created_at desc limit 1`);
|
|
366
|
+
const lastDbMigration = dbMigrations[0];
|
|
367
|
+
await session.transaction(async (tx) => {
|
|
368
|
+
for (const migration of migrations) {
|
|
369
|
+
if (!lastDbMigration
|
|
370
|
+
|| Number(lastDbMigration.created_at) < migration.folderMillis) {
|
|
371
|
+
for (const stmt of migration.sql) {
|
|
372
|
+
await tx.execute(sql.raw(stmt));
|
|
373
|
+
}
|
|
374
|
+
await tx.execute(sql `insert into ${name(migrationsTable)} (\`hash\`, \`created_at\`) values(${migration.hash}, ${migration.folderMillis})`);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
escapeName(name) {
|
|
380
|
+
return `\`${name}\``;
|
|
381
|
+
}
|
|
382
|
+
escapeParam(_num) {
|
|
383
|
+
return `?`;
|
|
384
|
+
}
|
|
385
|
+
escapeString(str) {
|
|
386
|
+
return `'${str.replace(/'/g, "''")}'`;
|
|
387
|
+
}
|
|
388
|
+
buildDeleteQuery({ table, where, returning }) {
|
|
389
|
+
const returningSql = returning
|
|
390
|
+
? sql ` returning ${this.buildSelection(returning, { isSingleTable: true })}`
|
|
391
|
+
: undefined;
|
|
392
|
+
const whereSql = where ? sql ` where ${where}` : undefined;
|
|
393
|
+
return sql `delete from ${table}${whereSql}${returningSql}`;
|
|
394
|
+
}
|
|
395
|
+
buildUpdateSet(table, set) {
|
|
396
|
+
const setEntries = Object.entries(set);
|
|
397
|
+
const setSize = setEntries.length;
|
|
398
|
+
return sql.fromList(setEntries
|
|
399
|
+
.flatMap(([colName, value], i) => {
|
|
400
|
+
const col = table[Table.Symbol.Columns][colName];
|
|
401
|
+
const res = sql `${name(col.name)} = ${value}`;
|
|
402
|
+
if (i < setSize - 1) {
|
|
403
|
+
return [res, sql.raw(', ')];
|
|
404
|
+
}
|
|
405
|
+
return [res];
|
|
406
|
+
}));
|
|
407
|
+
}
|
|
408
|
+
buildUpdateQuery({ table, set, where, returning }) {
|
|
409
|
+
const setSql = this.buildUpdateSet(table, set);
|
|
410
|
+
const returningSql = returning
|
|
411
|
+
? sql ` returning ${this.buildSelection(returning, { isSingleTable: true })}`
|
|
412
|
+
: undefined;
|
|
413
|
+
const whereSql = where ? sql ` where ${where}` : undefined;
|
|
414
|
+
return sql `update ${table} set ${setSql}${whereSql}${returningSql}`;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Builds selection SQL with provided fields/expressions
|
|
418
|
+
*
|
|
419
|
+
* Examples:
|
|
420
|
+
*
|
|
421
|
+
* `select <selection> from`
|
|
422
|
+
*
|
|
423
|
+
* `insert ... returning <selection>`
|
|
424
|
+
*
|
|
425
|
+
* If `isSingleTable` is true, then columns won't be prefixed with table name
|
|
426
|
+
*/
|
|
427
|
+
buildSelection(fields, { isSingleTable = false } = {}) {
|
|
428
|
+
const columnsLen = fields.length;
|
|
429
|
+
const chunks = fields
|
|
430
|
+
.flatMap(({ field }, i) => {
|
|
431
|
+
const chunk = [];
|
|
432
|
+
if (field instanceof SQL.Aliased && field.isSelectionField) {
|
|
433
|
+
chunk.push(name(field.fieldAlias));
|
|
434
|
+
}
|
|
435
|
+
else if (field instanceof SQL.Aliased || field instanceof SQL) {
|
|
436
|
+
const query = field instanceof SQL.Aliased ? field.sql : field;
|
|
437
|
+
if (isSingleTable) {
|
|
438
|
+
chunk.push(new SQL(query.queryChunks.map((c) => {
|
|
439
|
+
if (c instanceof MySqlColumn) {
|
|
440
|
+
return name(c.name);
|
|
441
|
+
}
|
|
442
|
+
return c;
|
|
443
|
+
})));
|
|
444
|
+
}
|
|
445
|
+
else {
|
|
446
|
+
chunk.push(query);
|
|
447
|
+
}
|
|
448
|
+
if (field instanceof SQL.Aliased) {
|
|
449
|
+
chunk.push(sql ` as ${name(field.fieldAlias)}`);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
else if (field instanceof Column) {
|
|
453
|
+
if (isSingleTable) {
|
|
454
|
+
chunk.push(name(field.name));
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
chunk.push(field);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (i < columnsLen - 1) {
|
|
461
|
+
chunk.push(sql `, `);
|
|
462
|
+
}
|
|
463
|
+
return chunk;
|
|
464
|
+
});
|
|
465
|
+
return sql.fromList(chunks);
|
|
466
|
+
}
|
|
467
|
+
buildSelectQuery({ withList, fields, fieldsFlat, where, having, table, joins, orderBy, groupBy, limit, offset, lockingClause }) {
|
|
468
|
+
const fieldsList = fieldsFlat ?? orderSelectedFields(fields);
|
|
469
|
+
for (const f of fieldsList) {
|
|
470
|
+
if (f.field instanceof Column
|
|
471
|
+
&& getTableName(f.field.table)
|
|
472
|
+
!== (table instanceof Subquery
|
|
473
|
+
? table[SubqueryConfig].alias
|
|
474
|
+
: table instanceof MySqlViewBase
|
|
475
|
+
? table[ViewBaseConfig].name
|
|
476
|
+
: table instanceof SQL
|
|
477
|
+
? undefined
|
|
478
|
+
: getTableName(table))
|
|
479
|
+
&& !((table) => joins.some(({ alias }) => alias === getTableName(table)))(f.field.table)) {
|
|
480
|
+
const tableName = getTableName(f.field.table);
|
|
481
|
+
throw new Error(`Your "${f.path.join('->')}" field references a column "${tableName}"."${f.field.name}", but the table "${tableName}" is not part of the query! Did you forget to join it?`);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
const isSingleTable = joins.length === 0;
|
|
485
|
+
let withSql;
|
|
486
|
+
if (withList.length) {
|
|
487
|
+
const withSqlChunks = [sql `with `];
|
|
488
|
+
for (const [i, w] of withList.entries()) {
|
|
489
|
+
withSqlChunks.push(sql `${name(w[SubqueryConfig].alias)} as (${w[SubqueryConfig].sql})`);
|
|
490
|
+
if (i < withList.length - 1) {
|
|
491
|
+
withSqlChunks.push(sql `, `);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
withSqlChunks.push(sql ` `);
|
|
495
|
+
withSql = sql.fromList(withSqlChunks);
|
|
496
|
+
}
|
|
497
|
+
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
498
|
+
const tableSql = (() => {
|
|
499
|
+
if (table instanceof Table && table[Table.Symbol.OriginalName] !== table[Table.Symbol.Name]) {
|
|
500
|
+
return sql `${name(table[Table.Symbol.OriginalName])} ${name(table[Table.Symbol.Name])}`;
|
|
501
|
+
}
|
|
502
|
+
return table;
|
|
503
|
+
})();
|
|
504
|
+
const joinsArray = [];
|
|
505
|
+
for (const [index, joinMeta] of joins.entries()) {
|
|
506
|
+
if (index === 0) {
|
|
507
|
+
joinsArray.push(sql ` `);
|
|
508
|
+
}
|
|
509
|
+
const table = joinMeta.table;
|
|
510
|
+
if (table instanceof MySqlTable) {
|
|
511
|
+
const tableName = table[MySqlTable.Symbol.Name];
|
|
512
|
+
const tableSchema = table[MySqlTable.Symbol.Schema];
|
|
513
|
+
const origTableName = table[MySqlTable.Symbol.OriginalName];
|
|
514
|
+
const alias = tableName === origTableName ? undefined : joinMeta.alias;
|
|
515
|
+
joinsArray.push(sql `${sql.raw(joinMeta.joinType)} join ${tableSchema ? sql `${name(tableSchema)}.` : undefined}${name(origTableName)}${alias && sql ` ${name(alias)}`} on ${joinMeta.on}`);
|
|
516
|
+
}
|
|
517
|
+
else if (table instanceof View) {
|
|
518
|
+
const viewName = table[ViewBaseConfig].name;
|
|
519
|
+
const viewSchema = table[ViewBaseConfig].schema;
|
|
520
|
+
const origViewName = table[ViewBaseConfig].originalName;
|
|
521
|
+
const alias = viewName === origViewName ? undefined : joinMeta.alias;
|
|
522
|
+
joinsArray.push(sql `${sql.raw(joinMeta.joinType)} join ${viewSchema ? sql `${name(viewSchema)}.` : undefined}${name(origViewName)}${alias && sql ` ${name(alias)}`} on ${joinMeta.on}`);
|
|
523
|
+
}
|
|
524
|
+
else {
|
|
525
|
+
joinsArray.push(sql `${sql.raw(joinMeta.joinType)} join ${table} on ${joinMeta.on}`);
|
|
526
|
+
}
|
|
527
|
+
if (index < joins.length - 1) {
|
|
528
|
+
joinsArray.push(sql ` `);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
const joinsSql = sql.fromList(joinsArray);
|
|
532
|
+
const whereSql = where ? sql ` where ${where}` : undefined;
|
|
533
|
+
const havingSql = having ? sql ` having ${having}` : undefined;
|
|
534
|
+
const orderByList = [];
|
|
535
|
+
for (const [index, orderByValue] of orderBy.entries()) {
|
|
536
|
+
orderByList.push(orderByValue);
|
|
537
|
+
if (index < orderBy.length - 1) {
|
|
538
|
+
orderByList.push(sql `, `);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
const orderBySql = orderByList.length > 0 ? sql ` order by ${sql.fromList(orderByList)}` : undefined;
|
|
542
|
+
const groupByList = [];
|
|
543
|
+
for (const [index, groupByValue] of groupBy.entries()) {
|
|
544
|
+
groupByList.push(groupByValue);
|
|
545
|
+
if (index < groupBy.length - 1) {
|
|
546
|
+
groupByList.push(sql `, `);
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
const groupBySql = groupByList.length > 0 ? sql ` group by ${sql.fromList(groupByList)}` : undefined;
|
|
550
|
+
const limitSql = limit ? sql ` limit ${limit}` : undefined;
|
|
551
|
+
const offsetSql = offset ? sql ` offset ${offset}` : undefined;
|
|
552
|
+
let lockingClausesSql;
|
|
553
|
+
if (lockingClause) {
|
|
554
|
+
const { config, strength } = lockingClause;
|
|
555
|
+
lockingClausesSql = sql ` for ${sql.raw(strength)}`;
|
|
556
|
+
if (config.noWait) {
|
|
557
|
+
lockingClausesSql.append(sql ` no wait`);
|
|
558
|
+
}
|
|
559
|
+
else if (config.skipLocked) {
|
|
560
|
+
lockingClausesSql.append(sql ` skip locked`);
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return sql `${withSql}select ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClausesSql}`;
|
|
564
|
+
}
|
|
565
|
+
buildInsertQuery({ table, values, ignore, onConflict }) {
|
|
566
|
+
const isSingleValue = values.length === 1;
|
|
567
|
+
const valuesSqlList = [];
|
|
568
|
+
const columns = table[Table.Symbol.Columns];
|
|
569
|
+
const colEntries = isSingleValue
|
|
570
|
+
? Object.keys(values[0]).map((fieldName) => [fieldName, columns[fieldName]])
|
|
571
|
+
: Object.entries(columns);
|
|
572
|
+
const insertOrder = colEntries.map(([, column]) => name(column.name));
|
|
573
|
+
for (const [valueIndex, value] of values.entries()) {
|
|
574
|
+
const valueList = [];
|
|
575
|
+
for (const [fieldName] of colEntries) {
|
|
576
|
+
const colValue = value[fieldName];
|
|
577
|
+
if (colValue === undefined || (colValue instanceof Param && colValue.value === undefined)) {
|
|
578
|
+
valueList.push(sql `default`);
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
valueList.push(colValue);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
valuesSqlList.push(valueList);
|
|
585
|
+
if (valueIndex < values.length - 1) {
|
|
586
|
+
valuesSqlList.push(sql `, `);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
const valuesSql = sql.fromList(valuesSqlList);
|
|
590
|
+
const ignoreSql = ignore ? sql ` ignore` : undefined;
|
|
591
|
+
const onConflictSql = onConflict ? sql ` on duplicate key ${onConflict}` : undefined;
|
|
592
|
+
return sql `insert${ignoreSql} into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}`;
|
|
593
|
+
}
|
|
594
|
+
sqlToQuery(sql) {
|
|
595
|
+
return sql.toQuery({
|
|
596
|
+
escapeName: this.escapeName,
|
|
597
|
+
escapeParam: this.escapeParam,
|
|
598
|
+
escapeString: this.escapeString,
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
buildRelationalQuery(fullSchema, schema, tableNamesMap, table, tableConfig, config, tableAlias, relationColumns, isRoot = false) {
|
|
602
|
+
if (config === true) {
|
|
603
|
+
const selectionEntries = Object.entries(tableConfig.columns);
|
|
604
|
+
const selection = selectionEntries.map(([key, value]) => ({
|
|
605
|
+
dbKey: value.name,
|
|
606
|
+
tsKey: key,
|
|
607
|
+
field: value,
|
|
608
|
+
tableTsKey: undefined,
|
|
609
|
+
isJson: false,
|
|
610
|
+
selection: [],
|
|
611
|
+
}));
|
|
612
|
+
return {
|
|
613
|
+
tableTsKey: tableConfig.tsName,
|
|
614
|
+
sql: this.buildSelectQuery({
|
|
615
|
+
table,
|
|
616
|
+
fields: {},
|
|
617
|
+
fieldsFlat: selectionEntries.map(([, c]) => ({
|
|
618
|
+
path: [c.name],
|
|
619
|
+
field: c,
|
|
620
|
+
})),
|
|
621
|
+
groupBy: [],
|
|
622
|
+
orderBy: [],
|
|
623
|
+
joins: [],
|
|
624
|
+
withList: [],
|
|
625
|
+
}),
|
|
626
|
+
selection,
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
const aliasedColumns = Object.fromEntries(Object.entries(tableConfig.columns).map(([key, value]) => [key, aliasedTableColumn(value, tableAlias)]));
|
|
630
|
+
const aliasedRelations = Object.fromEntries(Object.entries(tableConfig.relations).map(([key, value]) => [key, aliasedRelation(value, tableAlias)]));
|
|
631
|
+
const aliasedFields = Object.assign({}, aliasedColumns, aliasedRelations);
|
|
632
|
+
const fieldsSelection = {};
|
|
633
|
+
let selectedColumns = [];
|
|
634
|
+
let selectedExtras = [];
|
|
635
|
+
let selectedRelations = [];
|
|
636
|
+
if (config.columns) {
|
|
637
|
+
let isIncludeMode = false;
|
|
638
|
+
for (const [field, value] of Object.entries(config.columns)) {
|
|
639
|
+
if (value === undefined) {
|
|
640
|
+
continue;
|
|
641
|
+
}
|
|
642
|
+
if (field in tableConfig.columns) {
|
|
643
|
+
if (!isIncludeMode && value === true) {
|
|
644
|
+
isIncludeMode = true;
|
|
645
|
+
}
|
|
646
|
+
selectedColumns.push(field);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
if (selectedColumns.length > 0) {
|
|
650
|
+
selectedColumns = isIncludeMode
|
|
651
|
+
? selectedColumns.filter((c) => config.columns?.[c] === true)
|
|
652
|
+
: Object.keys(tableConfig.columns).filter((key) => !selectedColumns.includes(key));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
if (config.with) {
|
|
656
|
+
selectedRelations = Object.entries(config.with)
|
|
657
|
+
.filter((entry) => !!entry[1])
|
|
658
|
+
.map(([key, value]) => ({ key, value }));
|
|
659
|
+
}
|
|
660
|
+
if (!config.columns) {
|
|
661
|
+
selectedColumns = Object.keys(tableConfig.columns);
|
|
662
|
+
}
|
|
663
|
+
if (config.extras) {
|
|
664
|
+
const extrasOrig = typeof config.extras === 'function'
|
|
665
|
+
? config.extras(aliasedFields, { sql })
|
|
666
|
+
: config.extras;
|
|
667
|
+
selectedExtras = Object.entries(extrasOrig).map(([key, value]) => ({
|
|
668
|
+
key,
|
|
669
|
+
value: mapColumnsInAliasedSQLToAlias(value, tableAlias),
|
|
670
|
+
}));
|
|
671
|
+
}
|
|
672
|
+
for (const field of selectedColumns) {
|
|
673
|
+
const column = tableConfig.columns[field];
|
|
674
|
+
fieldsSelection[field] = column;
|
|
675
|
+
}
|
|
676
|
+
for (const { key, value } of selectedExtras) {
|
|
677
|
+
fieldsSelection[key] = value;
|
|
678
|
+
}
|
|
679
|
+
const builtRelations = [];
|
|
680
|
+
const joins = [];
|
|
681
|
+
const builtRelationFields = [];
|
|
682
|
+
for (const { key: selectedRelationKey, value: selectedRelationValue } of selectedRelations) {
|
|
683
|
+
let relation;
|
|
684
|
+
for (const [relationKey, relationValue] of Object.entries(tableConfig.relations)) {
|
|
685
|
+
if (relationValue instanceof Relation && relationKey === selectedRelationKey) {
|
|
686
|
+
relation = relationValue;
|
|
687
|
+
break;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
if (!relation) {
|
|
691
|
+
throw new Error(`Relation ${selectedRelationKey} not found`);
|
|
692
|
+
}
|
|
693
|
+
const normalizedRelation = normalizeRelation(schema, tableNamesMap, relation);
|
|
694
|
+
const relationAlias = `${tableAlias}_${selectedRelationKey}`;
|
|
695
|
+
const builtRelation = this.buildRelationalQuery(fullSchema, schema, tableNamesMap, fullSchema[tableNamesMap[relation.referencedTable[Table.Symbol.Name]]], schema[tableNamesMap[relation.referencedTable[Table.Symbol.Name]]], selectedRelationValue, relationAlias, normalizedRelation.references);
|
|
696
|
+
builtRelations.push({ key: selectedRelationKey, value: builtRelation });
|
|
697
|
+
joins.push({
|
|
698
|
+
table: new Subquery(builtRelation.sql, {}, relationAlias),
|
|
699
|
+
alias: selectedRelationKey,
|
|
700
|
+
on: and(...normalizedRelation.fields.map((field, i) => eq(aliasedTableColumn(field, tableAlias), aliasedTableColumn(normalizedRelation.references[i], relationAlias)))),
|
|
701
|
+
joinType: 'left',
|
|
702
|
+
});
|
|
703
|
+
const elseField = sql `json_arrayagg(json_array(${sql.join(builtRelation.selection.map(({ dbKey: key, isJson }) => {
|
|
704
|
+
const field = sql `${sql.identifier(relationAlias)}.${sql.identifier(key)}`;
|
|
705
|
+
return isJson ? sql `cast(${field} as json)` : field;
|
|
706
|
+
}), sql `, `)}))`;
|
|
707
|
+
const field = sql `if(count(${sql.join(normalizedRelation.references.map((c) => aliasedTableColumn(c, relationAlias)), sql.raw(' or '))}) = 0, '[]', ${elseField})`.as(selectedRelationKey);
|
|
708
|
+
builtRelationFields.push({
|
|
709
|
+
path: [selectedRelationKey],
|
|
710
|
+
field,
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
const finalFieldsSelection = Object.entries(fieldsSelection).map(([key, value]) => {
|
|
714
|
+
return {
|
|
715
|
+
path: [key],
|
|
716
|
+
field: value instanceof Column ? aliasedTableColumn(value, tableAlias) : value,
|
|
717
|
+
};
|
|
718
|
+
});
|
|
719
|
+
const initialWhere = and(...selectedRelations.filter(({ key }) => {
|
|
720
|
+
const relation = config.with?.[key];
|
|
721
|
+
return typeof relation === 'object' && relation.limit !== undefined;
|
|
722
|
+
}).map(({ key }) => {
|
|
723
|
+
const field = sql `${sql.identifier(`${tableAlias}_${key}`)}.${sql.identifier('__drizzle_row_number')}`;
|
|
724
|
+
const value = config.with[key];
|
|
725
|
+
const cond = or(and(sql `${field} <= ${value.limit}`), sql `(${field} is null)`);
|
|
726
|
+
return cond;
|
|
727
|
+
}));
|
|
728
|
+
const groupBy = (builtRelationFields.length
|
|
729
|
+
? (tableConfig.primaryKey.length ? tableConfig.primaryKey : Object.values(tableConfig.columns)).map((c) => aliasedTableColumn(c, tableAlias))
|
|
730
|
+
: []);
|
|
731
|
+
const finalFieldsFlat = isRoot
|
|
732
|
+
? [
|
|
733
|
+
...finalFieldsSelection.map(({ path, field }) => ({
|
|
734
|
+
path,
|
|
735
|
+
field: field instanceof SQL.Aliased ? sql `${sql.identifier(field.fieldAlias)}` : field,
|
|
736
|
+
})),
|
|
737
|
+
...builtRelationFields.map(({ path, field }) => ({
|
|
738
|
+
path,
|
|
739
|
+
field: sql `cast(${sql.identifier(field.fieldAlias)} as json)`,
|
|
740
|
+
})),
|
|
741
|
+
]
|
|
742
|
+
: [
|
|
743
|
+
...Object.entries(tableConfig.columns).map(([tsKey, column]) => ({
|
|
744
|
+
path: [tsKey],
|
|
745
|
+
field: aliasedTableColumn(column, tableAlias),
|
|
746
|
+
})),
|
|
747
|
+
...selectedExtras.map(({ key, value }) => ({
|
|
748
|
+
path: [key],
|
|
749
|
+
field: value,
|
|
750
|
+
})),
|
|
751
|
+
...builtRelationFields.map(({ path, field }) => ({
|
|
752
|
+
path,
|
|
753
|
+
field: sql `${sql.identifier(tableAlias)}.${sql.identifier(field.fieldAlias)}`,
|
|
754
|
+
})),
|
|
755
|
+
];
|
|
756
|
+
if (finalFieldsFlat.length === 0) {
|
|
757
|
+
finalFieldsFlat.push({
|
|
758
|
+
path: [],
|
|
759
|
+
field: sql.raw('1'),
|
|
760
|
+
});
|
|
761
|
+
}
|
|
762
|
+
const initialFieldsFlat = [
|
|
763
|
+
{
|
|
764
|
+
path: [],
|
|
765
|
+
field: sql `${sql.identifier(tableAlias)}.*`,
|
|
766
|
+
},
|
|
767
|
+
...selectedExtras.map(({ key, value }) => ({
|
|
768
|
+
path: [key],
|
|
769
|
+
field: value,
|
|
770
|
+
})),
|
|
771
|
+
...builtRelationFields,
|
|
772
|
+
];
|
|
773
|
+
let orderByOrig = typeof config.orderBy === 'function'
|
|
774
|
+
? config.orderBy(aliasedFields, orderByOperators)
|
|
775
|
+
: config.orderBy ?? [];
|
|
776
|
+
if (!Array.isArray(orderByOrig)) {
|
|
777
|
+
orderByOrig = [orderByOrig];
|
|
778
|
+
}
|
|
779
|
+
const orderBy = orderByOrig.map((orderByValue) => {
|
|
780
|
+
if (orderByValue instanceof Column) {
|
|
781
|
+
return aliasedTableColumn(orderByValue, tableAlias);
|
|
782
|
+
}
|
|
783
|
+
return mapColumnsInSQLToAlias(orderByValue, tableAlias);
|
|
784
|
+
});
|
|
785
|
+
if (!isRoot && !config.limit && orderBy.length > 0) {
|
|
786
|
+
finalFieldsFlat.push({
|
|
787
|
+
path: ['__drizzle_row_number'],
|
|
788
|
+
field: sql `row_number() over(order by ${sql.join(orderBy, sql `, `)})`,
|
|
789
|
+
});
|
|
790
|
+
}
|
|
791
|
+
let limit, offset;
|
|
792
|
+
if (config.limit !== undefined || config.offset !== undefined) {
|
|
793
|
+
if (isRoot) {
|
|
794
|
+
limit = config.limit;
|
|
795
|
+
offset = config.offset;
|
|
796
|
+
}
|
|
797
|
+
else {
|
|
798
|
+
finalFieldsFlat.push({
|
|
799
|
+
path: ['__drizzle_row_number'],
|
|
800
|
+
field: sql `row_number() over(partition by ${relationColumns.map((c) => aliasedTableColumn(c, tableAlias))}${(orderBy.length > 0 && !isRoot) ? sql ` order by ${sql.join(orderBy, sql `, `)}` : undefined})`
|
|
801
|
+
.as('__drizzle_row_number'),
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
let result = this.buildSelectQuery({
|
|
806
|
+
table: aliasedTable(table, tableAlias),
|
|
807
|
+
fields: {},
|
|
808
|
+
fieldsFlat: initialFieldsFlat,
|
|
809
|
+
where: initialWhere,
|
|
810
|
+
groupBy,
|
|
811
|
+
orderBy: [],
|
|
812
|
+
joins,
|
|
813
|
+
withList: [],
|
|
814
|
+
});
|
|
815
|
+
let where;
|
|
816
|
+
if (config.where) {
|
|
817
|
+
const whereSql = typeof config.where === 'function' ? config.where(aliasedFields, operators) : config.where;
|
|
818
|
+
where = whereSql && mapColumnsInSQLToAlias(whereSql, tableAlias);
|
|
819
|
+
}
|
|
820
|
+
result = this.buildSelectQuery({
|
|
821
|
+
table: new Subquery(result, {}, tableAlias),
|
|
822
|
+
fields: {},
|
|
823
|
+
fieldsFlat: finalFieldsFlat,
|
|
824
|
+
where,
|
|
825
|
+
groupBy: [],
|
|
826
|
+
orderBy: isRoot ? orderBy : [],
|
|
827
|
+
joins: [],
|
|
828
|
+
withList: [],
|
|
829
|
+
limit,
|
|
830
|
+
offset: offset,
|
|
831
|
+
});
|
|
832
|
+
return {
|
|
833
|
+
tableTsKey: tableConfig.tsName,
|
|
834
|
+
sql: result,
|
|
835
|
+
selection: [
|
|
836
|
+
...finalFieldsSelection.map(({ path, field }) => ({
|
|
837
|
+
dbKey: field instanceof SQL.Aliased ? field.fieldAlias : tableConfig.columns[path[0]].name,
|
|
838
|
+
tsKey: path[0],
|
|
839
|
+
field,
|
|
840
|
+
tableTsKey: undefined,
|
|
841
|
+
isJson: false,
|
|
842
|
+
selection: [],
|
|
843
|
+
})),
|
|
844
|
+
...builtRelations.map(({ key, value }) => ({
|
|
845
|
+
dbKey: key,
|
|
846
|
+
tsKey: key,
|
|
847
|
+
field: undefined,
|
|
848
|
+
tableTsKey: value.tableTsKey,
|
|
849
|
+
isJson: true,
|
|
850
|
+
selection: value.selection,
|
|
851
|
+
})),
|
|
852
|
+
],
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
class MySqlSelectBuilder {
|
|
858
|
+
constructor(fields, session, dialect, withList = []) {
|
|
859
|
+
this.fields = fields;
|
|
860
|
+
this.session = session;
|
|
861
|
+
this.dialect = dialect;
|
|
862
|
+
this.withList = withList;
|
|
863
|
+
}
|
|
864
|
+
from(source) {
|
|
865
|
+
const isPartialSelect = !!this.fields;
|
|
866
|
+
let fields;
|
|
867
|
+
if (this.fields) {
|
|
868
|
+
fields = this.fields;
|
|
869
|
+
}
|
|
870
|
+
else if (source instanceof Subquery) {
|
|
871
|
+
// This is required to use the proxy handler to get the correct field values from the subquery
|
|
872
|
+
fields = Object.fromEntries(Object.keys(source[SubqueryConfig].selection).map((key) => [key, source[key]]));
|
|
873
|
+
}
|
|
874
|
+
else if (source instanceof MySqlViewBase) {
|
|
875
|
+
fields = source[ViewBaseConfig].selectedFields;
|
|
876
|
+
}
|
|
877
|
+
else if (source instanceof SQL) {
|
|
878
|
+
fields = {};
|
|
879
|
+
}
|
|
880
|
+
else {
|
|
881
|
+
fields = getTableColumns(source);
|
|
882
|
+
}
|
|
883
|
+
return new MySqlSelect(source, fields, isPartialSelect, this.session, this.dialect, this.withList);
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
class MySqlSelectQueryBuilder extends TypedQueryBuilder {
|
|
887
|
+
constructor(table, fields, isPartialSelect,
|
|
888
|
+
/** @internal */
|
|
889
|
+
session, dialect, withList) {
|
|
890
|
+
super();
|
|
891
|
+
this.isPartialSelect = isPartialSelect;
|
|
892
|
+
this.session = session;
|
|
893
|
+
this.dialect = dialect;
|
|
894
|
+
this.leftJoin = this.createJoin('left');
|
|
895
|
+
this.rightJoin = this.createJoin('right');
|
|
896
|
+
this.innerJoin = this.createJoin('inner');
|
|
897
|
+
this.fullJoin = this.createJoin('full');
|
|
898
|
+
this.config = {
|
|
899
|
+
withList,
|
|
900
|
+
table,
|
|
901
|
+
fields: { ...fields },
|
|
902
|
+
joins: [],
|
|
903
|
+
orderBy: [],
|
|
904
|
+
groupBy: [],
|
|
905
|
+
};
|
|
906
|
+
this._ = {
|
|
907
|
+
selectedFields: fields,
|
|
908
|
+
};
|
|
909
|
+
this.tableName = getTableLikeName(table);
|
|
910
|
+
this.joinsNotNullableMap = typeof this.tableName === 'string' ? { [this.tableName]: true } : {};
|
|
911
|
+
}
|
|
912
|
+
createJoin(joinType) {
|
|
913
|
+
return (table, on) => {
|
|
914
|
+
const baseTableName = this.tableName;
|
|
915
|
+
const tableName = getTableLikeName(table);
|
|
916
|
+
if (typeof tableName === 'string' && this.config.joins.some((join) => join.alias === tableName)) {
|
|
917
|
+
throw new Error(`Alias "${tableName}" is already used in this query`);
|
|
918
|
+
}
|
|
919
|
+
if (!this.isPartialSelect) {
|
|
920
|
+
// If this is the first join and this is not a partial select and we're not selecting from raw SQL, "move" the fields from the main table to the nested object
|
|
921
|
+
if (Object.keys(this.joinsNotNullableMap).length === 1 && typeof baseTableName === 'string') {
|
|
922
|
+
this.config.fields = {
|
|
923
|
+
[baseTableName]: this.config.fields,
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
if (typeof tableName === 'string' && !(table instanceof SQL)) {
|
|
927
|
+
const selection = table instanceof Subquery
|
|
928
|
+
? table[SubqueryConfig].selection
|
|
929
|
+
: table instanceof View
|
|
930
|
+
? table[ViewBaseConfig].selectedFields
|
|
931
|
+
: table[Table.Symbol.Columns];
|
|
932
|
+
this.config.fields[tableName] = selection;
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
if (typeof on === 'function') {
|
|
936
|
+
on = on(new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: 'sql', sqlBehavior: 'sql' })));
|
|
937
|
+
}
|
|
938
|
+
this.config.joins.push({ on, table, joinType, alias: tableName });
|
|
939
|
+
if (typeof tableName === 'string') {
|
|
940
|
+
switch (joinType) {
|
|
941
|
+
case 'left': {
|
|
942
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
case 'right': {
|
|
946
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
947
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
948
|
+
break;
|
|
949
|
+
}
|
|
950
|
+
case 'inner': {
|
|
951
|
+
this.joinsNotNullableMap[tableName] = true;
|
|
952
|
+
break;
|
|
953
|
+
}
|
|
954
|
+
case 'full': {
|
|
955
|
+
this.joinsNotNullableMap = Object.fromEntries(Object.entries(this.joinsNotNullableMap).map(([key]) => [key, false]));
|
|
956
|
+
this.joinsNotNullableMap[tableName] = false;
|
|
957
|
+
break;
|
|
958
|
+
}
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return this;
|
|
962
|
+
};
|
|
963
|
+
}
|
|
964
|
+
where(where) {
|
|
965
|
+
if (typeof where === 'function') {
|
|
966
|
+
where = where(new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: 'sql', sqlBehavior: 'sql' })));
|
|
967
|
+
}
|
|
968
|
+
this.config.where = where;
|
|
969
|
+
return this;
|
|
970
|
+
}
|
|
971
|
+
having(having) {
|
|
972
|
+
if (typeof having === 'function') {
|
|
973
|
+
having = having(new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: 'sql', sqlBehavior: 'sql' })));
|
|
974
|
+
}
|
|
975
|
+
this.config.having = having;
|
|
976
|
+
return this;
|
|
977
|
+
}
|
|
978
|
+
groupBy(...columns) {
|
|
979
|
+
if (typeof columns[0] === 'function') {
|
|
980
|
+
const groupBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: 'alias', sqlBehavior: 'sql' })));
|
|
981
|
+
this.config.groupBy = Array.isArray(groupBy) ? groupBy : [groupBy];
|
|
982
|
+
}
|
|
983
|
+
else {
|
|
984
|
+
this.config.groupBy = columns;
|
|
985
|
+
}
|
|
986
|
+
return this;
|
|
987
|
+
}
|
|
988
|
+
orderBy(...columns) {
|
|
989
|
+
if (typeof columns[0] === 'function') {
|
|
990
|
+
const orderBy = columns[0](new Proxy(this.config.fields, new SelectionProxyHandler({ sqlAliasedBehavior: 'alias', sqlBehavior: 'sql' })));
|
|
991
|
+
this.config.orderBy = Array.isArray(orderBy) ? orderBy : [orderBy];
|
|
992
|
+
}
|
|
993
|
+
else {
|
|
994
|
+
this.config.orderBy = columns;
|
|
995
|
+
}
|
|
996
|
+
return this;
|
|
997
|
+
}
|
|
998
|
+
limit(limit) {
|
|
999
|
+
this.config.limit = limit;
|
|
1000
|
+
return this;
|
|
1001
|
+
}
|
|
1002
|
+
offset(offset) {
|
|
1003
|
+
this.config.offset = offset;
|
|
1004
|
+
return this;
|
|
1005
|
+
}
|
|
1006
|
+
for(strength, config = {}) {
|
|
1007
|
+
this.config.lockingClause = { strength, config };
|
|
1008
|
+
return this;
|
|
1009
|
+
}
|
|
1010
|
+
/** @internal */
|
|
1011
|
+
getSQL() {
|
|
1012
|
+
return this.dialect.buildSelectQuery(this.config);
|
|
1013
|
+
}
|
|
1014
|
+
toSQL() {
|
|
1015
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
1016
|
+
return rest;
|
|
1017
|
+
}
|
|
1018
|
+
as(alias) {
|
|
1019
|
+
return new Proxy(new Subquery(this.getSQL(), this.config.fields, alias), new SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }));
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
class MySqlSelect extends MySqlSelectQueryBuilder {
|
|
1023
|
+
constructor() {
|
|
1024
|
+
super(...arguments);
|
|
1025
|
+
this.execute = ((placeholderValues) => {
|
|
1026
|
+
return this.prepare().execute(placeholderValues);
|
|
1027
|
+
});
|
|
1028
|
+
this.createIterator = () => {
|
|
1029
|
+
const self = this;
|
|
1030
|
+
return async function* (placeholderValues) {
|
|
1031
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
1032
|
+
};
|
|
1033
|
+
};
|
|
1034
|
+
this.iterator = this.createIterator();
|
|
1035
|
+
}
|
|
1036
|
+
prepare() {
|
|
1037
|
+
if (!this.session) {
|
|
1038
|
+
throw new Error('Cannot execute a query on a query builder. Please use a database instance instead.');
|
|
1039
|
+
}
|
|
1040
|
+
const fieldsList = orderSelectedFields(this.config.fields);
|
|
1041
|
+
const query = this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), fieldsList);
|
|
1042
|
+
query.joinsNotNullableMap = this.joinsNotNullableMap;
|
|
1043
|
+
return query;
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
applyMixins(MySqlSelect, [QueryPromise]);
|
|
1047
|
+
|
|
1048
|
+
class QueryBuilder {
|
|
1049
|
+
$with(alias) {
|
|
1050
|
+
const queryBuilder = this;
|
|
1051
|
+
return {
|
|
1052
|
+
as(qb) {
|
|
1053
|
+
if (typeof qb === 'function') {
|
|
1054
|
+
qb = qb(queryBuilder);
|
|
1055
|
+
}
|
|
1056
|
+
return new Proxy(new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true), new SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }));
|
|
1057
|
+
},
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
with(...queries) {
|
|
1061
|
+
const self = this;
|
|
1062
|
+
function select(fields) {
|
|
1063
|
+
return new MySqlSelectBuilder(fields ?? undefined, undefined, self.getDialect(), queries);
|
|
1064
|
+
}
|
|
1065
|
+
return { select };
|
|
1066
|
+
}
|
|
1067
|
+
select(fields) {
|
|
1068
|
+
return new MySqlSelectBuilder(fields ?? undefined, undefined, this.getDialect());
|
|
1069
|
+
}
|
|
1070
|
+
// Lazy load dialect to avoid circular dependency
|
|
1071
|
+
getDialect() {
|
|
1072
|
+
if (!this.dialect) {
|
|
1073
|
+
this.dialect = new MySqlDialect();
|
|
1074
|
+
}
|
|
1075
|
+
return this.dialect;
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
class MySqlUpdateBuilder {
|
|
1080
|
+
constructor(table, session, dialect) {
|
|
1081
|
+
this.table = table;
|
|
1082
|
+
this.session = session;
|
|
1083
|
+
this.dialect = dialect;
|
|
1084
|
+
}
|
|
1085
|
+
set(values) {
|
|
1086
|
+
return new MySqlUpdate(this.table, mapUpdateSet(this.table, values), this.session, this.dialect);
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
class MySqlUpdate extends QueryPromise {
|
|
1090
|
+
constructor(table, set, session, dialect) {
|
|
1091
|
+
super();
|
|
1092
|
+
this.session = session;
|
|
1093
|
+
this.dialect = dialect;
|
|
1094
|
+
this.execute = (placeholderValues) => {
|
|
1095
|
+
return this.prepare().execute(placeholderValues);
|
|
1096
|
+
};
|
|
1097
|
+
this.createIterator = () => {
|
|
1098
|
+
const self = this;
|
|
1099
|
+
return async function* (placeholderValues) {
|
|
1100
|
+
yield* self.prepare().iterator(placeholderValues);
|
|
1101
|
+
};
|
|
1102
|
+
};
|
|
1103
|
+
this.iterator = this.createIterator();
|
|
1104
|
+
this.config = { set, table };
|
|
1105
|
+
}
|
|
1106
|
+
where(where) {
|
|
1107
|
+
this.config.where = where;
|
|
1108
|
+
return this;
|
|
1109
|
+
}
|
|
1110
|
+
/** @internal */
|
|
1111
|
+
getSQL() {
|
|
1112
|
+
return this.dialect.buildUpdateQuery(this.config);
|
|
1113
|
+
}
|
|
1114
|
+
toSQL() {
|
|
1115
|
+
const { typings: _typings, ...rest } = this.dialect.sqlToQuery(this.getSQL());
|
|
1116
|
+
return rest;
|
|
1117
|
+
}
|
|
1118
|
+
prepare() {
|
|
1119
|
+
return this.session.prepareQuery(this.dialect.sqlToQuery(this.getSQL()), this.config.returning);
|
|
1120
|
+
}
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
class RelationalQueryBuilder {
|
|
1124
|
+
constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session) {
|
|
1125
|
+
this.fullSchema = fullSchema;
|
|
1126
|
+
this.schema = schema;
|
|
1127
|
+
this.tableNamesMap = tableNamesMap;
|
|
1128
|
+
this.table = table;
|
|
1129
|
+
this.tableConfig = tableConfig;
|
|
1130
|
+
this.dialect = dialect;
|
|
1131
|
+
this.session = session;
|
|
1132
|
+
}
|
|
1133
|
+
findMany(config) {
|
|
1134
|
+
return new MySqlRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.dialect, this.session, config ? config : true, 'many');
|
|
1135
|
+
}
|
|
1136
|
+
findFirst(config) {
|
|
1137
|
+
return new MySqlRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.dialect, this.session, config ? { ...config, limit: 1 } : { limit: 1 }, 'first');
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
class MySqlRelationalQuery extends QueryPromise {
|
|
1141
|
+
constructor(fullSchema, schema, tableNamesMap, table, tableConfig, dialect, session, config, mode) {
|
|
1142
|
+
super();
|
|
1143
|
+
this.fullSchema = fullSchema;
|
|
1144
|
+
this.schema = schema;
|
|
1145
|
+
this.tableNamesMap = tableNamesMap;
|
|
1146
|
+
this.table = table;
|
|
1147
|
+
this.tableConfig = tableConfig;
|
|
1148
|
+
this.dialect = dialect;
|
|
1149
|
+
this.session = session;
|
|
1150
|
+
this.config = config;
|
|
1151
|
+
this.mode = mode;
|
|
1152
|
+
}
|
|
1153
|
+
prepare() {
|
|
1154
|
+
const query = this.dialect.buildRelationalQuery(this.fullSchema, this.schema, this.tableNamesMap, this.table, this.tableConfig, this.config, this.tableConfig.tsName, [], true);
|
|
1155
|
+
const builtQuery = this.dialect.sqlToQuery(query.sql);
|
|
1156
|
+
return this.session.prepareQuery(builtQuery, undefined, (rawRows) => {
|
|
1157
|
+
const rows = rawRows.map((row) => mapRelationalRow(this.schema, this.tableConfig, row, query.selection));
|
|
1158
|
+
if (this.mode === 'first') {
|
|
1159
|
+
return rows[0];
|
|
1160
|
+
}
|
|
1161
|
+
return rows;
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
execute() {
|
|
1165
|
+
return this.prepare().execute();
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
class MySqlDatabase {
|
|
1170
|
+
constructor(
|
|
1171
|
+
/** @internal */
|
|
1172
|
+
dialect,
|
|
1173
|
+
/** @internal */
|
|
1174
|
+
session, schema) {
|
|
1175
|
+
this.dialect = dialect;
|
|
1176
|
+
this.session = session;
|
|
1177
|
+
this._ = schema
|
|
1178
|
+
? { schema: schema.schema, tableNamesMap: schema.tableNamesMap }
|
|
1179
|
+
: { schema: undefined, tableNamesMap: {} };
|
|
1180
|
+
this.query = {};
|
|
1181
|
+
if (this._.schema) {
|
|
1182
|
+
for (const [tableName, columns] of Object.entries(this._.schema)) {
|
|
1183
|
+
this.query[tableName] = new RelationalQueryBuilder(schema.fullSchema, this._.schema, this._.tableNamesMap, schema.fullSchema[tableName], columns, dialect, session);
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
$with(alias) {
|
|
1188
|
+
return {
|
|
1189
|
+
as(qb) {
|
|
1190
|
+
if (typeof qb === 'function') {
|
|
1191
|
+
qb = qb(new QueryBuilder());
|
|
1192
|
+
}
|
|
1193
|
+
return new Proxy(new WithSubquery(qb.getSQL(), qb.getSelectedFields(), alias, true), new SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }));
|
|
1194
|
+
},
|
|
1195
|
+
};
|
|
1196
|
+
}
|
|
1197
|
+
with(...queries) {
|
|
1198
|
+
const self = this;
|
|
1199
|
+
function select(fields) {
|
|
1200
|
+
return new MySqlSelectBuilder(fields ?? undefined, self.session, self.dialect, queries);
|
|
1201
|
+
}
|
|
1202
|
+
return { select };
|
|
1203
|
+
}
|
|
1204
|
+
select(fields) {
|
|
1205
|
+
return new MySqlSelectBuilder(fields ?? undefined, this.session, this.dialect);
|
|
1206
|
+
}
|
|
1207
|
+
update(table) {
|
|
1208
|
+
return new MySqlUpdateBuilder(table, this.session, this.dialect);
|
|
1209
|
+
}
|
|
1210
|
+
insert(table) {
|
|
1211
|
+
return new MySqlInsertBuilder(table, this.session, this.dialect);
|
|
1212
|
+
}
|
|
1213
|
+
delete(table) {
|
|
1214
|
+
return new MySqlDelete(table, this.session, this.dialect);
|
|
1215
|
+
}
|
|
1216
|
+
execute(query) {
|
|
1217
|
+
return this.session.execute(query.getSQL());
|
|
1218
|
+
}
|
|
1219
|
+
transaction(transaction, config) {
|
|
1220
|
+
return this.session.transaction(transaction, config);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
class PreparedQuery {
|
|
1225
|
+
}
|
|
1226
|
+
class MySqlSession {
|
|
1227
|
+
constructor(dialect) {
|
|
1228
|
+
this.dialect = dialect;
|
|
1229
|
+
}
|
|
1230
|
+
execute(query) {
|
|
1231
|
+
return this.prepareQuery(this.dialect.sqlToQuery(query), undefined).execute();
|
|
1232
|
+
}
|
|
1233
|
+
getSetTransactionSQL(config) {
|
|
1234
|
+
const parts = [];
|
|
1235
|
+
if (config.isolationLevel) {
|
|
1236
|
+
parts.push(`isolation level ${config.isolationLevel}`);
|
|
1237
|
+
}
|
|
1238
|
+
return parts.length ? sql.fromList(['set transaction ', parts.join(' ')]) : undefined;
|
|
1239
|
+
}
|
|
1240
|
+
getStartTransactionSQL(config) {
|
|
1241
|
+
const parts = [];
|
|
1242
|
+
if (config.withConsistentSnapshot) {
|
|
1243
|
+
parts.push('with consistent snapshot');
|
|
1244
|
+
}
|
|
1245
|
+
if (config.accessMode) {
|
|
1246
|
+
parts.push(config.accessMode);
|
|
1247
|
+
}
|
|
1248
|
+
return parts.length ? sql.fromList(['start transaction ', parts.join(' ')]) : undefined;
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
class MySqlTransaction extends MySqlDatabase {
|
|
1252
|
+
constructor(dialect, session, schema, nestedIndex = 0) {
|
|
1253
|
+
super(dialect, session, schema);
|
|
1254
|
+
this.schema = schema;
|
|
1255
|
+
this.nestedIndex = nestedIndex;
|
|
1256
|
+
}
|
|
1257
|
+
rollback() {
|
|
1258
|
+
throw new TransactionRollbackError();
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
export { MySqlView as A, mysqlView as B, ForeignKeyBuilder as F, InlineForeignKeys as I, MySqlColumnBuilderWithAutoIncrement as M, PreparedQuery as P, QueryBuilder as Q, ViewBuilderCore as V, MySqlColumnWithAutoIncrement as a, MySqlColumnBuilder as b, MySqlColumn as c, MySqlTable as d, mysqlViewWithSchema as e, MySqlViewConfig as f, MySqlDatabase as g, MySqlDialect as h, ForeignKey as i, foreignKey as j, MySqlDelete as k, MySqlInsertBuilder as l, mysqlTableWithSchema as m, MySqlInsert as n, MySqlSelectBuilder as o, MySqlSelectQueryBuilder as p, MySqlSelect as q, MySqlUpdateBuilder as r, MySqlUpdate as s, MySqlSession as t, MySqlTransaction as u, mysqlTable as v, mysqlTableCreator as w, ViewBuilder as x, ManualViewBuilder as y, MySqlViewBase as z };
|
|
1263
|
+
//# sourceMappingURL=session-483ed08d.mjs.map
|