-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStoreCollection.js
163 lines (136 loc) · 4.57 KB
/
DataStoreCollection.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var _ = require('lodash');
let updater = require('./lib/updater');
export default class DataStoreCollection {
dataSet;
namespace;
constructor(collectionName:String, dataSet:Object, namespace:String) {
this.collectionName = collectionName;
this.dataSet = dataSet;
this.namespace = namespace;
}
/**
* Insert Object To dataStore
* @param object
* @returns {Promise}
*/
insertOne(object) {
let key = this.dataSet.key({namespace: this.namespace, path: [this.collectionName, object._id]});
var entity = {
key: key,
data: object
};
let that = this;
return new Promise(function (fulfill, reject) {
that.dataSet.save(entity, function (err) {
if (err) {
return reject(err);
}
fulfill({
ops: [object]
});
});
});
}
findOneAndUpdate(query, update) {
let dQuery = this.dataSet.createQuery(this.namespace, this.collectionName);
_.each(query, function (v, k) {
dQuery.filter(k, '=', v)
});
let that = this;
return new Promise(function (fulfill, reject) {
that.dataSet.runQuery(dQuery, function (err, entities) {
var entity = entities[0];
if (err)
return reject(err);
if (entity === undefined) {
reject();
} else {
updater(update, entity.data);
that.dataSet.save(entity, function (err) {
if (err) {
return reject(err);
}
fulfill(that.fromDataStore(entity));
});
}
});
});
}
/**
* Update One
* @param query
* @param update
* @returns {Promise}
*/
upsertOne(query, update) {
let that = this;
return new Promise(function (fulfill, reject) {
let key = that.dataSet.key({namespace: that.namespace, path: [that.collectionName, query._id]});
that.dataSet.get(key, function (err, entity) {
if (err)
return reject(err);
if (entity === undefined) {
} else {
updater(update, entity.data);
that.dataSet.save(entity, function (err) {
if (err) {
return reject(err);
}
fulfill(that.fromDataStore(entity));
});
}
});
});
}
/**
*Used to convert dataStore Format to Normal type
* @param obj
* @returns {*}
*/
fromDataStore(obj) {
obj.data.id = obj.key.path[obj.key.path.length - 1];
return obj.data;
}
find(query, {skip, limit, sort} = {}) {
return this._rawFind(query, {skip, limit, sort})
.catch(error => {
// Check for "no geoindex" error
if (error.code != 17007 || !error.message.match(/unable to find index for .geoNear/)) {
throw error;
}
// Figure out what key needs an index
let key = error.message.match(/field=([A-Za-z_0-9]+) /)[1];
if (!key) {
throw error;
}
var index = {};
index[key] = '2d';
//TODO: condiser moving index creation logic into Schema.js
return this._mongoCollection.createIndex(index)
// Retry, but just once.
.then(() => this._rawFind(query, {skip, limit, sort}));
});
}
_rawFind(query, {skip, limit, sort} = {}) {
console.log(this.collectionName);
console.log(query);
let dQuery = this.dataSet.createQuery(this.namespace, this.collectionName);
_.each(query, function (v, k) {
if (v === undefined)
v = "";
dQuery.filter(k, '=', v)
});
if (limit)
dQuery.limit(limit);
if (skip)
dQuery.offset(skip);
var that = this;
return new Promise(function (fulfill, reject) {
that.dataSet.runQuery(dQuery, function (err, entities) {
if (err)
return reject(err);
fulfill(entities.map(that.fromDataStore));
});
});
}
}