-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataStoreSchemaCollection.js
60 lines (47 loc) · 1.91 KB
/
DataStoreSchemaCollection.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
import MongoCollection from './DataStoreCollection';
function _dataStoreSchemaQueryFromNameQuery(name:string, query) {
return _dataStoreSchemaObjectFromNameFields(name, query);
}
function _dataStoreSchemaObjectFromNameFields(name:string, fields) {
let object = {_id: name};
if (fields) {
Object.keys(fields).forEach(key => {
object[key] = fields[key];
});
}
return object;
}
export default class DataStoreSchemaCollection {
_collection:MongoCollection;
constructor(collection:MongoCollection) {
this._collection = collection;
}
getAllSchemas() {
return this._collection._rawFind({});
}
findSchema(name:string) {
return this._collection._rawFind(_dataStoreSchemaQueryFromNameQuery(name), {limit: 1}).then(results => {
return results[0];
});
}
// Atomically find and delete an object based on query.
// The result is the promise with an object that was in the database before deleting.
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
findAndDeleteSchema(name:string) {
// arguments: query, sort
return this._collection._mongoCollection.findAndRemove(_dataStoreSchemaQueryFromNameQuery(name), []).then(document => {
// Value is the object where mongo returns multiple fields.
return document.value;
});
}
addSchema(name:string, fields) {
let mongoObject = _dataStoreSchemaObjectFromNameFields(name, fields);
return this._collection.insertOne(mongoObject);
}
updateSchema(name:string, update) {
return this._collection.updateOne(_dataStoreSchemaQueryFromNameQuery(name), update);
}
upsertSchema(name:string, query:string, update) {
return this._collection.upsertOne(_dataStoreSchemaQueryFromNameQuery(name, query), update);
}
}