diff --git a/apps/server-doj/src/server.ts b/apps/server-doj/src/server.ts index 79c38d82..2bd8d940 100644 --- a/apps/server-doj/src/server.ts +++ b/apps/server-doj/src/server.ts @@ -8,7 +8,7 @@ export const createCustomServer = async (ctx: { dbUri: string; }): Promise => { const db = createDatabaseGateway( - await createPostgresDatabaseContext(ctx.dbUri) + await createPostgresDatabaseContext(ctx.dbUri, true) ); return createServer({ diff --git a/apps/server-kansas/src/server.ts b/apps/server-kansas/src/server.ts index 79c38d82..2bd8d940 100644 --- a/apps/server-kansas/src/server.ts +++ b/apps/server-kansas/src/server.ts @@ -8,7 +8,7 @@ export const createCustomServer = async (ctx: { dbUri: string; }): Promise => { const db = createDatabaseGateway( - await createPostgresDatabaseContext(ctx.dbUri) + await createPostgresDatabaseContext(ctx.dbUri, true) ); return createServer({ diff --git a/packages/database/src/clients/knex.ts b/packages/database/src/clients/knex.ts index 40960cd7..56053b93 100644 --- a/packages/database/src/clients/knex.ts +++ b/packages/database/src/clients/knex.ts @@ -10,11 +10,15 @@ const migrationsDirectory = path.resolve( export const createKnex = (config: Knex.Config): Knex => knex(config); -export const getPostgresKnex = (connectionString: string): Knex => { +export const getPostgresKnex = ( + connectionString: string, + ssl: boolean = false +): Knex => { return knex({ client: 'pg', connection: { connectionString, + ssl, }, useNullAsDefault: true, migrations: { diff --git a/packages/database/src/clients/kysely/postgres.ts b/packages/database/src/clients/kysely/postgres.ts index 6c2024f5..e76b30f2 100644 --- a/packages/database/src/clients/kysely/postgres.ts +++ b/packages/database/src/clients/kysely/postgres.ts @@ -3,11 +3,15 @@ import pg from 'pg'; import { type Database } from './types.js'; -export const createPostgresDatabase = (connectionString: string) => { +export const createPostgresDatabase = ( + connectionString: string, + ssl: boolean +) => { return new Kysely({ dialect: new PostgresDialect({ pool: new pg.Pool({ connectionString, + ssl, }), }), }); diff --git a/packages/database/src/context/postgres.ts b/packages/database/src/context/postgres.ts index 327d9d09..95d2d6e3 100644 --- a/packages/database/src/context/postgres.ts +++ b/packages/database/src/context/postgres.ts @@ -13,18 +13,21 @@ export class PostgresDatabaseContext implements DatabaseContext { knex?: Knex; kysely?: Kysely; - constructor(public readonly connectionUri: string) {} + constructor( + public readonly connectionUri: string, + private ssl: boolean = false + ) {} async getKnex() { if (!this.knex) { - this.knex = getPostgresKnex(this.connectionUri); + this.knex = getPostgresKnex(this.connectionUri, this.ssl); } return this.knex; } async getKysely(): Promise> { if (!this.kysely) { - this.kysely = createPostgresDatabase(this.connectionUri); + this.kysely = createPostgresDatabase(this.connectionUri, this.ssl); } return this.kysely; } @@ -39,8 +42,11 @@ export class PostgresDatabaseContext implements DatabaseContext { } } -export const createPostgresDatabaseContext = async (connectionUri: string) => { - const ctx = new PostgresDatabaseContext(connectionUri); +export const createPostgresDatabaseContext = async ( + connectionUri: string, + ssl: boolean +) => { + const ctx = new PostgresDatabaseContext(connectionUri, ssl); await migrateDatabase(ctx); return ctx; };