41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
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<RulesetMongoData> {
|
|
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<Ruleset> {
|
|
return new Ruleset(data.id, data);
|
|
}
|
|
|
|
async read(id: ActiveRecordId): Promise<Ruleset> {
|
|
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; |