drizzle-orm
Version:
Drizzle ORM package for SQL databases
134 lines (129 loc) • 5.02 kB
JavaScript
'use strict';
var index = require('../index.cjs');
var alias = require('../alias-58b7b8c9.cjs');
var session = require('../session-f61b6e4d.cjs');
/// <reference types="bun-types" />
var _a, _b, _c;
class SQLiteBunSession extends session.SQLiteSession {
constructor(client, dialect, schema, options = {}) {
super(dialect);
this.client = client;
this.schema = schema;
this.logger = options.logger ?? new index.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;
}
}
_a = alias.entityKind;
SQLiteBunSession[_a] = 'SQLiteBunSession';
class SQLiteBunTransaction extends session.SQLiteTransaction {
transaction(transaction) {
const savepointName = `sp${this.nestedIndex}`;
const tx = new SQLiteBunTransaction('sync', this.dialect, this.session, this.schema, this.nestedIndex + 1);
this.session.run(alias.sql.raw(`savepoint ${savepointName}`));
try {
const result = transaction(tx);
this.session.run(alias.sql.raw(`release savepoint ${savepointName}`));
return result;
}
catch (err) {
this.session.run(alias.sql.raw(`rollback to savepoint ${savepointName}`));
throw err;
}
}
}
_b = alias.entityKind;
SQLiteBunTransaction[_b] = 'SQLiteBunTransaction';
class PreparedQuery extends session.PreparedQuery {
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 = alias.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 = alias.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) => alias.mapResultRow(fields, row, joinsNotNullableMap));
}
get(placeholderValues) {
const params = alias.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 alias.mapResultRow(fields, row, joinsNotNullableMap);
}
values(placeholderValues) {
const params = alias.fillPlaceholders(this.params, placeholderValues ?? {});
this.logger.logQuery(this.queryString, params);
return this.stmt.values(...params);
}
}
_c = alias.entityKind;
PreparedQuery[_c] = 'SQLiteBunPreparedQuery';
/// <reference types="bun-types" />
function drizzle(client, config = {}) {
const dialect = new session.SQLiteSyncDialect();
let logger;
if (config.logger === true) {
logger = new index.DefaultLogger();
}
else if (config.logger !== false) {
logger = config.logger;
}
let schema;
if (config.schema) {
const tablesConfig = alias.extractTablesRelationalConfig(config.schema, alias.createTableRelationsHelpers);
schema = {
fullSchema: config.schema,
schema: tablesConfig.tables,
tableNamesMap: tablesConfig.tableNamesMap,
};
}
const session$1 = new SQLiteBunSession(client, dialect, schema, { logger });
return new session.BaseSQLiteDatabase('sync', dialect, session$1, schema);
}
exports.PreparedQuery = PreparedQuery;
exports.SQLiteBunSession = SQLiteBunSession;
exports.SQLiteBunTransaction = SQLiteBunTransaction;
exports.drizzle = drizzle;
//# sourceMappingURL=index.cjs.map