import MongoStoredObjectCollection from "./MongoStoredObjectCollection"; import {DEFAULT_RULESET, DEFAULT_RULESET_NAME, RulesetSchema} from "../rulesets"; import {getMongoObjectCollection} from "../database"; import Ruleset from "../Objects/Ruleset"; import {ActiveRecordId} from "../Objects/ActiveRecord"; type RulesetMongoData = RulesetSchema & {id: ActiveRecordId}; class RulesetCollection extends MongoStoredObjectCollection { private static instance?: RulesetCollection; constructor() { super(); } static getInstance(): RulesetCollection { if (RulesetCollection.instance === undefined) { RulesetCollection.instance = new RulesetCollection(); } return RulesetCollection.instance; } async init() { this.mongoDbClientCollection = getMongoObjectCollection("rulesets"); } private async rulesetFrom(data: RulesetMongoData): Promise { return new Ruleset(data.id, data); } async read(id: ActiveRecordId): Promise { if (id === DEFAULT_RULESET_NAME) { return new Ruleset(DEFAULT_RULESET_NAME, DEFAULT_RULESET); } else { const foundRuleset = await this.mongoRead(id); return this.rulesetFrom(foundRuleset); } } } export default RulesetCollection.getInstance;