-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (55 loc) · 1.89 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*jslint node: true */
'use strict';
var MongoClient = require('mongodb').MongoClient;
module.exports = function (options) {
var self = this;
this.validateOptions = function () {
if (self.options === undefined) {
self.options = self.defaultOptions;
}
if (self.options.ip === undefined) {
self.options.ip = self.defaultOptions.ip;
}
if (self.options.dbname === undefined) {
self.options.dbname = self.defaultOptions.dbname;
}
if (self.options.collection === undefined) {
self.options.collection = self.defaultOptions.collection;
}
if (self.options.customObject === undefined) {
self.options.defaultOptions = self.defaultOptions.customObject;
}
if (self.options.customObject !== undefined) {
if (typeof self.options.customObject !== 'object') {
self.options.customObject = self.defaultOptions.customObject;
}
}
};
this.defaultOptions = {
ip: "127.0.0.1:27017",
dbname : "test",
collection: "test_insert",
customObject : null
};
this.options = options;
if (this.options === undefined) {
this.options = self.defaultOptions;
}
self.validateOptions();
this.fn = function (results, done) {
MongoClient.connect('mongodb://' + self.options.ip + '/' + self.options.dbname, function (err, db) {
if (err) {
throw err;
}
var collection = db.collection(self.options.collection);
if (self.options.customObject !== null) {
self.options.customObject.results = results;
results = self.options.customObject;
}
collection.insert(results, function (err, docs) {
db.close();
done();
});
});
};
};