mirror of
https://github.com/journey-ad/Moe-Counter.git
synced 2024-11-24 07:40:11 +01:00
56c4a37f6d
简化代码
40 lines
749 B
JavaScript
40 lines
749 B
JavaScript
'use strict'
|
|
|
|
const mongoose = require('mongoose')
|
|
const schema = require('./schema')
|
|
|
|
// the default mongodb url (local server)
|
|
const mongodbURL = process.env.DB_URL || 'mongodb://127.0.0.1:27017'
|
|
|
|
mongoose.connect(mongodbURL, {
|
|
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()
|
|
}
|
|
|
|
module.exports = {
|
|
getNum,
|
|
getAll,
|
|
setNum
|
|
}
|