kysely-sequelize
Version:
Kysely dialect for Sequelize
228 lines (221 loc) • 7.53 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var src_exports = {};
__export(src_exports, {
KyselySequelizeDialect: () => KyselySequelizeDialect,
KyselySequelizeDriver: () => KyselySequelizeDriver
});
module.exports = __toCommonJS(src_exports);
// src/isolation-levels.ts
var import_sequelize = require("sequelize");
var ISOLATION_LEVELS = {
"read committed": import_sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED,
"read uncommitted": import_sequelize.Transaction.ISOLATION_LEVELS.READ_UNCOMMITTED,
"repeatable read": import_sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ,
serializable: import_sequelize.Transaction.ISOLATION_LEVELS.SERIALIZABLE
};
// src/type-utils.ts
function isNumber(value) {
return typeof value === "number" && Number.isFinite(value) && !Number.isNaN(value);
}
function isObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
function isString(value) {
return typeof value === "string";
}
// src/connection.ts
var KyselySequelizeConnection = class {
#sequelize;
#connection;
#transaction;
constructor(sequelize) {
this.#sequelize = sequelize;
}
async beginTransaction(settings) {
if (this.#transaction) {
throw new Error("Transaction already begun!");
}
this.#transaction = await this.#sequelize.transaction(this.#translateTransactionSettings(settings));
}
async commitTransaction() {
if (!this.#transaction) {
throw new Error("No transaction!");
}
await this.#transaction.commit();
this.#transaction = void 0;
}
release() {
if (this.#connection) {
this.#sequelize.connectionManager.releaseConnection(this.#connection);
this.#connection = void 0;
}
}
async rollbackTransaction() {
if (!this.#transaction) {
throw new Error("No transaction!");
}
await this.#transaction.rollback();
this.#transaction = void 0;
}
async executeQuery(compiledQuery) {
const query = this.#getQuery(compiledQuery);
const queryOptions = await this.#getQueryOptions(compiledQuery);
const [rows, metadata] = await this.#sequelize.query(query, queryOptions);
return {
...this.#getLastInsertedIdAndNumAffectedRows(rows, metadata),
rows: Array.isArray(rows) ? rows : []
};
}
async *streamQuery(_compiledQuery, _chunkSize) {
throw new Error(
"Sequelize does not support streaming queries yet! follow https://github.com/sequelize/sequelize/issues/15827"
);
}
#translateTransactionSettings(settings) {
const { isolationLevel } = settings;
if (isolationLevel === "snapshot") {
throw new Error("Snapshot isolation level is not supported by Sequelize!");
}
return {
autocommit: false,
...isolationLevel ? { isolationLevel: ISOLATION_LEVELS[isolationLevel] } : {}
};
}
#getQuery(compiledQuery) {
const dialect = this.#sequelize.getDialect();
if (dialect === "mssql") {
let { sql } = compiledQuery;
compiledQuery.parameters.forEach((parameter, index) => {
sql = sql.replace(
new RegExp(`@${index + 1}([^\\d])?`),
isString(parameter) ? `N'${parameter}'$1` : `${parameter}$1`
);
});
return sql;
}
return compiledQuery.sql;
}
async #getQueryOptions(compiledQuery) {
const dialect = this.#sequelize.getDialect();
const paramsKey = ["mysql", "sqlite"].includes(dialect) ? "replacements" : ["postgres"].includes(dialect) ? "bind" : void 0;
return {
...paramsKey ? { [paramsKey]: compiledQuery.parameters } : {},
transaction: this.#transaction || {
connection: this.#connection ||= await this.#sequelize.connectionManager.getConnection({ type: "write" })
}
};
}
#getLastInsertedIdAndNumAffectedRows(rows, metadata) {
const dialect = this.#sequelize.getDialect();
if (dialect === "postgres") {
return {
numAffectedRows: isNumber(metadata) ? BigInt(metadata) : isObject(metadata) && "rowCount" in metadata && isNumber(metadata.rowCount) ? BigInt(metadata.rowCount) : void 0
};
}
if (dialect === "mysql") {
return {
insertId: isNumber(rows) ? BigInt(rows) : void 0,
numAffectedRows: isNumber(metadata) ? BigInt(metadata) : isObject(metadata) && "affectedRows" in metadata && isNumber(metadata.affectedRows) ? BigInt(metadata.affectedRows) : void 0,
numChangedRows: isObject(metadata) && "changedRows" in metadata && isNumber(metadata.changedRows) ? BigInt(metadata.changedRows) : void 0
};
}
if (dialect === "mssql") {
return {
insertId: isNumber(rows) ? BigInt(rows) : void 0,
numAffectedRows: isNumber(metadata) ? BigInt(metadata) : void 0
};
}
if (dialect === "sqlite") {
if (!isObject(metadata)) {
return {};
}
const { changes, lastID } = metadata;
return {
insertId: isNumber(lastID) ? BigInt(lastID) : void 0,
numAffectedRows: isNumber(changes) ? BigInt(changes) : void 0
};
}
return {};
}
};
// src/driver.ts
var KyselySequelizeDriver = class {
#sequelize;
constructor(config) {
this.#sequelize = config.sequelize;
}
async acquireConnection() {
return new KyselySequelizeConnection(this.#sequelize);
}
async beginTransaction(connection, settings) {
await connection.beginTransaction(settings);
}
async commitTransaction(connection) {
await connection.commitTransaction();
}
async destroy() {
await this.#sequelize.close();
}
async init() {
}
async releaseConnection(connection) {
connection.release();
}
async rollbackTransaction(connection) {
await connection.rollbackTransaction();
}
};
// src/supported-dialects.ts
var SUPPORTED_DIALECTS = ["mssql", "mysql", "postgres", "sqlite"];
function isDialectSupported(dialect) {
return SUPPORTED_DIALECTS.includes(dialect);
}
function assertSupportedDialect(dialect) {
if (!isDialectSupported(dialect)) {
throw new Error(`Unsupported dialect: ${dialect}!`);
}
}
// src/dialect.ts
var KyselySequelizeDialect = class {
#config;
constructor(config) {
assertSupportedDialect(config.sequelize.getDialect());
this.#config = config;
}
createAdapter() {
return this.#config.kyselySubDialect.createAdapter();
}
createDriver() {
return new KyselySequelizeDriver(this.#config);
}
createIntrospector(db) {
return this.#config.kyselySubDialect.createIntrospector(db);
}
createQueryCompiler() {
return this.#config.kyselySubDialect.createQueryCompiler();
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
KyselySequelizeDialect,
KyselySequelizeDriver
});
//# sourceMappingURL=index.js.map