-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
98 lines (73 loc) · 2.71 KB
/
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
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
const { MongoClient, ObjectID } = require('mongodb')
const DATABASE_NAME = process.env.DATABASE_NAME || 'koalat'
const DATABASE_URL = process.env.DATABASE_URL || 'mongodb://127.0.0.1:27017/'
let _db;
const init = async () => {
try {
const client = await MongoClient.connect(DATABASE_URL + DATABASE_NAME, { useNewUrlParser: true })
_db = client.db(DATABASE_NAME)
console.log('Mongo server connected:', DATABASE_URL + DATABASE_NAME)
} catch (err) {
console.error('Unable to connect to mongo server.', err)
}
}
// MongoClient.connect(DATABASE_URL + DATABASE_NAME, { useNewUrlParser: true },function (err, database) {
// if (err) {
// console.error('Unable to connect to mongo server.', err)
// } else {
// _db = database.db(DATABASE_NAME)
// console.log('Mongo server connected:', DATABASE_URL + DATABASE_NAME);
// }
// })
const createOrUpdate = async (collection, filter, value) => {
var r = await _db.collection(collection).find(filter).toArray()
if (r.length) {
return await update(collection, { _id: r[0]._id }, value)
} else {
return await create(collection, value)
}
}
const create = async (collection, value) => {
var r = await _db.collection(collection).insertOne(value)
var i = await _db.collection(collection).find({ _id: r.insertedId }).toArray()
return i[0]
}
const createMany = async (collection, docs) => {
await _db.collection(collection).insertMany(docs)
}
const read = async (collection, filter) => {
var r = await _db.collection(collection).find(filter).toArray()
return r[0]
}
const readMany = async (collection, query, options) => {
return await _db.collection(collection).find(query, options).toArray()
}
const update = async (collection, filter, value) => {
if (typeof collection !== 'string') throw 'DB.Update error: collection name must be a string'
var doc = await _db.collection(collection).findOneAndUpdate(filter, { $set: value }, { upsert: true })
if (doc.lastErrorObject.updatedExisting) {
return (await _db.collection(collection).find({ _id: doc.value._id }).toArray())[0]
}
if (doc.value) {
return doc.value
} else {
return (await _db.collection(collection).find({ _id: doc.lastErrorObject.upserted }).toArray())[0]
}
}
const Delete = async (collection, filter) => {
var r = await _db.collection(collection).deleteOne(filter)
if (r.deletedCount === 1) {
return { status: 'DELETE_SUCCESS' }
} else {
return { status: 'DELETE_FAIL' }
}
}
const DeleteMany = async (collection, filter) => {
var r = await _db.collection(collection).deleteMany(filter)
if (r.result.ok === 1) {
return { status: 'DELETE_SUCCESS' }
} else {
return { status: 'DELETE_FAIL' }
}
}
module.exports = {init, create, createMany, createOrUpdate, Delete, DeleteMany, read, readMany, update }