-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmongodb.js
More file actions
141 lines (127 loc) · 4.23 KB
/
Copy pathmongodb.js
File metadata and controls
141 lines (127 loc) · 4.23 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
/* eslint import/no-extraneous-dependencies:0, import/no-unresolved:0, import/extensions:0 */
import { MongoClient, ObjectId } from 'mongodb';
import Base from './base';
import { promisified } from '../util';
const pConnect = promisified(MongoClient.connect);
/**
* An instance of [Promise]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}.
* @typedef {Promise} Promise
*/
/**
* @module MongoDBStore
*/
/**
* The Store class
* @class
*/
class Store extends Base {
/**
* connecting to db
*/
async connectDb() {
const client = (await ((pConnect)(this.dburl)));
this.db = client.db(client.s.options.dbName);
}
/**
* resolving the collection name with the prefix
*/
getCollection(name) {
return this.db.collection(this.dbprefix + name);
}
/**
* generate a random id
* @param {String} inputId - the input string id to convert into object id
* @return {ObjectId} _id - return random object id
*/
static GenId(inputId) {
return new ObjectId(inputId);
}
/**
* list all the entries
* @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 = 10, count, fields } = options || {};
let total = 0;
const collection = this.getCollection(coll);
if (collection) {
let cursor = collection.find(filter, { fields });
if (sort) cursor = cursor.sort(sort);
let records = (await cursor.skip(skip).limit(limit).toArray());
if (!fields) {
records = records.map(doc => doc._id);
}
if (count) {
total = await collection.count(filter);
return { records, total };
}
return { records };
}
return { records: [] };
}
/**
* list all the collections.
* @return {Promise} promise - return a promise
*/
async listcolls() {
return (await this.db.listCollections().toArray())
.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 cont = await this.getCollection(coll).findOne({ _id: Store.GenId(_id) }, { fields });
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, Store.GenId(_id), options);
await collection.findOneAndUpdate({ _id: Store.GenId(_id) }, { $set: data });
this.emitDbEvent(`update:${coll}:${_id}`, data, prevData);
return 1;
}
const newId = (await collection.insertOne(data)).insertedId.toString();
this.emitDbEvent(`create:${coll}:${newId}`, data);
return newId;
}
/**
* 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, Store.GenId(_id), options);
await this.getCollection(coll).deleteOne({ _id: Store.GenId(_id) });
this.emitDbEvent(`delete:${coll}:${_id}`, prevData);
return 1;
}
/**
* remove a collection.
* @param {string} coll - the collection name that should be deleted
* @return {Promise} promise - return a promise
*/
rmcoll(coll) {
this.getCollection(coll).drop();
return 1;
}
}
export default Store;