-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
167 lines (138 loc) · 4.68 KB
/
index.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
164
165
166
167
const Db = require("./lib/json-db");
class JsonbDb extends Db {
constructor(configs = { db }) {
super(configs)
}
/**
* @return {string[]} arrays of collections name
*/
collections() {
return this.dbCollections();
}
/**
* create collection in a database
@param {String} collectionName
@param {Object} data
* @return {{status:Boolean,message:string}} responseMessage
*/
createCollection(collectionName, data) {
return this.dbCreateCollection(collectionName, data);
}
/**
* update collection in a database
*@param {String} oldcollectionName
@param {String} oldcollectionName
* @return {{status:Boolean,message:string}} responseMessage
*/
updateCollection(oldCollectionName, newCollectionName) {
return this.dbUpdateCollection(oldCollectionName, newCollectionName);
}
/**
* delete collection from database
* @param {String} collectionName
* @return {{status:Boolean,message:string}} responseMessage
*/
dropCollection(collectionName) {
return this.dbDropCollection(collectionName);
}
/**
* manipulate data stored in collections
* @param { String } collection
*/
collection(collection) {
if (!collection) throw new Error("CollectionName required");
return {
find: (criteria) => new class {
/**
*@param { JsonbDb } base
*/
constructor(base) {
this.query = [];
let dbQuery = base.dbCollection(collection).find(criteria);
if (dbQuery != null) {
this.query = dbQuery;
}
return this;
}
/**
* limit number of output for exeuted query
* @param {Number} rows
*/
take(rows) {
if (typeof rows != 'number') throw new Error("input row paramater must be a number");
try {
this.query.length = rows;
} catch (e) {
}
return this;
}
/**
* skip rows when retriving data from db
* @param {Number} rows
*/
skip(rows) {
if (typeof rows != 'number') throw new Error("input row paramater must be a number");
try {
this.query = this.query.slice(rows);
} catch (e) {
}
return this;
}
/**convert array to object
*@return { Object } object
*/
toObject() {
return Object.assign({}, this.query)
}
/**
* format output into pretty readable json fomart */
pretty() {
return JSON.stringify(this.query, null, 4);
}
/**
* format output into tabular form
*
* */
table() {
console.table(this.query);
return this.query.length + " rows fetched";
}
/**
* count total number of rows for provided query
* @return {Number} number
*/
count() {
if (Array.isArray(this.query)) {
return this.query.length;
}
return 0;
}
}(this),
/**
* insert single object into a databse
* @param {Object} value
* @return insertedIds
*/
insert: (value) => this.dbCollection(collection).insert(value),
/**
* insert multiple object into a database
* @param { Array } values
*/
insertMany: (values) => this.dbCollection(collection).insertMany(values),
/**
* update items into a databse
* @param {Object} Object
* @param {Object} value
*/
update: (criteria, value) => this.dbCollection(collection).update(criteria, value),
/**
* remove items from a databse
* @param {Object} criteria
* @param {Object} value
* @return removed items
*/
remove: (criteria) => this.dbCollection(collection).remove(criteria),
}
}
}
module.exports = { JsonbDb };