-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
819 additions
and
189 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = () => { | ||
return process.env.MAESTRO_MONGO_DATABASE || 'maestro-client'; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,5 @@ | ||
'use strict'; | ||
|
||
|
||
module.exports = () => { | ||
const uri = process.env.MAESTRO_MONGO_URI || 'mongodb://localhost'; | ||
const db = process.env.MAESTRO_MONGO_DATABASE || 'maestro-audit'; | ||
return `${uri}/${db}`; | ||
}; | ||
return process.env.MAESTRO_MONGO_URI || 'mongodb://localhost'; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const mongodb = require('mongodb'); | ||
const MongoClient = mongodb.MongoClient; | ||
|
||
/** | ||
* Connector | ||
* | ||
* Main class, manages mongodb connection and collections | ||
*/ | ||
|
||
class Connector {} | ||
|
||
Connector.connect = function (path, dbname, options = {}) { | ||
|
||
this.url = path; | ||
this.dbname = path; | ||
|
||
const strOpts = { | ||
useUnifiedTopology: true, | ||
useNewUrlParser: true | ||
}; | ||
|
||
let connection = MongoClient.connect(path, strOpts, options) | ||
.then((client) => { | ||
if (!this.db) | ||
this.db = client.db(dbname); | ||
|
||
return this.db; | ||
}) | ||
.catch(console.log); | ||
|
||
if (!this._connection) | ||
this._connection = connection; | ||
|
||
return connection; | ||
}; | ||
|
||
|
||
/** | ||
* Disconnect from a database | ||
* | ||
* @api public | ||
*/ | ||
|
||
Connector.disconnect = function () { | ||
return this.db.close(); | ||
}; | ||
|
||
|
||
/** | ||
* Alias for .disconnect() | ||
* | ||
* @api public | ||
*/ | ||
|
||
Connector.close = function () { | ||
return Connector.disconnect.apply(this, arguments); | ||
}; | ||
|
||
|
||
/** | ||
* Return a co-wrapped monk collection | ||
* | ||
* @api private | ||
*/ | ||
|
||
Connector._collection = function (db, name) { | ||
const url = this.url; | ||
let collections = this._collections[url]; | ||
|
||
if (!collections) | ||
collections = this._collections[url] = {}; | ||
|
||
if (collections[name]) | ||
return collections[name]; | ||
|
||
let collection = db.collection(name); | ||
collections[name] = collection; | ||
return collections[name]; | ||
}; | ||
|
||
|
||
/** | ||
* Cache for collections | ||
* | ||
* @api private | ||
*/ | ||
|
||
Connector._collections = {}; | ||
|
||
module.exports = Connector; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
const {ObjectId} = require('mongodb'); | ||
|
||
/** | ||
* Ensure that ids are always instances of ObjectId | ||
*/ | ||
|
||
function toObjectId (id) { | ||
if (id instanceof ObjectId) { | ||
return id; | ||
} | ||
|
||
return new ObjectId(id); | ||
} | ||
|
||
module.exports = toObjectId; |
Oops, something went wrong.