mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
833 B
46 lines
833 B
'use strict';
|
|
|
|
const Mongo = require(__dirname+'/db.js')
|
|
, db = Mongo.db.collection('roles')
|
|
, cache = require(__dirname+'/../lib/redis/redis.js');
|
|
|
|
module.exports = {
|
|
|
|
db,
|
|
|
|
findOne: async (id) => {
|
|
//is there any point even caching
|
|
let role = await cache.get(`role:${id}`);
|
|
if (role) {
|
|
return role;
|
|
} else {
|
|
role = await db.findOne({ '_id': id });
|
|
if (role) {
|
|
role.permissions = role.permissions.toString('base64');
|
|
cache.set(`role:${id}`, role);
|
|
}
|
|
}
|
|
return role;
|
|
},
|
|
|
|
find: () => {
|
|
return db.find({}).toArray();
|
|
},
|
|
|
|
updateOne: async (id, permissions) => {
|
|
const res = await db.updateOne({
|
|
'_id': id
|
|
}, {
|
|
'$set': {
|
|
'permissions': Mongo.Binary(permissions.array),
|
|
},
|
|
});
|
|
cache.del(`role:${id}`);
|
|
return res;
|
|
},
|
|
|
|
deleteAll: () => {
|
|
return db.deleteMany({});
|
|
},
|
|
|
|
};
|
|
|