drizzle-orm
Version:
Drizzle ORM package for SQL databases
134 lines (130 loc) • 5.11 kB
JavaScript
import { NoopLogger, DefaultLogger } from '../index.mjs';
import { e as entityKind, s as sql, f as fillPlaceholders, m as mapResultRow, a as extractTablesRelationalConfig, c as createTableRelationsHelpers } from '../alias-d302772a.mjs';
import { S as SQLiteSession, a as SQLiteTransaction, P as PreparedQuery$1, B as BaseSQLiteDatabase, b as SQLiteSyncDialect } from '../session-775da517.mjs';
/// <reference types="bun-types" />
class SQLiteBunSession extends SQLiteSession {
client;
schema;
static [entityKind] = 'SQLiteBunSession';
logger;
constructor(client, dialect, schema, options = {}) {
super(dialect);
this.client = client;
this.schema = schema;
this.logger = options.logger ?? new NoopLogger();
}
exec(query) {
this.client.exec(query);
}
prepareQuery(query, fields, executeMethod, customResultMapper) {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}
transaction(transaction, config = {}) {
const tx = new SQLiteBunTransaction('sync', this.dialect, this, this.schema);
let result;
const nativeTx = this.client.transaction(() => {
result = transaction(tx);
});
nativeTx[config.behavior ?? 'deferred']();
return result;
}
}
class SQLiteBunTransaction extends SQLiteTransaction {
static [entityKind] = 'SQLiteBunTransaction';
transaction(transaction) {
const savepointName = `sp${this.nestedIndex}`;
const tx = new SQLiteBunTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);
this.session.run(sql.raw(`savepoint ${savepointName}`));
try {
const result = transaction(tx);
this.session.run(sql.raw(`release savepoint ${savepointName}`));
return result;
}
catch (err) {
this.session.run(sql.raw(`rollback to savepoint ${savepointName}`));
throw err;
}
}
}
class PreparedQuery extends PreparedQuery$1 {
stmt;
queryString;
params;
logger;
fields;
customResultMapper;
static [entityKind] = 'SQLiteBunPreparedQuery';
constructor(stmt, queryString, params, logger, fields, executeMethod, customResultMapper) {
super('sync', executeMethod);
this.stmt = stmt;
this.queryString = queryString;
this.params = params;
this.logger = logger;
this.fields = fields;
this.customResultMapper = customResultMapper;
}
run(placeholderValues) {
const params = fillPlaceholders(this.params, placeholderValues ?? {});
this.logger.logQuery(this.queryString, params);
return this.stmt.run(...params);
}
all(placeholderValues) {
const { fields, queryString, logger, joinsNotNullableMap, stmt, customResultMapper } = this;
if (!fields && !customResultMapper) {
const params = fillPlaceholders(this.params, placeholderValues ?? {});
logger.logQuery(queryString, params);
return stmt.all(...params);
}
const rows = this.values(placeholderValues);
if (customResultMapper) {
return customResultMapper(rows);
}
return rows.map((row) => mapResultRow(fields, row, joinsNotNullableMap));
}
get(placeholderValues) {
const params = fillPlaceholders(this.params, placeholderValues ?? {});
this.logger.logQuery(this.queryString, params);
const row = this.stmt.get(...params);
if (!row) {
return undefined;
}
const { fields, joinsNotNullableMap, customResultMapper } = this;
if (!fields && !customResultMapper) {
return row;
}
if (customResultMapper) {
return customResultMapper([row]);
}
return mapResultRow(fields, row, joinsNotNullableMap);
}
values(placeholderValues) {
const params = fillPlaceholders(this.params, placeholderValues ?? {});
this.logger.logQuery(this.queryString, params);
return this.stmt.values(...params);
}
}
/// <reference types="bun-types" />
function drizzle(client, config = {}) {
const dialect = new SQLiteSyncDialect();
let logger;
if (config.logger === true) {
logger = new DefaultLogger();
}
else if (config.logger !== false) {
logger = config.logger;
}
let schema;
if (config.schema) {
const tablesConfig = extractTablesRelationalConfig(config.schema, createTableRelationsHelpers);
schema = {
fullSchema: config.schema,
schema: tablesConfig.tables,
tableNamesMap: tablesConfig.tableNamesMap,
};
}
const session = new SQLiteBunSession(client, dialect, schema, { logger });
return new BaseSQLiteDatabase('sync', dialect, session, schema);
}
export { PreparedQuery, SQLiteBunSession, SQLiteBunTransaction, drizzle };
//# sourceMappingURL=index.mjs.map