diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-02-13 14:15:59 +0100 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-02-13 14:15:59 +0100 |
commit | a44da1024dc39e2d2fef296fc1d4e5894090fce0 (patch) | |
tree | 0e9338baa9e64e8a8841913d18eddc5a2bb1a333 /src | |
parent | :sparkles: mongoose Schemas (diff) | |
download | server-a44da1024dc39e2d2fef296fc1d4e5894090fce0.tar.xz |
:zap: Config and database update
Diffstat (limited to 'src')
-rw-r--r-- | src/util/Config.ts | 37 | ||||
-rw-r--r-- | src/util/Database.ts | 7 | ||||
-rw-r--r-- | src/util/Permissions.ts | 3 |
3 files changed, 26 insertions, 21 deletions
diff --git a/src/util/Config.ts b/src/util/Config.ts index 91ffda01..ad70d37a 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -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"); diff --git a/src/util/Database.ts b/src/util/Database.ts index 56f53f9a..2304378c 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -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; } } diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts index f076e0c2..c3dcdb53 100644 --- a/src/util/Permissions.ts +++ b/src/util/Permissions.ts @@ -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); |