-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (34 loc) · 1.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var mongoose = require('mongoose'),
path = require('path');
/**
* Creates connection to MongoDB database.
*
* @module plott/mongo
* @category helper
* @param {String} conn MongoDB connection uri "mongodb://<host>/". Default ('mongodb://localhost/')
* @param {String} db database name. Default ('Plott')
* @return {Object} mongoose
* @example
* plott.mongo();
*
* //=mongoose
*/
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (position === undefined || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
module.exports = function (conn, db) {
var connection;
if (mongoose.connection.readyState === 2){ return mongoose; };
conn = conn === undefined ? 'mongodb://localhost/' : conn;
db = db === undefined ? 'Plott' : db;
connection = conn.endsWith('/') ? conn.concat(db) : conn.concat('/', db);
return mongoose.connect(connection);
}