This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoStore.js
131 lines (119 loc) · 4.18 KB
/
MongoStore.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var AbstractStore = require('springbokjs-db/lib/AbstractStore').AbstractStore;
var Cursor = require('./Cursor').Cursor;
var mongodb = require('mongodb');
var ObjectID = require('mongodb').ObjectID;
var regexpObjectId = /^[a-f\d]{24}$/i;
export class MongoStore extends AbstractStore {
initialize() {
this.manager.VO.keyPath = '_id';
return new Promise((resolve, reject) => {
if (!this.manager.VO.name || this.manager.VO.name.toLowerCase() === 'function') {
throw new Error('Unable to find model name ' + this.manager.VO.name +
', ' + this.manager.name);
}
this.db.connection.collection(this.manager.VO.name, {w: 1}, (err, collection) => {
if (err) {
return reject(err);
}
this.collection = collection;
resolve();
});
});
}
store() {
return this.collection;
}
toId(id) {
if (id instanceof ObjectID) {
return id;
}
if (typeof id === 'string' && id.length === 24 && regexpObjectId.test(id)) {
return new ObjectID(id);
}
return id;
}
createMongoId(vo) {
if (!vo.id) {
vo.data._id = new ObjectID();
}
}
insert(options) {
return new Promise((resolve, reject) => {
this.collection.insert(options.data, options, (err, item) => {
if (err) {
return reject(err);
}
resolve(item);
});
});
}
update(options) {
return new Promise((resolve, reject) => {
var data = !options.partialUpdate ? options.data : { $set: options.data };
this.collection.update(options.criteria, data, options, (err, result) => {
if (err) {
return reject(err);
}
resolve(result.n);
});
});
}
remove(options) {
return new Promise((resolve, reject) => {
this.collection.remove(options.criteria, options, (err, result) => {
if (err) {
return reject(err);
}
resolve(result.n);
});
});
}
/** @see http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#findone */
findOne(query, options){
return new Promise((resolve, reject) => {
//selector, options, callback?
//options= limit,sort,fields,skip,hint,tailable,tailableRetryInterval,returnKey,maxScan,min,max,comment,raw
this.collection.findOne(query, options, (err, result) => {
if (err) {
return reject(err);
}
resolve(result);
});
});
}
/** @see http://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find */
cursor(query, options) {
return new Promise((resolve, reject) => {
//selector, options, callback?
//options= limit,sort,fields,skip,hint,tailable,returnKey,maxScan,min,max,comment,raw
this.collection.find(query, options, (err, cursor) => {
if (err) {
return reject(err);
}
resolve(new Cursor(cursor, this, query));
});
});
}
}
MongoStore.ObjectID = ObjectID;
MongoStore.initialize = function(db) {
return new Promise((resolve, reject) => {
var options = Object.assign({
host: 'localhost',
port: '27017'
}, db.options);
var connectionString = 'mongodb://' + (options.user ? options.user + ':' + options.password + '@' : '' )
+ options.host + ':' + options.port + '/' + db.dbName;
mongodb.MongoClient.connect(connectionString, function(err, connection) {
if (err) {
console.error('connection error:', err);
return reject(err);
}
db.connection = connection;
resolve();
});
});
};
MongoStore.close = function(db) {
db.connection.close();
};