drizzle-orm 1.0.0-rc.4-de6c356 → 1.0.0-rc.4-bfc7cd0
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/cockroach-core/db.cjs +5 -53
- package/cockroach-core/db.cjs.map +1 -1
- package/cockroach-core/db.d.cts +8 -4
- package/cockroach-core/db.d.ts +8 -4
- package/cockroach-core/db.js +5 -53
- package/cockroach-core/db.js.map +1 -1
- package/cockroach-core/dialect.cjs +2 -2
- package/cockroach-core/dialect.cjs.map +1 -1
- package/cockroach-core/dialect.d.cts +2 -1
- package/cockroach-core/dialect.d.ts +2 -1
- package/cockroach-core/dialect.js +2 -2
- package/cockroach-core/dialect.js.map +1 -1
- package/cockroach-core/index.d.cts +2 -2
- package/cockroach-core/index.d.ts +2 -2
- package/cockroach-core/query-builders/index.d.cts +2 -2
- package/cockroach-core/query-builders/index.d.ts +2 -2
- package/cockroach-core/query-builders/insert.cjs +7 -5
- package/cockroach-core/query-builders/insert.cjs.map +1 -1
- package/cockroach-core/query-builders/insert.d.cts +17 -13
- package/cockroach-core/query-builders/insert.d.ts +17 -13
- package/cockroach-core/query-builders/insert.js +7 -5
- package/cockroach-core/query-builders/insert.js.map +1 -1
- package/effect-core/defaults.d.cts +1 -1
- package/effect-core/defaults.d.ts +1 -1
- package/effect-d1/driver.d.cts +1 -1
- package/effect-d1/driver.d.ts +1 -1
- package/effect-libsql/driver.d.cts +1 -1
- package/effect-libsql/driver.d.ts +1 -1
- package/effect-mysql2/driver.d.cts +1 -1
- package/effect-mysql2/driver.d.ts +1 -1
- package/effect-pglite/driver.d.cts +1 -1
- package/effect-pglite/driver.d.ts +1 -1
- package/effect-postgres/driver.d.cts +1 -1
- package/effect-postgres/driver.d.ts +1 -1
- package/effect-sqlite-bun/driver.d.cts +1 -1
- package/effect-sqlite-bun/driver.d.ts +1 -1
- package/effect-sqlite-do/driver.d.cts +1 -1
- package/effect-sqlite-do/driver.d.ts +1 -1
- package/effect-sqlite-node/driver.d.cts +1 -1
- package/effect-sqlite-node/driver.d.ts +1 -1
- package/effect-sqlite-wasm/driver.d.cts +1 -1
- package/effect-sqlite-wasm/driver.d.ts +1 -1
- package/index.d.cts +3 -3
- package/index.d.ts +3 -3
- package/mssql-core/db.cjs +3 -22
- package/mssql-core/db.cjs.map +1 -1
- package/mssql-core/db.d.cts +7 -3
- package/mssql-core/db.d.ts +7 -3
- package/mssql-core/db.js +3 -22
- package/mssql-core/db.js.map +1 -1
- package/mssql-core/dialect.cjs +2 -2
- package/mssql-core/dialect.cjs.map +1 -1
- package/mssql-core/dialect.d.cts +2 -1
- package/mssql-core/dialect.d.ts +2 -1
- package/mssql-core/dialect.js +2 -2
- package/mssql-core/dialect.js.map +1 -1
- package/mssql-core/index.d.cts +2 -2
- package/mssql-core/index.d.ts +2 -2
- package/mssql-core/query-builders/index.d.cts +2 -2
- package/mssql-core/query-builders/index.d.ts +2 -2
- package/mssql-core/query-builders/insert.cjs +7 -5
- package/mssql-core/query-builders/insert.cjs.map +1 -1
- package/mssql-core/query-builders/insert.d.cts +10 -7
- package/mssql-core/query-builders/insert.d.ts +10 -7
- package/mssql-core/query-builders/insert.js +7 -5
- package/mssql-core/query-builders/insert.js.map +1 -1
- package/mysql-core/async/db.cjs +3 -22
- package/mysql-core/async/db.cjs.map +1 -1
- package/mysql-core/async/db.d.cts +7 -2
- package/mysql-core/async/db.d.ts +7 -2
- package/mysql-core/async/db.js +3 -22
- package/mysql-core/async/db.js.map +1 -1
- package/mysql-core/columns/custom.cjs.map +1 -1
- package/mysql-core/columns/custom.d.cts +2 -6
- package/mysql-core/columns/custom.d.ts +2 -6
- package/mysql-core/columns/custom.js.map +1 -1
- package/mysql-core/dialect.cjs +2 -2
- package/mysql-core/dialect.cjs.map +1 -1
- package/mysql-core/dialect.d.cts +1 -0
- package/mysql-core/dialect.d.ts +1 -0
- package/mysql-core/dialect.js +2 -2
- package/mysql-core/dialect.js.map +1 -1
- package/mysql-core/effect/db.cjs +3 -22
- package/mysql-core/effect/db.cjs.map +1 -1
- package/mysql-core/effect/db.d.cts +7 -2
- package/mysql-core/effect/db.d.ts +7 -2
- package/mysql-core/effect/db.js +3 -22
- package/mysql-core/effect/db.js.map +1 -1
- package/mysql-core/index.d.cts +2 -2
- package/mysql-core/index.d.ts +2 -2
- package/mysql-core/query-builders/index.d.cts +2 -2
- package/mysql-core/query-builders/index.d.ts +2 -2
- package/mysql-core/query-builders/insert.cjs +7 -5
- package/mysql-core/query-builders/insert.cjs.map +1 -1
- package/mysql-core/query-builders/insert.d.cts +17 -13
- package/mysql-core/query-builders/insert.d.ts +17 -13
- package/mysql-core/query-builders/insert.js +7 -5
- package/mysql-core/query-builders/insert.js.map +1 -1
- package/package.json +1 -1
- package/pg-core/async/db.cjs +5 -53
- package/pg-core/async/db.cjs.map +1 -1
- package/pg-core/async/db.d.cts +12 -3
- package/pg-core/async/db.d.ts +12 -3
- package/pg-core/async/db.js +5 -53
- package/pg-core/async/db.js.map +1 -1
- package/pg-core/columns/custom.cjs.map +1 -1
- package/pg-core/columns/custom.d.cts +2 -6
- package/pg-core/columns/custom.d.ts +2 -6
- package/pg-core/columns/custom.js.map +1 -1
- package/pg-core/dialect.cjs +2 -2
- package/pg-core/dialect.cjs.map +1 -1
- package/pg-core/dialect.d.cts +1 -0
- package/pg-core/dialect.d.ts +1 -0
- package/pg-core/dialect.js +2 -2
- package/pg-core/dialect.js.map +1 -1
- package/pg-core/effect/db.cjs +5 -53
- package/pg-core/effect/db.cjs.map +1 -1
- package/pg-core/effect/db.d.cts +12 -3
- package/pg-core/effect/db.d.ts +12 -3
- package/pg-core/effect/db.js +5 -53
- package/pg-core/effect/db.js.map +1 -1
- package/pg-core/index.d.cts +2 -2
- package/pg-core/index.d.ts +2 -2
- package/pg-core/query-builders/index.d.cts +2 -2
- package/pg-core/query-builders/index.d.ts +2 -2
- package/pg-core/query-builders/insert.cjs +11 -7
- package/pg-core/query-builders/insert.cjs.map +1 -1
- package/pg-core/query-builders/insert.d.cts +20 -16
- package/pg-core/query-builders/insert.d.ts +20 -16
- package/pg-core/query-builders/insert.js +11 -7
- package/pg-core/query-builders/insert.js.map +1 -1
- package/postgres/driver.cjs +3 -0
- package/postgres/driver.cjs.map +1 -1
- package/postgres/driver.d.cts +5 -1
- package/postgres/driver.d.ts +5 -1
- package/postgres/driver.js +3 -0
- package/postgres/driver.js.map +1 -1
- package/postgres/index.cjs +1 -0
- package/postgres/index.d.cts +2 -2
- package/postgres/index.d.ts +2 -2
- package/postgres/index.js +2 -2
- package/postgres/session.cjs +36 -1
- package/postgres/session.cjs.map +1 -1
- package/postgres/session.d.cts +13 -2
- package/postgres/session.d.ts +13 -2
- package/postgres/session.js +36 -2
- package/postgres/session.js.map +1 -1
- package/postgres/shape.cjs +27 -16
- package/postgres/shape.cjs.map +1 -1
- package/postgres/shape.d.cts +3 -3
- package/postgres/shape.d.ts +3 -3
- package/postgres/shape.js +28 -17
- package/postgres/shape.js.map +1 -1
- package/singlestore-core/db.cjs +3 -22
- package/singlestore-core/db.cjs.map +1 -1
- package/singlestore-core/db.d.cts +7 -2
- package/singlestore-core/db.d.ts +7 -2
- package/singlestore-core/db.js +3 -22
- package/singlestore-core/db.js.map +1 -1
- package/singlestore-core/dialect.cjs +2 -2
- package/singlestore-core/dialect.cjs.map +1 -1
- package/singlestore-core/dialect.d.cts +2 -1
- package/singlestore-core/dialect.d.ts +2 -1
- package/singlestore-core/dialect.js +2 -2
- package/singlestore-core/dialect.js.map +1 -1
- package/singlestore-core/index.d.cts +2 -2
- package/singlestore-core/index.d.ts +2 -2
- package/singlestore-core/query-builders/index.d.cts +2 -2
- package/singlestore-core/query-builders/index.d.ts +2 -2
- package/singlestore-core/query-builders/insert.cjs +6 -4
- package/singlestore-core/query-builders/insert.cjs.map +1 -1
- package/singlestore-core/query-builders/insert.d.cts +11 -7
- package/singlestore-core/query-builders/insert.d.ts +11 -7
- package/singlestore-core/query-builders/insert.js +6 -4
- package/singlestore-core/query-builders/insert.js.map +1 -1
- package/sqlite-core/async/db.cjs +5 -53
- package/sqlite-core/async/db.cjs.map +1 -1
- package/sqlite-core/async/db.d.cts +8 -3
- package/sqlite-core/async/db.d.ts +8 -3
- package/sqlite-core/async/db.js +5 -53
- package/sqlite-core/async/db.js.map +1 -1
- package/sqlite-core/async/insert.cjs.map +1 -1
- package/sqlite-core/async/insert.d.cts +1 -1
- package/sqlite-core/async/insert.d.ts +1 -1
- package/sqlite-core/async/insert.js.map +1 -1
- package/sqlite-core/dialect.cjs +2 -2
- package/sqlite-core/dialect.cjs.map +1 -1
- package/sqlite-core/dialect.d.cts +2 -1
- package/sqlite-core/dialect.d.ts +2 -1
- package/sqlite-core/dialect.js +2 -2
- package/sqlite-core/dialect.js.map +1 -1
- package/sqlite-core/effect/db.cjs +5 -53
- package/sqlite-core/effect/db.cjs.map +1 -1
- package/sqlite-core/effect/db.d.cts +8 -2
- package/sqlite-core/effect/db.d.ts +8 -2
- package/sqlite-core/effect/db.js +5 -53
- package/sqlite-core/effect/db.js.map +1 -1
- package/sqlite-core/effect/insert.cjs.map +1 -1
- package/sqlite-core/effect/insert.d.cts +1 -1
- package/sqlite-core/effect/insert.d.ts +1 -1
- package/sqlite-core/effect/insert.js.map +1 -1
- package/sqlite-core/index.d.cts +2 -2
- package/sqlite-core/index.d.ts +2 -2
- package/sqlite-core/query-builders/index.d.cts +2 -2
- package/sqlite-core/query-builders/index.d.ts +2 -2
- package/sqlite-core/query-builders/insert.cjs +7 -5
- package/sqlite-core/query-builders/insert.cjs.map +1 -1
- package/sqlite-core/query-builders/insert.d.cts +17 -13
- package/sqlite-core/query-builders/insert.d.ts +17 -13
- package/sqlite-core/query-builders/insert.js +7 -5
- package/sqlite-core/query-builders/insert.js.map +1 -1
- package/table.cjs.map +1 -1
- package/table.d.cts +2 -1
- package/table.d.ts +2 -1
- package/table.js.map +1 -1
- package/utils.cjs +0 -4
- package/utils.cjs.map +1 -1
- package/utils.d.cts +6 -5
- package/utils.d.ts +6 -5
- package/utils.js +0 -4
- package/utils.js.map +1 -1
package/cockroach-core/db.cjs
CHANGED
|
@@ -160,32 +160,8 @@ var CockroachDatabase = class {
|
|
|
160
160
|
function update(table) {
|
|
161
161
|
return new __cockroach_core_query_builders_index_ts.CockroachUpdateBuilder(table, self.session, self.dialect, queries);
|
|
162
162
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
*
|
|
166
|
-
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
|
|
167
|
-
*
|
|
168
|
-
* See docs: {@link https://orm.drizzle.team/docs/insert}
|
|
169
|
-
*
|
|
170
|
-
* @param table The table to insert into.
|
|
171
|
-
*
|
|
172
|
-
* @example
|
|
173
|
-
*
|
|
174
|
-
* ```ts
|
|
175
|
-
* // Insert one row
|
|
176
|
-
* await db.insert(cars).values({ brand: 'BMW' });
|
|
177
|
-
*
|
|
178
|
-
* // Insert multiple rows
|
|
179
|
-
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
180
|
-
*
|
|
181
|
-
* // Insert with returning clause
|
|
182
|
-
* const insertedCar: Car[] = await db.insert(cars)
|
|
183
|
-
* .values({ brand: 'BMW' })
|
|
184
|
-
* .returning();
|
|
185
|
-
* ```
|
|
186
|
-
*/
|
|
187
|
-
function insert(table) {
|
|
188
|
-
return new __cockroach_core_query_builders_index_ts.CockroachInsertBuilder(table, self.session, self.dialect, queries);
|
|
163
|
+
function insert(table, ...columns) {
|
|
164
|
+
return new __cockroach_core_query_builders_index_ts.CockroachInsertBuilder(table, self.session, self.dialect, queries, columns.length ? columns : void 0);
|
|
189
165
|
}
|
|
190
166
|
/**
|
|
191
167
|
* Creates a delete query.
|
|
@@ -276,32 +252,8 @@ var CockroachDatabase = class {
|
|
|
276
252
|
update(table) {
|
|
277
253
|
return new __cockroach_core_query_builders_index_ts.CockroachUpdateBuilder(table, this.session, this.dialect);
|
|
278
254
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
*
|
|
282
|
-
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
|
|
283
|
-
*
|
|
284
|
-
* See docs: {@link https://orm.drizzle.team/docs/insert}
|
|
285
|
-
*
|
|
286
|
-
* @param table The table to insert into.
|
|
287
|
-
*
|
|
288
|
-
* @example
|
|
289
|
-
*
|
|
290
|
-
* ```ts
|
|
291
|
-
* // Insert one row
|
|
292
|
-
* await db.insert(cars).values({ brand: 'BMW' });
|
|
293
|
-
*
|
|
294
|
-
* // Insert multiple rows
|
|
295
|
-
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
296
|
-
*
|
|
297
|
-
* // Insert with returning clause
|
|
298
|
-
* const insertedCar: Car[] = await db.insert(cars)
|
|
299
|
-
* .values({ brand: 'BMW' })
|
|
300
|
-
* .returning();
|
|
301
|
-
* ```
|
|
302
|
-
*/
|
|
303
|
-
insert(table) {
|
|
304
|
-
return new __cockroach_core_query_builders_index_ts.CockroachInsertBuilder(table, this.session, this.dialect);
|
|
255
|
+
insert(table, ...columns) {
|
|
256
|
+
return new __cockroach_core_query_builders_index_ts.CockroachInsertBuilder(table, this.session, this.dialect, void 0, columns.length ? columns : void 0);
|
|
305
257
|
}
|
|
306
258
|
/**
|
|
307
259
|
* Creates a delete query.
|
|
@@ -352,7 +304,7 @@ const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(
|
|
|
352
304
|
const _with = (...args) => getReplica(replicas).with(...args);
|
|
353
305
|
const $with = (arg) => getReplica(replicas).$with(arg);
|
|
354
306
|
const update = (...args) => primary.update(...args);
|
|
355
|
-
const insert = (...args) => primary.insert(...args);
|
|
307
|
+
const insert = ((...args) => primary.insert(...args));
|
|
356
308
|
const $delete = (...args) => primary.delete(...args);
|
|
357
309
|
const execute = (...args) => primary.execute(...args);
|
|
358
310
|
const transaction = (...args) => primary.transaction(...args);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.cjs","names":["entityKind","RelationalQueryBuilder","QueryBuilder","WithSubquery","SelectionProxyHandler","CockroachCountBuilder","CockroachSelectBuilder","CockroachUpdateBuilder","CockroachInsertBuilder","CockroachDeleteBase","CockroachRefreshMaterializedView","sql","CockroachRaw"],"sources":["../../src/cockroach-core/db.ts"],"sourcesContent":["import type * as V1 from '~/_relations.ts';\nimport type { CockroachDialect } from '~/cockroach-core/dialect.ts';\nimport {\n\tCockroachDeleteBase,\n\tCockroachInsertBuilder,\n\tCockroachSelectBuilder,\n\tCockroachUpdateBuilder,\n\tQueryBuilder,\n} from '~/cockroach-core/query-builders/index.ts';\nimport type {\n\tCockroachQueryResultHKT,\n\tCockroachQueryResultKind,\n\tCockroachSession,\n\tCockroachTransaction,\n\tCockroachTransactionConfig,\n\tPreparedQueryConfig,\n} from '~/cockroach-core/session.ts';\nimport type { CockroachTable } from '~/cockroach-core/table.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\nimport { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport type { DrizzleTypeError, NeonAuthToken } from '~/utils.ts';\nimport type { CockroachColumn } from './columns/index.ts';\nimport { CockroachCountBuilder } from './query-builders/count.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport { CockroachRaw } from './query-builders/raw.ts';\nimport { CockroachRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type { WithBuilder } from './subquery.ts';\nimport type { CockroachViewBase } from './view-base.ts';\nimport type { CockroachMaterializedView } from './view.ts';\n\nexport class CockroachDatabase<\n\tTQueryResult extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'CockroachDatabase';\n\n\tdeclare readonly _: {\n\t\treadonly schema: TSchema | undefined;\n\t\treadonly fullSchema: TFullSchema;\n\t\treadonly tableNamesMap: Record<string, string>;\n\t\treadonly session: CockroachSession<TQueryResult, TFullSchema, TSchema>;\n\t};\n\n\t_query: TFullSchema extends Record<string, never>\n\t\t? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>\n\t\t: {\n\t\t\t[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: CockroachDialect,\n\t\t/** @internal */\n\t\treadonly session: CockroachSession<any, any, any>,\n\t\tschema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t) {\n\t\tthis._ = schema\n\t\t\t? {\n\t\t\t\tschema: schema.schema,\n\t\t\t\tfullSchema: schema.fullSchema as TFullSchema,\n\t\t\t\ttableNamesMap: schema.tableNamesMap,\n\t\t\t\tsession,\n\t\t\t}\n\t\t\t: {\n\t\t\t\tschema: undefined,\n\t\t\t\tfullSchema: {} as TFullSchema,\n\t\t\t\ttableNamesMap: {},\n\t\t\t\tsession,\n\t\t\t};\n\t\tthis._query = {} as typeof this['_query'];\n\t\tif (this._.schema) {\n\t\t\tfor (const [tableName, columns] of Object.entries(this._.schema)) {\n\t\t\t\t(this._query as CockroachDatabase<TQueryResult, Record<string, any>>['_query'])[tableName] =\n\t\t\t\t\tnew RelationalQueryBuilder(\n\t\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\t\tthis._.schema,\n\t\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\t\tschema!.fullSchema[tableName] as CockroachTable,\n\t\t\t\t\t\tcolumns,\n\t\t\t\t\t\tdialect,\n\t\t\t\t\t\tsession,\n\t\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Creates a subquery that defines a temporary named result set as a CTE.\n\t *\n\t * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param alias The alias for the subquery.\n\t *\n\t * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Create a subquery with alias 'sq' and use it in the select query\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t *\n\t * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:\n\t *\n\t * ```ts\n\t * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query\n\t * const sq = db.$with('sq').as(db.select({\n\t * name: sql<string>`upper(${users.name})`.as('name'),\n\t * })\n\t * .from(users));\n\t *\n\t * const result = await db.with(sq).select({ name: sq.name }).from(sq);\n\t * ```\n\t */\n\t$with: WithBuilder = (alias: string, selection?: ColumnsSelection) => {\n\t\tconst self = this;\n\t\tconst as = (\n\t\t\tqb:\n\t\t\t\t| TypedQueryBuilder<ColumnsSelection | undefined>\n\t\t\t\t| SQL\n\t\t\t\t| ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection | undefined> | SQL),\n\t\t) => {\n\t\t\tif (typeof qb === 'function') {\n\t\t\t\tqb = qb(new QueryBuilder(self.dialect));\n\t\t\t}\n\n\t\t\treturn new Proxy(\n\t\t\t\tnew WithSubquery(\n\t\t\t\t\tqb.getSQL(),\n\t\t\t\t\tselection ?? ('getSelectedFields' in qb ? qb.getSelectedFields() ?? {} : {}) as SelectedFields,\n\t\t\t\t\talias,\n\t\t\t\t\ttrue,\n\t\t\t\t),\n\t\t\t\tnew SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),\n\t\t\t);\n\t\t};\n\t\treturn { as };\n\t};\n\n\t$count(\n\t\tsource: CockroachTable | CockroachViewBase | SQL | SQLWrapper,\n\t\tfilters?: SQL<unknown>,\n\t) {\n\t\treturn new CockroachCountBuilder({ source, filters, session: this.session, dialect: this.dialect });\n\t}\n\n\t/**\n\t * Incorporates a previously defined CTE (using `$with`) into the main query.\n\t *\n\t * This method allows the main query to reference a temporary named result set.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param queries The CTEs to incorporate into the main query.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Define a subquery 'sq' as a CTE using $with\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * // Incorporate the CTE 'sq' into the main query and select from it\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t */\n\twith(...queries: WithSubquery[]) {\n\t\tconst self = this;\n\n\t\t/**\n\t\t * Creates a select query.\n\t\t *\n\t\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Select all columns and all rows from the 'cars' table\n\t\t * const allCars: Car[] = await db.select().from(cars);\n\t\t *\n\t\t * // Select specific columns and all rows from the 'cars' table\n\t\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * brand: cars.brand\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t *\n\t\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t\t *\n\t\t * ```ts\n\t\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t */\n\t\tfunction select(): CockroachSelectBuilder<undefined>;\n\t\tfunction select<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\t\tfunction select<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct` expression to the select query.\n\t\t *\n\t\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select all unique rows from the 'cars' table\n\t\t * await db.selectDistinct()\n\t\t * .from(cars)\n\t\t * .orderBy(cars.id, cars.brand, cars.color);\n\t\t *\n\t\t * // Select all unique brands from the 'cars' table\n\t\t * await db.selectDistinct({ brand: cars.brand })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinct(): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: true,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct on` expression to the select query.\n\t\t *\n\t\t * Calling this method will specify how the unique rows are determined.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param on The expression defining uniqueness.\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select the first row for each unique brand from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand])\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t *\n\t\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand, cars.color);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: { on },\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Creates an update query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t\t *\n\t\t * Use `.set()` method to specify which values to update.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t\t *\n\t\t * @param table The table to update.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Update all rows in the 'cars' table\n\t\t * await db.update(cars).set({ color: 'red' });\n\t\t *\n\t\t * // Update rows with filters and conditions\n\t\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t\t *\n\t\t * // Update with returning clause\n\t\t * const updatedCar: Car[] = await db.update(cars)\n\t\t * .set({ color: 'red' })\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction update<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachUpdateBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates an insert query.\n\t\t *\n\t\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t\t *\n\t\t * @param table The table to insert into.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Insert one row\n\t\t * await db.insert(cars).values({ brand: 'BMW' });\n\t\t *\n\t\t * // Insert multiple rows\n\t\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t\t *\n\t\t * // Insert with returning clause\n\t\t * const insertedCar: Car[] = await db.insert(cars)\n\t\t * .values({ brand: 'BMW' })\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction insert<TTable extends CockroachTable>(table: TTable): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachInsertBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates a delete query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t\t *\n\t\t * @param table The table to delete from.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Delete all rows in the 'cars' table\n\t\t * await db.delete(cars);\n\t\t *\n\t\t * // Delete rows with filters and conditions\n\t\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t\t *\n\t\t * // Delete with returning clause\n\t\t * const deletedCar: Car[] = await db.delete(cars)\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction delete_<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\t\treturn new CockroachDeleteBase(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\treturn { select, selectDistinct, selectDistinctOn, update, insert, delete: delete_ };\n\t}\n\n\t/**\n\t * Creates a select query.\n\t *\n\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Select all columns and all rows from the 'cars' table\n\t * const allCars: Car[] = await db.select().from(cars);\n\t *\n\t * // Select specific columns and all rows from the 'cars' table\n\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t * id: cars.id,\n\t * brand: cars.brand\n\t * })\n\t * .from(cars);\n\t * ```\n\t *\n\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t *\n\t * ```ts\n\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t * id: cars.id,\n\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t * })\n\t * .from(cars);\n\t * ```\n\t */\n\tselect(): CockroachSelectBuilder<undefined>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselect<TSelection extends SelectedFields>(fields?: TSelection): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct` expression to the select query.\n\t *\n\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select all unique rows from the 'cars' table\n\t * await db.selectDistinct()\n\t * .from(cars)\n\t * .orderBy(cars.id, cars.brand, cars.color);\n\t *\n\t * // Select all unique brands from the 'cars' table\n\t * await db.selectDistinct({ brand: cars.brand })\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t * ```\n\t */\n\tselectDistinct(): CockroachSelectBuilder<undefined>;\n\tselectDistinct<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselectDistinct<TSelection extends SelectedFields>(\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: true,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct on` expression to the select query.\n\t *\n\t * Calling this method will specify how the unique rows are determined.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param on The expression defining uniqueness.\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select the first row for each unique brand from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand])\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t *\n\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t * .from(cars)\n\t * .orderBy(cars.brand, cars.color);\n\t * ```\n\t */\n\tselectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields: TSelection,\n\t): CockroachSelectBuilder<TSelection>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: { on },\n\t\t});\n\t}\n\n\t/**\n\t * Creates an update query.\n\t *\n\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t *\n\t * Use `.set()` method to specify which values to update.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t *\n\t * @param table The table to update.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Update all rows in the 'cars' table\n\t * await db.update(cars).set({ color: 'red' });\n\t *\n\t * // Update rows with filters and conditions\n\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t *\n\t * // Update with returning clause\n\t * const updatedCar: Car[] = await db.update(cars)\n\t * .set({ color: 'red' })\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tupdate<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates an insert query.\n\t *\n\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t *\n\t * @param table The table to insert into.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Insert one row\n\t * await db.insert(cars).values({ brand: 'BMW' });\n\t *\n\t * // Insert multiple rows\n\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t *\n\t * // Insert with returning clause\n\t * const insertedCar: Car[] = await db.insert(cars)\n\t * .values({ brand: 'BMW' })\n\t * .returning();\n\t * ```\n\t */\n\tinsert<TTable extends CockroachTable>(table: TTable): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachInsertBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates a delete query.\n\t *\n\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t *\n\t * @param table The table to delete from.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Delete all rows in the 'cars' table\n\t * await db.delete(cars);\n\t *\n\t * // Delete rows with filters and conditions\n\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t *\n\t * // Delete with returning clause\n\t * const deletedCar: Car[] = await db.delete(cars)\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tdelete<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\treturn new CockroachDeleteBase(table, this.session, this.dialect);\n\t}\n\n\trefreshMaterializedView<TView extends CockroachMaterializedView>(\n\t\tview: TView,\n\t): CockroachRefreshMaterializedView<TQueryResult> {\n\t\treturn new CockroachRefreshMaterializedView(view, this.session, this.dialect);\n\t}\n\n\tprotected authToken?: NeonAuthToken;\n\n\texecute<TRow extends Record<string, unknown> = Record<string, unknown>>(\n\t\tquery: SQLWrapper | string,\n\t): CockroachRaw<CockroachQueryResultKind<TQueryResult, TRow>> {\n\t\tconst sequel = typeof query === 'string' ? sql.raw(query) : query.getSQL();\n\t\tconst builtQuery = this.dialect.sqlToQuery(sequel);\n\t\tconst prepared = this.session.prepareQuery<\n\t\t\tPreparedQueryConfig & { execute: CockroachQueryResultKind<TQueryResult, TRow> }\n\t\t>(\n\t\t\tbuiltQuery,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t);\n\t\treturn new CockroachRaw(\n\t\t\t() => prepared.execute(undefined, this.authToken),\n\t\t\tsequel,\n\t\t\tbuiltQuery,\n\t\t\t(result) => prepared.mapResult(result, true),\n\t\t);\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (tx: CockroachTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>,\n\t\tconfig?: CockroachTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type CockroachWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends V1.TablesRelationalConfig,\n\tQ extends CockroachDatabase<\n\t\tHKT,\n\t\tTFullSchema,\n\t\tTSchema extends Record<string, unknown> ? V1.ExtractTablesWithRelations<TFullSchema> : TSchema\n\t>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): CockroachWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);\n\tconst selectDistinctOn: Q['selectDistinctOn'] = (...args: [any]) => getReplica(replicas).selectDistinctOn(...args);\n\tconst $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);\n\tconst _with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);\n\tconst $with: Q['$with'] = (arg: any) => getReplica(replicas).$with(arg) as any;\n\n\tconst update: Q['update'] = (...args: [any]) => primary.update(...args);\n\tconst insert: Q['insert'] = (...args: [any]) => primary.insert(...args);\n\tconst $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);\n\tconst execute: Q['execute'] = (...args: [any]) => primary.execute(...args);\n\tconst transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);\n\tconst refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>\n\t\tprimary.refreshMaterializedView(...args);\n\n\treturn {\n\t\t...primary,\n\t\tupdate,\n\t\tinsert,\n\t\tdelete: $delete,\n\t\texecute,\n\t\ttransaction,\n\t\trefreshMaterializedView,\n\t\t$primary: primary,\n\t\t$replicas: replicas,\n\t\tselect,\n\t\tselectDistinct,\n\t\tselectDistinctOn,\n\t\t$count,\n\t\t$with,\n\t\twith: _with,\n\t\tget _query() {\n\t\t\treturn getReplica(replicas)._query;\n\t\t},\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;AAkCA,IAAa,oBAAb,MAIE;CACD,QAAiBA,0BAAsB;CASvC;CAMA,YAEC,AAAS,SAET,AAAS,SACT,QACC;EAJQ;EAEA;AAGT,OAAK,IAAI,SACN;GACD,QAAQ,OAAO;GACf,YAAY,OAAO;GACnB,eAAe,OAAO;GACtB;GACA,GACC;GACD,QAAQ;GACR,YAAY,EAAE;GACd,eAAe,EAAE;GACjB;GACA;AACF,OAAK,SAAS,EAAE;AAChB,MAAI,KAAK,EAAE,OACV,MAAK,MAAM,CAAC,WAAW,YAAY,OAAO,QAAQ,KAAK,EAAE,OAAO,CAC/D,CAAC,KAAK,OAA0E,aAC/E,IAAIC,mEACH,OAAQ,YACR,KAAK,EAAE,QACP,KAAK,EAAE,eACP,OAAQ,WAAW,YACnB,SACA,SACA,QACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCL,SAAsB,OAAe,cAAiC;EACrE,MAAM,OAAO;EACb,MAAM,MACL,OAII;AACJ,OAAI,OAAO,OAAO,WACjB,MAAK,GAAG,IAAIC,sDAAa,KAAK,QAAQ,CAAC;AAGxC,UAAO,IAAI,MACV,IAAIC,2BACH,GAAG,QAAQ,EACX,cAAc,uBAAuB,KAAK,GAAG,mBAAmB,IAAI,EAAE,GAAG,EAAE,GAC3E,OACA,KACA,EACD,IAAIC,2CAAsB;IAAE;IAAO,oBAAoB;IAAS,aAAa;IAAS,CAAC,CACvF;;AAEF,SAAO,EAAE,IAAI;;CAGd,OACC,QACA,SACC;AACD,SAAO,IAAIC,kEAAsB;GAAE;GAAQ;GAAS,SAAS,KAAK;GAAS,SAAS,KAAK;G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AAG,SAAgB,WAAW,SAAS,CAAC,OAAO,GAAG,KAAK;CACpF,MAAM,SAAoB,GAAG,SAAc,WAAW,SAAS,CAAC,KAAK,GAAG,KAAK;CAC7E,MAAM,SAAqB,QAAa,WAAW,SAAS,CAAC,MAAM,IAAI;CAEvE,MAAM,UAAuB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACvE,MAAM,UAAuB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACvE,MAAM,WAAwB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACxE,MAAM,WAAyB,GAAG,SAAgB,QAAQ,QAAQ,GAAG,KAAK;CAC1E,MAAM,eAAiC,GAAG,SAAgB,QAAQ,YAAY,GAAG,KAAK;CACtF,MAAM,2BAAyD,GAAG,SACjE,QAAQ,wBAAwB,GAAG,KAAK;AAEzC,QAAO;EACN,GAAG;EACH;EACA;EACA,QAAQ;EACR;EACA;EACA;EACA,UAAU;EACV,WAAW;EACX;EACA;EACA;EACA;EACA;EACA,MAAM;EACN,IAAI,SAAS;AACZ,UAAO,WAAW,SAAS,CAAC;;EAE7B"}
|
|
1
|
+
{"version":3,"file":"db.cjs","names":["entityKind","RelationalQueryBuilder","QueryBuilder","WithSubquery","SelectionProxyHandler","CockroachCountBuilder","CockroachSelectBuilder","CockroachUpdateBuilder","CockroachInsertBuilder","CockroachDeleteBase","CockroachRefreshMaterializedView","sql","CockroachRaw"],"sources":["../../src/cockroach-core/db.ts"],"sourcesContent":["import type * as V1 from '~/_relations.ts';\nimport type { CockroachDialect } from '~/cockroach-core/dialect.ts';\nimport {\n\tCockroachDeleteBase,\n\tCockroachInsertBuilder,\n\tCockroachSelectBuilder,\n\tCockroachUpdateBuilder,\n\ttype NoDuplicateColumns,\n\tQueryBuilder,\n} from '~/cockroach-core/query-builders/index.ts';\nimport type {\n\tCockroachQueryResultHKT,\n\tCockroachQueryResultKind,\n\tCockroachSession,\n\tCockroachTransaction,\n\tCockroachTransactionConfig,\n\tPreparedQueryConfig,\n} from '~/cockroach-core/session.ts';\nimport type { CockroachTable } from '~/cockroach-core/table.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\nimport { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport type { InferInsertModel, RequiredInsertKeys } from '~/table.ts';\nimport type { DrizzleTypeError, IsNever, JoinUnion, NeonAuthToken } from '~/utils.ts';\nimport type { CockroachColumn } from './columns/index.ts';\nimport { CockroachCountBuilder } from './query-builders/count.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport { CockroachRaw } from './query-builders/raw.ts';\nimport { CockroachRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type { WithBuilder } from './subquery.ts';\nimport type { CockroachViewBase } from './view-base.ts';\nimport type { CockroachMaterializedView } from './view.ts';\n\nexport class CockroachDatabase<\n\tTQueryResult extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'CockroachDatabase';\n\n\tdeclare readonly _: {\n\t\treadonly schema: TSchema | undefined;\n\t\treadonly fullSchema: TFullSchema;\n\t\treadonly tableNamesMap: Record<string, string>;\n\t\treadonly session: CockroachSession<TQueryResult, TFullSchema, TSchema>;\n\t};\n\n\t_query: TFullSchema extends Record<string, never>\n\t\t? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>\n\t\t: {\n\t\t\t[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: CockroachDialect,\n\t\t/** @internal */\n\t\treadonly session: CockroachSession<any, any, any>,\n\t\tschema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t) {\n\t\tthis._ = schema\n\t\t\t? {\n\t\t\t\tschema: schema.schema,\n\t\t\t\tfullSchema: schema.fullSchema as TFullSchema,\n\t\t\t\ttableNamesMap: schema.tableNamesMap,\n\t\t\t\tsession,\n\t\t\t}\n\t\t\t: {\n\t\t\t\tschema: undefined,\n\t\t\t\tfullSchema: {} as TFullSchema,\n\t\t\t\ttableNamesMap: {},\n\t\t\t\tsession,\n\t\t\t};\n\t\tthis._query = {} as typeof this['_query'];\n\t\tif (this._.schema) {\n\t\t\tfor (const [tableName, columns] of Object.entries(this._.schema)) {\n\t\t\t\t(this._query as CockroachDatabase<TQueryResult, Record<string, any>>['_query'])[tableName] =\n\t\t\t\t\tnew RelationalQueryBuilder(\n\t\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\t\tthis._.schema,\n\t\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\t\tschema!.fullSchema[tableName] as CockroachTable,\n\t\t\t\t\t\tcolumns,\n\t\t\t\t\t\tdialect,\n\t\t\t\t\t\tsession,\n\t\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Creates a subquery that defines a temporary named result set as a CTE.\n\t *\n\t * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param alias The alias for the subquery.\n\t *\n\t * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Create a subquery with alias 'sq' and use it in the select query\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t *\n\t * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:\n\t *\n\t * ```ts\n\t * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query\n\t * const sq = db.$with('sq').as(db.select({\n\t * name: sql<string>`upper(${users.name})`.as('name'),\n\t * })\n\t * .from(users));\n\t *\n\t * const result = await db.with(sq).select({ name: sq.name }).from(sq);\n\t * ```\n\t */\n\t$with: WithBuilder = (alias: string, selection?: ColumnsSelection) => {\n\t\tconst self = this;\n\t\tconst as = (\n\t\t\tqb:\n\t\t\t\t| TypedQueryBuilder<ColumnsSelection | undefined>\n\t\t\t\t| SQL\n\t\t\t\t| ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection | undefined> | SQL),\n\t\t) => {\n\t\t\tif (typeof qb === 'function') {\n\t\t\t\tqb = qb(new QueryBuilder(self.dialect));\n\t\t\t}\n\n\t\t\treturn new Proxy(\n\t\t\t\tnew WithSubquery(\n\t\t\t\t\tqb.getSQL(),\n\t\t\t\t\tselection ?? ('getSelectedFields' in qb ? qb.getSelectedFields() ?? {} : {}) as SelectedFields,\n\t\t\t\t\talias,\n\t\t\t\t\ttrue,\n\t\t\t\t),\n\t\t\t\tnew SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),\n\t\t\t);\n\t\t};\n\t\treturn { as };\n\t};\n\n\t$count(\n\t\tsource: CockroachTable | CockroachViewBase | SQL | SQLWrapper,\n\t\tfilters?: SQL<unknown>,\n\t) {\n\t\treturn new CockroachCountBuilder({ source, filters, session: this.session, dialect: this.dialect });\n\t}\n\n\t/**\n\t * Incorporates a previously defined CTE (using `$with`) into the main query.\n\t *\n\t * This method allows the main query to reference a temporary named result set.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param queries The CTEs to incorporate into the main query.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Define a subquery 'sq' as a CTE using $with\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * // Incorporate the CTE 'sq' into the main query and select from it\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t */\n\twith(...queries: WithSubquery[]) {\n\t\tconst self = this;\n\n\t\t/**\n\t\t * Creates a select query.\n\t\t *\n\t\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Select all columns and all rows from the 'cars' table\n\t\t * const allCars: Car[] = await db.select().from(cars);\n\t\t *\n\t\t * // Select specific columns and all rows from the 'cars' table\n\t\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * brand: cars.brand\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t *\n\t\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t\t *\n\t\t * ```ts\n\t\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t */\n\t\tfunction select(): CockroachSelectBuilder<undefined>;\n\t\tfunction select<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\t\tfunction select<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct` expression to the select query.\n\t\t *\n\t\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select all unique rows from the 'cars' table\n\t\t * await db.selectDistinct()\n\t\t * .from(cars)\n\t\t * .orderBy(cars.id, cars.brand, cars.color);\n\t\t *\n\t\t * // Select all unique brands from the 'cars' table\n\t\t * await db.selectDistinct({ brand: cars.brand })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinct(): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: true,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct on` expression to the select query.\n\t\t *\n\t\t * Calling this method will specify how the unique rows are determined.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param on The expression defining uniqueness.\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select the first row for each unique brand from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand])\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t *\n\t\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand, cars.color);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: { on },\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Creates an update query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t\t *\n\t\t * Use `.set()` method to specify which values to update.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t\t *\n\t\t * @param table The table to update.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Update all rows in the 'cars' table\n\t\t * await db.update(cars).set({ color: 'red' });\n\t\t *\n\t\t * // Update rows with filters and conditions\n\t\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t\t *\n\t\t * // Update with returning clause\n\t\t * const updatedCar: Car[] = await db.update(cars)\n\t\t * .set({ color: 'red' })\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction update<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachUpdateBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates an insert query.\n\t\t *\n\t\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t\t *\n\t\t * @param table The table to insert into.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Insert one row\n\t\t * await db.insert(cars).values({ brand: 'BMW' });\n\t\t *\n\t\t * // Insert multiple rows\n\t\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t\t *\n\t\t * // Insert only selected columns\n\t\t * await db.insert(cars, 'brand', 'productionYear').values([{ brand: 'BMW', productionYear: 1995 }, { brand: 'Porsche', productionYear: 1989 }]);\n\t\t *\n\t\t * // Insert with returning clause\n\t\t * const insertedCar: Car[] = await db.insert(cars)\n\t\t * .values({ brand: 'BMW' })\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction insert<\n\t\t\tTTable extends CockroachTable,\n\t\t\tTColumnList extends (keyof InferInsertModel<TTable>)[] = [],\n\t\t\tTRequiredKeys extends string = RequiredInsertKeys<TTable>,\n\t\t>(\n\t\t\ttable: TTable,\n\t\t\t...columns: TColumnList extends [] ? []\n\t\t\t\t: IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t\t: [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t\t: DrizzleTypeError<\n\t\t\t\t\t`Column selection is missing following required columns: ${JoinUnion<\n\t\t\t\t\t\t`\"${Exclude<TRequiredKeys, TColumnList[number]>}\"`,\n\t\t\t\t\t\t', '\n\t\t\t\t\t>}`\n\t\t\t\t>[]\n\t\t): CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? 'all' : TColumnList>;\n\t\tfunction insert<TTable extends CockroachTable>(\n\t\t\ttable: TTable,\n\t\t\t...columns: string[]\n\t\t): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachInsertBuilder(\n\t\t\t\ttable,\n\t\t\t\tself.session,\n\t\t\t\tself.dialect,\n\t\t\t\tqueries,\n\t\t\t\tcolumns.length ? columns : undefined,\n\t\t\t);\n\t\t}\n\n\t\t/**\n\t\t * Creates a delete query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t\t *\n\t\t * @param table The table to delete from.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Delete all rows in the 'cars' table\n\t\t * await db.delete(cars);\n\t\t *\n\t\t * // Delete rows with filters and conditions\n\t\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t\t *\n\t\t * // Delete with returning clause\n\t\t * const deletedCar: Car[] = await db.delete(cars)\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction delete_<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\t\treturn new CockroachDeleteBase(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\treturn { select, selectDistinct, selectDistinctOn, update, insert, delete: delete_ };\n\t}\n\n\t/**\n\t * Creates a select query.\n\t *\n\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Select all columns and all rows from the 'cars' table\n\t * const allCars: Car[] = await db.select().from(cars);\n\t *\n\t * // Select specific columns and all rows from the 'cars' table\n\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t * id: cars.id,\n\t * brand: cars.brand\n\t * })\n\t * .from(cars);\n\t * ```\n\t *\n\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t *\n\t * ```ts\n\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t * id: cars.id,\n\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t * })\n\t * .from(cars);\n\t * ```\n\t */\n\tselect(): CockroachSelectBuilder<undefined>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselect<TSelection extends SelectedFields>(fields?: TSelection): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct` expression to the select query.\n\t *\n\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select all unique rows from the 'cars' table\n\t * await db.selectDistinct()\n\t * .from(cars)\n\t * .orderBy(cars.id, cars.brand, cars.color);\n\t *\n\t * // Select all unique brands from the 'cars' table\n\t * await db.selectDistinct({ brand: cars.brand })\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t * ```\n\t */\n\tselectDistinct(): CockroachSelectBuilder<undefined>;\n\tselectDistinct<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselectDistinct<TSelection extends SelectedFields>(\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: true,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct on` expression to the select query.\n\t *\n\t * Calling this method will specify how the unique rows are determined.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param on The expression defining uniqueness.\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select the first row for each unique brand from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand])\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t *\n\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t * .from(cars)\n\t * .orderBy(cars.brand, cars.color);\n\t * ```\n\t */\n\tselectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields: TSelection,\n\t): CockroachSelectBuilder<TSelection>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: { on },\n\t\t});\n\t}\n\n\t/**\n\t * Creates an update query.\n\t *\n\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t *\n\t * Use `.set()` method to specify which values to update.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t *\n\t * @param table The table to update.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Update all rows in the 'cars' table\n\t * await db.update(cars).set({ color: 'red' });\n\t *\n\t * // Update rows with filters and conditions\n\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t *\n\t * // Update with returning clause\n\t * const updatedCar: Car[] = await db.update(cars)\n\t * .set({ color: 'red' })\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tupdate<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates an insert query.\n\t *\n\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t *\n\t * @param table The table to insert into.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Insert one row\n\t * await db.insert(cars).values({ brand: 'BMW' });\n\t *\n\t * // Insert multiple rows\n\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t *\n\t * // Insert only selected columns\n\t * await db.insert(cars, 'brand', 'productionYear').values([{ brand: 'BMW', productionYear: 1995 }, { brand: 'Porsche', productionYear: 1989 }]);\n\t *\n\t * // Insert with returning clause\n\t * const insertedCar: Car[] = await db.insert(cars)\n\t * .values({ brand: 'BMW' })\n\t * .returning();\n\t * ```\n\t */\n\tinsert<\n\t\tTTable extends CockroachTable,\n\t\tTColumnList extends (keyof InferInsertModel<TTable>)[] = [],\n\t\tTRequiredKeys extends string = RequiredInsertKeys<TTable>,\n\t>(\n\t\ttable: TTable,\n\t\t...columns: TColumnList extends [] ? []\n\t\t\t: IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t: [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t: DrizzleTypeError<\n\t\t\t\t`Column selection is missing following required columns: ${JoinUnion<\n\t\t\t\t\t`\"${Exclude<TRequiredKeys, TColumnList[number]>}\"`,\n\t\t\t\t\t', '\n\t\t\t\t>}`\n\t\t\t>[]\n\t): CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? 'all' : TColumnList>;\n\tinsert<TTable extends CockroachTable>(\n\t\ttable: TTable,\n\t\t...columns: string[]\n\t): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachInsertBuilder(\n\t\t\ttable,\n\t\t\tthis.session,\n\t\t\tthis.dialect,\n\t\t\tundefined,\n\t\t\tcolumns.length ? columns : undefined,\n\t\t);\n\t}\n\n\t/**\n\t * Creates a delete query.\n\t *\n\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t *\n\t * @param table The table to delete from.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Delete all rows in the 'cars' table\n\t * await db.delete(cars);\n\t *\n\t * // Delete rows with filters and conditions\n\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t *\n\t * // Delete with returning clause\n\t * const deletedCar: Car[] = await db.delete(cars)\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tdelete<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\treturn new CockroachDeleteBase(table, this.session, this.dialect);\n\t}\n\n\trefreshMaterializedView<TView extends CockroachMaterializedView>(\n\t\tview: TView,\n\t): CockroachRefreshMaterializedView<TQueryResult> {\n\t\treturn new CockroachRefreshMaterializedView(view, this.session, this.dialect);\n\t}\n\n\tprotected authToken?: NeonAuthToken;\n\n\texecute<TRow extends Record<string, unknown> = Record<string, unknown>>(\n\t\tquery: SQLWrapper | string,\n\t): CockroachRaw<CockroachQueryResultKind<TQueryResult, TRow>> {\n\t\tconst sequel = typeof query === 'string' ? sql.raw(query) : query.getSQL();\n\t\tconst builtQuery = this.dialect.sqlToQuery(sequel);\n\t\tconst prepared = this.session.prepareQuery<\n\t\t\tPreparedQueryConfig & { execute: CockroachQueryResultKind<TQueryResult, TRow> }\n\t\t>(\n\t\t\tbuiltQuery,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t);\n\t\treturn new CockroachRaw(\n\t\t\t() => prepared.execute(undefined, this.authToken),\n\t\t\tsequel,\n\t\t\tbuiltQuery,\n\t\t\t(result) => prepared.mapResult(result, true),\n\t\t);\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (tx: CockroachTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>,\n\t\tconfig?: CockroachTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type CockroachWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends V1.TablesRelationalConfig,\n\tQ extends CockroachDatabase<\n\t\tHKT,\n\t\tTFullSchema,\n\t\tTSchema extends Record<string, unknown> ? V1.ExtractTablesWithRelations<TFullSchema> : TSchema\n\t>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): CockroachWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);\n\tconst selectDistinctOn: Q['selectDistinctOn'] = (...args: [any]) => getReplica(replicas).selectDistinctOn(...args);\n\tconst $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);\n\tconst _with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);\n\tconst $with: Q['$with'] = (arg: any) => getReplica(replicas).$with(arg) as any;\n\n\tconst update: Q['update'] = (...args: [any]) => primary.update(...args);\n\tconst insert: Q['insert'] = ((...args: [any]) => primary.insert(...args)) as Q['insert'];\n\tconst $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);\n\tconst execute: Q['execute'] = (...args: [any]) => primary.execute(...args);\n\tconst transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);\n\tconst refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>\n\t\tprimary.refreshMaterializedView(...args);\n\n\treturn {\n\t\t...primary,\n\t\tupdate,\n\t\tinsert,\n\t\tdelete: $delete,\n\t\texecute,\n\t\ttransaction,\n\t\trefreshMaterializedView,\n\t\t$primary: primary,\n\t\t$replicas: replicas,\n\t\tselect,\n\t\tselectDistinct,\n\t\tselectDistinctOn,\n\t\t$count,\n\t\t$with,\n\t\twith: _with,\n\t\tget _query() {\n\t\t\treturn getReplica(replicas)._query;\n\t\t},\n\t};\n};\n"],"mappings":";;;;;;;;;;;;;AAoCA,IAAa,oBAAb,MAIE;CACD,QAAiBA,0BAAsB;CASvC;CAMA,YAEC,AAAS,SAET,AAAS,SACT,QACC;EAJQ;EAEA;AAGT,OAAK,IAAI,SACN;GACD,QAAQ,OAAO;GACf,YAAY,OAAO;GACnB,eAAe,OAAO;GACtB;GACA,GACC;GACD,QAAQ;GACR,YAAY,EAAE;GACd,eAAe,EAAE;GACjB;GACA;AACF,OAAK,SAAS,EAAE;AAChB,MAAI,KAAK,EAAE,OACV,MAAK,MAAM,CAAC,WAAW,YAAY,OAAO,QAAQ,KAAK,EAAE,OAAO,CAC/D,CAAC,KAAK,OAA0E,aAC/E,IAAIC,mEACH,OAAQ,YACR,KAAK,EAAE,QACP,KAAK,EAAE,eACP,OAAQ,WAAW,YACnB,SACA,SACA,QACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCL,SAAsB,OAAe,cAAiC;EACrE,MAAM,OAAO;EACb,MAAM,MACL,OAII;AACJ,OAAI,OAAO,OAAO,WACjB,MAAK,GAAG,IAAIC,sDAAa,KAAK,QAAQ,CAAC;AAGxC,UAAO,IAAI,MACV,IAAIC,2BACH,GAAG,QAAQ,EACX,cAAc,uBAAuB,KAAK,GAAG,mBAAmB,IAAI,EAAE,GAAG,EAAE,GAC3E,OACA,KACA,EACD,IAAIC,2CAAsB;IAAE;IAAO,oBAAoB;IAAS,aAAa;IAAS,CAAC,CACvF;;AAEF,SAAO,EAAE,IAAI;;CAGd,OACC,QACA,SACC;AACD,SAAO,IAAIC,kEAAsB;GAAE;GAAQ;GAAS,SAAS,KAAK;GAAS,SAAS,KAAK;GAAS,CAAC;;;;;;;;;;;;;;;;;;;;;CAsBpG,KAAK,GAAG,SAAyB;EAChC,MAAM,OAAO;EAwCb,S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S,CAAC,KAAK,GAAG,KAAK;CAC7E,MAAM,SAAqB,QAAa,WAAW,SAAS,CAAC,MAAM,IAAI;CAEvE,MAAM,UAAuB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACvE,MAAM,WAAwB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACxE,MAAM,WAAwB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACxE,MAAM,WAAyB,GAAG,SAAgB,QAAQ,QAAQ,GAAG,KAAK;CAC1E,MAAM,eAAiC,GAAG,SAAgB,QAAQ,YAAY,GAAG,KAAK;CACtF,MAAM,2BAAyD,GAAG,SACjE,QAAQ,wBAAwB,GAAG,KAAK;AAEzC,QAAO;EACN,GAAG;EACH;EACA;EACA,QAAQ;EACR;EACA;EACA;EACA,UAAU;EACV,WAAW;EACX;EACA;EACA;EACA;EACA;EACA,MAAM;EACN,IAAI,SAAS;AACZ,UAAO,WAAW,SAAS,CAAC;;EAE7B"}
|
package/cockroach-core/db.d.cts
CHANGED
|
@@ -11,10 +11,11 @@ import { entityKind } from "../entity.cjs";
|
|
|
11
11
|
import { CockroachTable } from "./table.cjs";
|
|
12
12
|
import { SQL, SQLWrapper } from "../sql/sql.cjs";
|
|
13
13
|
import { WithSubquery } from "../subquery.cjs";
|
|
14
|
-
import {
|
|
14
|
+
import { InferInsertModel, RequiredInsertKeys } from "../table.cjs";
|
|
15
|
+
import { DrizzleTypeError, IsNever, JoinUnion, NeonAuthToken } from "../utils.cjs";
|
|
15
16
|
import * as V1 from "../_relations.cjs";
|
|
16
17
|
import { CockroachDialect } from "./dialect.cjs";
|
|
17
|
-
import { CockroachDeleteBase, CockroachInsertBuilder, CockroachSelectBuilder, CockroachUpdateBuilder } from "./query-builders/index.cjs";
|
|
18
|
+
import { CockroachDeleteBase, CockroachInsertBuilder, CockroachSelectBuilder, CockroachUpdateBuilder, NoDuplicateColumns } from "./query-builders/index.cjs";
|
|
18
19
|
import { CockroachQueryResultHKT, CockroachQueryResultKind, CockroachSession, CockroachTransaction, CockroachTransactionConfig } from "./session.cjs";
|
|
19
20
|
|
|
20
21
|
//#region src/cockroach-core/db.d.ts
|
|
@@ -99,7 +100,7 @@ declare class CockroachDatabase<TQueryResult extends CockroachQueryResultHKT, TF
|
|
|
99
100
|
<TSelection extends SelectedFields>(on: (CockroachColumn | SQLWrapper)[], fields: TSelection): CockroachSelectBuilder<TSelection>;
|
|
100
101
|
};
|
|
101
102
|
update: <TTable extends CockroachTable>(table: TTable) => CockroachUpdateBuilder<TTable, TQueryResult>;
|
|
102
|
-
insert: <TTable extends CockroachTable>(table: TTable) => CockroachInsertBuilder<TTable, TQueryResult>;
|
|
103
|
+
insert: <TTable extends CockroachTable, TColumnList extends (keyof InferInsertModel<TTable>)[] = [], TRequiredKeys extends string = RequiredInsertKeys<TTable>>(table: TTable, ...columns: TColumnList extends [] ? [] : IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList> : [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList> : DrizzleTypeError<`Column selection is missing following required columns: ${JoinUnion<`"${Exclude<TRequiredKeys, TColumnList[number]>}"`, ", ">}`>[]) => CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? "all" : TColumnList>;
|
|
103
104
|
delete: <TTable extends CockroachTable>(table: TTable) => CockroachDeleteBase<TTable, TQueryResult>;
|
|
104
105
|
};
|
|
105
106
|
/**
|
|
@@ -239,13 +240,16 @@ declare class CockroachDatabase<TQueryResult extends CockroachQueryResultHKT, TF
|
|
|
239
240
|
* // Insert multiple rows
|
|
240
241
|
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
241
242
|
*
|
|
243
|
+
* // Insert only selected columns
|
|
244
|
+
* await db.insert(cars, 'brand', 'productionYear').values([{ brand: 'BMW', productionYear: 1995 }, { brand: 'Porsche', productionYear: 1989 }]);
|
|
245
|
+
*
|
|
242
246
|
* // Insert with returning clause
|
|
243
247
|
* const insertedCar: Car[] = await db.insert(cars)
|
|
244
248
|
* .values({ brand: 'BMW' })
|
|
245
249
|
* .returning();
|
|
246
250
|
* ```
|
|
247
251
|
*/
|
|
248
|
-
insert<TTable extends CockroachTable>(table: TTable): CockroachInsertBuilder<TTable, TQueryResult>;
|
|
252
|
+
insert<TTable extends CockroachTable, TColumnList extends (keyof InferInsertModel<TTable>)[] = [], TRequiredKeys extends string = RequiredInsertKeys<TTable>>(table: TTable, ...columns: TColumnList extends [] ? [] : IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList> : [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList> : DrizzleTypeError<`Column selection is missing following required columns: ${JoinUnion<`"${Exclude<TRequiredKeys, TColumnList[number]>}"`, ', '>}`>[]): CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? 'all' : TColumnList>;
|
|
249
253
|
/**
|
|
250
254
|
* Creates a delete query.
|
|
251
255
|
*
|
package/cockroach-core/db.d.ts
CHANGED
|
@@ -9,12 +9,13 @@ import { CockroachRefreshMaterializedView } from "./query-builders/refresh-mater
|
|
|
9
9
|
import { SelectedFields } from "./query-builders/select.types.js";
|
|
10
10
|
import { entityKind } from "../entity.js";
|
|
11
11
|
import { WithSubquery } from "../subquery.js";
|
|
12
|
-
import {
|
|
12
|
+
import { InferInsertModel, RequiredInsertKeys } from "../table.js";
|
|
13
|
+
import { DrizzleTypeError, IsNever, JoinUnion, NeonAuthToken } from "../utils.js";
|
|
13
14
|
import { SQL, SQLWrapper } from "../sql/sql.js";
|
|
14
15
|
import { CockroachDialect } from "./dialect.js";
|
|
15
16
|
import * as V1 from "../_relations.js";
|
|
16
17
|
import { CockroachTable } from "./table.js";
|
|
17
|
-
import { CockroachDeleteBase, CockroachInsertBuilder, CockroachSelectBuilder, CockroachUpdateBuilder } from "./query-builders/index.js";
|
|
18
|
+
import { CockroachDeleteBase, CockroachInsertBuilder, CockroachSelectBuilder, CockroachUpdateBuilder, NoDuplicateColumns } from "./query-builders/index.js";
|
|
18
19
|
import { CockroachQueryResultHKT, CockroachQueryResultKind, CockroachSession, CockroachTransaction, CockroachTransactionConfig } from "./session.js";
|
|
19
20
|
|
|
20
21
|
//#region src/cockroach-core/db.d.ts
|
|
@@ -99,7 +100,7 @@ declare class CockroachDatabase<TQueryResult extends CockroachQueryResultHKT, TF
|
|
|
99
100
|
<TSelection extends SelectedFields>(on: (CockroachColumn | SQLWrapper)[], fields: TSelection): CockroachSelectBuilder<TSelection>;
|
|
100
101
|
};
|
|
101
102
|
update: <TTable extends CockroachTable>(table: TTable) => CockroachUpdateBuilder<TTable, TQueryResult>;
|
|
102
|
-
insert: <TTable extends CockroachTable>(table: TTable) => CockroachInsertBuilder<TTable, TQueryResult>;
|
|
103
|
+
insert: <TTable extends CockroachTable, TColumnList extends (keyof InferInsertModel<TTable>)[] = [], TRequiredKeys extends string = RequiredInsertKeys<TTable>>(table: TTable, ...columns: TColumnList extends [] ? [] : IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList> : [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList> : DrizzleTypeError<`Column selection is missing following required columns: ${JoinUnion<`"${Exclude<TRequiredKeys, TColumnList[number]>}"`, ", ">}`>[]) => CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? "all" : TColumnList>;
|
|
103
104
|
delete: <TTable extends CockroachTable>(table: TTable) => CockroachDeleteBase<TTable, TQueryResult>;
|
|
104
105
|
};
|
|
105
106
|
/**
|
|
@@ -239,13 +240,16 @@ declare class CockroachDatabase<TQueryResult extends CockroachQueryResultHKT, TF
|
|
|
239
240
|
* // Insert multiple rows
|
|
240
241
|
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
241
242
|
*
|
|
243
|
+
* // Insert only selected columns
|
|
244
|
+
* await db.insert(cars, 'brand', 'productionYear').values([{ brand: 'BMW', productionYear: 1995 }, { brand: 'Porsche', productionYear: 1989 }]);
|
|
245
|
+
*
|
|
242
246
|
* // Insert with returning clause
|
|
243
247
|
* const insertedCar: Car[] = await db.insert(cars)
|
|
244
248
|
* .values({ brand: 'BMW' })
|
|
245
249
|
* .returning();
|
|
246
250
|
* ```
|
|
247
251
|
*/
|
|
248
|
-
insert<TTable extends CockroachTable>(table: TTable): CockroachInsertBuilder<TTable, TQueryResult>;
|
|
252
|
+
insert<TTable extends CockroachTable, TColumnList extends (keyof InferInsertModel<TTable>)[] = [], TRequiredKeys extends string = RequiredInsertKeys<TTable>>(table: TTable, ...columns: TColumnList extends [] ? [] : IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList> : [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList> : DrizzleTypeError<`Column selection is missing following required columns: ${JoinUnion<`"${Exclude<TRequiredKeys, TColumnList[number]>}"`, ', '>}`>[]): CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? 'all' : TColumnList>;
|
|
249
253
|
/**
|
|
250
254
|
* Creates a delete query.
|
|
251
255
|
*
|
package/cockroach-core/db.js
CHANGED
|
@@ -158,32 +158,8 @@ var CockroachDatabase = class {
|
|
|
158
158
|
function update(table) {
|
|
159
159
|
return new CockroachUpdateBuilder(table, self.session, self.dialect, queries);
|
|
160
160
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
*
|
|
164
|
-
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
|
|
165
|
-
*
|
|
166
|
-
* See docs: {@link https://orm.drizzle.team/docs/insert}
|
|
167
|
-
*
|
|
168
|
-
* @param table The table to insert into.
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
*
|
|
172
|
-
* ```ts
|
|
173
|
-
* // Insert one row
|
|
174
|
-
* await db.insert(cars).values({ brand: 'BMW' });
|
|
175
|
-
*
|
|
176
|
-
* // Insert multiple rows
|
|
177
|
-
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
178
|
-
*
|
|
179
|
-
* // Insert with returning clause
|
|
180
|
-
* const insertedCar: Car[] = await db.insert(cars)
|
|
181
|
-
* .values({ brand: 'BMW' })
|
|
182
|
-
* .returning();
|
|
183
|
-
* ```
|
|
184
|
-
*/
|
|
185
|
-
function insert(table) {
|
|
186
|
-
return new CockroachInsertBuilder(table, self.session, self.dialect, queries);
|
|
161
|
+
function insert(table, ...columns) {
|
|
162
|
+
return new CockroachInsertBuilder(table, self.session, self.dialect, queries, columns.length ? columns : void 0);
|
|
187
163
|
}
|
|
188
164
|
/**
|
|
189
165
|
* Creates a delete query.
|
|
@@ -274,32 +250,8 @@ var CockroachDatabase = class {
|
|
|
274
250
|
update(table) {
|
|
275
251
|
return new CockroachUpdateBuilder(table, this.session, this.dialect);
|
|
276
252
|
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
*
|
|
280
|
-
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
|
|
281
|
-
*
|
|
282
|
-
* See docs: {@link https://orm.drizzle.team/docs/insert}
|
|
283
|
-
*
|
|
284
|
-
* @param table The table to insert into.
|
|
285
|
-
*
|
|
286
|
-
* @example
|
|
287
|
-
*
|
|
288
|
-
* ```ts
|
|
289
|
-
* // Insert one row
|
|
290
|
-
* await db.insert(cars).values({ brand: 'BMW' });
|
|
291
|
-
*
|
|
292
|
-
* // Insert multiple rows
|
|
293
|
-
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
|
|
294
|
-
*
|
|
295
|
-
* // Insert with returning clause
|
|
296
|
-
* const insertedCar: Car[] = await db.insert(cars)
|
|
297
|
-
* .values({ brand: 'BMW' })
|
|
298
|
-
* .returning();
|
|
299
|
-
* ```
|
|
300
|
-
*/
|
|
301
|
-
insert(table) {
|
|
302
|
-
return new CockroachInsertBuilder(table, this.session, this.dialect);
|
|
253
|
+
insert(table, ...columns) {
|
|
254
|
+
return new CockroachInsertBuilder(table, this.session, this.dialect, void 0, columns.length ? columns : void 0);
|
|
303
255
|
}
|
|
304
256
|
/**
|
|
305
257
|
* Creates a delete query.
|
|
@@ -350,7 +302,7 @@ const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(
|
|
|
350
302
|
const _with = (...args) => getReplica(replicas).with(...args);
|
|
351
303
|
const $with = (arg) => getReplica(replicas).$with(arg);
|
|
352
304
|
const update = (...args) => primary.update(...args);
|
|
353
|
-
const insert = (...args) => primary.insert(...args);
|
|
305
|
+
const insert = ((...args) => primary.insert(...args));
|
|
354
306
|
const $delete = (...args) => primary.delete(...args);
|
|
355
307
|
const execute = (...args) => primary.execute(...args);
|
|
356
308
|
const transaction = (...args) => primary.transaction(...args);
|
package/cockroach-core/db.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"db.js","names":[],"sources":["../../src/cockroach-core/db.ts"],"sourcesContent":["import type * as V1 from '~/_relations.ts';\nimport type { CockroachDialect } from '~/cockroach-core/dialect.ts';\nimport {\n\tCockroachDeleteBase,\n\tCockroachInsertBuilder,\n\tCockroachSelectBuilder,\n\tCockroachUpdateBuilder,\n\tQueryBuilder,\n} from '~/cockroach-core/query-builders/index.ts';\nimport type {\n\tCockroachQueryResultHKT,\n\tCockroachQueryResultKind,\n\tCockroachSession,\n\tCockroachTransaction,\n\tCockroachTransactionConfig,\n\tPreparedQueryConfig,\n} from '~/cockroach-core/session.ts';\nimport type { CockroachTable } from '~/cockroach-core/table.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\nimport { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport type { DrizzleTypeError, NeonAuthToken } from '~/utils.ts';\nimport type { CockroachColumn } from './columns/index.ts';\nimport { CockroachCountBuilder } from './query-builders/count.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport { CockroachRaw } from './query-builders/raw.ts';\nimport { CockroachRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type { WithBuilder } from './subquery.ts';\nimport type { CockroachViewBase } from './view-base.ts';\nimport type { CockroachMaterializedView } from './view.ts';\n\nexport class CockroachDatabase<\n\tTQueryResult extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'CockroachDatabase';\n\n\tdeclare readonly _: {\n\t\treadonly schema: TSchema | undefined;\n\t\treadonly fullSchema: TFullSchema;\n\t\treadonly tableNamesMap: Record<string, string>;\n\t\treadonly session: CockroachSession<TQueryResult, TFullSchema, TSchema>;\n\t};\n\n\t_query: TFullSchema extends Record<string, never>\n\t\t? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>\n\t\t: {\n\t\t\t[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: CockroachDialect,\n\t\t/** @internal */\n\t\treadonly session: CockroachSession<any, any, any>,\n\t\tschema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t) {\n\t\tthis._ = schema\n\t\t\t? {\n\t\t\t\tschema: schema.schema,\n\t\t\t\tfullSchema: schema.fullSchema as TFullSchema,\n\t\t\t\ttableNamesMap: schema.tableNamesMap,\n\t\t\t\tsession,\n\t\t\t}\n\t\t\t: {\n\t\t\t\tschema: undefined,\n\t\t\t\tfullSchema: {} as TFullSchema,\n\t\t\t\ttableNamesMap: {},\n\t\t\t\tsession,\n\t\t\t};\n\t\tthis._query = {} as typeof this['_query'];\n\t\tif (this._.schema) {\n\t\t\tfor (const [tableName, columns] of Object.entries(this._.schema)) {\n\t\t\t\t(this._query as CockroachDatabase<TQueryResult, Record<string, any>>['_query'])[tableName] =\n\t\t\t\t\tnew RelationalQueryBuilder(\n\t\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\t\tthis._.schema,\n\t\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\t\tschema!.fullSchema[tableName] as CockroachTable,\n\t\t\t\t\t\tcolumns,\n\t\t\t\t\t\tdialect,\n\t\t\t\t\t\tsession,\n\t\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Creates a subquery that defines a temporary named result set as a CTE.\n\t *\n\t * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param alias The alias for the subquery.\n\t *\n\t * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Create a subquery with alias 'sq' and use it in the select query\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t *\n\t * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:\n\t *\n\t * ```ts\n\t * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query\n\t * const sq = db.$with('sq').as(db.select({\n\t * name: sql<string>`upper(${users.name})`.as('name'),\n\t * })\n\t * .from(users));\n\t *\n\t * const result = await db.with(sq).select({ name: sq.name }).from(sq);\n\t * ```\n\t */\n\t$with: WithBuilder = (alias: string, selection?: ColumnsSelection) => {\n\t\tconst self = this;\n\t\tconst as = (\n\t\t\tqb:\n\t\t\t\t| TypedQueryBuilder<ColumnsSelection | undefined>\n\t\t\t\t| SQL\n\t\t\t\t| ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection | undefined> | SQL),\n\t\t) => {\n\t\t\tif (typeof qb === 'function') {\n\t\t\t\tqb = qb(new QueryBuilder(self.dialect));\n\t\t\t}\n\n\t\t\treturn new Proxy(\n\t\t\t\tnew WithSubquery(\n\t\t\t\t\tqb.getSQL(),\n\t\t\t\t\tselection ?? ('getSelectedFields' in qb ? qb.getSelectedFields() ?? {} : {}) as SelectedFields,\n\t\t\t\t\talias,\n\t\t\t\t\ttrue,\n\t\t\t\t),\n\t\t\t\tnew SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),\n\t\t\t);\n\t\t};\n\t\treturn { as };\n\t};\n\n\t$count(\n\t\tsource: CockroachTable | CockroachViewBase | SQL | SQLWrapper,\n\t\tfilters?: SQL<unknown>,\n\t) {\n\t\treturn new CockroachCountBuilder({ source, filters, session: this.session, dialect: this.dialect });\n\t}\n\n\t/**\n\t * Incorporates a previously defined CTE (using `$with`) into the main query.\n\t *\n\t * This method allows the main query to reference a temporary named result set.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param queries The CTEs to incorporate into the main query.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Define a subquery 'sq' as a CTE using $with\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * // Incorporate the CTE 'sq' into the main query and select from it\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t */\n\twith(...queries: WithSubquery[]) {\n\t\tconst self = this;\n\n\t\t/**\n\t\t * Creates a select query.\n\t\t *\n\t\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Select all columns and all rows from the 'cars' table\n\t\t * const allCars: Car[] = await db.select().from(cars);\n\t\t *\n\t\t * // Select specific columns and all rows from the 'cars' table\n\t\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * brand: cars.brand\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t *\n\t\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t\t *\n\t\t * ```ts\n\t\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t */\n\t\tfunction select(): CockroachSelectBuilder<undefined>;\n\t\tfunction select<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\t\tfunction select<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct` expression to the select query.\n\t\t *\n\t\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select all unique rows from the 'cars' table\n\t\t * await db.selectDistinct()\n\t\t * .from(cars)\n\t\t * .orderBy(cars.id, cars.brand, cars.color);\n\t\t *\n\t\t * // Select all unique brands from the 'cars' table\n\t\t * await db.selectDistinct({ brand: cars.brand })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinct(): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: true,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct on` expression to the select query.\n\t\t *\n\t\t * Calling this method will specify how the unique rows are determined.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param on The expression defining uniqueness.\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select the first row for each unique brand from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand])\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t *\n\t\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand, cars.color);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: { on },\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Creates an update query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t\t *\n\t\t * Use `.set()` method to specify which values to update.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t\t *\n\t\t * @param table The table to update.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Update all rows in the 'cars' table\n\t\t * await db.update(cars).set({ color: 'red' });\n\t\t *\n\t\t * // Update rows with filters and conditions\n\t\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t\t *\n\t\t * // Update with returning clause\n\t\t * const updatedCar: Car[] = await db.update(cars)\n\t\t * .set({ color: 'red' })\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction update<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachUpdateBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates an insert query.\n\t\t *\n\t\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t\t *\n\t\t * @param table The table to insert into.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Insert one row\n\t\t * await db.insert(cars).values({ brand: 'BMW' });\n\t\t *\n\t\t * // Insert multiple rows\n\t\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t\t *\n\t\t * // Insert with returning clause\n\t\t * const insertedCar: Car[] = await db.insert(cars)\n\t\t * .values({ brand: 'BMW' })\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction insert<TTable extends CockroachTable>(table: TTable): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachInsertBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates a delete query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t\t *\n\t\t * @param table The table to delete from.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Delete all rows in the 'cars' table\n\t\t * await db.delete(cars);\n\t\t *\n\t\t * // Delete rows with filters and conditions\n\t\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t\t *\n\t\t * // Delete with returning clause\n\t\t * const deletedCar: Car[] = await db.delete(cars)\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction delete_<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\t\treturn new CockroachDeleteBase(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\treturn { select, selectDistinct, selectDistinctOn, update, insert, delete: delete_ };\n\t}\n\n\t/**\n\t * Creates a select query.\n\t *\n\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Select all columns and all rows from the 'cars' table\n\t * const allCars: Car[] = await db.select().from(cars);\n\t *\n\t * // Select specific columns and all rows from the 'cars' table\n\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t * id: cars.id,\n\t * brand: cars.brand\n\t * })\n\t * .from(cars);\n\t * ```\n\t *\n\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t *\n\t * ```ts\n\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t * id: cars.id,\n\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t * })\n\t * .from(cars);\n\t * ```\n\t */\n\tselect(): CockroachSelectBuilder<undefined>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselect<TSelection extends SelectedFields>(fields?: TSelection): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct` expression to the select query.\n\t *\n\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select all unique rows from the 'cars' table\n\t * await db.selectDistinct()\n\t * .from(cars)\n\t * .orderBy(cars.id, cars.brand, cars.color);\n\t *\n\t * // Select all unique brands from the 'cars' table\n\t * await db.selectDistinct({ brand: cars.brand })\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t * ```\n\t */\n\tselectDistinct(): CockroachSelectBuilder<undefined>;\n\tselectDistinct<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselectDistinct<TSelection extends SelectedFields>(\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: true,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct on` expression to the select query.\n\t *\n\t * Calling this method will specify how the unique rows are determined.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param on The expression defining uniqueness.\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select the first row for each unique brand from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand])\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t *\n\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t * .from(cars)\n\t * .orderBy(cars.brand, cars.color);\n\t * ```\n\t */\n\tselectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields: TSelection,\n\t): CockroachSelectBuilder<TSelection>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: { on },\n\t\t});\n\t}\n\n\t/**\n\t * Creates an update query.\n\t *\n\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t *\n\t * Use `.set()` method to specify which values to update.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t *\n\t * @param table The table to update.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Update all rows in the 'cars' table\n\t * await db.update(cars).set({ color: 'red' });\n\t *\n\t * // Update rows with filters and conditions\n\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t *\n\t * // Update with returning clause\n\t * const updatedCar: Car[] = await db.update(cars)\n\t * .set({ color: 'red' })\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tupdate<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates an insert query.\n\t *\n\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t *\n\t * @param table The table to insert into.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Insert one row\n\t * await db.insert(cars).values({ brand: 'BMW' });\n\t *\n\t * // Insert multiple rows\n\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t *\n\t * // Insert with returning clause\n\t * const insertedCar: Car[] = await db.insert(cars)\n\t * .values({ brand: 'BMW' })\n\t * .returning();\n\t * ```\n\t */\n\tinsert<TTable extends CockroachTable>(table: TTable): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachInsertBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates a delete query.\n\t *\n\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t *\n\t * @param table The table to delete from.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Delete all rows in the 'cars' table\n\t * await db.delete(cars);\n\t *\n\t * // Delete rows with filters and conditions\n\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t *\n\t * // Delete with returning clause\n\t * const deletedCar: Car[] = await db.delete(cars)\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tdelete<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\treturn new CockroachDeleteBase(table, this.session, this.dialect);\n\t}\n\n\trefreshMaterializedView<TView extends CockroachMaterializedView>(\n\t\tview: TView,\n\t): CockroachRefreshMaterializedView<TQueryResult> {\n\t\treturn new CockroachRefreshMaterializedView(view, this.session, this.dialect);\n\t}\n\n\tprotected authToken?: NeonAuthToken;\n\n\texecute<TRow extends Record<string, unknown> = Record<string, unknown>>(\n\t\tquery: SQLWrapper | string,\n\t): CockroachRaw<CockroachQueryResultKind<TQueryResult, TRow>> {\n\t\tconst sequel = typeof query === 'string' ? sql.raw(query) : query.getSQL();\n\t\tconst builtQuery = this.dialect.sqlToQuery(sequel);\n\t\tconst prepared = this.session.prepareQuery<\n\t\t\tPreparedQueryConfig & { execute: CockroachQueryResultKind<TQueryResult, TRow> }\n\t\t>(\n\t\t\tbuiltQuery,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t);\n\t\treturn new CockroachRaw(\n\t\t\t() => prepared.execute(undefined, this.authToken),\n\t\t\tsequel,\n\t\t\tbuiltQuery,\n\t\t\t(result) => prepared.mapResult(result, true),\n\t\t);\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (tx: CockroachTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>,\n\t\tconfig?: CockroachTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type CockroachWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends V1.TablesRelationalConfig,\n\tQ extends CockroachDatabase<\n\t\tHKT,\n\t\tTFullSchema,\n\t\tTSchema extends Record<string, unknown> ? V1.ExtractTablesWithRelations<TFullSchema> : TSchema\n\t>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): CockroachWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);\n\tconst selectDistinctOn: Q['selectDistinctOn'] = (...args: [any]) => getReplica(replicas).selectDistinctOn(...args);\n\tconst $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);\n\tconst _with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);\n\tconst $with: Q['$with'] = (arg: any) => getReplica(replicas).$with(arg) as any;\n\n\tconst update: Q['update'] = (...args: [any]) => primary.update(...args);\n\tconst insert: Q['insert'] = (...args: [any]) => primary.insert(...args);\n\tconst $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);\n\tconst execute: Q['execute'] = (...args: [any]) => primary.execute(...args);\n\tconst transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);\n\tconst refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>\n\t\tprimary.refreshMaterializedView(...args);\n\n\treturn {\n\t\t...primary,\n\t\tupdate,\n\t\tinsert,\n\t\tdelete: $delete,\n\t\texecute,\n\t\ttransaction,\n\t\trefreshMaterializedView,\n\t\t$primary: primary,\n\t\t$replicas: replicas,\n\t\tselect,\n\t\tselectDistinct,\n\t\tselectDistinctOn,\n\t\t$count,\n\t\t$with,\n\t\twith: _with,\n\t\tget _query() {\n\t\t\treturn getReplica(replicas)._query;\n\t\t},\n\t};\n};\n"],"mappings":";;;;;;;;;;;AAkCA,IAAa,oBAAb,MAIE;CACD,QAAiB,cAAsB;CASvC;CAMA,YAEC,AAAS,SAET,AAAS,SACT,QACC;EAJQ;EAEA;AAGT,OAAK,IAAI,SACN;GACD,QAAQ,OAAO;GACf,YAAY,OAAO;GACnB,eAAe,OAAO;GACtB;GACA,GACC;GACD,QAAQ;GACR,YAAY,EAAE;GACd,eAAe,EAAE;GACjB;GACA;AACF,OAAK,SAAS,EAAE;AAChB,MAAI,KAAK,EAAE,OACV,MAAK,MAAM,CAAC,WAAW,YAAY,OAAO,QAAQ,KAAK,EAAE,OAAO,CAC/D,CAAC,KAAK,OAA0E,aAC/E,IAAI,uBACH,OAAQ,YACR,KAAK,EAAE,QACP,KAAK,EAAE,eACP,OAAQ,WAAW,YACnB,SACA,SACA,QACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCL,SAAsB,OAAe,cAAiC;EACrE,MAAM,OAAO;EACb,MAAM,MACL,OAII;AACJ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;AACb,SAAO,KAAK,QAAQ,YAAY,aAAa,OAAO;;;AAMtD,MAAa,gBAUZ,SACA,UACA,mBAAyC,SAAS,KAAK,MAAM,KAAK,QAAQ,GAAG,SAAS,OAAO,MAC/D;CAC9B,MAAM,UAAuB,GAAG,SAAa,WAAW,SAAS,CAAC,OAAO,GAAG,KAAK;CACjF,MAAM,kBAAuC,GAAG,SAAa,WAAW,SAAS,CAAC,eAAe,GAAG,KAAK;CACzG,MAAM,oBAA2C,GAAG,SAAgB,WAAW,SAAS,CAAC,iBAAiB,GAAG,KAAK;CAClH,MAAM,UAAuB,GAAG,SAAgB,WAAW,SAAS,CAAC,OAAO,GAAG,KAAK;CACpF,MAAM,SAAoB,GAAG,SAAc,WAAW,SAAS,CAAC,KAAK,GAAG,KAAK;CAC7E,MAAM,SAAqB,QAAa,WAAW,SAAS,CAAC,MAAM,IAAI;CAEvE,MAAM,UAAuB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACvE,MAAM,UAAuB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACvE,MAAM,WAAwB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACxE,MAAM,WAAyB,GAAG,SAAgB,QAAQ,QAAQ,GAAG,KAAK;CAC1E,MAAM,eAAiC,GAAG,SAAgB,QAAQ,YAAY,GAAG,KAAK;CACtF,MAAM,2BAAyD,GAAG,SACjE,QAAQ,wBAAwB,GAAG,KAAK;AAEzC,QAAO;EACN,GAAG;EACH;EACA;EACA,QAAQ;EACR;EACA;EACA;EACA,UAAU;EACV,WAAW;EACX;EACA;EACA;EACA;EACA;EACA,MAAM;EACN,IAAI,SAAS;AACZ,UAAO,WAAW,SAAS,CAAC;;EAE7B"}
|
|
1
|
+
{"version":3,"file":"db.js","names":[],"sources":["../../src/cockroach-core/db.ts"],"sourcesContent":["import type * as V1 from '~/_relations.ts';\nimport type { CockroachDialect } from '~/cockroach-core/dialect.ts';\nimport {\n\tCockroachDeleteBase,\n\tCockroachInsertBuilder,\n\tCockroachSelectBuilder,\n\tCockroachUpdateBuilder,\n\ttype NoDuplicateColumns,\n\tQueryBuilder,\n} from '~/cockroach-core/query-builders/index.ts';\nimport type {\n\tCockroachQueryResultHKT,\n\tCockroachQueryResultKind,\n\tCockroachSession,\n\tCockroachTransaction,\n\tCockroachTransactionConfig,\n\tPreparedQueryConfig,\n} from '~/cockroach-core/session.ts';\nimport type { CockroachTable } from '~/cockroach-core/table.ts';\nimport { entityKind } from '~/entity.ts';\nimport type { TypedQueryBuilder } from '~/query-builders/query-builder.ts';\nimport { SelectionProxyHandler } from '~/selection-proxy.ts';\nimport { type ColumnsSelection, type SQL, sql, type SQLWrapper } from '~/sql/sql.ts';\nimport { WithSubquery } from '~/subquery.ts';\nimport type { InferInsertModel, RequiredInsertKeys } from '~/table.ts';\nimport type { DrizzleTypeError, IsNever, JoinUnion, NeonAuthToken } from '~/utils.ts';\nimport type { CockroachColumn } from './columns/index.ts';\nimport { CockroachCountBuilder } from './query-builders/count.ts';\nimport { RelationalQueryBuilder } from './query-builders/query.ts';\nimport { CockroachRaw } from './query-builders/raw.ts';\nimport { CockroachRefreshMaterializedView } from './query-builders/refresh-materialized-view.ts';\nimport type { SelectedFields } from './query-builders/select.types.ts';\nimport type { WithBuilder } from './subquery.ts';\nimport type { CockroachViewBase } from './view-base.ts';\nimport type { CockroachMaterializedView } from './view.ts';\n\nexport class CockroachDatabase<\n\tTQueryResult extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown> = Record<string, never>,\n\tTSchema extends V1.TablesRelationalConfig = V1.ExtractTablesWithRelations<TFullSchema>,\n> {\n\tstatic readonly [entityKind]: string = 'CockroachDatabase';\n\n\tdeclare readonly _: {\n\t\treadonly schema: TSchema | undefined;\n\t\treadonly fullSchema: TFullSchema;\n\t\treadonly tableNamesMap: Record<string, string>;\n\t\treadonly session: CockroachSession<TQueryResult, TFullSchema, TSchema>;\n\t};\n\n\t_query: TFullSchema extends Record<string, never>\n\t\t? DrizzleTypeError<'Seems like the schema generic is missing - did you forget to add it to your DB type?'>\n\t\t: {\n\t\t\t[K in keyof TSchema]: RelationalQueryBuilder<TSchema, TSchema[K]>;\n\t\t};\n\n\tconstructor(\n\t\t/** @internal */\n\t\treadonly dialect: CockroachDialect,\n\t\t/** @internal */\n\t\treadonly session: CockroachSession<any, any, any>,\n\t\tschema: V1.RelationalSchemaConfig<TSchema> | undefined,\n\t) {\n\t\tthis._ = schema\n\t\t\t? {\n\t\t\t\tschema: schema.schema,\n\t\t\t\tfullSchema: schema.fullSchema as TFullSchema,\n\t\t\t\ttableNamesMap: schema.tableNamesMap,\n\t\t\t\tsession,\n\t\t\t}\n\t\t\t: {\n\t\t\t\tschema: undefined,\n\t\t\t\tfullSchema: {} as TFullSchema,\n\t\t\t\ttableNamesMap: {},\n\t\t\t\tsession,\n\t\t\t};\n\t\tthis._query = {} as typeof this['_query'];\n\t\tif (this._.schema) {\n\t\t\tfor (const [tableName, columns] of Object.entries(this._.schema)) {\n\t\t\t\t(this._query as CockroachDatabase<TQueryResult, Record<string, any>>['_query'])[tableName] =\n\t\t\t\t\tnew RelationalQueryBuilder(\n\t\t\t\t\t\tschema!.fullSchema,\n\t\t\t\t\t\tthis._.schema,\n\t\t\t\t\t\tthis._.tableNamesMap,\n\t\t\t\t\t\tschema!.fullSchema[tableName] as CockroachTable,\n\t\t\t\t\t\tcolumns,\n\t\t\t\t\t\tdialect,\n\t\t\t\t\t\tsession,\n\t\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Creates a subquery that defines a temporary named result set as a CTE.\n\t *\n\t * It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param alias The alias for the subquery.\n\t *\n\t * Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Create a subquery with alias 'sq' and use it in the select query\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t *\n\t * To select arbitrary SQL values as fields in a CTE and reference them in other CTEs or in the main query, you need to add aliases to them:\n\t *\n\t * ```ts\n\t * // Select an arbitrary SQL value as a field in a CTE and reference it in the main query\n\t * const sq = db.$with('sq').as(db.select({\n\t * name: sql<string>`upper(${users.name})`.as('name'),\n\t * })\n\t * .from(users));\n\t *\n\t * const result = await db.with(sq).select({ name: sq.name }).from(sq);\n\t * ```\n\t */\n\t$with: WithBuilder = (alias: string, selection?: ColumnsSelection) => {\n\t\tconst self = this;\n\t\tconst as = (\n\t\t\tqb:\n\t\t\t\t| TypedQueryBuilder<ColumnsSelection | undefined>\n\t\t\t\t| SQL\n\t\t\t\t| ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection | undefined> | SQL),\n\t\t) => {\n\t\t\tif (typeof qb === 'function') {\n\t\t\t\tqb = qb(new QueryBuilder(self.dialect));\n\t\t\t}\n\n\t\t\treturn new Proxy(\n\t\t\t\tnew WithSubquery(\n\t\t\t\t\tqb.getSQL(),\n\t\t\t\t\tselection ?? ('getSelectedFields' in qb ? qb.getSelectedFields() ?? {} : {}) as SelectedFields,\n\t\t\t\t\talias,\n\t\t\t\t\ttrue,\n\t\t\t\t),\n\t\t\t\tnew SelectionProxyHandler({ alias, sqlAliasedBehavior: 'alias', sqlBehavior: 'error' }),\n\t\t\t);\n\t\t};\n\t\treturn { as };\n\t};\n\n\t$count(\n\t\tsource: CockroachTable | CockroachViewBase | SQL | SQLWrapper,\n\t\tfilters?: SQL<unknown>,\n\t) {\n\t\treturn new CockroachCountBuilder({ source, filters, session: this.session, dialect: this.dialect });\n\t}\n\n\t/**\n\t * Incorporates a previously defined CTE (using `$with`) into the main query.\n\t *\n\t * This method allows the main query to reference a temporary named result set.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#with-clause}\n\t *\n\t * @param queries The CTEs to incorporate into the main query.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Define a subquery 'sq' as a CTE using $with\n\t * const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));\n\t *\n\t * // Incorporate the CTE 'sq' into the main query and select from it\n\t * const result = await db.with(sq).select().from(sq);\n\t * ```\n\t */\n\twith(...queries: WithSubquery[]) {\n\t\tconst self = this;\n\n\t\t/**\n\t\t * Creates a select query.\n\t\t *\n\t\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Select all columns and all rows from the 'cars' table\n\t\t * const allCars: Car[] = await db.select().from(cars);\n\t\t *\n\t\t * // Select specific columns and all rows from the 'cars' table\n\t\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * brand: cars.brand\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t *\n\t\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t\t *\n\t\t * ```ts\n\t\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t\t * id: cars.id,\n\t\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t\t * })\n\t\t * .from(cars);\n\t\t * ```\n\t\t */\n\t\tfunction select(): CockroachSelectBuilder<undefined>;\n\t\tfunction select<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\t\tfunction select<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct` expression to the select query.\n\t\t *\n\t\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select all unique rows from the 'cars' table\n\t\t * await db.selectDistinct()\n\t\t * .from(cars)\n\t\t * .orderBy(cars.id, cars.brand, cars.color);\n\t\t *\n\t\t * // Select all unique brands from the 'cars' table\n\t\t * await db.selectDistinct({ brand: cars.brand })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinct(): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinct<TSelection extends SelectedFields>(\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: true,\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Adds `distinct on` expression to the select query.\n\t\t *\n\t\t * Calling this method will specify how the unique rows are determined.\n\t\t *\n\t\t * Use `.from()` method to specify which table to select from.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t\t *\n\t\t * @param on The expression defining uniqueness.\n\t\t * @param fields The selection object.\n\t\t *\n\t\t * @example\n\t\t * ```ts\n\t\t * // Select the first row for each unique brand from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand])\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand);\n\t\t *\n\t\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t\t * .from(cars)\n\t\t * .orderBy(cars.brand, cars.color);\n\t\t * ```\n\t\t */\n\t\tfunction selectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields: TSelection,\n\t\t): CockroachSelectBuilder<TSelection>;\n\t\tfunction selectDistinctOn<TSelection extends SelectedFields>(\n\t\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\t\tfields?: TSelection,\n\t\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\t\treturn new CockroachSelectBuilder({\n\t\t\t\tfields: fields ?? undefined,\n\t\t\t\tsession: self.session,\n\t\t\t\tdialect: self.dialect,\n\t\t\t\twithList: queries,\n\t\t\t\tdistinct: { on },\n\t\t\t});\n\t\t}\n\n\t\t/**\n\t\t * Creates an update query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t\t *\n\t\t * Use `.set()` method to specify which values to update.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t\t *\n\t\t * @param table The table to update.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Update all rows in the 'cars' table\n\t\t * await db.update(cars).set({ color: 'red' });\n\t\t *\n\t\t * // Update rows with filters and conditions\n\t\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t\t *\n\t\t * // Update with returning clause\n\t\t * const updatedCar: Car[] = await db.update(cars)\n\t\t * .set({ color: 'red' })\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction update<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachUpdateBuilder(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\t/**\n\t\t * Creates an insert query.\n\t\t *\n\t\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t\t *\n\t\t * @param table The table to insert into.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Insert one row\n\t\t * await db.insert(cars).values({ brand: 'BMW' });\n\t\t *\n\t\t * // Insert multiple rows\n\t\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t\t *\n\t\t * // Insert only selected columns\n\t\t * await db.insert(cars, 'brand', 'productionYear').values([{ brand: 'BMW', productionYear: 1995 }, { brand: 'Porsche', productionYear: 1989 }]);\n\t\t *\n\t\t * // Insert with returning clause\n\t\t * const insertedCar: Car[] = await db.insert(cars)\n\t\t * .values({ brand: 'BMW' })\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction insert<\n\t\t\tTTable extends CockroachTable,\n\t\t\tTColumnList extends (keyof InferInsertModel<TTable>)[] = [],\n\t\t\tTRequiredKeys extends string = RequiredInsertKeys<TTable>,\n\t\t>(\n\t\t\ttable: TTable,\n\t\t\t...columns: TColumnList extends [] ? []\n\t\t\t\t: IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t\t: [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t\t: DrizzleTypeError<\n\t\t\t\t\t`Column selection is missing following required columns: ${JoinUnion<\n\t\t\t\t\t\t`\"${Exclude<TRequiredKeys, TColumnList[number]>}\"`,\n\t\t\t\t\t\t', '\n\t\t\t\t\t>}`\n\t\t\t\t>[]\n\t\t): CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? 'all' : TColumnList>;\n\t\tfunction insert<TTable extends CockroachTable>(\n\t\t\ttable: TTable,\n\t\t\t...columns: string[]\n\t\t): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\t\treturn new CockroachInsertBuilder(\n\t\t\t\ttable,\n\t\t\t\tself.session,\n\t\t\t\tself.dialect,\n\t\t\t\tqueries,\n\t\t\t\tcolumns.length ? columns : undefined,\n\t\t\t);\n\t\t}\n\n\t\t/**\n\t\t * Creates a delete query.\n\t\t *\n\t\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t\t *\n\t\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t\t *\n\t\t * @param table The table to delete from.\n\t\t *\n\t\t * @example\n\t\t *\n\t\t * ```ts\n\t\t * // Delete all rows in the 'cars' table\n\t\t * await db.delete(cars);\n\t\t *\n\t\t * // Delete rows with filters and conditions\n\t\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t\t *\n\t\t * // Delete with returning clause\n\t\t * const deletedCar: Car[] = await db.delete(cars)\n\t\t * .where(eq(cars.id, 1))\n\t\t * .returning();\n\t\t * ```\n\t\t */\n\t\tfunction delete_<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\t\treturn new CockroachDeleteBase(table, self.session, self.dialect, queries);\n\t\t}\n\n\t\treturn { select, selectDistinct, selectDistinctOn, update, insert, delete: delete_ };\n\t}\n\n\t/**\n\t * Creates a select query.\n\t *\n\t * Calling this method with no arguments will select all columns from the table. Pass a selection object to specify the columns you want to select.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Select all columns and all rows from the 'cars' table\n\t * const allCars: Car[] = await db.select().from(cars);\n\t *\n\t * // Select specific columns and all rows from the 'cars' table\n\t * const carsIdsAndBrands: { id: number; brand: string }[] = await db.select({\n\t * id: cars.id,\n\t * brand: cars.brand\n\t * })\n\t * .from(cars);\n\t * ```\n\t *\n\t * Like in SQL, you can use arbitrary expressions as selection fields, not just table columns:\n\t *\n\t * ```ts\n\t * // Select specific columns along with expression and all rows from the 'cars' table\n\t * const carsIdsAndLowerNames: { id: number; lowerBrand: string }[] = await db.select({\n\t * id: cars.id,\n\t * lowerBrand: sql<string>`lower(${cars.brand})`,\n\t * })\n\t * .from(cars);\n\t * ```\n\t */\n\tselect(): CockroachSelectBuilder<undefined>;\n\tselect<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselect<TSelection extends SelectedFields>(fields?: TSelection): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct` expression to the select query.\n\t *\n\t * Calling this method will return only unique values. When multiple columns are selected, it returns rows with unique combinations of values in these columns.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select all unique rows from the 'cars' table\n\t * await db.selectDistinct()\n\t * .from(cars)\n\t * .orderBy(cars.id, cars.brand, cars.color);\n\t *\n\t * // Select all unique brands from the 'cars' table\n\t * await db.selectDistinct({ brand: cars.brand })\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t * ```\n\t */\n\tselectDistinct(): CockroachSelectBuilder<undefined>;\n\tselectDistinct<TSelection extends SelectedFields>(fields: TSelection): CockroachSelectBuilder<TSelection>;\n\tselectDistinct<TSelection extends SelectedFields>(\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: true,\n\t\t});\n\t}\n\n\t/**\n\t * Adds `distinct on` expression to the select query.\n\t *\n\t * Calling this method will specify how the unique rows are determined.\n\t *\n\t * Use `.from()` method to specify which table to select from.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/select#distinct}\n\t *\n\t * @param on The expression defining uniqueness.\n\t * @param fields The selection object.\n\t *\n\t * @example\n\t * ```ts\n\t * // Select the first row for each unique brand from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand])\n\t * .from(cars)\n\t * .orderBy(cars.brand);\n\t *\n\t * // Selects the first occurrence of each unique car brand along with its color from the 'cars' table\n\t * await db.selectDistinctOn([cars.brand], { brand: cars.brand, color: cars.color })\n\t * .from(cars)\n\t * .orderBy(cars.brand, cars.color);\n\t * ```\n\t */\n\tselectDistinctOn(on: (CockroachColumn | SQLWrapper)[]): CockroachSelectBuilder<undefined>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields: TSelection,\n\t): CockroachSelectBuilder<TSelection>;\n\tselectDistinctOn<TSelection extends SelectedFields>(\n\t\ton: (CockroachColumn | SQLWrapper)[],\n\t\tfields?: TSelection,\n\t): CockroachSelectBuilder<TSelection | undefined> {\n\t\treturn new CockroachSelectBuilder({\n\t\t\tfields: fields ?? undefined,\n\t\t\tsession: this.session,\n\t\t\tdialect: this.dialect,\n\t\t\tdistinct: { on },\n\t\t});\n\t}\n\n\t/**\n\t * Creates an update query.\n\t *\n\t * Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.\n\t *\n\t * Use `.set()` method to specify which values to update.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/update}\n\t *\n\t * @param table The table to update.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Update all rows in the 'cars' table\n\t * await db.update(cars).set({ color: 'red' });\n\t *\n\t * // Update rows with filters and conditions\n\t * await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));\n\t *\n\t * // Update with returning clause\n\t * const updatedCar: Car[] = await db.update(cars)\n\t * .set({ color: 'red' })\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tupdate<TTable extends CockroachTable>(table: TTable): CockroachUpdateBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachUpdateBuilder(table, this.session, this.dialect);\n\t}\n\n\t/**\n\t * Creates an insert query.\n\t *\n\t * Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/insert}\n\t *\n\t * @param table The table to insert into.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Insert one row\n\t * await db.insert(cars).values({ brand: 'BMW' });\n\t *\n\t * // Insert multiple rows\n\t * await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);\n\t *\n\t * // Insert only selected columns\n\t * await db.insert(cars, 'brand', 'productionYear').values([{ brand: 'BMW', productionYear: 1995 }, { brand: 'Porsche', productionYear: 1989 }]);\n\t *\n\t * // Insert with returning clause\n\t * const insertedCar: Car[] = await db.insert(cars)\n\t * .values({ brand: 'BMW' })\n\t * .returning();\n\t * ```\n\t */\n\tinsert<\n\t\tTTable extends CockroachTable,\n\t\tTColumnList extends (keyof InferInsertModel<TTable>)[] = [],\n\t\tTRequiredKeys extends string = RequiredInsertKeys<TTable>,\n\t>(\n\t\ttable: TTable,\n\t\t...columns: TColumnList extends [] ? []\n\t\t\t: IsNever<TRequiredKeys> extends true ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t: [TRequiredKeys] extends [TColumnList[number]] ? TColumnList & NoDuplicateColumns<TColumnList>\n\t\t\t: DrizzleTypeError<\n\t\t\t\t`Column selection is missing following required columns: ${JoinUnion<\n\t\t\t\t\t`\"${Exclude<TRequiredKeys, TColumnList[number]>}\"`,\n\t\t\t\t\t', '\n\t\t\t\t>}`\n\t\t\t>[]\n\t): CockroachInsertBuilder<TTable, TQueryResult, TColumnList extends [] ? 'all' : TColumnList>;\n\tinsert<TTable extends CockroachTable>(\n\t\ttable: TTable,\n\t\t...columns: string[]\n\t): CockroachInsertBuilder<TTable, TQueryResult> {\n\t\treturn new CockroachInsertBuilder(\n\t\t\ttable,\n\t\t\tthis.session,\n\t\t\tthis.dialect,\n\t\t\tundefined,\n\t\t\tcolumns.length ? columns : undefined,\n\t\t);\n\t}\n\n\t/**\n\t * Creates a delete query.\n\t *\n\t * Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.\n\t *\n\t * See docs: {@link https://orm.drizzle.team/docs/delete}\n\t *\n\t * @param table The table to delete from.\n\t *\n\t * @example\n\t *\n\t * ```ts\n\t * // Delete all rows in the 'cars' table\n\t * await db.delete(cars);\n\t *\n\t * // Delete rows with filters and conditions\n\t * await db.delete(cars).where(eq(cars.color, 'green'));\n\t *\n\t * // Delete with returning clause\n\t * const deletedCar: Car[] = await db.delete(cars)\n\t * .where(eq(cars.id, 1))\n\t * .returning();\n\t * ```\n\t */\n\tdelete<TTable extends CockroachTable>(table: TTable): CockroachDeleteBase<TTable, TQueryResult> {\n\t\treturn new CockroachDeleteBase(table, this.session, this.dialect);\n\t}\n\n\trefreshMaterializedView<TView extends CockroachMaterializedView>(\n\t\tview: TView,\n\t): CockroachRefreshMaterializedView<TQueryResult> {\n\t\treturn new CockroachRefreshMaterializedView(view, this.session, this.dialect);\n\t}\n\n\tprotected authToken?: NeonAuthToken;\n\n\texecute<TRow extends Record<string, unknown> = Record<string, unknown>>(\n\t\tquery: SQLWrapper | string,\n\t): CockroachRaw<CockroachQueryResultKind<TQueryResult, TRow>> {\n\t\tconst sequel = typeof query === 'string' ? sql.raw(query) : query.getSQL();\n\t\tconst builtQuery = this.dialect.sqlToQuery(sequel);\n\t\tconst prepared = this.session.prepareQuery<\n\t\t\tPreparedQueryConfig & { execute: CockroachQueryResultKind<TQueryResult, TRow> }\n\t\t>(\n\t\t\tbuiltQuery,\n\t\t\tundefined,\n\t\t\tundefined,\n\t\t);\n\t\treturn new CockroachRaw(\n\t\t\t() => prepared.execute(undefined, this.authToken),\n\t\t\tsequel,\n\t\t\tbuiltQuery,\n\t\t\t(result) => prepared.mapResult(result, true),\n\t\t);\n\t}\n\n\ttransaction<T>(\n\t\ttransaction: (tx: CockroachTransaction<TQueryResult, TFullSchema, TSchema>) => Promise<T>,\n\t\tconfig?: CockroachTransactionConfig,\n\t): Promise<T> {\n\t\treturn this.session.transaction(transaction, config);\n\t}\n}\n\nexport type CockroachWithReplicas<Q> = Q & { $primary: Q };\n\nexport const withReplicas = <\n\tHKT extends CockroachQueryResultHKT,\n\tTFullSchema extends Record<string, unknown>,\n\tTSchema extends V1.TablesRelationalConfig,\n\tQ extends CockroachDatabase<\n\t\tHKT,\n\t\tTFullSchema,\n\t\tTSchema extends Record<string, unknown> ? V1.ExtractTablesWithRelations<TFullSchema> : TSchema\n\t>,\n>(\n\tprimary: Q,\n\treplicas: [Q, ...Q[]],\n\tgetReplica: (replicas: Q[]) => Q = () => replicas[Math.floor(Math.random() * replicas.length)]!,\n): CockroachWithReplicas<Q> => {\n\tconst select: Q['select'] = (...args: []) => getReplica(replicas).select(...args);\n\tconst selectDistinct: Q['selectDistinct'] = (...args: []) => getReplica(replicas).selectDistinct(...args);\n\tconst selectDistinctOn: Q['selectDistinctOn'] = (...args: [any]) => getReplica(replicas).selectDistinctOn(...args);\n\tconst $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);\n\tconst _with: Q['with'] = (...args: any) => getReplica(replicas).with(...args);\n\tconst $with: Q['$with'] = (arg: any) => getReplica(replicas).$with(arg) as any;\n\n\tconst update: Q['update'] = (...args: [any]) => primary.update(...args);\n\tconst insert: Q['insert'] = ((...args: [any]) => primary.insert(...args)) as Q['insert'];\n\tconst $delete: Q['delete'] = (...args: [any]) => primary.delete(...args);\n\tconst execute: Q['execute'] = (...args: [any]) => primary.execute(...args);\n\tconst transaction: Q['transaction'] = (...args: [any]) => primary.transaction(...args);\n\tconst refreshMaterializedView: Q['refreshMaterializedView'] = (...args: [any]) =>\n\t\tprimary.refreshMaterializedView(...args);\n\n\treturn {\n\t\t...primary,\n\t\tupdate,\n\t\tinsert,\n\t\tdelete: $delete,\n\t\texecute,\n\t\ttransaction,\n\t\trefreshMaterializedView,\n\t\t$primary: primary,\n\t\t$replicas: replicas,\n\t\tselect,\n\t\tselectDistinct,\n\t\tselectDistinctOn,\n\t\t$count,\n\t\t$with,\n\t\twith: _with,\n\t\tget _query() {\n\t\t\treturn getReplica(replicas)._query;\n\t\t},\n\t};\n};\n"],"mappings":";;;;;;;;;;;AAoCA,IAAa,oBAAb,MAIE;CACD,QAAiB,cAAsB;CASvC;CAMA,YAEC,AAAS,SAET,AAAS,SACT,QACC;EAJQ;EAEA;AAGT,OAAK,IAAI,SACN;GACD,QAAQ,OAAO;GACf,YAAY,OAAO;GACnB,eAAe,OAAO;GACtB;GACA,GACC;GACD,QAAQ;GACR,YAAY,EAAE;GACd,eAAe,EAAE;GACjB;GACA;AACF,OAAK,SAAS,EAAE;AAChB,MAAI,KAAK,EAAE,OACV,MAAK,MAAM,CAAC,WAAW,YAAY,OAAO,QAAQ,KAAK,EAAE,OAAO,CAC/D,CAAC,KAAK,OAA0E,aAC/E,IAAI,uBACH,OAAQ,YACR,KAAK,EAAE,QACP,KAAK,EAAE,eACP,OAAQ,WAAW,YACnB,SACA,SACA,QACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAqCL,SAAsB,OAAe,cAAiC;EACrE,MAAM,OAAO;EACb,MAAM,MACL,OAII;AACJ,OAAI,OAAO,OAAO,WACjB,MAAK,GAAG,IAAI,aAAa,KAAK,QAAQ,CAAC;AAGxC,UAAO,IAAI,MACV,IAAI,aACH,GAAG,QAAQ,EACX,cAAc,uBAAuB,KAAK,GAAG,mBAAmB,IAAI,EAAE,GAAG,EAAE,GAC3E,OACA,KACA,EACD,IAAI,sBAAsB;IAAE;IAAO,oBAAoB;IAAS,aAAa;IAAS,CAAC,CACvF;;AAEF,SA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;CACzG,MAAM,oBAA2C,GAAG,SAAgB,WAAW,SAAS,CAAC,iBAAiB,GAAG,KAAK;CAClH,MAAM,UAAuB,GAAG,SAAgB,WAAW,SAAS,CAAC,OAAO,GAAG,KAAK;CACpF,MAAM,SAAoB,GAAG,SAAc,WAAW,SAAS,CAAC,KAAK,GAAG,KAAK;CAC7E,MAAM,SAAqB,QAAa,WAAW,SAAS,CAAC,MAAM,IAAI;CAEvE,MAAM,UAAuB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACvE,MAAM,WAAwB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACxE,MAAM,WAAwB,GAAG,SAAgB,QAAQ,OAAO,GAAG,KAAK;CACxE,MAAM,WAAyB,GAAG,SAAgB,QAAQ,QAAQ,GAAG,KAAK;CAC1E,MAAM,eAAiC,GAAG,SAAgB,QAAQ,YAAY,GAAG,KAAK;CACtF,MAAM,2BAAyD,GAAG,SACjE,QAAQ,wBAAwB,GAAG,KAAK;AAEzC,QAAO;EACN,GAAG;EACH;EACA;EACA,QAAQ;EACR;EACA;EACA;EACA,UAAU;EACV,WAAW;EACX;EACA;EACA;EACA;EACA;EACA,MAAM;EACN,IAAI,SAAS;AACZ,UAAO,WAAW,SAAS,CAAC;;EAE7B"}
|
|
@@ -302,10 +302,10 @@ var CockroachDialect = class {
|
|
|
302
302
|
const offsetSql = offset ? __sql_sql_ts.sql` offset ${offset}` : void 0;
|
|
303
303
|
return __sql_sql_ts.sql`${leftChunk}${operatorChunk}${rightChunk}${orderBySql}${limitSql}${offsetSql}`;
|
|
304
304
|
}
|
|
305
|
-
buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select }) {
|
|
305
|
+
buildInsertQuery({ table, values: valuesOrSelect, onConflict, returning, withList, select, columnList }) {
|
|
306
306
|
const valuesSqlList = [];
|
|
307
307
|
const columns = table[__table_ts.Table.Symbol.Columns];
|
|
308
|
-
const colEntries = Object.entries(columns);
|
|
308
|
+
const colEntries = columnList ? columnList.map((name) => [name, columns[name]]) : Object.entries(columns);
|
|
309
309
|
const colEntriesFiltered = select && !(0, __entity_ts.is)(valuesOrSelect, __sql_sql_ts.SQL) ? Object.keys(valuesOrSelect.getSelectedFields()).map((key) => [key, columns[key]]) : colEntries.filter(([_, col]) => !col.shouldDisableInsert());
|
|
310
310
|
const insertOrder = colEntriesFiltered.map(([, column]) => __sql_sql_ts.sql.identifier(column.name));
|
|
311
311
|
if (select) {
|