forked from MidSpike/go-mongo-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (118 loc) · 4.03 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
'use strict';
//---------------------------------------------------------------------------------------------------------------//
const { MongoClient } = require('mongodb');
//---------------------------------------------------------------------------------------------------------------//
class GoMongoDB {
#client;
/**
* Provides a simplistic method of interacting with MongoDB servers
* @param {String} connection_url the entire connection url
*/
constructor(connection_url) {
this.#client = new MongoClient(connection_url, {
useUnifiedTopology: true,
});
}
/**
* @returns {MongoClient} the MongoDB client instance
*/
get client() {
return this.#client;
}
/**
* Used internally to ensure a connection is initiated to the database before performing actions
*/
async connect() {
if (!this.client.isConnected()) await this.client.connect();
return this;
}
/**
* Destroys the connection to the database with no way to re-connect
*/
async destroy() {
return this.client.close();
}
/**
* Fetches the specified database
* @param {String} database_name
*/
database(database_name) {
return this.#client.db(database_name);
}
/**
* Fetches the specified collection from the database
* @param {String} database_name
* @param {String} collection_name
*/
collection(database_name, collection_name) {
return this.database(database_name).collection(collection_name);
}
/**
* Finds all documents matching the filter
* https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#find
* @param {String} database_name
* @param {String} collection_name
* @param {*} filter (see url)
*/
async find(database_name, collection_name, filter={}) {
try {
await this.connect();
return await this.collection(database_name, collection_name).find(filter).toArray();
} catch (error) {
throw error;
}
}
/**
* Updates all documents matching the filter
* https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateMany
* @param {String} database_name
* @param {String} collection_name
* @param {*} filter (see url)
* @param {*} update (see url)
* @param {*} options (see url)
*/
async update(database_name, collection_name, filter={}, update={}, options={}) {
try {
await this.connect();
return await this.collection(database_name, collection_name).updateMany(filter, update, options);
} catch (error) {
throw error;
}
}
/**
* Adds documents to a specified collection
* https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateMany
* @param {String} database_name
* @param {String} collection_name
* @param {Array<*>} items (see url)
* @param {*} options (see url)
*/
async add(database_name, collection_name, items=[], options={}) {
try {
await this.connect();
return await this.collection(database_name, collection_name).insertMany(items, options);
} catch (error) {
throw error;
}
}
/**
* Removes documents in a specified collection
* https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#updateMany
* @param {String} database_name
* @param {String} collection_name
* @param {*} filter (see url)
* @param {*} options (see url)
*/
async remove(database_name, collection_name, filter={}, options={}) {
try {
await this.connect();
return await this.collection(database_name, collection_name).deleteMany(filter, options);
} catch (error) {
throw error;
}
}
}
//---------------------------------------------------------------------------------------------------------------//
module.exports = {
GoMongoDB,
};