Skip to content

Commit 1ac6cb2

Browse files
committed
mongodb service
1 parent 3f8a023 commit 1ac6cb2

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

mongodb/node/services/mongodb.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { MongoClient } = require("mongodb");
2+
3+
const DBClient = new MongoClient("mongodb://localhost:27017",
4+
{
5+
useNewUrlParser: true,
6+
useUnifiedTopology: true,
7+
}
8+
);
9+
10+
let DB;
11+
12+
async function setupDb() {
13+
await DBClient.connect();
14+
DB = DBClient.db("heroes");
15+
}
16+
17+
const ItemsDb = {
18+
Get: async (collection, query) => DB.collection(collection).find(query).toArray(),
19+
20+
Insert: async (collection, value) => DB.collection(collection).insertOne(value),
21+
22+
InsertMany: async (collection, value) => DB.collection(collection).insertMany(value),
23+
24+
Update: async (collection, query, value) => DB.collection(collection).updateOne(query, { $set: value }),
25+
26+
DeleteOne: async (collection, query) => DB.collection(collection).deleteOne(query),
27+
28+
Delete: async (collection, query) => DB.collection(collection).deleteMany(query),
29+
};
30+
31+
function createItemsDb(collection) {
32+
return {
33+
Get: async (query) => ItemsDb.Get(collection, query),
34+
Insert: async (value) => ItemsDb.Insert(collection, value),
35+
InsertMany: async(value) => ItemsDb.InsertMany(collection, value),
36+
Update: async (query, value) => ItemsDb.Update(collection, query, value),
37+
DeleteOne: async (query) => ItemsDb.DeleteOne(collection, query),
38+
Delete: async (query) => ItemsDb.Delete(collection, query),
39+
};
40+
}
41+
42+
const HeroesDb = createItemsDb("Heroes");
43+
44+
module.exports = {
45+
HeroesDb,
46+
setupDb
47+
}

0 commit comments

Comments
 (0)