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.
- 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/pg-core/dialect.cjs
CHANGED
|
@@ -28,37 +28,39 @@ var import_errors = require("../errors.cjs");
|
|
|
28
28
|
var import_columns = require("./columns/index.cjs");
|
|
29
29
|
var import_table = require("./table.cjs");
|
|
30
30
|
var import_relations = require("../relations.cjs");
|
|
31
|
-
var import_sql = require("../sql/
|
|
31
|
+
var import_sql = require("../sql/index.cjs");
|
|
32
|
+
var import_sql2 = require("../sql/sql.cjs");
|
|
32
33
|
var import_subquery = require("../subquery.cjs");
|
|
33
34
|
var import_table2 = require("../table.cjs");
|
|
34
35
|
var import_utils = require("../utils.cjs");
|
|
35
36
|
var import_view_common = require("../view-common.cjs");
|
|
36
|
-
var import_sql2 = require("../sql/index.cjs");
|
|
37
37
|
var import_view_base = require("./view-base.cjs");
|
|
38
38
|
class PgDialect {
|
|
39
39
|
static [import_entity.entityKind] = "PgDialect";
|
|
40
|
-
async migrate(migrations, session) {
|
|
41
|
-
const
|
|
42
|
-
|
|
40
|
+
async migrate(migrations, session, config) {
|
|
41
|
+
const migrationsTable = typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
|
|
42
|
+
const migrationsSchema = typeof config === "string" ? "drizzle" : config.migrationsSchema ?? "drizzle";
|
|
43
|
+
const migrationTableCreate = import_sql2.sql`
|
|
44
|
+
CREATE TABLE IF NOT EXISTS ${import_sql2.sql.identifier(migrationsSchema)}.${import_sql2.sql.identifier(migrationsTable)} (
|
|
43
45
|
id SERIAL PRIMARY KEY,
|
|
44
46
|
hash text NOT NULL,
|
|
45
47
|
created_at bigint
|
|
46
48
|
)
|
|
47
49
|
`;
|
|
48
|
-
await session.execute(
|
|
50
|
+
await session.execute(import_sql2.sql`CREATE SCHEMA IF NOT EXISTS ${import_sql2.sql.identifier(migrationsSchema)}`);
|
|
49
51
|
await session.execute(migrationTableCreate);
|
|
50
52
|
const dbMigrations = await session.all(
|
|
51
|
-
|
|
53
|
+
import_sql2.sql`select id, hash, created_at from ${import_sql2.sql.identifier(migrationsSchema)}.${import_sql2.sql.identifier(migrationsTable)} order by created_at desc limit 1`
|
|
52
54
|
);
|
|
53
55
|
const lastDbMigration = dbMigrations[0];
|
|
54
56
|
await session.transaction(async (tx) => {
|
|
55
57
|
for await (const migration of migrations) {
|
|
56
58
|
if (!lastDbMigration || Number(lastDbMigration.created_at) < migration.folderMillis) {
|
|
57
59
|
for (const stmt of migration.sql) {
|
|
58
|
-
await tx.execute(
|
|
60
|
+
await tx.execute(import_sql2.sql.raw(stmt));
|
|
59
61
|
}
|
|
60
62
|
await tx.execute(
|
|
61
|
-
|
|
63
|
+
import_sql2.sql`insert into ${import_sql2.sql.identifier(migrationsSchema)}.${import_sql2.sql.identifier(migrationsTable)} ("hash", "created_at") values(${migration.hash}, ${migration.folderMillis})`
|
|
62
64
|
);
|
|
63
65
|
}
|
|
64
66
|
}
|
|
@@ -73,30 +75,45 @@ class PgDialect {
|
|
|
73
75
|
escapeString(str) {
|
|
74
76
|
return `'${str.replace(/'/g, "''")}'`;
|
|
75
77
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
buildWithCTE(queries) {
|
|
79
|
+
if (!queries?.length)
|
|
80
|
+
return void 0;
|
|
81
|
+
const withSqlChunks = [import_sql2.sql`with `];
|
|
82
|
+
for (const [i, w] of queries.entries()) {
|
|
83
|
+
withSqlChunks.push(import_sql2.sql`${import_sql2.sql.identifier(w[import_subquery.SubqueryConfig].alias)} as (${w[import_subquery.SubqueryConfig].sql})`);
|
|
84
|
+
if (i < queries.length - 1) {
|
|
85
|
+
withSqlChunks.push(import_sql2.sql`, `);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
withSqlChunks.push(import_sql2.sql` `);
|
|
89
|
+
return import_sql2.sql.join(withSqlChunks);
|
|
90
|
+
}
|
|
91
|
+
buildDeleteQuery({ table, where, returning, withList }) {
|
|
92
|
+
const withSql = this.buildWithCTE(withList);
|
|
93
|
+
const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
94
|
+
const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
|
|
95
|
+
return import_sql2.sql`${withSql}delete from ${table}${whereSql}${returningSql}`;
|
|
80
96
|
}
|
|
81
97
|
buildUpdateSet(table, set) {
|
|
82
98
|
const setEntries = Object.entries(set);
|
|
83
99
|
const setSize = setEntries.length;
|
|
84
|
-
return
|
|
100
|
+
return import_sql2.sql.join(
|
|
85
101
|
setEntries.flatMap(([colName, value], i) => {
|
|
86
102
|
const col = table[import_table2.Table.Symbol.Columns][colName];
|
|
87
|
-
const res =
|
|
103
|
+
const res = import_sql2.sql`${import_sql2.sql.identifier(col.name)} = ${value}`;
|
|
88
104
|
if (i < setSize - 1) {
|
|
89
|
-
return [res,
|
|
105
|
+
return [res, import_sql2.sql.raw(", ")];
|
|
90
106
|
}
|
|
91
107
|
return [res];
|
|
92
108
|
})
|
|
93
109
|
);
|
|
94
110
|
}
|
|
95
|
-
buildUpdateQuery({ table, set, where, returning }) {
|
|
111
|
+
buildUpdateQuery({ table, set, where, returning, withList }) {
|
|
112
|
+
const withSql = this.buildWithCTE(withList);
|
|
96
113
|
const setSql = this.buildUpdateSet(table, set);
|
|
97
|
-
const returningSql = returning ?
|
|
98
|
-
const whereSql = where ?
|
|
99
|
-
return
|
|
114
|
+
const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
115
|
+
const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
|
|
116
|
+
return import_sql2.sql`${withSql}update ${table} set ${setSql}${whereSql}${returningSql}`;
|
|
100
117
|
}
|
|
101
118
|
/**
|
|
102
119
|
* Builds selection SQL with provided fields/expressions
|
|
@@ -113,16 +130,16 @@ class PgDialect {
|
|
|
113
130
|
const columnsLen = fields.length;
|
|
114
131
|
const chunks = fields.flatMap(({ field }, i) => {
|
|
115
132
|
const chunk = [];
|
|
116
|
-
if ((0, import_entity.is)(field,
|
|
117
|
-
chunk.push(
|
|
118
|
-
} else if ((0, import_entity.is)(field,
|
|
119
|
-
const query = (0, import_entity.is)(field,
|
|
133
|
+
if ((0, import_entity.is)(field, import_sql2.SQL.Aliased) && field.isSelectionField) {
|
|
134
|
+
chunk.push(import_sql2.sql.identifier(field.fieldAlias));
|
|
135
|
+
} else if ((0, import_entity.is)(field, import_sql2.SQL.Aliased) || (0, import_entity.is)(field, import_sql2.SQL)) {
|
|
136
|
+
const query = (0, import_entity.is)(field, import_sql2.SQL.Aliased) ? field.sql : field;
|
|
120
137
|
if (isSingleTable) {
|
|
121
138
|
chunk.push(
|
|
122
|
-
new
|
|
139
|
+
new import_sql2.SQL(
|
|
123
140
|
query.queryChunks.map((c) => {
|
|
124
141
|
if ((0, import_entity.is)(c, import_columns.PgColumn)) {
|
|
125
|
-
return
|
|
142
|
+
return import_sql2.sql.identifier(c.name);
|
|
126
143
|
}
|
|
127
144
|
return c;
|
|
128
145
|
})
|
|
@@ -131,22 +148,22 @@ class PgDialect {
|
|
|
131
148
|
} else {
|
|
132
149
|
chunk.push(query);
|
|
133
150
|
}
|
|
134
|
-
if ((0, import_entity.is)(field,
|
|
135
|
-
chunk.push(
|
|
151
|
+
if ((0, import_entity.is)(field, import_sql2.SQL.Aliased)) {
|
|
152
|
+
chunk.push(import_sql2.sql` as ${import_sql2.sql.identifier(field.fieldAlias)}`);
|
|
136
153
|
}
|
|
137
154
|
} else if ((0, import_entity.is)(field, import_column.Column)) {
|
|
138
155
|
if (isSingleTable) {
|
|
139
|
-
chunk.push(
|
|
156
|
+
chunk.push(import_sql2.sql.identifier(field.name));
|
|
140
157
|
} else {
|
|
141
158
|
chunk.push(field);
|
|
142
159
|
}
|
|
143
160
|
}
|
|
144
161
|
if (i < columnsLen - 1) {
|
|
145
|
-
chunk.push(
|
|
162
|
+
chunk.push(import_sql2.sql`, `);
|
|
146
163
|
}
|
|
147
164
|
return chunk;
|
|
148
165
|
});
|
|
149
|
-
return
|
|
166
|
+
return import_sql2.sql.join(chunks);
|
|
150
167
|
}
|
|
151
168
|
buildSelectQuery({
|
|
152
169
|
withList,
|
|
@@ -166,7 +183,7 @@ class PgDialect {
|
|
|
166
183
|
}) {
|
|
167
184
|
const fieldsList = fieldsFlat ?? (0, import_utils.orderSelectedFields)(fields);
|
|
168
185
|
for (const f of fieldsList) {
|
|
169
|
-
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.PgViewBase) ? table[import_view_common.ViewBaseConfig].name : (0, import_entity.is)(table,
|
|
186
|
+
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.PgViewBase) ? table[import_view_common.ViewBaseConfig].name : (0, import_entity.is)(table, import_sql2.SQL) ? void 0 : (0, import_table2.getTableName)(table)) && !((table2) => joins?.some(
|
|
170
187
|
({ alias }) => alias === (table2[import_table2.Table.Symbol.IsAlias] ? (0, import_table2.getTableName)(table2) : table2[import_table2.Table.Symbol.BaseName])
|
|
171
188
|
))(f.field.table)) {
|
|
172
189
|
const tableName = (0, import_table2.getTableName)(f.field.table);
|
|
@@ -176,30 +193,19 @@ class PgDialect {
|
|
|
176
193
|
}
|
|
177
194
|
}
|
|
178
195
|
const isSingleTable = !joins || joins.length === 0;
|
|
179
|
-
|
|
180
|
-
if (withList?.length) {
|
|
181
|
-
const withSqlChunks = [import_sql.sql`with `];
|
|
182
|
-
for (const [i, w] of withList.entries()) {
|
|
183
|
-
withSqlChunks.push(import_sql.sql`${import_sql.sql.identifier(w[import_subquery.SubqueryConfig].alias)} as (${w[import_subquery.SubqueryConfig].sql})`);
|
|
184
|
-
if (i < withList.length - 1) {
|
|
185
|
-
withSqlChunks.push(import_sql.sql`, `);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
withSqlChunks.push(import_sql.sql` `);
|
|
189
|
-
withSql = import_sql.sql.join(withSqlChunks);
|
|
190
|
-
}
|
|
196
|
+
const withSql = this.buildWithCTE(withList);
|
|
191
197
|
let distinctSql;
|
|
192
198
|
if (distinct) {
|
|
193
|
-
distinctSql = distinct === true ?
|
|
199
|
+
distinctSql = distinct === true ? import_sql2.sql` distinct` : import_sql2.sql` distinct on (${import_sql2.sql.join(distinct.on, import_sql2.sql`, `)})`;
|
|
194
200
|
}
|
|
195
201
|
const selection = this.buildSelection(fieldsList, { isSingleTable });
|
|
196
202
|
const tableSql = (() => {
|
|
197
203
|
if ((0, import_entity.is)(table, import_table2.Table) && table[import_table2.Table.Symbol.OriginalName] !== table[import_table2.Table.Symbol.Name]) {
|
|
198
|
-
let fullName =
|
|
204
|
+
let fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.OriginalName])}`;
|
|
199
205
|
if (table[import_table2.Table.Symbol.Schema]) {
|
|
200
|
-
fullName =
|
|
206
|
+
fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Schema])}.${fullName}`;
|
|
201
207
|
}
|
|
202
|
-
return
|
|
208
|
+
return import_sql2.sql`${fullName} ${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Name])}`;
|
|
203
209
|
}
|
|
204
210
|
return table;
|
|
205
211
|
})();
|
|
@@ -207,68 +213,68 @@ class PgDialect {
|
|
|
207
213
|
if (joins) {
|
|
208
214
|
for (const [index, joinMeta] of joins.entries()) {
|
|
209
215
|
if (index === 0) {
|
|
210
|
-
joinsArray.push(
|
|
216
|
+
joinsArray.push(import_sql2.sql` `);
|
|
211
217
|
}
|
|
212
218
|
const table2 = joinMeta.table;
|
|
213
|
-
const lateralSql = joinMeta.lateral ?
|
|
219
|
+
const lateralSql = joinMeta.lateral ? import_sql2.sql` lateral` : void 0;
|
|
214
220
|
if ((0, import_entity.is)(table2, import_table.PgTable)) {
|
|
215
221
|
const tableName = table2[import_table.PgTable.Symbol.Name];
|
|
216
222
|
const tableSchema = table2[import_table.PgTable.Symbol.Schema];
|
|
217
223
|
const origTableName = table2[import_table.PgTable.Symbol.OriginalName];
|
|
218
224
|
const alias = tableName === origTableName ? void 0 : joinMeta.alias;
|
|
219
225
|
joinsArray.push(
|
|
220
|
-
|
|
226
|
+
import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${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}`
|
|
221
227
|
);
|
|
222
|
-
} else if ((0, import_entity.is)(table2,
|
|
228
|
+
} else if ((0, import_entity.is)(table2, import_sql.View)) {
|
|
223
229
|
const viewName = table2[import_view_common.ViewBaseConfig].name;
|
|
224
230
|
const viewSchema = table2[import_view_common.ViewBaseConfig].schema;
|
|
225
231
|
const origViewName = table2[import_view_common.ViewBaseConfig].originalName;
|
|
226
232
|
const alias = viewName === origViewName ? void 0 : joinMeta.alias;
|
|
227
233
|
joinsArray.push(
|
|
228
|
-
|
|
234
|
+
import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? import_sql2.sql`${import_sql2.sql.identifier(viewSchema)}.` : void 0}${import_sql2.sql.identifier(origViewName)}${alias && import_sql2.sql` ${import_sql2.sql.identifier(alias)}`} on ${joinMeta.on}`
|
|
229
235
|
);
|
|
230
236
|
} else {
|
|
231
237
|
joinsArray.push(
|
|
232
|
-
|
|
238
|
+
import_sql2.sql`${import_sql2.sql.raw(joinMeta.joinType)} join${lateralSql} ${table2} on ${joinMeta.on}`
|
|
233
239
|
);
|
|
234
240
|
}
|
|
235
241
|
if (index < joins.length - 1) {
|
|
236
|
-
joinsArray.push(
|
|
242
|
+
joinsArray.push(import_sql2.sql` `);
|
|
237
243
|
}
|
|
238
244
|
}
|
|
239
245
|
}
|
|
240
|
-
const joinsSql =
|
|
241
|
-
const whereSql = where ?
|
|
242
|
-
const havingSql = having ?
|
|
246
|
+
const joinsSql = import_sql2.sql.join(joinsArray);
|
|
247
|
+
const whereSql = where ? import_sql2.sql` where ${where}` : void 0;
|
|
248
|
+
const havingSql = having ? import_sql2.sql` having ${having}` : void 0;
|
|
243
249
|
let orderBySql;
|
|
244
250
|
if (orderBy && orderBy.length > 0) {
|
|
245
|
-
orderBySql =
|
|
251
|
+
orderBySql = import_sql2.sql` order by ${import_sql2.sql.join(orderBy, import_sql2.sql`, `)}`;
|
|
246
252
|
}
|
|
247
253
|
let groupBySql;
|
|
248
254
|
if (groupBy && groupBy.length > 0) {
|
|
249
|
-
groupBySql =
|
|
255
|
+
groupBySql = import_sql2.sql` group by ${import_sql2.sql.join(groupBy, import_sql2.sql`, `)}`;
|
|
250
256
|
}
|
|
251
|
-
const limitSql = limit ?
|
|
252
|
-
const offsetSql = offset ?
|
|
253
|
-
const lockingClauseSql =
|
|
257
|
+
const limitSql = limit ? import_sql2.sql` limit ${limit}` : void 0;
|
|
258
|
+
const offsetSql = offset ? import_sql2.sql` offset ${offset}` : void 0;
|
|
259
|
+
const lockingClauseSql = import_sql2.sql.empty();
|
|
254
260
|
if (lockingClause) {
|
|
255
|
-
const clauseSql =
|
|
261
|
+
const clauseSql = import_sql2.sql` for ${import_sql2.sql.raw(lockingClause.strength)}`;
|
|
256
262
|
if (lockingClause.config.of) {
|
|
257
263
|
clauseSql.append(
|
|
258
|
-
|
|
264
|
+
import_sql2.sql` of ${import_sql2.sql.join(
|
|
259
265
|
Array.isArray(lockingClause.config.of) ? lockingClause.config.of : [lockingClause.config.of],
|
|
260
|
-
|
|
266
|
+
import_sql2.sql`, `
|
|
261
267
|
)}`
|
|
262
268
|
);
|
|
263
269
|
}
|
|
264
270
|
if (lockingClause.config.noWait) {
|
|
265
|
-
clauseSql.append(
|
|
271
|
+
clauseSql.append(import_sql2.sql` no wait`);
|
|
266
272
|
} else if (lockingClause.config.skipLocked) {
|
|
267
|
-
clauseSql.append(
|
|
273
|
+
clauseSql.append(import_sql2.sql` skip locked`);
|
|
268
274
|
}
|
|
269
275
|
lockingClauseSql.append(clauseSql);
|
|
270
276
|
}
|
|
271
|
-
const finalQuery =
|
|
277
|
+
const finalQuery = import_sql2.sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClauseSql}`;
|
|
272
278
|
if (setOperators.length > 0) {
|
|
273
279
|
return this.buildSetOperations(finalQuery, setOperators);
|
|
274
280
|
}
|
|
@@ -291,49 +297,49 @@ class PgDialect {
|
|
|
291
297
|
leftSelect,
|
|
292
298
|
setOperator: { type, isAll, rightSelect, limit, orderBy, offset }
|
|
293
299
|
}) {
|
|
294
|
-
const leftChunk =
|
|
295
|
-
const rightChunk =
|
|
300
|
+
const leftChunk = import_sql2.sql`(${leftSelect.getSQL()}) `;
|
|
301
|
+
const rightChunk = import_sql2.sql`(${rightSelect.getSQL()})`;
|
|
296
302
|
let orderBySql;
|
|
297
303
|
if (orderBy && orderBy.length > 0) {
|
|
298
304
|
const orderByValues = [];
|
|
299
305
|
for (const singleOrderBy of orderBy) {
|
|
300
306
|
if ((0, import_entity.is)(singleOrderBy, import_columns.PgColumn)) {
|
|
301
|
-
orderByValues.push(
|
|
302
|
-
} else if ((0, import_entity.is)(singleOrderBy,
|
|
307
|
+
orderByValues.push(import_sql2.sql.identifier(singleOrderBy.name));
|
|
308
|
+
} else if ((0, import_entity.is)(singleOrderBy, import_sql2.SQL)) {
|
|
303
309
|
for (let i = 0; i < singleOrderBy.queryChunks.length; i++) {
|
|
304
310
|
const chunk = singleOrderBy.queryChunks[i];
|
|
305
311
|
if ((0, import_entity.is)(chunk, import_columns.PgColumn)) {
|
|
306
|
-
singleOrderBy.queryChunks[i] =
|
|
312
|
+
singleOrderBy.queryChunks[i] = import_sql2.sql.identifier(chunk.name);
|
|
307
313
|
}
|
|
308
314
|
}
|
|
309
|
-
orderByValues.push(
|
|
315
|
+
orderByValues.push(import_sql2.sql`${singleOrderBy}`);
|
|
310
316
|
} else {
|
|
311
|
-
orderByValues.push(
|
|
317
|
+
orderByValues.push(import_sql2.sql`${singleOrderBy}`);
|
|
312
318
|
}
|
|
313
319
|
}
|
|
314
|
-
orderBySql =
|
|
320
|
+
orderBySql = import_sql2.sql` order by ${import_sql2.sql.join(orderByValues, import_sql2.sql`, `)} `;
|
|
315
321
|
}
|
|
316
|
-
const limitSql = limit ?
|
|
317
|
-
const operatorChunk =
|
|
318
|
-
const offsetSql = offset ?
|
|
319
|
-
return
|
|
322
|
+
const limitSql = limit ? import_sql2.sql` limit ${limit}` : void 0;
|
|
323
|
+
const operatorChunk = import_sql2.sql.raw(`${type} ${isAll ? "all " : ""}`);
|
|
324
|
+
const offsetSql = offset ? import_sql2.sql` offset ${offset}` : void 0;
|
|
325
|
+
return import_sql2.sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
|
|
320
326
|
}
|
|
321
|
-
buildInsertQuery({ table, values, onConflict, returning }) {
|
|
327
|
+
buildInsertQuery({ table, values, onConflict, returning, withList }) {
|
|
322
328
|
const valuesSqlList = [];
|
|
323
329
|
const columns = table[import_table2.Table.Symbol.Columns];
|
|
324
330
|
const colEntries = Object.entries(columns);
|
|
325
|
-
const insertOrder = colEntries.map(([, column]) =>
|
|
331
|
+
const insertOrder = colEntries.map(([, column]) => import_sql2.sql.identifier(column.name));
|
|
326
332
|
for (const [valueIndex, value] of values.entries()) {
|
|
327
333
|
const valueList = [];
|
|
328
334
|
for (const [fieldName, col] of colEntries) {
|
|
329
335
|
const colValue = value[fieldName];
|
|
330
|
-
if (colValue === void 0 || (0, import_entity.is)(colValue,
|
|
336
|
+
if (colValue === void 0 || (0, import_entity.is)(colValue, import_sql2.Param) && colValue.value === void 0) {
|
|
331
337
|
if (col.defaultFn !== void 0) {
|
|
332
338
|
const defaultFnResult = col.defaultFn();
|
|
333
|
-
const defaultValue = (0, import_entity.is)(defaultFnResult,
|
|
339
|
+
const defaultValue = (0, import_entity.is)(defaultFnResult, import_sql2.SQL) ? defaultFnResult : import_sql2.sql.param(defaultFnResult, col);
|
|
334
340
|
valueList.push(defaultValue);
|
|
335
341
|
} else {
|
|
336
|
-
valueList.push(
|
|
342
|
+
valueList.push(import_sql2.sql`default`);
|
|
337
343
|
}
|
|
338
344
|
} else {
|
|
339
345
|
valueList.push(colValue);
|
|
@@ -341,18 +347,19 @@ class PgDialect {
|
|
|
341
347
|
}
|
|
342
348
|
valuesSqlList.push(valueList);
|
|
343
349
|
if (valueIndex < values.length - 1) {
|
|
344
|
-
valuesSqlList.push(
|
|
350
|
+
valuesSqlList.push(import_sql2.sql`, `);
|
|
345
351
|
}
|
|
346
352
|
}
|
|
347
|
-
const
|
|
348
|
-
const
|
|
349
|
-
const
|
|
350
|
-
|
|
353
|
+
const withSql = this.buildWithCTE(withList);
|
|
354
|
+
const valuesSql = import_sql2.sql.join(valuesSqlList);
|
|
355
|
+
const returningSql = returning ? import_sql2.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
|
|
356
|
+
const onConflictSql = onConflict ? import_sql2.sql` on conflict ${onConflict}` : void 0;
|
|
357
|
+
return import_sql2.sql`${withSql}insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
|
|
351
358
|
}
|
|
352
359
|
buildRefreshMaterializedViewQuery({ view, concurrently, withNoData }) {
|
|
353
|
-
const concurrentlySql = concurrently ?
|
|
354
|
-
const withNoDataSql = withNoData ?
|
|
355
|
-
return
|
|
360
|
+
const concurrentlySql = concurrently ? import_sql2.sql` concurrently` : void 0;
|
|
361
|
+
const withNoDataSql = withNoData ? import_sql2.sql` with no data` : void 0;
|
|
362
|
+
return import_sql2.sql`refresh materialized view${concurrentlySql} ${view}${withNoDataSql}`;
|
|
356
363
|
}
|
|
357
364
|
prepareTyping(encoder) {
|
|
358
365
|
if ((0, import_entity.is)(encoder, import_columns.PgJsonb) || (0, import_entity.is)(encoder, import_columns.PgJson)) {
|
|
@@ -923,7 +930,7 @@ class PgDialect {
|
|
|
923
930
|
}
|
|
924
931
|
let extras;
|
|
925
932
|
if (config.extras) {
|
|
926
|
-
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql:
|
|
933
|
+
extras = typeof config.extras === "function" ? config.extras(aliasedColumns, { sql: import_sql2.sql }) : config.extras;
|
|
927
934
|
for (const [tsKey, value] of Object.entries(extras)) {
|
|
928
935
|
fieldsSelection.push({
|
|
929
936
|
tsKey,
|
|
@@ -933,7 +940,7 @@ class PgDialect {
|
|
|
933
940
|
}
|
|
934
941
|
for (const { tsKey, value } of fieldsSelection) {
|
|
935
942
|
selection.push({
|
|
936
|
-
dbKey: (0, import_entity.is)(value,
|
|
943
|
+
dbKey: (0, import_entity.is)(value, import_sql2.SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
|
|
937
944
|
tsKey,
|
|
938
945
|
field: (0, import_entity.is)(value, import_column.Column) ? (0, import_alias.aliasedTableColumn)(value, tableAlias) : value,
|
|
939
946
|
relationTableTsKey: void 0,
|
|
@@ -962,9 +969,9 @@ class PgDialect {
|
|
|
962
969
|
const relationTableName = relation.referencedTable[import_table2.Table.Symbol.Name];
|
|
963
970
|
const relationTableTsName = tableNamesMap[relationTableName];
|
|
964
971
|
const relationTableAlias = `${tableAlias}_${selectedRelationTsKey}`;
|
|
965
|
-
const joinOn2 = (0,
|
|
972
|
+
const joinOn2 = (0, import_sql.and)(
|
|
966
973
|
...normalizedRelation.fields.map(
|
|
967
|
-
(field2, i) => (0,
|
|
974
|
+
(field2, i) => (0, import_sql.eq)(
|
|
968
975
|
(0, import_alias.aliasedTableColumn)(normalizedRelation.references[i], relationTableAlias),
|
|
969
976
|
(0, import_alias.aliasedTableColumn)(field2, tableAlias)
|
|
970
977
|
)
|
|
@@ -981,9 +988,9 @@ class PgDialect {
|
|
|
981
988
|
joinOn: joinOn2,
|
|
982
989
|
nestedQueryRelation: relation
|
|
983
990
|
});
|
|
984
|
-
const field =
|
|
991
|
+
const field = import_sql2.sql`${import_sql2.sql.identifier(relationTableAlias)}.${import_sql2.sql.identifier("data")}`.as(selectedRelationTsKey);
|
|
985
992
|
joins.push({
|
|
986
|
-
on:
|
|
993
|
+
on: import_sql2.sql`true`,
|
|
987
994
|
table: new import_subquery.Subquery(builtRelation.sql, {}, relationTableAlias),
|
|
988
995
|
alias: relationTableAlias,
|
|
989
996
|
joinType: "left",
|
|
@@ -1003,16 +1010,16 @@ class PgDialect {
|
|
|
1003
1010
|
throw new import_errors.DrizzleError({ message: `No fields selected for table "${tableConfig.tsName}" ("${tableAlias}")` });
|
|
1004
1011
|
}
|
|
1005
1012
|
let result;
|
|
1006
|
-
where = (0,
|
|
1013
|
+
where = (0, import_sql.and)(joinOn, where);
|
|
1007
1014
|
if (nestedQueryRelation) {
|
|
1008
|
-
let field =
|
|
1015
|
+
let field = import_sql2.sql`json_build_array(${import_sql2.sql.join(
|
|
1009
1016
|
selection.map(
|
|
1010
|
-
({ field: field2, tsKey, isJson }) => isJson ?
|
|
1017
|
+
({ field: field2, tsKey, isJson }) => isJson ? import_sql2.sql`${import_sql2.sql.identifier(`${tableAlias}_${tsKey}`)}.${import_sql2.sql.identifier("data")}` : (0, import_entity.is)(field2, import_sql2.SQL.Aliased) ? field2.sql : field2
|
|
1011
1018
|
),
|
|
1012
|
-
|
|
1019
|
+
import_sql2.sql`, `
|
|
1013
1020
|
)})`;
|
|
1014
1021
|
if ((0, import_entity.is)(nestedQueryRelation, import_relations.Many)) {
|
|
1015
|
-
field =
|
|
1022
|
+
field = import_sql2.sql`coalesce(json_agg(${field}${orderBy.length > 0 ? import_sql2.sql` order by ${import_sql2.sql.join(orderBy, import_sql2.sql`, `)}` : void 0}), '[]'::json)`;
|
|
1016
1023
|
}
|
|
1017
1024
|
const nestedSelection = [{
|
|
1018
1025
|
dbKey: "data",
|
|
@@ -1029,7 +1036,7 @@ class PgDialect {
|
|
|
1029
1036
|
fields: {},
|
|
1030
1037
|
fieldsFlat: [{
|
|
1031
1038
|
path: [],
|
|
1032
|
-
field:
|
|
1039
|
+
field: import_sql2.sql.raw("*")
|
|
1033
1040
|
}],
|
|
1034
1041
|
where,
|
|
1035
1042
|
limit,
|