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.

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,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/sql.cjs");
32
- var import_sql2 = require("../sql/index.cjs");
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
- buildDeleteQuery({ table, where, returning }) {
52
- const returningSql = returning ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
53
- const whereSql = where ? import_sql.sql` where ${where}` : void 0;
54
- return import_sql.sql`delete from ${table}${whereSql}${returningSql}`;
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 import_sql.sql.join(
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 = import_sql.sql`${import_sql.sql.identifier(col.name)} = ${value}`;
76
+ const res = import_sql2.sql`${import_sql2.sql.identifier(col.name)} = ${value}`;
63
77
  if (i < setSize - 1) {
64
- return [res, import_sql.sql.raw(", ")];
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 ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
73
- const whereSql = where ? import_sql.sql` where ${where}` : void 0;
74
- return import_sql.sql`update ${table} set ${setSql}${whereSql}${returningSql}`;
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, import_sql.SQL.Aliased) && field.isSelectionField) {
92
- chunk.push(import_sql.sql.identifier(field.fieldAlias));
93
- } else if ((0, import_entity.is)(field, import_sql.SQL.Aliased) || (0, import_entity.is)(field, import_sql.SQL)) {
94
- const query = (0, import_entity.is)(field, import_sql.SQL.Aliased) ? field.sql : 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 import_sql.SQL(
112
+ new import_sql2.SQL(
98
113
  query.queryChunks.map((c) => {
99
114
  if ((0, import_entity.is)(c, import_column.Column)) {
100
- return import_sql.sql.identifier(c.name);
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, import_sql.SQL.Aliased)) {
110
- chunk.push(import_sql.sql` as ${import_sql.sql.identifier(field.fieldAlias)}`);
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(import_sql.sql.identifier(columnName));
131
+ chunk.push(import_sql2.sql.identifier(columnName));
117
132
  } else {
118
- chunk.push(import_sql.sql`${import_sql.sql.identifier(tableName)}.${import_sql.sql.identifier(columnName)}`);
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(import_sql.sql`, `);
137
+ chunk.push(import_sql2.sql`, `);
123
138
  }
124
139
  return chunk;
125
140
  });
126
- return import_sql.sql.join(chunks);
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, import_sql.SQL) ? void 0 : (0, import_table2.getTableName)(table)) && !((table2) => joins?.some(
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
- let withSql;
156
- if (withList?.length) {
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 import_sql.sql`${import_sql.sql.identifier(table[import_table2.Table.Symbol.OriginalName])} ${import_sql.sql.identifier(table[import_table2.Table.Symbol.Name])}`;
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(import_sql.sql` `);
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
- import_sql.sql`${import_sql.sql.raw(joinMeta.joinType)} join ${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}`
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
- import_sql.sql`${import_sql.sql.raw(joinMeta.joinType)} join ${table2} on ${joinMeta.on}`
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(import_sql.sql` `);
200
+ joinsArray.push(import_sql2.sql` `);
197
201
  }
198
202
  }
199
203
  }
200
- const joinsSql = import_sql.sql.join(joinsArray);
201
- const whereSql = where ? import_sql.sql` where ${where}` : void 0;
202
- const havingSql = having ? import_sql.sql` having ${having}` : void 0;
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(import_sql.sql`, `);
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(import_sql.sql`, `);
221
+ groupByList.push(import_sql2.sql`, `);
218
222
  }
219
223
  }
220
224
  }
221
- const groupBySql = groupByList.length > 0 ? import_sql.sql` group by ${import_sql.sql.join(groupByList)}` : void 0;
222
- const orderBySql = orderByList.length > 0 ? import_sql.sql` order by ${import_sql.sql.join(orderByList)}` : void 0;
223
- const limitSql = limit ? import_sql.sql` limit ${limit}` : void 0;
224
- const offsetSql = offset ? import_sql.sql` offset ${offset}` : void 0;
225
- const finalQuery = import_sql.sql`${withSql}select${distinctSql} ${selection} from ${tableSql}${joinsSql}${whereSql}${groupBySql}${havingSql}${orderBySql}${limitSql}${offsetSql}`;
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 = import_sql.sql`${leftSelect.getSQL()} `;
249
- const rightChunk = import_sql.sql`${rightSelect.getSQL()}`;
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(import_sql.sql.identifier(singleOrderBy.name));
256
- } else if ((0, import_entity.is)(singleOrderBy, import_sql.SQL)) {
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] = import_sql.sql.identifier(chunk.name);
264
+ singleOrderBy.queryChunks[i] = import_sql2.sql.identifier(chunk.name);
261
265
  }
262
266
  }
263
- orderByValues.push(import_sql.sql`${singleOrderBy}`);
267
+ orderByValues.push(import_sql2.sql`${singleOrderBy}`);
264
268
  } else {
265
- orderByValues.push(import_sql.sql`${singleOrderBy}`);
269
+ orderByValues.push(import_sql2.sql`${singleOrderBy}`);
266
270
  }
267
271
  }
268
- orderBySql = import_sql.sql` order by ${import_sql.sql.join(orderByValues, import_sql.sql`, `)}`;
272
+ orderBySql = import_sql2.sql` order by ${import_sql2.sql.join(orderByValues, import_sql2.sql`, `)}`;
269
273
  }
270
- const limitSql = limit ? import_sql.sql` limit ${limit}` : void 0;
271
- const operatorChunk = import_sql.sql.raw(`${type} ${isAll ? "all " : ""}`);
272
- const offsetSql = offset ? import_sql.sql` offset ${offset}` : void 0;
273
- return import_sql.sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
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]) => import_sql.sql.identifier(column.name));
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, import_sql.Param) && colValue.value === void 0) {
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, import_sql.SQL) ? col.default : import_sql.sql.param(col.default, col);
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, import_sql.SQL) ? defaultFnResult : import_sql.sql.param(defaultFnResult, col);
294
+ defaultValue = (0, import_entity.is)(defaultFnResult, import_sql2.SQL) ? defaultFnResult : import_sql2.sql.param(defaultFnResult, col);
291
295
  } else {
292
- defaultValue = import_sql.sql`null`;
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(import_sql.sql`, `);
305
+ valuesSqlList.push(import_sql2.sql`, `);
302
306
  }
303
307
  }
304
- const valuesSql = import_sql.sql.join(valuesSqlList);
305
- const returningSql = returning ? import_sql.sql` returning ${this.buildSelection(returning, { isSingleTable: true })}` : void 0;
306
- const onConflictSql = onConflict ? import_sql.sql` on conflict ${onConflict}` : void 0;
307
- return import_sql.sql`insert into ${table} ${insertOrder} values ${valuesSql}${onConflictSql}${returningSql}`;
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: import_sql.sql }) : config.extras;
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, import_sql.SQL.Aliased) ? value.fieldAlias : tableConfig.columns[tsKey].name,
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, import_sql2.and)(
423
+ const joinOn2 = (0, import_sql.and)(
419
424
  ...normalizedRelation.fields.map(
420
- (field2, i) => (0, import_sql2.eq)(
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 = import_sql.sql`(${builtRelation.sql})`.as(selectedRelationTsKey);
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, import_sql2.and)(joinOn, where);
459
+ where = (0, import_sql.and)(joinOn, where);
455
460
  if (nestedQueryRelation) {
456
- let field = import_sql.sql`json_array(${import_sql.sql.join(
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) ? import_sql.sql.identifier(field2.name) : (0, import_entity.is)(field2, import_sql.SQL.Aliased) ? field2.sql : field2
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
- import_sql.sql`, `
465
+ import_sql2.sql`, `
461
466
  )})`;
462
467
  if ((0, import_entity.is)(nestedQueryRelation, import_relations.Many)) {
463
- field = import_sql.sql`coalesce(json_group_array(${field}), json_array())`;
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: import_sql.sql.raw("*")
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 migrationTableCreate = import_sql.sql`
538
- CREATE TABLE IF NOT EXISTS "__drizzle_migrations" (
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
- import_sql.sql`SELECT id, hash, created_at FROM "__drizzle_migrations" ORDER BY created_at DESC LIMIT 1`
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(import_sql.sql`BEGIN`);
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(import_sql.sql.raw(stmt));
560
+ session.run(import_sql2.sql.raw(stmt));
555
561
  }
556
562
  session.run(
557
- import_sql.sql`INSERT INTO "__drizzle_migrations" ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`
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(import_sql.sql`COMMIT`);
567
+ session.run(import_sql2.sql`COMMIT`);
562
568
  } catch (e) {
563
- session.run(import_sql.sql`ROLLBACK`);
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 migrationTableCreate = import_sql.sql`
572
- CREATE TABLE IF NOT EXISTS "__drizzle_migrations" (
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
- import_sql.sql`SELECT id, hash, created_at FROM "__drizzle_migrations" ORDER BY created_at DESC LIMIT 1`
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(import_sql.sql.raw(stmt));
594
+ await tx.run(import_sql2.sql.raw(stmt));
588
595
  }
589
596
  await tx.run(
590
- import_sql.sql`INSERT INTO "__drizzle_migrations" ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`
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
  }