Skip to content

Commit

Permalink
feat: make findByCid calls go through read replica
Browse files Browse the repository at this point in the history
  • Loading branch information
Samika Kashyap committed May 22, 2024
1 parent b82f094 commit 2569933
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 28 deletions.
7 changes: 5 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 1 addition & 7 deletions config/env/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 0 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions src/db-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ export async function createDbConnection(dbConfig: Db = config.db): Promise<Knex
return connection
}

export async function createReplicaDbConnection(dbConfig: Db = config.db): Promise<Knex> {
export async function createReplicaDbConnection(
replica_db_config: Replicadb = config.replica_db
): Promise<Knex> {
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.
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
9 changes: 3 additions & 6 deletions src/repositories/replication-request-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -31,14 +31,11 @@ export class ReplicationRequestRepository implements IReplicationRequestReposito
* @returns Promise for the associated request
*/
async findByCid(cid: CID | string): Promise<Request | undefined> {
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)
}
return undefined
}
// Add more methods that utilize the replica connection here
}

0 comments on commit 2569933

Please sign in to comment.