← Files RivetARCHIVED FILE
node_modules/drizzle-orm/sqlite-core/async/db.cjs
13.4 KB · Nov 5, 2026 · 12:09 UTC
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const require_runtime = require('../../_virtual/_rolldown/runtime.cjs');
const require_sqlite_core_async_count = require('./count.cjs');
const require_sqlite_core_query_builders_query = require('../query-builders/query.cjs');
const require_sqlite_core_async_query = require('./query.cjs');
let __entity_ts = require("../../entity.cjs");
let __subquery_ts = require("../../subquery.cjs");
let __sql_sql_ts = require("../../sql/sql.cjs");
let __selection_proxy_ts = require("../../selection-proxy.cjs");
let __sqlite_core_async_delete_ts = require("./delete.cjs");
let __sqlite_core_async_insert_ts = require("./insert.cjs");
let __sqlite_core_async_raw_ts = require("./raw.cjs");
let __sqlite_core_async_select_ts = require("./select.cjs");
let __sqlite_core_async_update_ts = require("./update.cjs");
let __sqlite_core_query_builders_index_ts = require("../query-builders/index.cjs");
//#region src/sqlite-core/async/db.ts
var SQLiteAsyncDatabase = class {
static [__entity_ts.entityKind] = "BaseSQLiteDatabase";
query;
constructor(resultKind, dialect, session, relations, forbidJsonb) {
this.resultKind = resultKind;
this.dialect = dialect;
this.session = session;
this.forbidJsonb = forbidJsonb;
this._ = {
relations,
session,
resultKind
};
this.query = {};
for (const [tableName, relation] of Object.entries(relations)) this.query[tableName] = new require_sqlite_core_query_builders_query.RelationalQueryBuilder(resultKind, relations, relations[relation.name].table, relation, dialect, session, forbidJsonb, resultKind === "sync" ? require_sqlite_core_async_query.SQLiteSyncRelationalQuery : require_sqlite_core_async_query.SQLiteAsyncRelationalQuery);
this.$cache = { invalidate: async (_params) => {} };
}
/**
* Creates a subquery that defines a temporary named result set as a CTE.
*
* It is useful for breaking down complex queries into simpler parts and for reusing the result set in subsequent parts of the query.
*
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
*
* @param alias The alias for the subquery.
*
* Failure to provide an alias will result in a DrizzleTypeError, preventing the subquery from being referenced in other queries.
*
* @example
*
* ```ts
* // Create a subquery with alias 'sq' and use it in the select query
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
*
* const result = await db.with(sq).select().from(sq);
* ```
*
* 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:
*
* ```ts
* // Select an arbitrary SQL value as a field in a CTE and reference it in the main query
* const sq = db.$with('sq').as(db.select({
* name: sql<string>`upper(${users.name})`.as('name'),
* })
* .from(users));
*
* const result = await db.with(sq).select({ name: sq.name }).from(sq);
* ```
*/
$with = (alias, selection) => {
const self = this;
const as = (qb) => {
if (typeof qb === "function") qb = qb(new __sqlite_core_query_builders_index_ts.QueryBuilder(self.dialect));
return new Proxy(new __subquery_ts.WithSubquery(qb.getSQL(), selection ?? ("getSelectedFields" in qb ? qb.getSelectedFields() ?? {} : {}), alias, true), new __selection_proxy_ts.SelectionProxyHandler({
alias,
sqlAliasedBehavior: "alias",
sqlBehavior: "error"
}));
};
return { as };
};
$count(source, filters) {
return this.resultKind === "async" ? new require_sqlite_core_async_count.SQLiteAsyncCountBuilder({
source,
filters,
session: this.session,
dialect: this.dialect
}) : new require_sqlite_core_async_count.SQLiteSyncCountBuilder({
source,
filters,
session: this.session,
dialect: this.dialect
});
}
/**
* Incorporates a previously defined CTE (using `$with`) into the main query.
*
* This method allows the main query to reference a temporary named result set.
*
* See docs: {@link https://orm.drizzle.team/docs/select#with-clause}
*
* @param queries The CTEs to incorporate into the main query.
*
* @example
*
* ```ts
* // Define a subquery 'sq' as a CTE using $with
* const sq = db.$with('sq').as(db.select().from(users).where(eq(users.id, 42)));
*
* // Incorporate the CTE 'sq' into the main query and select from it
* const result = await db.with(sq).select().from(sq);
* ```
*/
with(...queries) {
const self = this;
function select(fields) {
return new __sqlite_core_query_builders_index_ts.SQLiteSelectBuilder({
fields: fields ?? void 0,
session: self.session,
dialect: self.dialect,
withList: queries
}, __sqlite_core_async_select_ts.SQLiteAsyncSelectBase);
}
function selectDistinct(fields) {
return new __sqlite_core_query_builders_index_ts.SQLiteSelectBuilder({
fields: fields ?? void 0,
session: self.session,
dialect: self.dialect,
withList: queries,
distinct: true
}, __sqlite_core_async_select_ts.SQLiteAsyncSelectBase);
}
/**
* Creates an update query.
*
* Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
*
* Use `.set()` method to specify which values to update.
*
* See docs: {@link https://orm.drizzle.team/docs/update}
*
* @param table The table to update.
*
* @example
*
* ```ts
* // Update all rows in the 'cars' table
* await db.update(cars).set({ color: 'red' });
*
* // Update rows with filters and conditions
* await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
*
* // Update with returning clause
* const updatedCar: Car[] = await db.update(cars)
* .set({ color: 'red' })
* .where(eq(cars.id, 1))
* .returning();
* ```
*/
function update(table) {
return new __sqlite_core_query_builders_index_ts.SQLiteUpdateBuilder(table, self.session, self.dialect, queries, __sqlite_core_async_update_ts.SQLiteAsyncUpdateBase);
}
/**
* Creates an insert query.
*
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
*
* See docs: {@link https://orm.drizzle.team/docs/insert}
*
* @param table The table to insert into.
*
* @example
*
* ```ts
* // Insert one row
* await db.insert(cars).values({ brand: 'BMW' });
*
* // Insert multiple rows
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
*
* // Insert with returning clause
* const insertedCar: Car[] = await db.insert(cars)
* .values({ brand: 'BMW' })
* .returning();
* ```
*/
function insert(into) {
return new __sqlite_core_query_builders_index_ts.SQLiteInsertBuilder(into, self.session, self.dialect, queries, __sqlite_core_async_insert_ts.SQLiteAsyncInsertBase);
}
/**
* Creates a delete query.
*
* Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
*
* See docs: {@link https://orm.drizzle.team/docs/delete}
*
* @param table The table to delete from.
*
* @example
*
* ```ts
* // Delete all rows in the 'cars' table
* await db.delete(cars);
*
* // Delete rows with filters and conditions
* await db.delete(cars).where(eq(cars.color, 'green'));
*
* // Delete with returning clause
* const deletedCar: Car[] = await db.delete(cars)
* .where(eq(cars.id, 1))
* .returning();
* ```
*/
function delete_(from) {
return new __sqlite_core_async_delete_ts.SQLiteAsyncDeleteBase(from, self.session, self.dialect, queries);
}
return {
select,
selectDistinct,
update,
insert,
delete: delete_
};
}
select(fields) {
return new __sqlite_core_query_builders_index_ts.SQLiteSelectBuilder({
fields: fields ?? void 0,
session: this.session,
dialect: this.dialect
}, __sqlite_core_async_select_ts.SQLiteAsyncSelectBase);
}
selectDistinct(fields) {
return new __sqlite_core_query_builders_index_ts.SQLiteSelectBuilder({
fields: fields ?? void 0,
session: this.session,
dialect: this.dialect,
distinct: true
}, __sqlite_core_async_select_ts.SQLiteAsyncSelectBase);
}
/**
* Creates an update query.
*
* Calling this method without `.where()` clause will update all rows in a table. The `.where()` clause specifies which rows should be updated.
*
* Use `.set()` method to specify which values to update.
*
* See docs: {@link https://orm.drizzle.team/docs/update}
*
* @param table The table to update.
*
* @example
*
* ```ts
* // Update all rows in the 'cars' table
* await db.update(cars).set({ color: 'red' });
*
* // Update rows with filters and conditions
* await db.update(cars).set({ color: 'red' }).where(eq(cars.brand, 'BMW'));
*
* // Update with returning clause
* const updatedCar: Car[] = await db.update(cars)
* .set({ color: 'red' })
* .where(eq(cars.id, 1))
* .returning();
* ```
*/
update(table) {
return new __sqlite_core_query_builders_index_ts.SQLiteUpdateBuilder(table, this.session, this.dialect, void 0, __sqlite_core_async_update_ts.SQLiteAsyncUpdateBase);
}
$cache;
/**
* Creates an insert query.
*
* Calling this method will create new rows in a table. Use `.values()` method to specify which values to insert.
*
* See docs: {@link https://orm.drizzle.team/docs/insert}
*
* @param table The table to insert into.
*
* @example
*
* ```ts
* // Insert one row
* await db.insert(cars).values({ brand: 'BMW' });
*
* // Insert multiple rows
* await db.insert(cars).values([{ brand: 'BMW' }, { brand: 'Porsche' }]);
*
* // Insert with returning clause
* const insertedCar: Car[] = await db.insert(cars)
* .values({ brand: 'BMW' })
* .returning();
* ```
*/
insert(into) {
return new __sqlite_core_query_builders_index_ts.SQLiteInsertBuilder(into, this.session, this.dialect, void 0, __sqlite_core_async_insert_ts.SQLiteAsyncInsertBase);
}
/**
* Creates a delete query.
*
* Calling this method without `.where()` clause will delete all rows in a table. The `.where()` clause specifies which rows should be deleted.
*
* See docs: {@link https://orm.drizzle.team/docs/delete}
*
* @param table The table to delete from.
*
* @example
*
* ```ts
* // Delete all rows in the 'cars' table
* await db.delete(cars);
*
* // Delete rows with filters and conditions
* await db.delete(cars).where(eq(cars.color, 'green'));
*
* // Delete with returning clause
* const deletedCar: Car[] = await db.delete(cars)
* .where(eq(cars.id, 1))
* .returning();
* ```
*/
delete(from) {
return new __sqlite_core_async_delete_ts.SQLiteAsyncDeleteBase(from, this.session, this.dialect);
}
run(query) {
const sequel = typeof query === "string" ? __sql_sql_ts.sql.raw(query) : query.getSQL();
const builtQuery = this.dialect.sqlToQuery(sequel);
const prepared = this.session.prepareQuery(builtQuery, "raw", false, "run");
if (this.resultKind === "async") return new __sqlite_core_async_raw_ts.SQLiteAsyncRaw(prepared, sequel, builtQuery);
return this.session.run(sequel);
}
all(query) {
const sequel = typeof query === "string" ? __sql_sql_ts.sql.raw(query) : query.getSQL();
const builtQuery = this.dialect.sqlToQuery(sequel);
const prepared = this.session.prepareQuery(builtQuery, "objects", false, "all");
if (this.resultKind === "async") return new __sqlite_core_async_raw_ts.SQLiteAsyncRaw(prepared, sequel, builtQuery);
return this.session.objects(sequel);
}
get(query) {
const sequel = typeof query === "string" ? __sql_sql_ts.sql.raw(query) : query.getSQL();
const builtQuery = this.dialect.sqlToQuery(sequel);
const prepared = this.session.prepareQuery(builtQuery, "objects", false, "get");
if (this.resultKind === "async") return new __sqlite_core_async_raw_ts.SQLiteAsyncRaw(prepared, sequel, builtQuery);
return this.session.object(sequel);
}
values(query) {
const sequel = typeof query === "string" ? __sql_sql_ts.sql.raw(query) : query.getSQL();
const builtQuery = this.dialect.sqlToQuery(sequel);
const prepared = this.session.prepareQuery(builtQuery, "objects", false, "values");
if (this.resultKind === "async") return new __sqlite_core_async_raw_ts.SQLiteAsyncRaw(prepared, sequel, builtQuery);
return this.session.arrays(sequel);
}
transaction(transaction, config) {
return this.session.transaction(transaction, config);
}
};
const withReplicas = (primary, replicas, getReplica = () => replicas[Math.floor(Math.random() * replicas.length)]) => {
const select = (...args) => getReplica(replicas).select(...args);
const selectDistinct = (...args) => getReplica(replicas).selectDistinct(...args);
const $count = (...args) => getReplica(replicas).$count(...args);
const $with = (...args) => getReplica(replicas).with(...args);
const update = (...args) => primary.update(...args);
const insert = (...args) => primary.insert(...args);
const $delete = (...args) => primary.delete(...args);
const run = (...args) => primary.run(...args);
const all = (...args) => primary.all(...args);
const get = (...args) => primary.get(...args);
const values = (...args) => primary.values(...args);
const transaction = (...args) => primary.transaction(...args);
return {
...primary,
update,
insert,
delete: $delete,
run,
all,
get,
values,
transaction,
$primary: primary,
$replicas: replicas,
select,
selectDistinct,
$count,
with: $with,
get query() {
return getReplica(replicas).query;
}
};
};
//#endregion
exports.SQLiteAsyncDatabase = SQLiteAsyncDatabase;
exports.withReplicas = withReplicas;
//# sourceMappingURL=db.cjs.mapSHA-256: e6e8ddd3f4a5f588d2ef525852378e02fee41c4e56ed39b008791a16e3fcacdc