Created
October 1, 2020 13:26
-
-
Save stfsy/1984f90166bea5ec7ec7e14d76f48801 to your computer and use it in GitHub Desktop.
Example for connecting to MongoDB instance and enable chema validation for a collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const Mongo = require('mongodb').MongoClient | |
const configuration = require('../configuration/configuration') | |
const mongoUrl = configuration.getMongoDbUrl() | |
const mongoDbName = configuration.getMongoDbName() | |
module.exports = class { | |
constructor(schemaName) { | |
this._schemaName = schemaName | |
this._db = null | |
this._client = null | |
process.on('SIGINT', () => { | |
this._close() | |
}) | |
} | |
async _verifyConnected() { | |
if (this._db) { | |
return Promise.resolve(this._db) | |
} else { | |
return this._connect() | |
} | |
} | |
async _connect() { | |
this._client = await Mongo.connect(mongoUrl, { | |
useUnifiedTopology: true | |
}) | |
this._db = this._client.db(mongoDbName) | |
const collections = await this._db.listCollections().toArray() | |
const isAlreadyCreated = collections.some(collection => { | |
return collection.name === this._schemaName | |
}) | |
// if the collection already exists we only update the schema | |
// but there's a catch.. read the comment below | |
if (!isAlreadyCreated) { | |
try { | |
await this._db.createCollection(this._schemaName) | |
} catch (e) { | |
// may be only a unit/integration-test test problem | |
// but we can't always determine if collection already exists | |
// if codeName equals NamespaceExists the collection | |
// was created asynchronously and we can carry on | |
// else.. throw the error | |
if (e.codeName != 'NamespaceExists') { | |
throw e | |
} | |
} | |
} | |
await this._db.command({ | |
collMod: this._schemaName, | |
validator: { | |
$jsonSchema: require('./schemas/' + this._schemaName + '.json') | |
}, | |
validationLevel: "strict" | |
}) | |
return this._db | |
} | |
async _close() { | |
if (!this._client) { | |
return Promise.resolve() | |
} else { | |
await this._client.close() | |
this._client = null | |
this._db = null | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment