Skip to content

Commit

Permalink
feat: split mongo env vars (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
gempain authored Mar 24, 2021
1 parent 5dc005e commit 236401c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
60 changes: 60 additions & 0 deletions server/src/db/build-mongo-uri.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { buildMongoUri } from './build-mongo-uri';
import { env } from '../env/env';

describe('buildMongoUri', () => {

afterEach(() => {
jest.restoreAllMocks();
delete env.MELI_MONGO_HOST;
delete env.MELI_MONGO_PORT;
delete env.MELI_MONGO_USER;
delete env.MELI_MONGO_PASSWORD;
delete env.MELI_MONGO_DB;
});

it('should throw error when host not defined', async () => {
env.MELI_MONGO_HOST = undefined;
env.MELI_MONGO_PORT = 27017;
env.MELI_MONGO_DB = 'db';
expect(() => buildMongoUri()).toThrow();
});

it('should throw error when port not defined', async () => {
env.MELI_MONGO_HOST = 'localhost';
env.MELI_MONGO_PORT = undefined;
env.MELI_MONGO_DB = 'db';
expect(() => buildMongoUri()).toThrow();
});

it('should throw error when db not defined', async () => {
env.MELI_MONGO_HOST = 'localhost';
env.MELI_MONGO_PORT = 27017;
env.MELI_MONGO_DB = undefined;
expect(() => buildMongoUri()).toThrow();
});

it('should build url with host and port', async () => {
env.MELI_MONGO_HOST = 'localhost';
env.MELI_MONGO_PORT = 27017;
env.MELI_MONGO_DB = 'db';
expect(buildMongoUri()).toEqual('mongodb://localhost:27017/db');
});

it('should build url with user', async () => {
env.MELI_MONGO_HOST = 'localhost';
env.MELI_MONGO_PORT = 27017;
env.MELI_MONGO_USER = 'user';
env.MELI_MONGO_DB = 'db';
expect(buildMongoUri()).toEqual('mongodb://user@localhost:27017/db');
});

it('should build url with user and password', async () => {
env.MELI_MONGO_HOST = 'localhost';
env.MELI_MONGO_PORT = 27017;
env.MELI_MONGO_USER = 'user';
env.MELI_MONGO_PASSWORD = 'password';
env.MELI_MONGO_DB = 'db';
expect(buildMongoUri()).toEqual('mongodb://user:password@localhost:27017/db');
});

});
16 changes: 16 additions & 0 deletions server/src/db/build-mongo-uri.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { env } from '../env/env';
import { InvalidEnvironmentError } from '../commons/errors/invalid-environment-error';

// https://docs.mongodb.com/manual/reference/connection-string/
// mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]
export function buildMongoUri(): string {
if (!env.MELI_MONGO_HOST || !env.MELI_MONGO_PORT || !env.MELI_MONGO_DB) {
throw new InvalidEnvironmentError();
}

return `mongodb://${
!env.MELI_MONGO_USER ? '' : (
`${env.MELI_MONGO_USER}${env.MELI_MONGO_PASSWORD ? `:${env.MELI_MONGO_PASSWORD}` : ''}@`
)
}${env.MELI_MONGO_HOST}:${env.MELI_MONGO_PORT}/${env.MELI_MONGO_DB}`;
}
5 changes: 4 additions & 1 deletion server/src/db/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Db, MongoClient } from 'mongodb';
import { env } from '../env/env';
import { buildMongoUri } from './build-mongo-uri';

const client = new MongoClient(env.MELI_MONGO_URI, {
const url = env.MELI_MONGO_URI || buildMongoUri();

const client = new MongoClient(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
Expand Down
17 changes: 16 additions & 1 deletion server/src/env/env-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,22 @@ export const envSpec: EnvSpec<Env> = {
schema: string().optional(),
},
MELI_MONGO_URI: {
schema: string().required(),
schema: string().optional(),
},
MELI_MONGO_USER: {
schema: string().optional(),
},
MELI_MONGO_PASSWORD: {
schema: string().optional(),
},
MELI_MONGO_HOST: {
schema: string().optional(),
},
MELI_MONGO_PORT: {
schema: number().optional().default(27017),
},
MELI_MONGO_DB: {
schema: string().optional(),
},
MELI_MIGRATE_ROLLBACK: {
transform: stringToBoolean(),
Expand Down
5 changes: 5 additions & 0 deletions server/src/env/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export interface Env {
MELI_JWT_SECRET: string;
MELI_JWT_TOKEN_EXPIRATION: number;
MELI_MONGO_URI: string;
MELI_MONGO_USER: string;
MELI_MONGO_PASSWORD: string;
MELI_MONGO_HOST: string;
MELI_MONGO_PORT: number;
MELI_MONGO_DB: string;
MELI_GITLAB_URL: string;
MELI_GITLAB_CLIENT_ID: string;
MELI_GITLAB_CLIENT_SECRET: string;
Expand Down

0 comments on commit 236401c

Please sign in to comment.