diff --git a/packages/slonik/src/createDatabase.ts b/packages/slonik/src/createDatabase.ts new file mode 100644 index 000000000..e9e7f7039 --- /dev/null +++ b/packages/slonik/src/createDatabase.ts @@ -0,0 +1,26 @@ +import { createPool } from "slonik"; + +import createClientConfiguration from "./factories/createClientConfiguration"; + +import type { Database } from "./types"; +import type { ClientConfiguration, DatabasePool } from "slonik"; + +const createDatabase = async ( + connectionString: string, + clientConfiguration?: Partial +): Promise => { + const pool: DatabasePool = await createPool( + connectionString, + createClientConfiguration(clientConfiguration) + ); + + const database: Database = { + connect: pool.connect.bind(pool), + pool, + query: pool.query.bind(pool), + }; + + return database; +}; + +export default createDatabase; diff --git a/packages/slonik/src/index.ts b/packages/slonik/src/index.ts index d828b143e..009917ef9 100644 --- a/packages/slonik/src/index.ts +++ b/packages/slonik/src/index.ts @@ -34,15 +34,6 @@ declare module "@dzangolab/fastify-config" { export { default } from "./plugin"; -export type { - Database, - FilterInput, - Service, - SlonikConfig, - SortInput, - SqlFactory, -} from "./types"; - export { createFilterFragment, createLimitFragment, @@ -52,5 +43,15 @@ export { createWhereIdFragment, } from "./sql"; +export { default as createDatabase } from "./createDatabase"; export { default as BaseService } from "./service"; export { default as DefaultSqlFactory } from "./sqlFactory"; + +export type { + Database, + FilterInput, + Service, + SlonikConfig, + SortInput, + SqlFactory, +} from "./types"; diff --git a/packages/slonik/src/slonik.ts b/packages/slonik/src/slonik.ts index 149796e59..b176fc2b5 100644 --- a/packages/slonik/src/slonik.ts +++ b/packages/slonik/src/slonik.ts @@ -1,7 +1,10 @@ // [OP 2023-JAN-28] Copy/pasted from https://github.com/spa5k/fastify-slonik/blob/main/src/index.ts import fastifyPlugin from "fastify-plugin"; -import { createPool, sql } from "slonik"; +import { sql } from "slonik"; +import createDatabase from "./createDatabase"; + +import type { Database } from "./types"; import type { FastifyInstance } from "fastify"; import type { DatabasePool } from "slonik"; import type { @@ -38,28 +41,19 @@ declare module "fastify" { const plugin = async (fastify: FastifyInstance, options: SlonikOptions) => { const { connectionString, clientConfiguration } = options; - let pool: DatabasePool; - try { - pool = await createPool(connectionString, clientConfiguration); - } catch (error) { - fastify.log.error("🔴 Error happened while connecting to Postgres DB"); - throw new Error(error as string); - } + let db: Database; try { - await pool.connect(async () => { + db = await createDatabase(connectionString, clientConfiguration); + + await db.pool.connect(async () => { fastify.log.info("✅ Connected to Postgres DB"); }); - } catch { + } catch (error) { fastify.log.error("🔴 Error happened while connecting to Postgres DB"); + throw new Error(error as string); } - const db = { - connect: pool.connect.bind(pool), - pool, - query: pool.query.bind(pool), - }; - if (!fastify.hasDecorator("slonik") && !fastify.hasDecorator("sql")) { fastify.decorate("slonik", db); fastify.decorate("sql", sql);