MongoDB Transactions (#12335)

* add run-rs to dependencies

* wip: add replica set to api unit github action

* wip: add replica set to api unit github action

* wip: fix gh actions mongodb replica set setting

* usa replica set for integration tests

* add correct mongodb version matrix for integration tests

* use different db connection on gh actions

* Revert "use different db connection on gh actions"

This reverts commit aa8db759d3.

* add example transaction

* add mongo script to package.json

* abstract mongodb utils, connect using hostname on windows

* npm scripts: mongo -> mongo:dev

* add setup script for run-rs on windows

* gh actions: run in test environment

* remove test files

* better error handling, use cross-spawn to avoid issues on windows

* fix lint
This commit is contained in:
Matteo Pagliazzi
2020-07-14 18:55:47 +02:00
committed by GitHub
parent e89ff95a21
commit c93bf3e498
11 changed files with 1432 additions and 864 deletions

View File

@@ -1,6 +1,10 @@
import nconf from 'nconf';
import mongoose from 'mongoose';
import logger from './logger';
import {
getDevelopmentConnectionUrl,
getDefaultConnectionOptions,
} from './mongodb';
const IS_PROD = nconf.get('IS_PROD');
const MAINTENANCE_MODE = nconf.get('MAINTENANCE_MODE');
@@ -8,22 +12,14 @@ const POOL_SIZE = nconf.get('MONGODB_POOL_SIZE');
// Do not connect to MongoDB when in maintenance mode
if (MAINTENANCE_MODE !== 'true') {
const commonOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
const mongooseOptions = !IS_PROD ? commonOptions : {
keepAlive: 120,
connectTimeoutMS: 30000,
...commonOptions,
};
const mongooseOptions = getDefaultConnectionOptions();
if (POOL_SIZE) mongooseOptions.poolSize = Number(POOL_SIZE);
const NODE_DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
const DB_URI = nconf.get('IS_TEST') ? nconf.get('TEST_DB_URI') : nconf.get('NODE_DB_URI');
const connectionUrl = IS_PROD ? DB_URI : getDevelopmentConnectionUrl(DB_URI);
mongoose.connect(NODE_DB_URI, mongooseOptions, err => {
mongoose.connect(connectionUrl, mongooseOptions, err => {
if (err) throw err;
logger.info('Connected with Mongoose.');
});