kysely-planetscale
Version:
Kysely dialect for PlanetScale Serverless
146 lines (144 loc) • 5.14 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, {
PlanetScaleDialect: () => PlanetScaleDialect,
inflateDates: () => inflateDates
});
module.exports = __toCommonJS(src_exports);
var import_database = require("@planetscale/database");
var import_date_fns = require("date-fns");
var import_kysely = require("kysely");
var PlanetScaleDialect = class {
#config;
constructor(config) {
this.#config = config;
}
createAdapter() {
return new import_kysely.MysqlAdapter();
}
createDriver() {
return new PlanetScaleDriver(this.#config);
}
createQueryCompiler() {
return new import_kysely.MysqlQueryCompiler();
}
createIntrospector(db) {
return new import_kysely.MysqlIntrospector(db);
}
};
var PlanetScaleDriver = class {
#client;
constructor(config) {
this.#client = new import_database.Client({ cast: inflateDates, ...config });
}
async init() {
}
async acquireConnection() {
return new PlanetScaleConnection(this.#client);
}
async beginTransaction(conn, settings) {
return await conn.beginTransaction(settings);
}
async commitTransaction(conn) {
return await conn.commitTransaction();
}
async rollbackTransaction(conn) {
return await conn.rollbackTransaction();
}
async releaseConnection(_conn) {
}
async destroy() {
}
};
var sharedConnections = /* @__PURE__ */ new WeakMap();
var PlanetScaleConnection = class {
#client;
#transactionConn;
#useSharedConnection;
get #config() {
return this.#client.config;
}
constructor(client, useSharedConnection = false, isForTransaction = false) {
this.#client = client;
this.#useSharedConnection = useSharedConnection && !isForTransaction;
if (this.#useSharedConnection) {
sharedConnections.set(this.#config, sharedConnections.get(this.#config) ?? this.#client.connection());
}
}
async executeQuery(compiledQuery) {
if (this.#transactionConn) return this.execute(compiledQuery, this.#transactionConn);
return this.#useSharedConnection ? this.execute(compiledQuery, sharedConnections.get(this.#config) || this.#client) : this.execute(compiledQuery, this.#client);
}
async execute(compiledQuery, conn) {
const parameters = this.#config.format ? compiledQuery.parameters : compiledQuery.parameters.map((param) => param instanceof Date ? formatDate(param) : param);
const results = await conn.execute(compiledQuery.sql, parameters);
if (results.error) {
throw results.error;
}
const numAffectedRows = results.rowsAffected == null ? void 0 : BigInt(results.rowsAffected);
return {
insertId: results.insertId !== null && results.insertId.toString() !== "0" ? BigInt(results.insertId) : void 0,
rows: results.rows,
// @ts-ignore replaces `QueryResult.numUpdatedOrDeletedRows` in kysely > 0.22
// following https://github.com/koskimas/kysely/pull/188
numAffectedRows
};
}
async beginTransaction(settings) {
this.#transactionConn = this.#transactionConn ?? this.#client.connection();
if (settings.isolationLevel) {
await this.#transactionConn.execute(`SET TRANSACTION ISOLATION LEVEL ${settings.isolationLevel}`);
}
await this.#transactionConn.execute("BEGIN");
}
async commitTransaction() {
if (!this.#transactionConn) throw new Error("No transaction to commit");
try {
await this.#transactionConn.execute("COMMIT");
} finally {
this.#transactionConn = void 0;
}
}
async rollbackTransaction() {
if (!this.#transactionConn) throw new Error("No transaction to rollback");
try {
await this.#transactionConn.execute("ROLLBACK");
} finally {
this.#transactionConn = void 0;
}
}
async *streamQuery(_compiledQuery, _chunkSize) {
throw new Error("PlanetScale Serverless Driver does not support streaming");
}
};
function inflateDates(field, value) {
if (field.type === "DATETIME" && value) return (0, import_date_fns.parseJSON)(value);
if (field.type === "TIMESTAMP" && value) return (0, import_date_fns.parseJSON)(value);
return (0, import_database.cast)(field, value);
}
function formatDate(date) {
return date.toISOString().replace(/[TZ]/g, " ").trim();
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PlanetScaleDialect,
inflateDates
});