60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
|
const redis = require("../redis")
|
||
|
|
||
|
const { ObjectId } = require('mongodb');
|
||
|
const connector = require("./CONNECTOR");
|
||
|
var dbo;
|
||
|
connector.connect().then((ret) => {
|
||
|
dbo = ret;
|
||
|
});
|
||
|
|
||
|
exports.byId = function (id, callback) {
|
||
|
dbo.collection("shares")
|
||
|
.findOne({ _id: ObjectId(id) })
|
||
|
.then((result) => {
|
||
|
callback(result);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
exports.exists = function (object_id, callback) {
|
||
|
dbo.collection("shares")
|
||
|
.findOne({ object_id: ObjectId(object_id) })
|
||
|
.then(result => {
|
||
|
callback(result);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
exports.add = function (item, callback) {
|
||
|
let redis_key = item.type + "Id__" + item.object_id;
|
||
|
redis.del(redis_key);
|
||
|
|
||
|
dbo.collection("shares")
|
||
|
.insertOne(item, err => {
|
||
|
if (err) throw err;
|
||
|
if (callback) {
|
||
|
dbo.collection("shares")
|
||
|
.aggregate([
|
||
|
{ $sort: { _id: -1 } },
|
||
|
{ $limit: 1 }
|
||
|
])
|
||
|
.toArray((err, result) => {
|
||
|
if (err) throw err;
|
||
|
callback(result[0]);
|
||
|
})
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
exports.delete = function (item, callback) {
|
||
|
let redis_key = item.type + "Id__" + item.object_id;
|
||
|
redis.del(redis_key);
|
||
|
|
||
|
dbo.collection("shares")
|
||
|
.deleteMany({ object_id: ObjectId(item.object_id) }, (err) => {
|
||
|
if (err) throw err;
|
||
|
if (callback) {
|
||
|
callback();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
|
||
|
}
|