-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
28 lines (21 loc) · 858 Bytes
/
db.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
const mongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
mongoClient.connect("mongodb://localhost:27017")
.then(conn => global.conn = conn.db('crud-node-mongo'))
.catch(error => console.log(error))
function findAll() {
return global.conn.collection("customers").find().toArray();
}
function insert(customer) {
return global.conn.collection("customers").insertOne(customer);
}
function findOne(id) {
return global.conn.collection("customers").findOne(new ObjectId(id));
}
function update(id, customer) {
return global.conn.collection("customers").updateOne({ _id: new ObjectId(id) }, { $set: customer });
}
function deleteOne(id) {
return global.conn.collection("customers").deleteOne({ _id: new ObjectId(id) });
}
module.exports = { findAll, insert, findOne, update, deleteOne }