@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
444 lines (443 loc) • 20.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DB = exports.queryOptionsToUrlOptions = exports.queryFilterToUrlFilter = exports.dbCollections = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const lodash_1 = require("lodash");
const app_config_1 = require("../../app.config");
const fetchApi_1 = __importDefault(require("./fetchApi"));
exports.dbCollections = [
"app",
"build",
"database",
"db_backup",
"provider",
"cluster",
"git",
"git_repo",
"registry",
"framework",
"project",
"release",
"role",
"route",
"team",
"user",
"api_key_user",
"service_account",
"workspace",
"cronjob",
"webhook",
"notification",
"storage",
"monitor/namespaces",
];
function queryFilterToUrlFilter(filter = {}) {
return new URLSearchParams(filter).toString();
}
exports.queryFilterToUrlFilter = queryFilterToUrlFilter;
function queryOptionsToUrlOptions(options = {}) {
let optionsStr = "";
const { $or, order, populate, select, total, total_items, total_pages, current_page, page_size, next_page, prev_page, filter, func, ignorable, ...rest } = options;
if (!(0, lodash_1.isEmpty)(order)) {
const orderStr = Object.entries(options.order)
.map(([key, val]) => {
return val === 1 ? key : `-${key}`;
})
.join(",");
optionsStr += "sort=" + orderStr;
}
if (!(0, lodash_1.isEmpty)(populate)) {
const populateStr = options.populate.join(",");
optionsStr += (optionsStr && "&") + "populate=" + populateStr;
}
if (!(0, lodash_1.isEmpty)(select)) {
const selectStr = options.select.join(",");
optionsStr += (optionsStr && "&") + "select=" + selectStr;
}
if (!(0, lodash_1.isEmpty)(rest))
optionsStr += (optionsStr && "&") + new URLSearchParams(rest).toString();
if (!(0, lodash_1.isEmpty)($or)) {
optionsStr += (optionsStr && "&") + "or=" + JSON.stringify($or);
}
return optionsStr;
}
exports.queryOptionsToUrlOptions = queryOptionsToUrlOptions;
class DB {
static async getService(collection, ownership) {
let svc;
switch (collection) {
case "app":
const { AppService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new AppService();
break;
case "build":
const { BuildService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new BuildService();
break;
case "database":
const { CloudDatabaseService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new CloudDatabaseService();
break;
case "db_backup":
const { CloudDatabaseBackupService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new CloudDatabaseBackupService();
break;
case "provider":
const { CloudProviderService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new CloudProviderService();
break;
case "cronjob":
const { CronjobService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new CronjobService();
break;
case "cluster":
const { ClusterService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new ClusterService();
break;
case "registry":
const { ContainerRegistryService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new ContainerRegistryService();
break;
case "framework":
const { FrameworkService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new FrameworkService();
break;
case "git":
case "git_repo":
const { GitProviderService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new GitProviderService();
break;
case "project":
const { ProjectService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new ProjectService();
break;
case "release":
const { ReleaseService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new ReleaseService();
break;
case "role":
const { RoleService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new RoleService();
break;
case "route":
const { RouteService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new RouteService();
break;
case "team":
const { TeamService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new TeamService();
break;
case "user":
const { UserService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new UserService();
break;
case "api_key_user":
const { ApiKeyUserService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new ApiKeyUserService();
break;
case "service_account":
const { ServiceAccountService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new ServiceAccountService();
break;
case "workspace":
const { WorkspaceService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new WorkspaceService();
break;
case "webhook":
const { WebhookService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new WebhookService();
break;
case "notification":
const { NotificationService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new NotificationService();
break;
case "storage":
const { CloudStorageService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new CloudStorageService();
break;
case "monitor/namespaces":
const { MonitorNamespaceService } = await Promise.resolve().then(() => __importStar(require("../../services")));
svc = new MonitorNamespaceService();
break;
}
// assign ownership
if (svc)
svc.ownership = ownership;
return svc;
}
static async count(collection, filter = {}, options, pagination) {
let amount;
if (app_config_1.isServerMode) {
const svc = await DB.getService(collection, options === null || options === void 0 ? void 0 : options.ownership);
if (!svc) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] COUNT :>> Service "${collection}" not found.`);
return;
}
try {
amount = (await svc.count(filter)) || 0;
}
catch (e) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] COUNT > Service "${collection}" :>>`, e);
}
}
else {
// extract "subpath", then delete it from "options"
const { subpath = "" } = options;
delete options.subpath;
delete options.filter;
delete options.ownership;
const filterStr = queryFilterToUrlFilter(filter);
const optionStr = (filterStr ? "&" : "") + queryOptionsToUrlOptions(options);
// special case
const path = collection === "git_repo" ? "git" : collection;
const url = `/api/v1/${path}${subpath}?${filterStr}${optionStr === "&" ? "" : optionStr}`;
const { data = [], status, messages = [""] } = await (0, fetchApi_1.default)({ url });
if (!status && messages[0] && !(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] COUNT - ${url} :>>`, messages);
amount = data;
}
return amount;
}
static async find(collection, filter = {}, options = {}, pagination) {
let items;
if (app_config_1.isServerMode) {
const svc = await DB.getService(collection, options === null || options === void 0 ? void 0 : options.ownership);
if (!svc) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] FIND :>> Service "${collection}" not found.`);
return [];
}
try {
items = (await svc.find(filter, options, pagination)) || [];
}
catch (e) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] FIND > Service "${collection}" :>>`, e);
items = [];
}
}
else {
// extract "subpath", then delete it from "options"
const { subpath = "" } = options;
delete options.subpath;
delete options.filter;
delete options.ownership;
const filterStr = queryFilterToUrlFilter(filter);
const optionStr = (filterStr ? "&" : "") + queryOptionsToUrlOptions(options);
// special case
const path = collection === "git_repo" ? "git" : collection;
const url = `/api/v1/${path}${subpath}?${filterStr}${optionStr === "&" ? "" : optionStr}`;
const { data = [], status, messages = [""] } = await (0, fetchApi_1.default)({ url, isDebugging: options.isDebugging });
if (!status && messages[0] && !(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] FIND MANY - ${url} :>>`, messages);
items = data;
}
return items;
}
static async findOne(collection, filter = {}, options = {}) {
// extract "subpath", then delete it from "options"
const { subpath = "", func } = options;
delete options.subpath;
delete options.filter;
delete options.func;
let item;
if (app_config_1.isServerMode) {
const svc = await DB.getService(collection, options === null || options === void 0 ? void 0 : options.ownership);
if (!svc) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] FIND ONE > Service "${collection}" not found.`);
return;
}
try {
item = await svc.findOne(filter, options);
}
catch (e) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] FIND ONE > Service "${collection}" :>>`, e);
}
}
else {
delete options.ownership;
const filterStr = queryFilterToUrlFilter(filter);
const optionStr = (filterStr ? "&" : "") + queryOptionsToUrlOptions(options);
// special case
const path = collection === "git_repo" ? "git" : collection;
const url = `/api/v1/${path}${subpath}?${filterStr}${optionStr === "&" ? "" : optionStr}`;
const res = await (0, fetchApi_1.default)({ url });
if (options === null || options === void 0 ? void 0 : options.isDebugging)
console.log("[DB] FIND ONE > " + url + " > response :>> ", res);
const { data = [], status, messages = [""] } = res;
if (!status && messages[0] && !(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] FIND ONE - ${url} :>>`, messages);
item = (0, lodash_1.isArray)(data) ? data[0] : data;
}
return item;
}
static async create(collection, data, options = {}) {
const { subpath = "", filter, func } = options;
delete options.subpath;
delete options.filter;
delete options.func;
let item;
if (app_config_1.isServerMode) {
const svc = await DB.getService(collection, options === null || options === void 0 ? void 0 : options.ownership);
if (!svc) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] CREATE :>> Service "${collection}" not found.`);
return;
}
// if (func) {
// return svc[func]();
// }
try {
item = await svc.create(data);
}
catch (e) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] CREATE > Service "${collection}" :>>`, e);
}
}
else {
delete options.ownership;
let newData = data;
const filterStr = queryFilterToUrlFilter(filter);
const optionStr = (filterStr ? "&" : "") + queryOptionsToUrlOptions(options);
// special case
const path = collection === "git_repo" ? "git" : collection;
const url = `/api/v1/${path}${subpath}?${filterStr}${optionStr === "&" ? "" : optionStr}`;
if (options === null || options === void 0 ? void 0 : options.isDebugging)
console.log(`[CLI] DB > CREATE > ${url} :>>`, newData);
// console.log("newData :>> ", newData);
const { data: result, status, messages = [""], } = await (0, fetchApi_1.default)({
url,
method: "POST",
data: newData,
});
if (!status && messages[0] && !(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] CREATE - ${url} :>>`, messages);
item = result;
}
return item;
}
static async update(collection, filter, data, options = {}) {
let items;
if (app_config_1.isServerMode) {
const svc = await DB.getService(collection, options === null || options === void 0 ? void 0 : options.ownership);
if (!svc) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] UPDATE > Service "${collection}" :>> Service not found.`);
return;
}
svc.ownership = options.ownership;
if (options.isDebugging)
console.log("[DB] data :>> ", data);
if (options.isDebugging)
console.log("[DB] options :>> ", options);
try {
items = (await svc.update(filter, data, options)) || [];
}
catch (e) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] UPDATE > Service "${collection}" :>>`, e.stack);
items = [];
}
if (options.isDebugging)
console.log("[DB] items :>> ", items);
}
else {
const { subpath = "" } = options;
delete options.subpath;
delete options.ownership;
const filterStr = queryFilterToUrlFilter(filter);
const optionStr = (filterStr ? "&" : "") + queryOptionsToUrlOptions(options);
// special case
const path = collection === "git_repo" ? "git" : collection;
const url = `/api/v1/${path}${subpath}?${filterStr}${optionStr === "&" ? "" : optionStr}`;
const updateData = data;
if (options.isDebugging)
console.log(`[DB] UPDATE > ${url} > updateData :>> `, updateData);
const { status, data: result = [], messages = [""], } = await (0, fetchApi_1.default)({
url,
method: "PATCH",
data: updateData,
});
if (options.isDebugging)
console.log("[DB] UPDATE > " + url + " > result :>> ", status, "-", result, "-", messages);
if (!status && messages[0] && !(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] UPDATE - ${url} :>>`, messages);
items = (0, lodash_1.isArray)(result) ? result : [result];
}
return items;
}
static async updateOne(collection, filter, data, options = {}) {
let items = await this.update(collection, filter, data, options);
if (!items || items.length === 0)
return;
return items[0];
}
static async delete(collection, filter, data = {}, options = {}) {
let item;
if (app_config_1.isServerMode) {
const svc = await DB.getService(collection, options === null || options === void 0 ? void 0 : options.ownership);
if (!svc) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] DELETE > Service "${collection}" :>> Service not found.`);
return;
}
try {
item = await svc.softDelete(filter);
}
catch (e) {
if (!(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] DELETE > Service "${collection}" :>>`, e);
}
}
else {
const { subpath = "" } = options;
delete options.subpath;
delete options.ownership;
const filterStr = queryFilterToUrlFilter(filter);
// special case
const path = collection === "git_repo" ? "git" : collection;
const url = `/api/v1/${path}${subpath}?${filterStr}`;
const { data: result, status, messages = [""], } = await (0, fetchApi_1.default)({
method: "DELETE",
url,
data,
});
if (!status && messages[0] && !(options === null || options === void 0 ? void 0 : options.ignorable))
(0, log_1.logError)(`[DB] DELETE - ${url} :>>`, messages);
item = result;
}
return item;
}
}
exports.DB = DB;