diff --git a/replacements/src/app/loaders/mongoose.ts b/replacements/src/app/loaders/mongoose.ts index 740e903..3b10403 100644 --- a/replacements/src/app/loaders/mongoose.ts +++ b/replacements/src/app/loaders/mongoose.ts @@ -4,6 +4,38 @@ import mongoose, { Connection } from 'mongoose' import config from '../config/config' import { createLoggerWithLabel } from '../config/logger' +// Invoke this at module load time +mongoose.plugin((schema) => { + if (schema.get('read') === 'secondary') { + // Inspect cluster topology from client + const client = mongoose.connection.getClient() + const { + description: { servers, type }, + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + } = client.topology + + if ( + type === 'ReplicaSetWithPrimary' && + !servers + .values() + .some(({ type }: { type: string }) => type === 'RSSecondary') + ) { + // There are no secondary nodes in ReplicaSetWithPrimary cluster. + // Queries with `secondary` read preferences will fail, so rewrite + // those to be `secondaryPreferred`. + logger.warn({ + message: 'Forcing secondary read preference to secondaryPreferred.', + meta: { + action: 'schema', + }, + }) + + schema.set('read', 'secondaryPreferred') + } + } +}) + const logger = createLoggerWithLabel(module) export default async (): Promise => { @@ -90,30 +122,6 @@ export default async (): Promise => { }) }) - // Seed db with initial agency if we have none - if ( - process.env.INIT_AGENCY_DOMAIN && - process.env.INIT_AGENCY_SHORTNAME && - process.env.INIT_AGENCY_FULLNAME - ) { - const Agency = mongoose.model('Agency') - const agencyCount = await Agency.count() - if (agencyCount === 0) { - await mongoose.connection.collection(Agency.collection.name).updateOne( - { shortName: process.env.INIT_AGENCY_SHORTNAME }, - { - $setOnInsert: { - shortName: process.env.INIT_AGENCY_SHORTNAME, - fullName: process.env.INIT_AGENCY_FULLNAME, - emailDomain: [process.env.INIT_AGENCY_DOMAIN], - logo: '/logo192.png', - }, - }, - { upsert: true }, - ) - } - } - // Seed the db with govtech agency if using the mocked db if (usingMockedDb) { const Agency = mongoose.model('Agency') diff --git a/replacements/src/app/server.ts b/replacements/src/app/server.ts new file mode 100755 index 0000000..9c034dc --- /dev/null +++ b/replacements/src/app/server.ts @@ -0,0 +1,27 @@ +import './loaders/datadog-tracer' +import './loaders/mongoose' + +import config from './config/config' +import { createLoggerWithLabel } from './config/logger' +import loadApp from './loaders' + +const logger = createLoggerWithLabel(module) + +const initServer = async () => { + const app = await loadApp() + + // Configure aws-sdk based on environment + // sdk is later used to upload images to S3 + await config.configureAws() + + app.listen(config.port) + + logger.info({ + message: `[${config.nodeEnv}] Connected to port ${config.port}`, + meta: { + action: 'initServer', + }, + }) +} + +void initServer()