From 25699334499c616b34e9b9ecf29a1e167dae4acb Mon Sep 17 00:00:00 2001 From: Samika Kashyap Date: Wed, 22 May 2024 09:43:56 -0700 Subject: [PATCH] feat: make findByCid calls go through read replica --- config/default.json | 7 +++++-- config/env/dev.json | 8 +------- package.json | 2 +- src/app.ts | 2 -- src/db-connection.ts | 20 ++++++++++--------- src/main.ts | 2 +- .../replication-request-repository.ts | 9 +++------ 7 files changed, 22 insertions(+), 28 deletions(-) diff --git a/config/default.json b/config/default.json index 89a38376..6d562fc5 100644 --- a/config/default.json +++ b/config/default.json @@ -81,13 +81,16 @@ } }, "replica_db": { + "client": "postgresql", "connection": { "database": "@@REPLICA_DB_NAME", "host": "@@REPLICA_DB_HOST", "user": "@@REPLICA_DB_USERNAME", "password": "@@REPLICA_DB_PASSWORD", - "port": "@@REPLICA_DB_PORT" - } + "port": "@@REPLICA_DB_PORT", + "connectionString": "" + }, + "debug": false }, "queue": { "type": "sqs", diff --git a/config/env/dev.json b/config/env/dev.json index 4ed6032c..f0e9ed76 100644 --- a/config/env/dev.json +++ b/config/env/dev.json @@ -72,13 +72,7 @@ "host": "@@DB_HOST", "user": "@@DB_USERNAME", "password": "@@DB_PASSWORD", - "port": "@@DB_PORT", - "replicaDatabase": "@@REPLICA_DB_NAME", - "replicaHost": "@@REPLICA_DB_HOST", - "replicaUser": "@@REPLICA_DB_USERNAME", - "replicaPassword": "@@REPLICA_DB_PASSWORD", - "replicaPort": "@@REPLICA_DB_PORT", - "replicaConnectionString": "@@REPLICA_DB_CONNECTION_STRING" + "port": "@@DB_PORT" } }, "replica_db": { diff --git a/package.json b/package.json index 5ac294ce..2fb0e4e2 100644 --- a/package.json +++ b/package.json @@ -142,7 +142,7 @@ "supertest": "^6.3.3", "tmp-promise": "^3.0.3", "ts-essentials": "^9.3.2", - "typescript": "^5.0.4" + "typescript": "^5.4.5" }, "release-it": { "git": { diff --git a/src/app.ts b/src/app.ts index 83b5c446..8ef6ba91 100644 --- a/src/app.ts +++ b/src/app.ts @@ -45,7 +45,6 @@ import { } from './services/queue/sqs-queue-service.js' import { makeMerkleCarService, type IMerkleCarService } from './services/merkle-car-service.js' import { makeWitnessService, type IWitnessService } from './services/witness-service.js' -import { ReplicationRequestRepository } from './repositories/replication-request-repository.js' type DependenciesContext = { config: Config @@ -100,7 +99,6 @@ export class CeramicAnchorApp { // register repositories .provideClass('metadataRepository', MetadataRepository) .provideFactory('requestRepository', RequestRepository.make) - .provideFactory('replicationRequestRepository', ReplicationRequestRepository.make) .provideClass('anchorRepository', AnchorRepository) .provideClass('transactionRepository', TransactionRepository) .provideClass('replicationRequestRepository', ReplicationRequestRepository) diff --git a/src/db-connection.ts b/src/db-connection.ts index e2a7e3d2..b993780c 100644 --- a/src/db-connection.ts +++ b/src/db-connection.ts @@ -69,17 +69,19 @@ export async function createDbConnection(dbConfig: Db = config.db): Promise { +export async function createReplicaDbConnection( + replica_db_config: Replicadb = config.replica_db +): Promise { const replicaKnexConfig: Knex.Config = { - client: dbConfig.client, - connection: dbConfig.connection.replicaConnectionString || { - host: dbConfig.connection.replicaHost, - port: dbConfig.connection.replicaPort, - user: dbConfig.connection.replicaUser, - password: dbConfig.connection.replicaPassword, - database: dbConfig.connection.replicaDatabase, + client: replica_db_config.client, + connection: replica_db_config.connection.connectionString || { + host: replica_db_config.connection.host, + port: replica_db_config.connection.port, + user: replica_db_config.connection.user, + password: replica_db_config.connection.password, + database: replica_db_config.connection.database, }, - debug: dbConfig.debug, + debug: replica_db_config.debug, pool: { min: 3, max: 30 }, // In our DB, identifiers have snake case formatting while in our code identifiers have camel case formatting. // We use the following transformers so we can always use camel case formatting in our code. diff --git a/src/main.ts b/src/main.ts index 970be817..c91296e0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,9 +15,9 @@ async function startApp() { const container = createInjector() .provideValue('config', config) + .provideValue('dbConnection', connection) .provideValue('replicaDbConnection', replicaConnection) - .provideValue('dbConnection', connection) const app = new CeramicAnchorApp(container) await app.start() } diff --git a/src/repositories/replication-request-repository.ts b/src/repositories/replication-request-repository.ts index 0856922b..54f58f78 100644 --- a/src/repositories/replication-request-repository.ts +++ b/src/repositories/replication-request-repository.ts @@ -20,8 +20,8 @@ export interface IReplicationRequestRepository { export class ReplicationRequestRepository implements IReplicationRequestRepository { static inject = ['replicaDbConnection'] as const - constructor( private readonly connection: Knex) {} - + constructor(private readonly connection: Knex) {} + get table(): Knex.QueryBuilder { return this.connection(TABLE_NAME) } @@ -31,9 +31,7 @@ export class ReplicationRequestRepository implements IReplicationRequestReposito * @returns Promise for the associated request */ async findByCid(cid: CID | string): Promise { - const found = await this.table - .where({ cid: String(cid) }) - .first() + const found = await this.table.where({ cid: String(cid) }).first() if (found) { return new Request(found) } @@ -41,4 +39,3 @@ export class ReplicationRequestRepository implements IReplicationRequestReposito } // Add more methods that utilize the replica connection here } -