import { serializeError } from '@finalytic/utils';
import { CamelCasePlugin, Kysely as K } from 'kysely';
import { PostgresJSDialect } from 'kysely-postgres-js';
import postgres from 'postgres';
export * from './v1.generated';
import { join, relative } from 'node:path';
export { sql } from 'kysely';
export function useKysely(postgres) {
    return new K({
        dialect: new PostgresJSDialect({
            postgres,
        }),
        plugins: [new CamelCasePlugin()],
    });
}
export function usePostgres(connectionString, ssl) {
    return postgres(connectionString, {
        ssl: ssl ? 'require' : false,
        max: 1,
    });
}
export class DatabaseError extends Error {
    constructor(message, cause) {
        super(message);
        this.cause = cause;
    }
}
const dirname = typeof __dirname === 'string' ? join(__dirname, '../../..') : '';
export 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
        ? 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 = serializeError(e);
        delete serialized.stack;
        error.cause = serialized;
        console.error(`Error in execute: ${firstLine}`, serializeError(error));
        console.log(`[${firstLine}] Execution time: ${Math.round(performance.now() - now)}ms`);
        throw error;
    }
}
//# sourceMappingURL=index.js.map