drizzle-orm 0.29.4 → 0.29.5-859260e
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.
Potentially problematic release.
This version of drizzle-orm might be problematic. Click here for more details.
- package/aws-data-api/pg/migrator.cjs +1 -1
- package/aws-data-api/pg/migrator.cjs.map +1 -1
- package/aws-data-api/pg/migrator.js +1 -1
- package/aws-data-api/pg/migrator.js.map +1 -1
- package/better-sqlite3/migrator.cjs +1 -1
- package/better-sqlite3/migrator.cjs.map +1 -1
- package/better-sqlite3/migrator.js +1 -1
- package/better-sqlite3/migrator.js.map +1 -1
- package/bun-sqlite/migrator.cjs +1 -1
- package/bun-sqlite/migrator.cjs.map +1 -1
- package/bun-sqlite/migrator.js +1 -1
- package/bun-sqlite/migrator.js.map +1 -1
- package/d1/migrator.cjs +1 -1
- package/d1/migrator.cjs.map +1 -1
- package/d1/migrator.js +1 -1
- package/d1/migrator.js.map +1 -1
- package/expo-sqlite/migrator.cjs.map +1 -1
- package/expo-sqlite/migrator.js.map +1 -1
- package/libsql/migrator.cjs +1 -1
- package/libsql/migrator.cjs.map +1 -1
- package/libsql/migrator.js +1 -1
- package/libsql/migrator.js.map +1 -1
- package/migrator.cjs.map +1 -1
- package/migrator.d.cts +1 -0
- package/migrator.d.ts +1 -0
- package/migrator.js.map +1 -1
- package/mysql-core/db.cjs +7 -1
- package/mysql-core/db.cjs.map +1 -1
- package/mysql-core/db.d.cts +2 -0
- package/mysql-core/db.d.ts +2 -0
- package/mysql-core/db.js +7 -1
- package/mysql-core/db.js.map +1 -1
- package/mysql-core/dialect.cjs +20 -16
- package/mysql-core/dialect.cjs.map +1 -1
- package/mysql-core/dialect.d.cts +4 -3
- package/mysql-core/dialect.d.ts +4 -3
- package/mysql-core/dialect.js +20 -16
- package/mysql-core/dialect.js.map +1 -1
- package/mysql-core/query-builders/delete.cjs +2 -2
- package/mysql-core/query-builders/delete.cjs.map +1 -1
- package/mysql-core/query-builders/delete.d.cts +3 -1
- package/mysql-core/query-builders/delete.d.ts +3 -1
- package/mysql-core/query-builders/delete.js +2 -2
- package/mysql-core/query-builders/delete.js.map +1 -1
- package/mysql-core/query-builders/update.cjs +5 -4
- package/mysql-core/query-builders/update.cjs.map +1 -1
- package/mysql-core/query-builders/update.d.cts +5 -2
- package/mysql-core/query-builders/update.d.ts +5 -2
- package/mysql-core/query-builders/update.js +5 -4
- package/mysql-core/query-builders/update.js.map +1 -1
- package/neon-http/migrator.cjs +6 -4
- package/neon-http/migrator.cjs.map +1 -1
- package/neon-http/migrator.js +6 -4
- package/neon-http/migrator.js.map +1 -1
- package/neon-serverless/migrator.cjs +1 -1
- package/neon-serverless/migrator.cjs.map +1 -1
- package/neon-serverless/migrator.js +1 -1
- package/neon-serverless/migrator.js.map +1 -1
- package/node-postgres/migrator.cjs +1 -1
- package/node-postgres/migrator.cjs.map +1 -1
- package/node-postgres/migrator.js +1 -1
- package/node-postgres/migrator.js.map +1 -1
- package/package.json +2 -2
- package/pg-core/db.cjs +28 -1
- package/pg-core/db.cjs.map +1 -1
- package/pg-core/db.d.cts +11 -0
- package/pg-core/db.d.ts +11 -0
- package/pg-core/db.js +28 -1
- package/pg-core/db.js.map +1 -1
- package/pg-core/dialect.cjs +113 -106
- package/pg-core/dialect.cjs.map +1 -1
- package/pg-core/dialect.d.cts +6 -5
- package/pg-core/dialect.d.ts +6 -5
- package/pg-core/dialect.js +31 -24
- package/pg-core/dialect.js.map +1 -1
- package/pg-core/query-builders/delete.cjs +2 -2
- package/pg-core/query-builders/delete.cjs.map +1 -1
- package/pg-core/query-builders/delete.d.cts +3 -1
- package/pg-core/query-builders/delete.d.ts +3 -1
- package/pg-core/query-builders/delete.js +2 -2
- package/pg-core/query-builders/delete.js.map +1 -1
- package/pg-core/query-builders/insert.cjs +5 -4
- package/pg-core/query-builders/insert.cjs.map +1 -1
- package/pg-core/query-builders/insert.d.cts +5 -2
- package/pg-core/query-builders/insert.d.ts +5 -2
- package/pg-core/query-builders/insert.js +5 -4
- package/pg-core/query-builders/insert.js.map +1 -1
- package/pg-core/query-builders/update.cjs +6 -4
- package/pg-core/query-builders/update.cjs.map +1 -1
- package/pg-core/query-builders/update.d.cts +5 -2
- package/pg-core/query-builders/update.d.ts +5 -2
- package/pg-core/query-builders/update.js +6 -4
- package/pg-core/query-builders/update.js.map +1 -1
- package/postgres-js/migrator.cjs +1 -1
- package/postgres-js/migrator.cjs.map +1 -1
- package/postgres-js/migrator.js +1 -1
- package/postgres-js/migrator.js.map +1 -1
- package/sql-js/migrator.cjs +1 -1
- package/sql-js/migrator.cjs.map +1 -1
- package/sql-js/migrator.js +1 -1
- package/sql-js/migrator.js.map +1 -1
- package/sqlite-core/db.cjs +3 -3
- package/sqlite-core/db.cjs.map +1 -1
- package/sqlite-core/db.js +3 -3
- package/sqlite-core/db.js.map +1 -1
- package/sqlite-core/dialect.cjs +110 -103
- package/sqlite-core/dialect.cjs.map +1 -1
- package/sqlite-core/dialect.d.cts +7 -6
- package/sqlite-core/dialect.d.ts +7 -6
- package/sqlite-core/dialect.js +34 -27
- package/sqlite-core/dialect.js.map +1 -1
- package/sqlite-core/query-builders/delete.cjs +2 -2
- package/sqlite-core/query-builders/delete.cjs.map +1 -1
- package/sqlite-core/query-builders/delete.d.cts +3 -1
- package/sqlite-core/query-builders/delete.d.ts +3 -1
- package/sqlite-core/query-builders/delete.js +2 -2
- package/sqlite-core/query-builders/delete.js.map +1 -1
- package/sqlite-core/query-builders/insert.cjs +5 -4
- package/sqlite-core/query-builders/insert.cjs.map +1 -1
- package/sqlite-core/query-builders/insert.d.cts +5 -2
- package/sqlite-core/query-builders/insert.d.ts +5 -2
- package/sqlite-core/query-builders/insert.js +5 -4
- package/sqlite-core/query-builders/insert.js.map +1 -1
- package/sqlite-core/query-builders/raw.cjs +1 -1
- package/sqlite-core/query-builders/raw.cjs.map +1 -1
- package/sqlite-core/query-builders/raw.d.cts +6 -1
- package/sqlite-core/query-builders/raw.d.ts +6 -1
- package/sqlite-core/query-builders/raw.js +1 -1
- package/sqlite-core/query-builders/raw.js.map +1 -1
- package/sqlite-core/query-builders/update.cjs +11 -4
- package/sqlite-core/query-builders/update.cjs.map +1 -1
- package/sqlite-core/query-builders/update.d.cts +5 -2
- package/sqlite-core/query-builders/update.d.ts +5 -2
- package/sqlite-core/query-builders/update.js +11 -4
- package/sqlite-core/query-builders/update.js.map +1 -1
- package/sqlite-proxy/driver.cjs +30 -10
- package/sqlite-proxy/driver.cjs.map +1 -1
- package/sqlite-proxy/driver.d.cts +14 -1
- package/sqlite-proxy/driver.d.ts +14 -1
- package/sqlite-proxy/driver.js +30 -14
- package/sqlite-proxy/driver.js.map +1 -1
- package/sqlite-proxy/migrator.cjs +4 -3
- package/sqlite-proxy/migrator.cjs.map +1 -1
- package/sqlite-proxy/migrator.js +4 -3
- package/sqlite-proxy/migrator.js.map +1 -1
- package/sqlite-proxy/session.cjs +79 -21
- package/sqlite-proxy/session.cjs.map +1 -1
- package/sqlite-proxy/session.d.cts +20 -8
- package/sqlite-proxy/session.d.ts +20 -8
- package/sqlite-proxy/session.js +79 -21
- package/sqlite-proxy/session.js.map +1 -1
- package/vercel-postgres/migrator.cjs +1 -1
- package/vercel-postgres/migrator.cjs.map +1 -1
- package/vercel-postgres/migrator.js +1 -1
- package/vercel-postgres/migrator.js.map +1 -1
- package/version.cjs +1 -1
- package/version.d.cts +1 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
package/sqlite-core/dialect.cjs
CHANGED
|
@@ -28,8 +28,8 @@ var import_column = require("../column.cjs");
|
|
|
28
28
|
var import_entity = require("../entity.cjs");
|
|
29
29
|
var import_errors = require("../errors.cjs");
|
|
30
30
|
var import_relations = require("../relations.cjs");
|
|
31
|
-
var import_sql = require("../sql/
|
|
32
|
-
var import_sql2 = require("../sql/
|
|
31
|
+
var import_sql = require("../sql/index.cjs");
|
|
32
|
+
var import_sql2 = require("../sql/sql.cjs");
|
|
33
33
|
var import_columns = require("./columns/index.cjs");
|
|
34
34
|
var import_table = require("./table.cjs");
|
|
35
35
|
var import_subquery = require("../subquery.cjs");
|
|
@@ -48,30 +48,45 @@ class SQLiteDialect {
|
|
|
48
48
|
escapeString(str) {
|
|
49
49
|
return `'${str.replace(/'/g, "''")}'`;
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
buildWithCTE(queries) {
|
|
52
|
+
if (!queries?.length)
|
|
53
|
+
return void 0;
|
|
54
|
+
const withSqlChunks = [import_sql2.sql`with `];
|
|
55
|
+
for (const [i, w] of queries.entries()) {
|
|
56
|
+
withSqlChunks.push(import_sql2.sql`${import_sql2.sql.identifier(w[import_subquery.SubqueryConfig].alias)} as (${w[import_subquery.SubqueryConfig].sql})`);
|
|
57
|
+
if (i < queries.length - 1) {
|
|
58
|
+
withSqlChunks.push(import_sql2.sql`, `);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
withSqlChunks.push(import_sql2.sql` `);
|
|
62
|
+
return import_sql2.sql.join(withSqlChunks);
|
|
63
|
+
}
|
|
64
|
+
buildDeleteQuery({ table, where, returning, withList }) {
|
|
65
|
+
const withSql = this.buildWithCTE(withList);
|
|
66
|
+
const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
67
|
+
const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
|
|
68
|
+
return import_sql2.sql`${withSql}delete from ${table}${whereSql}${returningSql}`;
|
|
55
69
|
}
|
|
56
70
|
buildUpdateSet(table, set) {
|
|
57
71
|
const setEntries = Object.entries(set);
|
|
58
72
|
const setSize = setEntries.length;
|
|
59
|
-
return
|
|
73
|
+
return import_sql2.sql.join(
|
|
60
74
|
setEntries.flatMap(([colName, value], i) => {
|
|
61
75
|
const col = table[import_table2.Table.Symbol.Columns][colName];
|
|
62
|
-
const res =
|
|
76
|
+
const res = import_sql2.sql`${import_sql2.sql.identifier(col.name)} = ${value}`;
|
|
63
77
|
if (i < setSize - 1) {
|
|
64
|
-
return [res,
|
|
78
|
+
return [res, import_sql2.sql.raw(", ")];
|
|
65
79
|
}
|
|
66
80
|
return [res];
|
|
67
81
|
})
|
|
68
82
|
);
|
|
69
83
|
}
|
|
70
|
-
buildUpdateQuery({ table, set, where, returning }) {
|
|
84
|
+
buildUpdateQuery({ table, set, where, returning, withList }) {
|
|
85
|
+
const withSql = this.buildWithCTE(withList);
|
|
71
86
|
const setSql = this.buildUpdateSet(table, set);
|
|
72
|
-
const returningSql = returning ?
|
|
73
|
-
const whereSql = where ?
|
|
74
|
-
return
|
|
87
|
+
const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
88
|
+
const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
|
|
89
|
+
return import_sql2.sql`${withSql}update ${table} set ${setSql}${whereSql}${returningSql}`;
|
|
75
90
|
}
|
|
76
91
|
/**
|
|
77
92
|
* Builds selection SQL with provided fields/expressions
|
|
@@ -88,16 +103,16 @@ class SQLiteDialect {
|
|
|
88
103
|
const columnsLen = fields.length;
|
|
89
104
|
const chunks = fields.flatMap(({ field }, i) => {
|
|
90
105
|
const chunk = [];
|
|
91
|
-
if ((0, import_entity.is)(field,
|
|
92
|
-
chunk.push(
|
|
93
|
-
} else if ((0, import_entity.is)(field,
|
|
94
|
-
const query = (0, import_entity.is)(field,
|
|
106
|
+
if ((0, import_entity.is)(field, import_sql2.SQL.Aliased) && field.isSelectionField) {
|
|
107
|
+
chunk.push(import_sql2.sql.identifier(field.fieldAlias));
|
|
108
|
+
} else if ((0, import_entity.is)(field, import_sql2.SQL.Aliased) || (0, import_entity.is)(field, import_sql2.SQL)) {
|
|
109
|
+
const query = (0, import_entity.is)(field, import_sql2.SQL.Aliased) ? field.sql : field;
|
|
95
110
|
if (isSingleTable) {
|
|
96
111
|
chunk.push(
|
|
97
|
-
new
|
|
112
|
+
new import_sql2.SQL(
|
|
98
113
|
query.queryChunks.map((c) => {
|
|
99
114
|
if ((0, import_entity.is)(c, import_column.Column)) {
|
|
100
|
-
return
|
|
115
|
+
return import_sql2.sql.identifier(c.name);
|
|
101
116
|
}
|
|
102
117
|
return c;
|
|
103
118
|
})
|
|
@@ -106,24 +121,24 @@ class SQLiteDialect {
|
|
|
106
121
|
} else {
|
|
107
122
|
chunk.push(query);
|
|
108
123
|
}
|
|
109
|
-
if ((0, import_entity.is)(field,
|
|
110
|
-
chunk.push(
|
|
124
|
+
if ((0, import_entity.is)(field, import_sql2.SQL.Aliased)) {
|
|
125
|
+
chunk.push(import_sql2.sql` as ${import_sql2.sql.identifier(field.fieldAlias)}`);
|
|
111
126
|
}
|
|
112
127
|
} else if ((0, import_entity.is)(field, import_column.Column)) {
|
|
113
128
|
const tableName = field.table[import_table2.Table.Symbol.Name];
|
|
114
129
|
const columnName = field.name;
|
|
115
130
|
if (isSingleTable) {
|
|
116
|
-
chunk.push(
|
|
131
|
+
chunk.push(import_sql2.sql.identifier(columnName));
|
|
117
132
|
} else {
|
|
118
|
-
chunk.push(
|
|
133
|
+
chunk.push(import_sql2.sql`${import_sql2.sql.identifier(tableName)}.${import_sql2.sql.identifier(columnName)}`);
|
|
119
134
|
}
|
|
120
135
|
}
|
|
121
136
|
if (i < columnsLen - 1) {
|
|
122
|
-
chunk.push(
|
|
137
|
+
chunk.push(import_sql2.sql`, `);
|
|
123
138
|
}
|
|
124
139
|
return chunk;
|
|
125
140
|
});
|
|
126
|
-
return
|
|
141
|
+
return import_sql2.sql.join(chunks);
|
|
127
142
|
}
|
|
128
143
|
buildSelectQuery({
|
|
129
144
|
withList,
|
|
@@ -142,7 +157,7 @@ class SQLiteDialect {
|
|
|
142
157
|
}) {
|
|
143
158
|
const fieldsList = fieldsFlat ?? (0, import_utils.orderSelectedFields)(fields);
|
|
144
159
|
for (const f of fieldsList) {
|
|
145
|
-
if ((0, import_entity.is)(f.field, import_column.Column) && (0, import_table2.getTableName)(f.field.table) !== ((0, import_entity.is)(table, import_subquery.Subquery) ? table[import_subquery.SubqueryConfig].alias : (0, import_entity.is)(table, import_view_base.SQLiteViewBase) ? table[import_view_common.ViewBaseConfig].name : (0, import_entity.is)(table,
|
|
160
|
+
if ((0, import_entity.is)(f.field, import_column.Column) && (0, import_table2.getTableName)(f.field.table) !== ((0, import_entity.is)(table, import_subquery.Subquery) ? table[import_subquery.SubqueryConfig].alias : (0, import_entity.is)(table, import_view_base.SQLiteViewBase) ? table[import_view_common.ViewBaseConfig].name : (0, import_entity.is)(table, import_sql2.SQL) ? void 0 : (0, import_table2.getTableName)(table)) && !((table2) => joins?.some(
|
|
146
161
|
({ alias }) => alias === (table2[import_table2.Table.Symbol.IsAlias] ? (0, import_table2.getTableName)(table2) : table2[import_table2.Table.Symbol.BaseName])
|
|
147
162
|
))(f.field.table)) {
|
|
148
163
|
const tableName = (0, import_table2.getTableName)(f.field.table);
|
|
@@ -152,23 +167,12 @@ class SQLiteDialect {
|
|
|
152
167
|
}
|
|
153
168
|
}
|
|
154
169
|
const isSingleTable = !joins || joins.length === 0;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const withSqlChunks = [import_sql.sql`with `];
|
|
158
|
-
for (const [i, w] of withList.entries()) {
|
|
159
|
-
withSqlChunks.push(import_sql.sql`${import_sql.sql.identifier(w[import_subquery.SubqueryConfig].alias)} as (${w[import_subquery.SubqueryConfig].sql})`);
|
|
160
|
-
if (i < withList.length - 1) {
|
|
161
|
-
withSqlChunks.push(import_sql.sql`, `);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
withSqlChunks.push(import_sql.sql` `);
|
|
165
|
-
withSql = import_sql.sql.join(withSqlChunks);
|
|
166
|
-
}
|
|
167
|
-
const distinctSql = distinct ? import_sql.sql` distinct` : void 0;
|
|
170
|
+
const withSql = this.buildWithCTE(withList);
|
|
171
|
+
const distinctSql = distinct ? import_sql2.sql` distinct` : void 0;
|
|
168
172
|
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
169
173
|
const tableSql = (() => {
|
|
170
174
|
if ((0, import_entity.is)(table, import_table2.Table) && table[import_table2.Table.Symbol.OriginalName] !== table[import_table2.Table.Symbol.Name]) {
|
|
171
|
-
return
|
|
175
|
+
return import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.OriginalName])} ${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Name])}`;
|
|
172
176
|
}
|
|
173
177
|
return table;
|
|
174
178
|
})();
|
|
@@ -176,7 +180,7 @@ class SQLiteDialect {
|
|
|
176
180
|
if (joins) {
|
|
177
181
|
for (const [index, joinMeta] of joins.entries()) {
|
|
178
182
|
if (index === 0) {
|
|
179
|
-
joinsArray.push(
|
|
183
|
+
joinsArray.push(import_sql2.sql` `);
|
|
180
184
|
}
|
|
181
185
|
const table2 = joinMeta.table;
|
|
182
186
|
if ((0, import_entity.is)(table2, import_table.SQLiteTable)) {
|
|
@@ -185,27 +189,27 @@ class SQLiteDialect {
|
|
|
185
189
|
const origTableName = table2[import_table.SQLiteTable.Symbol.OriginalName];
|
|
186
190
|
const alias = tableName === origTableName ? void 0 : joinMeta.alias;
|
|
187
191
|
joinsArray.push(
|
|
188
|
-
|
|
192
|
+
import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join ${tableSchema ? import_sql2.sql`${import_sql2.sql.identifier(tableSchema)}.` : void 0}${import_sql2.sql.identifier(origTableName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`} on ${joinMeta.on}`
|
|
189
193
|
);
|
|
190
194
|
} else {
|
|
191
195
|
joinsArray.push(
|
|
192
|
-
|
|
196
|
+
import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join ${table2} on ${joinMeta.on}`
|
|
193
197
|
);
|
|
194
198
|
}
|
|
195
199
|
if (index < joins.length - 1) {
|
|
196
|
-
joinsArray.push(
|
|
200
|
+
joinsArray.push(import_sql2.sql` `);
|
|
197
201
|
}
|
|
198
202
|
}
|
|
199
203
|
}
|
|
200
|
-
const joinsSql =
|
|
201
|
-
const whereSql = where ?
|
|
202
|
-
const havingSql = having ?
|
|
204
|
+
const joinsSql = import_sql2.sql.join(joinsArray);
|
|
205
|
+
const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
|
|
206
|
+
const havingSql = having ? import_sql2.sql` having ${having}` : void 0;
|
|
203
207
|
const orderByList = [];
|
|
204
208
|
if (orderBy) {
|
|
205
209
|
for (const [index, orderByValue] of orderBy.entries()) {
|
|
206
210
|
orderByList.push(orderByValue);
|
|
207
211
|
if (index < orderBy.length - 1) {
|
|
208
|
-
orderByList.push(
|
|
212
|
+
orderByList.push(import_sql2.sql`, `);
|
|
209
213
|
}
|
|
210
214
|
}
|
|
211
215
|
}
|
|
@@ -214,15 +218,15 @@ class SQLiteDialect {
|
|
|
214
218
|
for (const [index, groupByValue] of groupBy.entries()) {
|
|
215
219
|
groupByList.push(groupByValue);
|
|
216
220
|
if (index < groupBy.length - 1) {
|
|
217
|
-
groupByList.push(
|
|
221
|
+
groupByList.push(import_sql2.sql`, `);
|
|
218
222
|
}
|
|
219
223
|
}
|
|
220
224
|
}
|
|
221
|
-
const groupBySql = groupByList.length > 0 ?
|
|
222
|
-
const orderBySql = orderByList.length > 0 ?
|
|
223
|
-
const limitSql = limit ?
|
|
224
|
-
const offsetSql = offset ?
|
|
225
|
-
const finalQuery =
|
|
225
|
+
const groupBySql = groupByList.length > 0 ? import_sql2.sql` group by ${import_sql2.sql.join(groupByList)}` : void 0;
|
|
226
|
+
const orderBySql = orderByList.length > 0 ? import_sql2.sql` order by ${import_sql2.sql.join(orderByList)}` : void 0;
|
|
227
|
+
const limitSql = limit ? import_sql2.sql` limit ${limit}` : void 0;
|
|
228
|
+
const offsetSql = offset ? import_sql2.sql` offset ${offset}` : void 0;
|
|
229
|
+
const finalQuery = import_sql2.sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}`;
|
|
226
230
|
if (setOperators.length > 0) {
|
|
227
231
|
return this.buildSetOperations(finalQuery, setOperators);
|
|
228
232
|
}
|
|
@@ -245,51 +249,51 @@ class SQLiteDialect {
|
|
|
245
249
|
leftSelect,
|
|
246
250
|
setOperator: { type, isAll, rightSelect, limit, orderBy, offset }
|
|
247
251
|
}) {
|
|
248
|
-
const leftChunk =
|
|
249
|
-
const rightChunk =
|
|
252
|
+
const leftChunk = import_sql2.sql`${leftSelect.getSQL()} `;
|
|
253
|
+
const rightChunk = import_sql2.sql`${rightSelect.getSQL()}`;
|
|
250
254
|
let orderBySql;
|
|
251
255
|
if (orderBy && orderBy.length > 0) {
|
|
252
256
|
const orderByValues = [];
|
|
253
257
|
for (const singleOrderBy of orderBy) {
|
|
254
258
|
if ((0, import_entity.is)(singleOrderBy, import_columns.SQLiteColumn)) {
|
|
255
|
-
orderByValues.push(
|
|
256
|
-
} else if ((0, import_entity.is)(singleOrderBy,
|
|
259
|
+
orderByValues.push(import_sql2.sql.identifier(singleOrderBy.name));
|
|
260
|
+
} else if ((0, import_entity.is)(singleOrderBy, import_sql2.SQL)) {
|
|
257
261
|
for (let i = 0; i < singleOrderBy.queryChunks.length; i++) {
|
|
258
262
|
const chunk = singleOrderBy.queryChunks[i];
|
|
259
263
|
if ((0, import_entity.is)(chunk, import_columns.SQLiteColumn)) {
|
|
260
|
-
singleOrderBy.queryChunks[i] =
|
|
264
|
+
singleOrderBy.queryChunks[i] = import_sql2.sql.identifier(chunk.name);
|
|
261
265
|
}
|
|
262
266
|
}
|
|
263
|
-
orderByValues.push(
|
|
267
|
+
orderByValues.push(import_sql2.sql`${singleOrderBy}`);
|
|
264
268
|
} else {
|
|
265
|
-
orderByValues.push(
|
|
269
|
+
orderByValues.push(import_sql2.sql`${singleOrderBy}`);
|
|
266
270
|
}
|
|
267
271
|
}
|
|
268
|
-
orderBySql =
|
|
272
|
+
orderBySql = import_sql2.sql` order by ${import_sql2.sql.join(orderByValues, import_sql2.sql`, `)}`;
|
|
269
273
|
}
|
|
270
|
-
const limitSql = limit ?
|
|
271
|
-
const operatorChunk =
|
|
272
|
-
const offsetSql = offset ?
|
|
273
|
-
return
|
|
274
|
+
const limitSql = limit ? import_sql2.sql` limit ${limit}` : void 0;
|
|
275
|
+
const operatorChunk = import_sql2.sql.raw(`${type} ${isAll ? "all " : ""}`);
|
|
276
|
+
const offsetSql = offset ? import_sql2.sql` offset ${offset}` : void 0;
|
|
277
|
+
return import_sql2.sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
|
|
274
278
|
}
|
|
275
|
-
buildInsertQuery({ table, values, onConflict, returning }) {
|
|
279
|
+
buildInsertQuery({ table, values, onConflict, returning, withList }) {
|
|
276
280
|
const valuesSqlList = [];
|
|
277
281
|
const columns = table[import_table2.Table.Symbol.Columns];
|
|
278
282
|
const colEntries = Object.entries(columns);
|
|
279
|
-
const insertOrder = colEntries.map(([, column]) =>
|
|
283
|
+
const insertOrder = colEntries.map(([, column]) => import_sql2.sql.identifier(column.name));
|
|
280
284
|
for (const [valueIndex, value] of values.entries()) {
|
|
281
285
|
const valueList = [];
|
|
282
286
|
for (const [fieldName, col] of colEntries) {
|
|
283
287
|
const colValue = value[fieldName];
|
|
284
|
-
if (colValue === void 0 || (0, import_entity.is)(colValue,
|
|
288
|
+
if (colValue === void 0 || (0, import_entity.is)(colValue, import_sql2.Param) && colValue.value === void 0) {
|
|
285
289
|
let defaultValue;
|
|
286
290
|
if (col.default !== null && col.default !== void 0) {
|
|
287
|
-
defaultValue = (0, import_entity.is)(col.default,
|
|
291
|
+
defaultValue = (0, import_entity.is)(col.default, import_sql2.SQL) ? col.default : import_sql2.sql.param(col.default, col);
|
|
288
292
|
} else if (col.defaultFn !== void 0) {
|
|
289
293
|
const defaultFnResult = col.defaultFn();
|
|
290
|
-
defaultValue = (0, import_entity.is)(defaultFnResult,
|
|
294
|
+
defaultValue = (0, import_entity.is)(defaultFnResult, import_sql2.SQL) ? defaultFnResult : import_sql2.sql.param(defaultFnResult, col);
|
|
291
295
|
} else {
|
|
292
|
-
defaultValue =
|
|
296
|
+
defaultValue = import_sql2.sql`null`;
|
|
293
297
|
}
|
|
294
298
|
valueList.push(defaultValue);
|
|
295
299
|
} else {
|
|
@@ -298,13 +302,14 @@ class SQLiteDialect {
|
|
|
298
302
|
}
|
|
299
303
|
valuesSqlList.push(valueList);
|
|
300
304
|
if (valueIndex < values.length - 1) {
|
|
301
|
-
valuesSqlList.push(
|
|
305
|
+
valuesSqlList.push(import_sql2.sql`, `);
|
|
302
306
|
}
|
|
303
307
|
}
|
|
304
|
-
const
|
|
305
|
-
const
|
|
306
|
-
const
|
|
307
|
-
|
|
308
|
+
const withSql = this.buildWithCTE(withList);
|
|
309
|
+
const valuesSql = import_sql2.sql.join(valuesSqlList);
|
|
310
|
+
const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
311
|
+
const onConflictSql = onConflict ? import_sql2.sql` on conflict ${onConflict}` : void 0;
|
|
312
|
+
return import_sql2.sql`${withSql}insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
|
|
308
313
|
}
|
|
309
314
|
sqlToQuery(sql2) {
|
|
310
315
|
return sql2.toQuery({
|
|
@@ -376,7 +381,7 @@ class SQLiteDialect {
|
|
|
376
381
|
}
|
|
377
382
|
let extras;
|
|
378
383
|
if (config.extras) {
|
|
379
|
-
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql:
|
|
384
|
+
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql: import_sql2.sql }) : config.extras;
|
|
380
385
|
for (const [tsKey, value] of Object.entries(extras)) {
|
|
381
386
|
fieldsSelection.push({
|
|
382
387
|
tsKey,
|
|
@@ -386,7 +391,7 @@ class SQLiteDialect {
|
|
|
386
391
|
}
|
|
387
392
|
for (const { tsKey, value } of fieldsSelection) {
|
|
388
393
|
selection.push({
|
|
389
|
-
dbKey: (0, import_entity.is)(value,
|
|
394
|
+
dbKey: (0, import_entity.is)(value, import_sql2.SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
|
|
390
395
|
tsKey,
|
|
391
396
|
field: (0, import_entity.is)(value, import_column.Column) ? (0, import_alias.aliasedTableColumn)(value, tableAlias) : value,
|
|
392
397
|
relationTableTsKey: void 0,
|
|
@@ -415,9 +420,9 @@ class SQLiteDialect {
|
|
|
415
420
|
const relationTableName = relation.referencedTable[import_table2.Table.Symbol.Name];
|
|
416
421
|
const relationTableTsName = tableNamesMap[relationTableName];
|
|
417
422
|
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
|
|
418
|
-
const joinOn2 = (0,
|
|
423
|
+
const joinOn2 = (0, import_sql.and)(
|
|
419
424
|
...normalizedRelation.fields.map(
|
|
420
|
-
(field2, i) => (0,
|
|
425
|
+
(field2, i) => (0, import_sql.eq)(
|
|
421
426
|
(0, import_alias.aliasedTableColumn)(normalizedRelation.references[i], relationTableAlias),
|
|
422
427
|
(0, import_alias.aliasedTableColumn)(field2, tableAlias)
|
|
423
428
|
)
|
|
@@ -434,7 +439,7 @@ class SQLiteDialect {
|
|
|
434
439
|
joinOn: joinOn2,
|
|
435
440
|
nestedQueryRelation: relation
|
|
436
441
|
});
|
|
437
|
-
const field =
|
|
442
|
+
const field = import_sql2.sql`(${builtRelation.sql})`.as(selectedRelationTsKey);
|
|
438
443
|
selection.push({
|
|
439
444
|
dbKey: selectedRelationTsKey,
|
|
440
445
|
tsKey: selectedRelationTsKey,
|
|
@@ -451,16 +456,16 @@ class SQLiteDialect {
|
|
|
451
456
|
});
|
|
452
457
|
}
|
|
453
458
|
let result;
|
|
454
|
-
where = (0,
|
|
459
|
+
where = (0, import_sql.and)(joinOn, where);
|
|
455
460
|
if (nestedQueryRelation) {
|
|
456
|
-
let field =
|
|
461
|
+
let field = import_sql2.sql`json_array(${import_sql2.sql.join(
|
|
457
462
|
selection.map(
|
|
458
|
-
({ field: field2 }) => (0, import_entity.is)(field2, import_columns.SQLiteColumn) ?
|
|
463
|
+
({ field: field2 }) => (0, import_entity.is)(field2, import_columns.SQLiteColumn) ? import_sql2.sql.identifier(field2.name) : (0, import_entity.is)(field2, import_sql2.SQL.Aliased) ? field2.sql : field2
|
|
459
464
|
),
|
|
460
|
-
|
|
465
|
+
import_sql2.sql`, `
|
|
461
466
|
)})`;
|
|
462
467
|
if ((0, import_entity.is)(nestedQueryRelation, import_relations.Many)) {
|
|
463
|
-
field =
|
|
468
|
+
field = import_sql2.sql`coalesce(json_group_array(${field}), json_array())`;
|
|
464
469
|
}
|
|
465
470
|
const nestedSelection = [{
|
|
466
471
|
dbKey: "data",
|
|
@@ -478,7 +483,7 @@ class SQLiteDialect {
|
|
|
478
483
|
fieldsFlat: [
|
|
479
484
|
{
|
|
480
485
|
path: [],
|
|
481
|
-
field:
|
|
486
|
+
field: import_sql2.sql.raw("*")
|
|
482
487
|
}
|
|
483
488
|
],
|
|
484
489
|
where,
|
|
@@ -533,9 +538,10 @@ class SQLiteDialect {
|
|
|
533
538
|
}
|
|
534
539
|
class SQLiteSyncDialect extends SQLiteDialect {
|
|
535
540
|
static [import_entity.entityKind] = "SQLiteSyncDialect";
|
|
536
|
-
migrate(migrations, session) {
|
|
537
|
-
const
|
|
538
|
-
|
|
541
|
+
migrate(migrations, session, config) {
|
|
542
|
+
const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
|
|
543
|
+
const migrationTableCreate = import_sql2.sql`
|
|
544
|
+
CREATE TABLE IF NOT EXISTS ${import_sql2.sql.identifier(migrationsTable)} (
|
|
539
545
|
id SERIAL PRIMARY KEY,
|
|
540
546
|
hash text NOT NULL,
|
|
541
547
|
created_at numeric
|
|
@@ -543,33 +549,34 @@ class SQLiteSyncDialect extends SQLiteDialect {
|
|
|
543
549
|
`;
|
|
544
550
|
session.run(migrationTableCreate);
|
|
545
551
|
const dbMigrations = session.values(
|
|
546
|
-
|
|
552
|
+
import_sql2.sql`SELECT id, hash, created_at FROM ${import_sql2.sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
|
|
547
553
|
);
|
|
548
554
|
const lastDbMigration = dbMigrations[0] ?? void 0;
|
|
549
|
-
session.run(
|
|
555
|
+
session.run(import_sql2.sql`BEGIN`);
|
|
550
556
|
try {
|
|
551
557
|
for (const migration of migrations) {
|
|
552
558
|
if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
|
|
553
559
|
for (const stmt of migration.sql) {
|
|
554
|
-
session.run(
|
|
560
|
+
session.run(import_sql2.sql.raw(stmt));
|
|
555
561
|
}
|
|
556
562
|
session.run(
|
|
557
|
-
|
|
563
|
+
import_sql2.sql`INSERT INTO ${import_sql2.sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`
|
|
558
564
|
);
|
|
559
565
|
}
|
|
560
566
|
}
|
|
561
|
-
session.run(
|
|
567
|
+
session.run(import_sql2.sql`COMMIT`);
|
|
562
568
|
} catch (e) {
|
|
563
|
-
session.run(
|
|
569
|
+
session.run(import_sql2.sql`ROLLBACK`);
|
|
564
570
|
throw e;
|
|
565
571
|
}
|
|
566
572
|
}
|
|
567
573
|
}
|
|
568
574
|
class SQLiteAsyncDialect extends SQLiteDialect {
|
|
569
575
|
static [import_entity.entityKind] = "SQLiteAsyncDialect";
|
|
570
|
-
async migrate(migrations, session) {
|
|
571
|
-
const
|
|
572
|
-
|
|
576
|
+
async migrate(migrations, session, config) {
|
|
577
|
+
const migrationsTable = typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
|
|
578
|
+
const migrationTableCreate = import_sql2.sql`
|
|
579
|
+
CREATE TABLE IF NOT EXISTS ${import_sql2.sql.identifier(migrationsTable)} (
|
|
573
580
|
id SERIAL PRIMARY KEY,
|
|
574
581
|
hash text NOT NULL,
|
|
575
582
|
created_at numeric
|
|
@@ -577,17 +584,17 @@ class SQLiteAsyncDialect extends SQLiteDialect {
|
|
|
577
584
|
`;
|
|
578
585
|
await session.run(migrationTableCreate);
|
|
579
586
|
const dbMigrations = await session.values(
|
|
580
|
-
|
|
587
|
+
import_sql2.sql`SELECT id, hash, created_at FROM ${import_sql2.sql.identifier(migrationsTable)} ORDER BY created_at DESC LIMIT 1`
|
|
581
588
|
);
|
|
582
589
|
const lastDbMigration = dbMigrations[0] ?? void 0;
|
|
583
590
|
await session.transaction(async (tx) => {
|
|
584
591
|
for (const migration of migrations) {
|
|
585
592
|
if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
|
|
586
593
|
for (const stmt of migration.sql) {
|
|
587
|
-
await tx.run(
|
|
594
|
+
await tx.run(import_sql2.sql.raw(stmt));
|
|
588
595
|
}
|
|
589
596
|
await tx.run(
|
|
590
|
-
|
|
597
|
+
import_sql2.sql`INSERT INTO ${import_sql2.sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`
|
|
591
598
|
);
|
|
592
599
|
}
|
|
593
600
|
}
|