Config and database update

This commit is contained in:
Flam3rboy
2021-02-13 14:15:59 +01:00
parent 8595646b72
commit a44da1024d
51 changed files with 836 additions and 97 deletions
+22 -15
View File
@@ -1,31 +1,38 @@
import { Schema, model, Types, Document } from "mongoose";
import "missing-native-js-functions";
import db from "./Database";
import { ProviderCache } from "lambert-db";
var Config: ProviderCache;
import db, { MongooseCache } from "./Database";
var Config = new MongooseCache(db.collection("config"), [], { onlyEvents: false });
export default {
init: async function init(opts: DefaultOptions = DefaultOptions) {
await db.collection("config").findOne({});
Config = await db.data.config({}).cache();
init: async function init() {
await Config.init();
await Config.set(opts.merge(Config.cache || {}));
return this.setAll(Config.data.merge(DefaultOptions));
},
getAll: function get() {
return <DefaultOptions>Config.get();
return <DefaultOptions>Config.data;
},
setAll: function set(val: any) {
return Config.set(val);
return db.collection("config").updateOne({}, { $set: val });
},
};
export interface DefaultOptions {
export const DefaultOptions = {
api: {},
gateway: {},
voice: {},
};
export interface DefaultOptions extends Document {
api?: any;
gateway?: any;
voice?: any;
}
export const DefaultOptions: DefaultOptions = {
api: {},
gateway: {},
voice: {},
};
export const ConfigSchema = new Schema({
api: Object,
gateway: Object,
voice: Object,
});
export const ConfigModel = model<DefaultOptions>("Config", ConfigSchema, "config");
+4 -3
View File
@@ -1,12 +1,12 @@
import "./MongoBigInt";
import mongoose, { Collection } from "mongoose";
import mongoose, { Collection, Connection } from "mongoose";
import { ChangeStream, ChangeEvent, Long } from "mongodb";
import EventEmitter from "events";
const uri = process.env.MONGO_URL || "mongodb://localhost:27017/fosscord?readPreference=secondaryPreferred";
const connection = mongoose.createConnection(uri, { autoIndex: true });
export default connection;
export default <Connection>connection;
export interface MongooseCache {
on(event: "delete", listener: (id: string) => void): this;
@@ -37,7 +37,8 @@ export class MongooseCache extends EventEmitter {
this.stream.on("error", console.error);
if (!this.opts.onlyEvents) {
this.data = await this.collection.aggregate(this.pipeline).toArray();
const arr = await this.collection.aggregate(this.pipeline).toArray();
this.data = arr.length ? arr[0] : arr;
}
}
-3
View File
@@ -46,9 +46,6 @@ export class Permissions extends BitField {
/**
* Checks whether the bitfield has a permission, or multiple permissions.
* @param {PermissionResolvable} permission Permission(s) to check for
* @param {boolean} [checkAdmin=true] Whether to allow the administrator permission to override
* @returns {boolean}
*/
has(permission: PermissionResolvable, checkAdmin = true) {
return (checkAdmin && super.has(Permissions.FLAGS.ADMINISTRATOR)) || super.has(permission);