2020-08-12 12:03:35 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const mongoose = require('mongoose')
|
|
|
|
const schema = require('./schema')
|
|
|
|
|
2020-08-21 18:11:20 +02:00
|
|
|
// the default mongodb url (local server)
|
2020-08-22 14:36:34 +02:00
|
|
|
const mongodbURL = process.env.DB_URL || 'mongodb://127.0.0.1:27017'
|
2020-08-21 18:11:20 +02:00
|
|
|
|
|
|
|
mongoose.connect(mongodbURL, {
|
2020-08-12 12:03:35 +02:00
|
|
|
useNewUrlParser: true,
|
|
|
|
useUnifiedTopology: true,
|
|
|
|
useFindAndModify: false
|
|
|
|
})
|
|
|
|
|
|
|
|
const Count = mongoose.connection.model('Count', schema)
|
|
|
|
|
|
|
|
function getNum(name) {
|
|
|
|
return Count
|
|
|
|
.findOne({ name }, '-_id -__v')
|
|
|
|
.exec()
|
|
|
|
}
|
|
|
|
|
|
|
|
function getAll() {
|
|
|
|
return Count
|
|
|
|
.find({ }, '-_id -__v')
|
|
|
|
.exec()
|
|
|
|
}
|
|
|
|
|
|
|
|
function setNum(name, num) {
|
|
|
|
return Count
|
|
|
|
.findOneAndUpdate({ name }, { name, num }, { upsert: true })
|
|
|
|
.exec()
|
|
|
|
}
|
|
|
|
|
2023-03-06 11:57:41 +01:00
|
|
|
function setNumMulti(counters) {
|
|
|
|
const bulkOps = counters.map(obj => {
|
|
|
|
const { name, num } = obj
|
|
|
|
return {
|
|
|
|
updateOne: {
|
|
|
|
filter: { name },
|
|
|
|
update: { name, num },
|
|
|
|
upsert: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return Count.bulkWrite(bulkOps, { ordered : false })
|
|
|
|
}
|
|
|
|
|
2020-08-12 12:03:35 +02:00
|
|
|
module.exports = {
|
|
|
|
getNum,
|
|
|
|
getAll,
|
2023-03-06 11:57:41 +01:00
|
|
|
setNum,
|
|
|
|
setNumMulti
|
2020-08-22 14:36:34 +02:00
|
|
|
}
|