-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbClient.js
103 lines (91 loc) · 2.52 KB
/
dbClient.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const assert = require('assert');
const MongoClient = require('mongodb').MongoClient;
// const co = require('co');
// connection URL
const mongoUrl = "mongodb://127.0.0.1:27017/evidence-manager";
DBClient = {};
DBClient.dbObject = null;
/**
* 接続用。mongoUrlは固定とする
*/
DBClient.connect = function(callback) {
MongoClient.connect(mongoUrl, function(err, db) {
DBClient.dbObject = db;
assert.equal(null, err);
console.log("Connected successfully to server");
callback();
});
}
/**
* 指定したデータを取得する
* @return promise
*/
DBClient.get = function(collectionName, opt_where, opt_limit) {
assert.notEqual(DBClient.dbObject, null);
var collection = DBClient.dbObject.collection(collectionName);
var result;
if (opt_where) {
result = collection.find(opt_where);
} else {
result = collection.find();
}
if (opt_limit !== undefined && opt_limit !== null) {
result = result.limit(opt_limit);
}
return result.toArray();
}
/**
* aggregateを使用してデータを取得する
* @return promise
*/
DBClient.aggregate = function(collectionName, pipeline) {
assert.notEqual(DBClient.dbObject, null);
var collection = DBClient.dbObject.collection(collectionName);
return new Promise(function(resolve, reject){
collection.aggregate(pipeline, function(err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
};
/**
* distinctで取得する
* @return promise
*/
DBClient.distinct = function(collectionName, key) {
assert.notEqual(DBClient.dbObject, null);
var collection = DBClient.dbObject.collection(collectionName);
return collection.distinct(key);
};
/**
* 指定したCollectionに値をinsertする
* データがない場合は処理しない
*/
DBClient.insert = function (collectionName, data, callback) {
assert.notEqual(DBClient.dbObject, null);
// dataがない場合は抜ける
if (!data) {
console.log("no Data");
callback();
return;
}
var collection = DBClient.dbObject.collection(collectionName);
// var iid = data.iid;
collection.insertOne(data, function(err, result) {
assert.equal(err, null);
console.log("Insert OK!");
if (callback) {
callback();
}
});
}
/**
* 接続をcloseする
*/
DBClient.close = function () {
console.log("close");
DBClient.dbObject.close();
}