This repository was archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (170 loc) · 4.64 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const Mongoose = require('mongoose');
const EventEmmiter = require('events');
module.exports = class DatabaseMongooseConnection extends EventEmmiter {
constructor (url, name = 'easy-mongodb', config) {
super();
this.url = url;
this.config = config;
this.name = name;
this.firstConnect = true;
this._usefulMethods();
}
// Connect to mongodb
connect () {
Mongoose.connect(this.url, this.config ? this.config : { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
this.db = Mongoose.connection;
if(this.firstConnect) this._setupEvents();
this.firstConnect = false;
this.schema = new Mongoose.Schema({
name: {},
value: {}
});
this.model = Mongoose.model(this.name, this.schema);
return Mongoose.connection;
}
// Disconnect from mongodb
disconnect () {
this.forcedDisconnect = true;
return Mongoose.disconnect();
}
// Get database connection
get connection () {
return Mongoose.connection;
}
// Set database value
async set (key, value) {
// We search for the key object
const obj = await this.model.findOne({ name: key });
// We check if the key already exists, if it exists, we just change the value
if (obj) {
obj.value = value;
return obj.save();
}
// If it doesn't exist, we create a new object
else {
const newObj = new this.model({ name: key, value: value });
return newObj.save();
}
}
// Get database value
async get (key) {
const obj = await this.model.findOne({ name: key });
return obj ? obj.value : undefined;
}
// Remove database value
async remove (key) {
const obj = await this.model.findOne({ name: key });
return obj ? obj.remove() : null;
}
// Remove database value
async delete (key) {
const obj = await this.model.findOne({ name: key });
return obj ? obj.remove() : null;
}
// Get all database values
async getAll () {
const obj = await this.model.find({}).exec();
return obj ? obj : [];
}
// Get all database keys
async getKeys () {
const obj = await this.model.find({}).exec();
return obj ? obj.map(o => o.name) : [];
}
// Delete all database values
async deleteAll () {
const obj = await this.model.find({}).exec();
return obj ? obj.map(o => o.remove()) : [];
}
// Exists database value
async exists (key) {
const obj = await this.model.findOne({ name: key });
return obj ? true : false;
}
// Increment database value
async inc (key, value) {
const obj = await this.model.findOne({ name: key });
if (obj) {
obj.value = obj.value + value;
return obj.save();
}
else {
const newObj = new this.model({ name: key, value: value });
return newObj.save();
}
}
// Decrement database value
async dec (key, value) {
const obj = await this.model.findOne({ name: key });
if (obj) {
obj.value = obj.value - value;
return obj.save();
}
else {
const newObj = new this.model({ name: key, value: value });
return newObj.save();
}
}
// Pull database value (array)
async pull (key, value) {
const obj = await this.model.findOne({ name: key });
if (obj) {
obj.value = obj.value.filter(v => v !== value);
return obj.save();
}
else {
const newObj = new this.model({ name: key, value: value });
return newObj.save();
}
}
// Push database value (array)
async push (key, value) {
const obj = await this.model.findOne({ name: key });
if (obj) {
obj.value.push(value);
return obj.save();
}
else {
const newObj = new this.model({ name: key, value: value });
return newObj.save();
}
}
// Database value includes (array)
async includes (key, value) {
const obj = await this.model.findOne({ name: key });
if (obj) {
return obj.value.includes(value);
}
else {
return false;
}
};
// Moongose object
get mongoose () {
return this.model;
};
// Setup events
_setupEvents () {
this.db.on('open', () => this.emit('ready'));
this.db.on('error', e => this.emit('error', e));
this.db.on('disconnect', () => {
this.emit('disconnect');
// Reconnect to mongodb
if(!this.forcedDisconnect) Mongoose.connect(this.url, this.config ? this.config : { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true });
});
}
// Useful methods
_usefulMethods () {
this.add = this.inc;
this.rem = this.dec;
this.aumentar = this.inc;
this.diminuir = this.dec;
this.adicionar = this.inc;
this.remover = this.dec;
this.all = this.getAll;
this.keyArray = this.getAll;
this.clear = this.deleteAll;
this.has = this.exists;
this.keys = this.getKeys;
}
};