@wocker/pgsql-plugin
Version:
PostgreSQL plugin for wocker
96 lines (95 loc) • 3.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Config = void 0;
const Service_1 = require("./Service");
class Config {
constructor(data) {
const { default: defaultService, admin: { enabled: adminEnabled = true, host: adminHost = data === null || data === void 0 ? void 0 : data.adminHost, email: adminEmail = data === null || data === void 0 ? void 0 : data.adminEmail, password: adminPassword = data === null || data === void 0 ? void 0 : data.adminPassword, skipPassword: adminSkipPassword = data === null || data === void 0 ? void 0 : data.adminSkipPassword } = {}, services = [] } = data || {};
this.default = defaultService;
this.admin = {
enabled: adminEnabled,
host: adminHost,
email: adminEmail,
password: adminPassword,
skipPassword: adminSkipPassword
};
this.services = services.map((s) => {
return new Service_1.Service(s);
});
}
hasService(name) {
const service = this.services.find((service) => {
return service.name === name;
});
return !!service;
}
getService(name) {
const service = this.services.find((service) => {
return service.name === name;
});
return service || null;
}
getDefaultService() {
if (!this.default) {
return null;
}
return this.getService(this.default);
}
getServiceOrDefault(name) {
const service = name
? this.getService(name)
: this.getDefaultService();
if (!service) {
throw new Error("Service not found");
}
return service;
}
setService(service) {
let exists = false;
for (let i = 0; i < this.services.length; i++) {
if (this.services[i].name === service.name) {
exists = true;
this.services[i] = service;
}
}
if (!exists) {
this.services.push(service);
}
if (!this.default) {
this.default = service.name;
}
}
unsetService(name) {
this.services = this.services.filter((service) => {
return service.name !== name;
});
if (this.default === name) {
delete this.default;
}
}
toJSON() {
return {
default: this.default,
admin: this.admin,
services: this.services.length > 0 ? this.services.map((service) => {
return service.toObject();
}) : undefined
};
}
static make(fs) {
const data = fs.exists("config.json")
? fs.readJSON("config.json")
: {};
return new class extends Config {
save() {
if (!fs.exists("")) {
fs.mkdir("", {
recursive: true
});
}
fs.writeJSON("config.json", this.toJSON());
}
}(data);
}
}
exports.Config = Config;