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.
Files changed (159) hide show
  1. package/aws-data-api/pg/migrator.cjs +1 -1
  2. package/aws-data-api/pg/migrator.cjs.map +1 -1
  3. package/aws-data-api/pg/migrator.js +1 -1
  4. package/aws-data-api/pg/migrator.js.map +1 -1
  5. package/better-sqlite3/migrator.cjs +1 -1
  6. package/better-sqlite3/migrator.cjs.map +1 -1
  7. package/better-sqlite3/migrator.js +1 -1
  8. package/better-sqlite3/migrator.js.map +1 -1
  9. package/bun-sqlite/migrator.cjs +1 -1
  10. package/bun-sqlite/migrator.cjs.map +1 -1
  11. package/bun-sqlite/migrator.js +1 -1
  12. package/bun-sqlite/migrator.js.map +1 -1
  13. package/d1/migrator.cjs +1 -1
  14. package/d1/migrator.cjs.map +1 -1
  15. package/d1/migrator.js +1 -1
  16. package/d1/migrator.js.map +1 -1
  17. package/expo-sqlite/migrator.cjs.map +1 -1
  18. package/expo-sqlite/migrator.js.map +1 -1
  19. package/libsql/migrator.cjs +1 -1
  20. package/libsql/migrator.cjs.map +1 -1
  21. package/libsql/migrator.js +1 -1
  22. package/libsql/migrator.js.map +1 -1
  23. package/migrator.cjs.map +1 -1
  24. package/migrator.d.cts +1 -0
  25. package/migrator.d.ts +1 -0
  26. package/migrator.js.map +1 -1
  27. package/mysql-core/db.cjs +7 -1
  28. package/mysql-core/db.cjs.map +1 -1
  29. package/mysql-core/db.d.cts +2 -0
  30. package/mysql-core/db.d.ts +2 -0
  31. package/mysql-core/db.js +7 -1
  32. package/mysql-core/db.js.map +1 -1
  33. package/mysql-core/dialect.cjs +20 -16
  34. package/mysql-core/dialect.cjs.map +1 -1
  35. package/mysql-core/dialect.d.cts +4 -3
  36. package/mysql-core/dialect.d.ts +4 -3
  37. package/mysql-core/dialect.js +20 -16
  38. package/mysql-core/dialect.js.map +1 -1
  39. package/mysql-core/query-builders/delete.cjs +2 -2
  40. package/mysql-core/query-builders/delete.cjs.map +1 -1
  41. package/mysql-core/query-builders/delete.d.cts +3 -1
  42. package/mysql-core/query-builders/delete.d.ts +3 -1
  43. package/mysql-core/query-builders/delete.js +2 -2
  44. package/mysql-core/query-builders/delete.js.map +1 -1
  45. package/mysql-core/query-builders/update.cjs +5 -4
  46. package/mysql-core/query-builders/update.cjs.map +1 -1
  47. package/mysql-core/query-builders/update.d.cts +5 -2
  48. package/mysql-core/query-builders/update.d.ts +5 -2
  49. package/mysql-core/query-builders/update.js +5 -4
  50. package/mysql-core/query-builders/update.js.map +1 -1
  51. package/neon-http/migrator.cjs +6 -4
  52. package/neon-http/migrator.cjs.map +1 -1
  53. package/neon-http/migrator.js +6 -4
  54. package/neon-http/migrator.js.map +1 -1
  55. package/neon-serverless/migrator.cjs +1 -1
  56. package/neon-serverless/migrator.cjs.map +1 -1
  57. package/neon-serverless/migrator.js +1 -1
  58. package/neon-serverless/migrator.js.map +1 -1
  59. package/node-postgres/migrator.cjs +1 -1
  60. package/node-postgres/migrator.cjs.map +1 -1
  61. package/node-postgres/migrator.js +1 -1
  62. package/node-postgres/migrator.js.map +1 -1
  63. package/package.json +2 -2
  64. package/pg-core/db.cjs +28 -1
  65. package/pg-core/db.cjs.map +1 -1
  66. package/pg-core/db.d.cts +11 -0
  67. package/pg-core/db.d.ts +11 -0
  68. package/pg-core/db.js +28 -1
  69. package/pg-core/db.js.map +1 -1
  70. package/pg-core/dialect.cjs +113 -106
  71. package/pg-core/dialect.cjs.map +1 -1
  72. package/pg-core/dialect.d.cts +6 -5
  73. package/pg-core/dialect.d.ts +6 -5
  74. package/pg-core/dialect.js +31 -24
  75. package/pg-core/dialect.js.map +1 -1
  76. package/pg-core/query-builders/delete.cjs +2 -2
  77. package/pg-core/query-builders/delete.cjs.map +1 -1
  78. package/pg-core/query-builders/delete.d.cts +3 -1
  79. package/pg-core/query-builders/delete.d.ts +3 -1
  80. package/pg-core/query-builders/delete.js +2 -2
  81. package/pg-core/query-builders/delete.js.map +1 -1
  82. package/pg-core/query-builders/insert.cjs +5 -4
  83. package/pg-core/query-builders/insert.cjs.map +1 -1
  84. package/pg-core/query-builders/insert.d.cts +5 -2
  85. package/pg-core/query-builders/insert.d.ts +5 -2
  86. package/pg-core/query-builders/insert.js +5 -4
  87. package/pg-core/query-builders/insert.js.map +1 -1
  88. package/pg-core/query-builders/update.cjs +6 -4
  89. package/pg-core/query-builders/update.cjs.map +1 -1
  90. package/pg-core/query-builders/update.d.cts +5 -2
  91. package/pg-core/query-builders/update.d.ts +5 -2
  92. package/pg-core/query-builders/update.js +6 -4
  93. package/pg-core/query-builders/update.js.map +1 -1
  94. package/postgres-js/migrator.cjs +1 -1
  95. package/postgres-js/migrator.cjs.map +1 -1
  96. package/postgres-js/migrator.js +1 -1
  97. package/postgres-js/migrator.js.map +1 -1
  98. package/sql-js/migrator.cjs +1 -1
  99. package/sql-js/migrator.cjs.map +1 -1
  100. package/sql-js/migrator.js +1 -1
  101. package/sql-js/migrator.js.map +1 -1
  102. package/sqlite-core/db.cjs +3 -3
  103. package/sqlite-core/db.cjs.map +1 -1
  104. package/sqlite-core/db.js +3 -3
  105. package/sqlite-core/db.js.map +1 -1
  106. package/sqlite-core/dialect.cjs +110 -103
  107. package/sqlite-core/dialect.cjs.map +1 -1
  108. package/sqlite-core/dialect.d.cts +7 -6
  109. package/sqlite-core/dialect.d.ts +7 -6
  110. package/sqlite-core/dialect.js +34 -27
  111. package/sqlite-core/dialect.js.map +1 -1
  112. package/sqlite-core/query-builders/delete.cjs +2 -2
  113. package/sqlite-core/query-builders/delete.cjs.map +1 -1
  114. package/sqlite-core/query-builders/delete.d.cts +3 -1
  115. package/sqlite-core/query-builders/delete.d.ts +3 -1
  116. package/sqlite-core/query-builders/delete.js +2 -2
  117. package/sqlite-core/query-builders/delete.js.map +1 -1
  118. package/sqlite-core/query-builders/insert.cjs +5 -4
  119. package/sqlite-core/query-builders/insert.cjs.map +1 -1
  120. package/sqlite-core/query-builders/insert.d.cts +5 -2
  121. package/sqlite-core/query-builders/insert.d.ts +5 -2
  122. package/sqlite-core/query-builders/insert.js +5 -4
  123. package/sqlite-core/query-builders/insert.js.map +1 -1
  124. package/sqlite-core/query-builders/raw.cjs +1 -1
  125. package/sqlite-core/query-builders/raw.cjs.map +1 -1
  126. package/sqlite-core/query-builders/raw.d.cts +6 -1
  127. package/sqlite-core/query-builders/raw.d.ts +6 -1
  128. package/sqlite-core/query-builders/raw.js +1 -1
  129. package/sqlite-core/query-builders/raw.js.map +1 -1
  130. package/sqlite-core/query-builders/update.cjs +11 -4
  131. package/sqlite-core/query-builders/update.cjs.map +1 -1
  132. package/sqlite-core/query-builders/update.d.cts +5 -2
  133. package/sqlite-core/query-builders/update.d.ts +5 -2
  134. package/sqlite-core/query-builders/update.js +11 -4
  135. package/sqlite-core/query-builders/update.js.map +1 -1
  136. package/sqlite-proxy/driver.cjs +30 -10
  137. package/sqlite-proxy/driver.cjs.map +1 -1
  138. package/sqlite-proxy/driver.d.cts +14 -1
  139. package/sqlite-proxy/driver.d.ts +14 -1
  140. package/sqlite-proxy/driver.js +30 -14
  141. package/sqlite-proxy/driver.js.map +1 -1
  142. package/sqlite-proxy/migrator.cjs +4 -3
  143. package/sqlite-proxy/migrator.cjs.map +1 -1
  144. package/sqlite-proxy/migrator.js +4 -3
  145. package/sqlite-proxy/migrator.js.map +1 -1
  146. package/sqlite-proxy/session.cjs +79 -21
  147. package/sqlite-proxy/session.cjs.map +1 -1
  148. package/sqlite-proxy/session.d.cts +20 -8
  149. package/sqlite-proxy/session.d.ts +20 -8
  150. package/sqlite-proxy/session.js +79 -21
  151. package/sqlite-proxy/session.js.map +1 -1
  152. package/vercel-postgres/migrator.cjs +1 -1
  153. package/vercel-postgres/migrator.cjs.map +1 -1
  154. package/vercel-postgres/migrator.js +1 -1
  155. package/vercel-postgres/migrator.js.map +1 -1
  156. package/version.cjs +1 -1
  157. package/version.d.cts +1 -1
  158. package/version.d.ts +1 -1
  159. package/version.js +1 -1
@@ -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/sql.cjs");
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 migrationTableCreate = import_sql.sql`
42
- CREATE TABLE IF NOT EXISTS "drizzle"."__drizzle_migrations" (
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(import_sql.sql`CREATE SCHEMA IF NOT EXISTS "drizzle"`);
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
- import_sql.sql`select id, hash, created_at from "drizzle"."__drizzle_migrations" order by created_at desc limit 1`
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(import_sql.sql.raw(stmt));
60
+ await tx.execute(import_sql2.sql.raw(stmt));
59
61
  }
60
62
  await tx.execute(
61
- import_sql.sql`insert into "drizzle"."__drizzle_migrations" ("hash", "created_at") values(${migration.hash}, ${migration.folderMillis})`
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
- buildDeleteQuery({ table, where, returning }) {
77
- const returningSql = returning ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
78
- const whereSql = where ? import_sql.sql` where ${where}` : void 0;
79
- return import_sql.sql`delete from ${table}${whereSql}${returningSql}`;
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 import_sql.sql.join(
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 = import_sql.sql`${import_sql.sql.identifier(col.name)} = ${value}`;
103
+ const res = import_sql2.sql`${import_sql2.sql.identifier(col.name)} = ${value}`;
88
104
  if (i < setSize - 1) {
89
- return [res, import_sql.sql.raw(", ")];
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 ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
98
- const whereSql = where ? import_sql.sql` where ${where}` : void 0;
99
- return import_sql.sql`update ${table} set ${setSql}${whereSql}${returningSql}`;
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, import_sql.SQL.Aliased) && field.isSelectionField) {
117
- chunk.push(import_sql.sql.identifier(field.fieldAlias));
118
- } else if ((0, import_entity.is)(field, import_sql.SQL.Aliased) || (0, import_entity.is)(field, import_sql.SQL)) {
119
- const query = (0, import_entity.is)(field, import_sql.SQL.Aliased) ? field.sql : 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 import_sql.SQL(
139
+ new import_sql2.SQL(
123
140
  query.queryChunks.map((c) => {
124
141
  if ((0, import_entity.is)(c, import_columns.PgColumn)) {
125
- return import_sql.sql.identifier(c.name);
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, import_sql.SQL.Aliased)) {
135
- chunk.push(import_sql.sql` as ${import_sql.sql.identifier(field.fieldAlias)}`);
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(import_sql.sql.identifier(field.name));
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(import_sql.sql`, `);
162
+ chunk.push(import_sql2.sql`, `);
146
163
  }
147
164
  return chunk;
148
165
  });
149
- return import_sql.sql.join(chunks);
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, import_sql.SQL) ? void 0 : (0, import_table2.getTableName)(table)) && !((table2) => joins?.some(
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
- let withSql;
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 ? import_sql.sql` distinct` : import_sql.sql` distinct on (${import_sql.sql.join(distinct.on, import_sql.sql`, `)})`;
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 = import_sql.sql`${import_sql.sql.identifier(table[import_table2.Table.Symbol.OriginalName])}`;
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 = import_sql.sql`${import_sql.sql.identifier(table[import_table2.Table.Symbol.Schema])}.${fullName}`;
206
+ fullName = import_sql2.sql`${import_sql2.sql.identifier(table[import_table2.Table.Symbol.Schema])}.${fullName}`;
201
207
  }
202
- return import_sql.sql`${fullName} ${import_sql.sql.identifier(table[import_table2.Table.Symbol.Name])}`;
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(import_sql.sql` `);
216
+ joinsArray.push(import_sql2.sql` `);
211
217
  }
212
218
  const table2 = joinMeta.table;
213
- const lateralSql = joinMeta.lateral ? import_sql.sql` lateral` : void 0;
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
- import_sql.sql`${import_sql.sql.raw(joinMeta.joinType)} join${lateralSql} ${tableSchema ? import_sql.sql`${import_sql.sql.identifier(tableSchema)}.` : void 0}${import_sql.sql.identifier(origTableName)}${alias && import_sql.sql` ${import_sql.sql.identifier(alias)}`} on ${joinMeta.on}`
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, import_sql2.View)) {
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
- import_sql.sql`${import_sql.sql.raw(joinMeta.joinType)} join${lateralSql} ${viewSchema ? import_sql.sql`${import_sql.sql.identifier(viewSchema)}.` : void 0}${import_sql.sql.identifier(origViewName)}${alias && import_sql.sql` ${import_sql.sql.identifier(alias)}`} on ${joinMeta.on}`
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
- import_sql.sql`${import_sql.sql.raw(joinMeta.joinType)} join${lateralSql} ${table2} on ${joinMeta.on}`
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(import_sql.sql` `);
242
+ joinsArray.push(import_sql2.sql` `);
237
243
  }
238
244
  }
239
245
  }
240
- const joinsSql = import_sql.sql.join(joinsArray);
241
- const whereSql = where ? import_sql.sql` where ${where}` : void 0;
242
- const havingSql = having ? import_sql.sql` having ${having}` : void 0;
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 = import_sql.sql` order by ${import_sql.sql.join(orderBy, import_sql.sql`, `)}`;
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 = import_sql.sql` group by ${import_sql.sql.join(groupBy, import_sql.sql`, `)}`;
255
+ groupBySql = import_sql2.sql` group by ${import_sql2.sql.join(groupBy, import_sql2.sql`, `)}`;
250
256
  }
251
- const limitSql = limit ? import_sql.sql` limit ${limit}` : void 0;
252
- const offsetSql = offset ? import_sql.sql` offset ${offset}` : void 0;
253
- const lockingClauseSql = import_sql.sql.empty();
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 = import_sql.sql` for ${import_sql.sql.raw(lockingClause.strength)}`;
261
+ const clauseSql = import_sql2.sql` for ${import_sql2.sql.raw(lockingClause.strength)}`;
256
262
  if (lockingClause.config.of) {
257
263
  clauseSql.append(
258
- import_sql.sql` of ${import_sql.sql.join(
264
+ import_sql2.sql` of ${import_sql2.sql.join(
259
265
  Array.isArray(lockingClause.config.of) ? lockingClause.config.of : [lockingClause.config.of],
260
- import_sql.sql`, `
266
+ import_sql2.sql`, `
261
267
  )}`
262
268
  );
263
269
  }
264
270
  if (lockingClause.config.noWait) {
265
- clauseSql.append(import_sql.sql` no wait`);
271
+ clauseSql.append(import_sql2.sql` no wait`);
266
272
  } else if (lockingClause.config.skipLocked) {
267
- clauseSql.append(import_sql.sql` skip locked`);
273
+ clauseSql.append(import_sql2.sql` skip locked`);
268
274
  }
269
275
  lockingClauseSql.append(clauseSql);
270
276
  }
271
- const finalQuery = import_sql.sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}${lockingClauseSql}`;
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 = import_sql.sql`(${leftSelect.getSQL()}) `;
295
- const rightChunk = import_sql.sql`(${rightSelect.getSQL()})`;
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(import_sql.sql.identifier(singleOrderBy.name));
302
- } else if ((0, import_entity.is)(singleOrderBy, import_sql.SQL)) {
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] = import_sql.sql.identifier(chunk.name);
312
+ singleOrderBy.queryChunks[i] = import_sql2.sql.identifier(chunk.name);
307
313
  }
308
314
  }
309
- orderByValues.push(import_sql.sql`${singleOrderBy}`);
315
+ orderByValues.push(import_sql2.sql`${singleOrderBy}`);
310
316
  } else {
311
- orderByValues.push(import_sql.sql`${singleOrderBy}`);
317
+ orderByValues.push(import_sql2.sql`${singleOrderBy}`);
312
318
  }
313
319
  }
314
- orderBySql = import_sql.sql` order by ${import_sql.sql.join(orderByValues, import_sql.sql`, `)} `;
320
+ orderBySql = import_sql2.sql` order by ${import_sql2.sql.join(orderByValues, import_sql2.sql`, `)} `;
315
321
  }
316
- const limitSql = limit ? import_sql.sql` limit ${limit}` : void 0;
317
- const operatorChunk = import_sql.sql.raw(`${type} ${isAll ? "all " : ""}`);
318
- const offsetSql = offset ? import_sql.sql` offset ${offset}` : void 0;
319
- return import_sql.sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
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]) => import_sql.sql.identifier(column.name));
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, import_sql.Param) && colValue.value === void 0) {
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, import_sql.SQL) ? defaultFnResult : import_sql.sql.param(defaultFnResult, col);
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(import_sql.sql`default`);
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(import_sql.sql`, `);
350
+ valuesSqlList.push(import_sql2.sql`, `);
345
351
  }
346
352
  }
347
- const valuesSql = import_sql.sql.join(valuesSqlList);
348
- const returningSql = returning ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
349
- const onConflictSql = onConflict ? import_sql.sql` on conflict ${onConflict}` : void 0;
350
- return import_sql.sql`insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
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 ? import_sql.sql` concurrently` : void 0;
354
- const withNoDataSql = withNoData ? import_sql.sql` with no data` : void 0;
355
- return import_sql.sql`refresh materialized view${concurrentlySql} ${view}${withNoDataSql}`;
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: import_sql.sql }) : config.extras;
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, import_sql.SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
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, import_sql2.and)(
972
+ const joinOn2 = (0, import_sql.and)(
966
973
  ...normalizedRelation.fields.map(
967
- (field2, i) => (0, import_sql2.eq)(
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 = import_sql.sql`${import_sql.sql.identifier(relationTableAlias)}.${import_sql.sql.identifier("data")}`.as(selectedRelationTsKey);
991
+ const field = import_sql2.sql`${import_sql2.sql.identifier(relationTableAlias)}.${import_sql2.sql.identifier("data")}`.as(selectedRelationTsKey);
985
992
  joins.push({
986
- on: import_sql.sql`true`,
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, import_sql2.and)(joinOn, where);
1013
+ where = (0, import_sql.and)(joinOn, where);
1007
1014
  if (nestedQueryRelation) {
1008
- let field = import_sql.sql`json_build_array(${import_sql.sql.join(
1015
+ let field = import_sql2.sql`json_build_array(${import_sql2.sql.join(
1009
1016
  selection.map(
1010
- ({ field: field2, tsKey, isJson }) => isJson ? import_sql.sql`${import_sql.sql.identifier(`${tableAlias}_${tsKey}`)}.${import_sql.sql.identifier("data")}` : (0, import_entity.is)(field2, import_sql.SQL.Aliased) ? field2.sql : field2
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
- import_sql.sql`, `
1019
+ import_sql2.sql`, `
1013
1020
  )})`;
1014
1021
  if ((0, import_entity.is)(nestedQueryRelation, import_relations.Many)) {
1015
- field = import_sql.sql`coalesce(json_agg(${field}${orderBy.length > 0 ? import_sql.sql` order by ${import_sql.sql.join(orderBy, import_sql.sql`, `)}` : void 0}), '[]'::json)`;
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: import_sql.sql.raw("*")
1039
+ field: import_sql2.sql.raw("*")
1033
1040
  }],
1034
1041
  where,
1035
1042
  limit,