@onurege3467/zerohelper 2.1.1 → 2.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const mongodb = require("mongodb");
|
|
2
|
+
const { Collection } = require("./Collection.js");
|
|
3
|
+
|
|
4
|
+
class Database {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* It has Db feature in MongoDB. Most operations are done with this area.
|
|
8
|
+
* @type {mongodb.Db}
|
|
9
|
+
*/
|
|
10
|
+
Db;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* It carries the Client connected to MongoDB.
|
|
14
|
+
* @type {mongodb.MongoClient}
|
|
15
|
+
*/
|
|
16
|
+
Client;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param {mongodb.MongoClient} client
|
|
21
|
+
* @param {mongodb.Db} databaseName
|
|
22
|
+
*/
|
|
23
|
+
constructor(client, databaseName) {
|
|
24
|
+
this.Client = client;
|
|
25
|
+
this.Db = this.Client.db(databaseName);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* If collection does not exists, creates collection then returns the collection. Otherwise just returns collection.
|
|
30
|
+
* @param {String} collectionName
|
|
31
|
+
* @return {Collection}
|
|
32
|
+
*/
|
|
33
|
+
collection(collectionName) {
|
|
34
|
+
const dbCollection = this.Db.collection(collectionName);
|
|
35
|
+
dbCollection.createIndex({ key: 1 });
|
|
36
|
+
const collection = new Collection(dbCollection);
|
|
37
|
+
|
|
38
|
+
return collection;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Drops the Database Collection.
|
|
43
|
+
* @param {String} collectionName
|
|
44
|
+
* @return {Promise<void>}
|
|
45
|
+
*/
|
|
46
|
+
async dropCollection(collectionName) {
|
|
47
|
+
const flag = await this.Db.dropCollection(collectionName);
|
|
48
|
+
|
|
49
|
+
return flag;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
module.exports = { Database };
|