"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
    for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseError = exports.sql = void 0;
exports.useKysely = useKysely;
exports.usePostgres = usePostgres;
exports.withStack = withStack;
const utils_1 = require("@finalytic/utils");
const kysely_1 = require("kysely");
const kysely_postgres_js_1 = require("kysely-postgres-js");
const postgres_1 = __importDefault(require("postgres"));
__exportStar(require("./v1.generated"), exports);
const node_path_1 = require("node:path");
var kysely_2 = require("kysely");
Object.defineProperty(exports, "sql", { enumerable: true, get: function () { return kysely_2.sql; } });
function useKysely(postgres) {
    return new kysely_1.Kysely({
        dialect: new kysely_postgres_js_1.PostgresJSDialect({
            postgres,
        }),
        plugins: [new kysely_1.CamelCasePlugin()],
    });
}
function usePostgres(connectionString, ssl) {
    return (0, postgres_1.default)(connectionString, {
        ssl: ssl ? 'prefer' : false,
    });
}
class DatabaseError extends Error {
    constructor(message, cause) {
        super(message);
        this.cause = cause;
    }
}
exports.DatabaseError = DatabaseError;
const dirname = typeof __dirname === 'string' ? (0, node_path_1.join)(__dirname, '../../..') : '';
async function withStack(a) {
    // skip if we can't get the stack, probably in a worker
    if (!dirname)
        return await a;
    const now = performance.now();
    const error = new DatabaseError('Error in execute', undefined);
    const stack = error.stack?.split('\n').slice(3) || undefined;
    const firstLine = stack?.length && stack[0]?.includes('(') && dirname
        ? (0, node_path_1.relative)(dirname, stack[0].split('(')[1].split(')')[0])
        : undefined;
    try {
        const result = await a;
        console.log(`[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`);
        return result;
    }
    catch (e) {
        if (stack)
            error.stack = stack.join('\n').trim();
        else
            error.stack = e.stack;
        // error.cause = e;
        error.message = e.message;
        const serialized = (0, utils_1.serializeError)(e);
        delete serialized.stack;
        error.cause = serialized;
        console.error(`Error in execute: ${firstLine}`, (0, utils_1.serializeError)(error));
        console.log(`[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`);
        throw error;
    }
}
//# sourceMappingURL=index.js.map