Skip to content

Commit

Permalink
fix(notes:infra): fix note deployment and separate r/w endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kschelonka committed Nov 22, 2024
1 parent bc66071 commit e56fcc0
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 26 deletions.
6 changes: 5 additions & 1 deletion infrastructure/notes-api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,11 @@ class NotesAPI extends TerraformStack {
valueFrom: `${rds.secretARN}:database_url::`,
},
{
name: 'DATABASE_HOST',
name: 'DB_WRITE_HOST',
valueFrom: `${rds.secretARN}:host::`,
},
{
name: 'DB_READ_HOST',
valueFrom: `${rds.secretARN}:host::`,
},
{
Expand Down
12 changes: 8 additions & 4 deletions packages/terraform-modules/src/base/ApplicationRDSCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,14 @@ export class ApplicationRDSCluster extends Construct {
port: rdsPort,
};

// Add a database URL to a MySQL-compatible Aurora instance
if (engine && engine === 'aurora-mysql') {
secretValues.database_url = `mysql://${rds.masterUsername}:${rds.masterPassword}@${rds.endpoint}:${rdsPort}/${rds.databaseName}`;
secretValues.reader_database_url = `mysql://${rds.masterUsername}:${rds.masterPassword}@${rds.readerEndpoint}:${rdsPort}/${rds.databaseName}`;
// Add a database URL to a MySQL or Postgres-compatible Aurora instance
if (
(engine && engine === 'aurora-mysql') ||
engine === 'aurora-postgresql'
) {
const protocol = engine === 'aurora-mysql' ? 'mysql' : 'postgresql';
secretValues.database_url = `${protocol}://${rds.masterUsername}:${rds.masterPassword}@${rds.endpoint}:${rdsPort}/${rds.databaseName}`;
secretValues.reader_database_url = `${protocol}://${rds.masterUsername}:${rds.masterPassword}@${rds.readerEndpoint}:${rdsPort}/${rds.databaseName}`;
}

//Create the initial secret version
Expand Down
3 changes: 2 additions & 1 deletion servers/notes-api/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ process.env.DATABASE_URL =
process.env.DATABASE_NAME = 'pocketnotes';
process.env.DATABASE_USER = 'pocket';
process.env.DATABASE_PASSWORD = 'password';
process.env.DATABASE_HOST = 'localhost';
process.env.DB_WRITE_HOST = 'localhost';
process.env.DB_READ_HOST = 'localhost';
process.env.DATABASE_PORT = '5432';
8 changes: 4 additions & 4 deletions servers/notes-api/src/apollo/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from '@pocket-tools/apollo-utils';
import { Kysely } from 'kysely';
import { DB } from '../__generated__/db';
import { db } from '../datasources/db';
import { roDb } from '../datasources/db';
import { NoteModel } from '../models/Note';

/**
Expand All @@ -27,13 +27,13 @@ export async function getContext({
}

export interface IContext extends PocketContext {
db: Kysely<DB>;
roDb: Kysely<DB>;
userId: string;
NoteModel: NoteModel;
}

export class ContextManager extends PocketContextManager implements IContext {
db: Kysely<DB>;
roDb: Kysely<DB>;
_userId: string;
NoteModel: NoteModel;
constructor(options: { request: Request }) {
Expand All @@ -47,7 +47,7 @@ export class ContextManager extends PocketContextManager implements IContext {
} else {
this._userId = super.userId;
}
this.db = db;
this.roDb = roDb;
this.NoteModel = new NoteModel(this);
}
/** Override because userId is guaranteed in this Context */
Expand Down
3 changes: 2 additions & 1 deletion servers/notes-api/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export const config = {
timeout: 2 * 1000, // ms
},
database: {
host: process.env.DATABASE_HOST || 'localhost',
readHost: process.env.DB_READ_HOST || 'localhost',
writeHost: process.env.DB_WRITE_HOST || 'localhost',
username: process.env.DATABASE_USER || 'pocket',
password: process.env.DATABASE_PASSWORD || 'password',
dbname: process.env.DATABASE_NAME || 'pocketnotes',
Expand Down
4 changes: 2 additions & 2 deletions servers/notes-api/src/datasources/NoteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class NotesService {
* @returns
*/
async get(noteId: string) {
const result = await this.context.db
const result = await this.context.roDb
.selectFrom('Note')
.selectAll()
.where('noteId', '=', noteId)
Expand All @@ -28,7 +28,7 @@ export class NotesService {
* @returns
*/
async getMany(noteIds: readonly string[]) {
const result = await this.context.db
const result = await this.context.roDb
.selectFrom('Note')
.selectAll()
.where('userId', '=', this.context.userId)
Expand Down
43 changes: 30 additions & 13 deletions servers/notes-api/src/datasources/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,44 @@ import { Pool } from 'pg';
import { Kysely, PostgresDialect } from 'kysely';
import { config } from '../config';

const dialect = new PostgresDialect({
pool: new Pool({
database: config.database.dbname,
host: config.database.host,
user: config.database.username,
password: config.database.password,
port: config.database.port,
max: config.database.maxPool,
}),
});
const dialect = (host: string) =>
new PostgresDialect({
pool: new Pool({
database: config.database.dbname,
host: host,
user: config.database.username,
password: config.database.password,
port: config.database.port,
max: config.database.maxPool,
}),
});

// Readonly
let _roDb: Kysely<DB>;
// Writer
let _db: Kysely<DB>;
const lazyDb = (): Kysely<DB> => {

const lazyRoDb = (): Kysely<DB> => {
if (_roDb != null) {
return _roDb;
} else {
_roDb = new Kysely<DB>({
dialect: dialect(config.database.readHost),
});
return _roDb;
}
};

const lazyWriteDb = (): Kysely<DB> => {
if (_db != null) {
return _db;
} else {
_db = new Kysely<DB>({
dialect,
dialect: dialect(config.database.writeHost),
});
return _db;
}
};

export const db = lazyDb();
export const roDb = lazyRoDb();
export const db = lazyWriteDb();

0 comments on commit e56fcc0

Please sign in to comment.