Skip to content

Commit

Permalink
on cloud, use ssl with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnaab committed Aug 19, 2024
1 parent 85c3db3 commit c268582
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/server-doj/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const createCustomServer = async (ctx: {
dbUri: string;
}): Promise<any> => {
const db = createDatabaseGateway(
await createPostgresDatabaseContext(ctx.dbUri)
await createPostgresDatabaseContext(ctx.dbUri, true)
);

return createServer({
Expand Down
2 changes: 1 addition & 1 deletion apps/server-kansas/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const createCustomServer = async (ctx: {
dbUri: string;
}): Promise<any> => {
const db = createDatabaseGateway(
await createPostgresDatabaseContext(ctx.dbUri)
await createPostgresDatabaseContext(ctx.dbUri, true)
);

return createServer({
Expand Down
6 changes: 5 additions & 1 deletion packages/database/src/clients/knex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
6 changes: 5 additions & 1 deletion packages/database/src/clients/kysely/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Database>({
dialect: new PostgresDialect({
pool: new pg.Pool({
connectionString,
ssl,
}),
}),
});
Expand Down
16 changes: 11 additions & 5 deletions packages/database/src/context/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ export class PostgresDatabaseContext implements DatabaseContext {
knex?: Knex;
kysely?: Kysely<Database>;

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<Kysely<Database>> {
if (!this.kysely) {
this.kysely = createPostgresDatabase(this.connectionUri);
this.kysely = createPostgresDatabase(this.connectionUri, this.ssl);
}
return this.kysely;
}
Expand All @@ -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;
};

0 comments on commit c268582

Please sign in to comment.