-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloki.js
More file actions
170 lines (157 loc) · 4.9 KB
/
Copy pathloki.js
File metadata and controls
170 lines (157 loc) · 4.9 KB
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
164
165
166
167
168
169
170
import LokiJS from 'lokijs';
import Base from './base';
import { pick } from '../util';
/**
* @module LokiJSStore
*/
/**
* The Store class
* @class
*/
class Store extends Base {
/**
* method to convert $loki in document to _id and vice verca
* @param {object} doc - the input document or filter
* @return {Object} mdoc - return the modified document
*/
static resolveId(doc, reverse) {
let key1 = '$loki';
let key2 = '_id';
if (reverse) {
[key1, key2] = [key2, key1];
}
const dc = Object.assign({}, doc);
if (Object.hasOwnProperty.call(dc, key1)) {
dc[key2] = dc[key1];
delete dc[key1];
}
return dc;
}
/**
* connecting to db
*/
async connectDb() {
this.db = new LokiJS(this.dburl);
}
/**
* resolving the collection name with the prefix
*/
getCollection(name) {
return this.db.getCollection(this.dbprefix + name);
}
/**
* generate a random id
* @return {string} text - return random string id
*/
static GenId() {
throw new Error('Id generation is not available currently.');
}
/**
* list all the entries, based on pagination
* @param {string} coll - the collection, that should be read
* @param {object} filter - the filter to apply
* @param {object} options - the options to list the collection
* @return {Promise} promise - return a promise
*/
async list(coll, filter, options) {
const { sort, skip = 0, limit = this.defaultPageSize, count, fields } = options || {};
const fl = Store.resolveId(filter, true);
let total = 0;
const collection = this.getCollection(coll);
if (collection) {
let cursor = collection.chain().find(fl);
if (typeof sort === 'object') {
const sortKey = Object.keys(sort).shift();
cursor = cursor.simplesort(sortKey, sort[sortKey] === -1);
}
const records = cursor.offset(skip).limit(limit).data().map((ob) => {
if (!fields) {
return ob.$loki;
}
return Store.resolveId(pick(ob, ...(Object.keys(fields))));
});
if (count) {
total = collection.count((typeof fl === 'object' && fl && Object.keys(fl).length)
? fl : undefined);
return { records, total };
}
return { records };
}
return { records: [] };
}
/**
* list all the collections.
* @return {Promise} promise - return a promise
*/
listcolls() {
return this.db.collections.map(cl => cl.name.substring(this.dbprefix.length));
}
/**
* read a document.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the document id, that should be read
* @param {object} fields - the fields to retrieve
* @return {Promise} promise - return a promise
*/
async read(coll, _id, fields) {
const doc = this.getCollection(coll).get(_id);
const cont = Store.resolveId(pick(doc, ...(Object.keys(fields || doc))));
this.emitDbEvent(`read:${coll}:${_id}`, cont);
return cont;
}
/**
* create or update a document.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the doc id at which, that should be created/updated
* @param {object} data - the content in to write
* @param {object} options - the options to update the collection
* @return {Promise} promise - return a promise
*/
async write(coll, _id, data, options) {
const collection = this.getCollection(coll);
if (_id) {
const prevData = await this.getPrevDoc(coll, _id, options);
collection.findAndUpdate({ $loki: _id }, dc => Object.assign(dc, data));
this.emitDbEvent(`update:${coll}:${_id}`, data, prevData);
return 1;
}
const ob = collection.insert(data);
this.emitDbEvent(`create:${coll}:${ob.$loki}`, data);
return ob.$loki;
}
/**
* delete a document.
* @param {string} coll - the collection, that should be read
* @param {string} _id - the doc id at which, that should be deleted
* @param {object} options - the options to delete the collection
* @return {Promise} promise - return a promise
*/
async del(coll, _id, options) {
const prevData = await this.getPrevDoc(coll, _id, options);
this.getCollection(coll).findAndRemove({ $loki: _id });
this.emitDbEvent(`delete:${coll}:${_id}`, prevData);
return 1;
}
/**
* create a collection.
* @param {string} coll - the new collection, that should be created
* @param {Object} opts - the options to create collection
* @return {String} coll - the collection name
*/
mkcoll(coll, opts) {
if (!this.db.getCollection(coll)) {
this.db.addCollection(this.dbprefix + coll, opts);
}
return 1;
}
/**
* remove a collection.
* @param {string} coll - the collection name that should be deleted
* @return {String} coll - the collection name
*/
rmcoll(coll, opts) {
this.db.removeCollection(this.dbprefix + coll, opts);
return 1;
}
}
export default Store;