UNPKG

@brontosaurus/db

Version:
352 lines (351 loc) 15.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getApplicationsByQueryLean = exports.getApplicationsByQuery = exports.refreshGreen = exports.checkGreenApplicationMatch = exports.getAllApplicationsByPageLean = exports.getAllApplicationsByPage = exports.getAllActiveApplicationsByPageLean = exports.getAllActiveApplicationsByPage = exports.getApplicationsByPageLean = exports.getApplicationsByPage = exports.getActiveApplicationsByPageLean = exports.getActiveApplicationsByPage = exports.getSelectedApplicationsByPageLean = exports.getSelectedApplicationsByPage = exports.getSelectedActiveApplicationsByPageLean = exports.getSelectedActiveApplicationsByPage = exports.getApplicationPagesByKeyword = exports.getActiveApplicationPagesByKeyword = exports.getTotalActiveApplicationPages = exports.getSelectedActiveApplicationPages = exports.getSelectedApplicationPages = exports.getTotalApplicationPages = exports.isApplicationDuplicatedByKey = exports.getApplicationByKeyLean = exports.getApplicationByKey = exports.getAllApplicationsLean = exports.getAllApplications = exports.getApplicationsByIds = exports.getApplicationById = exports.createUnsavedApplication = void 0; const core_1 = require("@brontosaurus/core"); const random_1 = require("@sudoo/bark/random"); const common_1 = require("../data/common"); const application_1 = require("../model/application"); const createUnsavedApplication = (name, key, expire, others) => { const tempGreen = random_1.trustable(new Date(), undefined, 64); const anchor = common_1.fitAnchor(key); const secret = core_1.Brontosaurus.generateBrontosaurusKey(); const config = { anchor, key, avatar: others.avatar, backgroundImage: others.backgroundImage, favicon: others.favicon, helpLink: others.helpLink, privacyPolicy: others.privacyPolicy, name, green: tempGreen, greenAccess: false, portalAccess: false, expire, publicKey: secret.public, privateKey: secret.private, redirections: [], iFrameProtocol: false, postProtocol: false, alertProtocol: false, noneProtocol: false, groups: [], requires: [], requireTags: [], }; return new application_1.ApplicationModel(config); }; exports.createUnsavedApplication = createUnsavedApplication; const getApplicationById = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield application_1.ApplicationModel.findOne({ _id: id, }); }); exports.getApplicationById = getApplicationById; const getApplicationsByIds = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield application_1.ApplicationModel.find({ _id: { $in: ids, }, }); }); exports.getApplicationsByIds = getApplicationsByIds; const getAllApplications = () => __awaiter(void 0, void 0, void 0, function* () { return application_1.ApplicationModel.find({}); }); exports.getAllApplications = getAllApplications; const getAllApplicationsLean = () => __awaiter(void 0, void 0, void 0, function* () { return application_1.ApplicationModel.find({}).lean(); }); exports.getAllApplicationsLean = getAllApplicationsLean; const getApplicationByKey = (key) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(key); return yield application_1.ApplicationModel.findOne({ anchor, }); }); exports.getApplicationByKey = getApplicationByKey; const getApplicationByKeyLean = (key) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(key); return yield application_1.ApplicationModel.findOne({ anchor, }).lean(); }); exports.getApplicationByKeyLean = getApplicationByKeyLean; const isApplicationDuplicatedByKey = (key) => __awaiter(void 0, void 0, void 0, function* () { const application = yield exports.getApplicationByKey(key); return Boolean(application); }); exports.isApplicationDuplicatedByKey = isApplicationDuplicatedByKey; const getTotalApplicationPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield application_1.ApplicationModel.estimatedDocumentCount({})) / limit; }); exports.getTotalApplicationPages = getTotalApplicationPages; const getSelectedApplicationPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getApplicationPagesByKeyword(limit, keyword); } return yield exports.getTotalApplicationPages(limit); }); exports.getSelectedApplicationPages = getSelectedApplicationPages; const getSelectedActiveApplicationPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveApplicationPagesByKeyword(limit, keyword); } return yield exports.getTotalActiveApplicationPages(limit); }); exports.getSelectedActiveApplicationPages = getSelectedActiveApplicationPages; const getTotalActiveApplicationPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield application_1.ApplicationModel.countDocuments({ active: true, })) / limit; }); exports.getTotalActiveApplicationPages = getTotalActiveApplicationPages; const getActiveApplicationPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(keyword, 'i'); const anchorRegExp = new RegExp(anchor); return (yield application_1.ApplicationModel.countDocuments({ $or: [{ name: { $regex: regexp, }, }, { anchor: { $regex: anchorRegExp, }, }], active: true, })) / limit; }); exports.getActiveApplicationPagesByKeyword = getActiveApplicationPagesByKeyword; const getApplicationPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(keyword, 'i'); const anchorRegExp = new RegExp(anchor); return (yield application_1.ApplicationModel.countDocuments({ $or: [{ name: { $regex: regexp, }, }, { anchor: { $regex: anchorRegExp, }, }], })) / limit; }); exports.getApplicationPagesByKeyword = getApplicationPagesByKeyword; const getSelectedActiveApplicationsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveApplicationsByPage(keyword, limit, page); } return yield exports.getAllActiveApplicationsByPage(limit, page); }); exports.getSelectedActiveApplicationsByPage = getSelectedActiveApplicationsByPage; const getSelectedActiveApplicationsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveApplicationsByPageLean(keyword, limit, page); } return yield exports.getAllActiveApplicationsByPageLean(limit, page); }); exports.getSelectedActiveApplicationsByPageLean = getSelectedActiveApplicationsByPageLean; const getSelectedApplicationsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getApplicationsByPage(keyword, limit, page); } return yield exports.getAllApplicationsByPage(limit, page); }); exports.getSelectedApplicationsByPage = getSelectedApplicationsByPage; const getSelectedApplicationsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getApplicationsByPageLean(keyword, limit, page); } return yield exports.getAllApplicationsByPageLean(limit, page); }); exports.getSelectedApplicationsByPageLean = getSelectedApplicationsByPageLean; const getActiveApplicationsByPage = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(keyword, 'i'); const anchorRegExp = new RegExp(anchor); const applications = yield application_1.ApplicationModel.find({ $or: [{ name: { $regex: regexp, }, }, { anchor: { $regex: anchorRegExp, }, }], active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return applications; }); exports.getActiveApplicationsByPage = getActiveApplicationsByPage; const getActiveApplicationsByPageLean = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(keyword, 'i'); const anchorRegExp = new RegExp(anchor); const applications = yield application_1.ApplicationModel.find({ $or: [{ name: { $regex: regexp, }, }, { anchor: { $regex: anchorRegExp, }, }], active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return applications; }); exports.getActiveApplicationsByPageLean = getActiveApplicationsByPageLean; const getApplicationsByPage = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(keyword, 'i'); const anchorRegExp = new RegExp(anchor); const applications = yield application_1.ApplicationModel.find({ $or: [{ name: { $regex: regexp, }, }, { anchor: { $regex: anchorRegExp, }, }], }).skip(page * limit).limit(limit).sort({ _id: -1 }); return applications; }); exports.getApplicationsByPage = getApplicationsByPage; const getApplicationsByPageLean = (keyword, limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(keyword, 'i'); const anchorRegExp = new RegExp(anchor); const applications = yield application_1.ApplicationModel.find({ $or: [{ name: { $regex: regexp, }, }, { anchor: { $regex: anchorRegExp, }, }], }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return applications; }); exports.getApplicationsByPageLean = getApplicationsByPageLean; const getAllActiveApplicationsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const applications = yield application_1.ApplicationModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return applications; }); exports.getAllActiveApplicationsByPage = getAllActiveApplicationsByPage; const getAllActiveApplicationsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const applications = yield application_1.ApplicationModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return applications; }); exports.getAllActiveApplicationsByPageLean = getAllActiveApplicationsByPageLean; const getAllApplicationsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const applications = yield application_1.ApplicationModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }); return applications; }); exports.getAllApplicationsByPage = getAllApplicationsByPage; const getAllApplicationsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const applications = yield application_1.ApplicationModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return applications; }); exports.getAllApplicationsByPageLean = getAllApplicationsByPageLean; const checkGreenApplicationMatch = (applicationKey, green) => __awaiter(void 0, void 0, void 0, function* () { const application = yield exports.getApplicationByKey(applicationKey); if (!application) { return false; } return application.green === green; }); exports.checkGreenApplicationMatch = checkGreenApplicationMatch; const refreshGreen = (applicationKey) => __awaiter(void 0, void 0, void 0, function* () { const application = yield exports.getApplicationByKey(applicationKey); if (!application) { return null; } application.refreshGreen(); yield application.save(); return application; }); exports.refreshGreen = refreshGreen; const getApplicationsByQuery = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield application_1.ApplicationModel.find(query); }); exports.getApplicationsByQuery = getApplicationsByQuery; const getApplicationsByQueryLean = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield application_1.ApplicationModel.find(query).lean(); }); exports.getApplicationsByQueryLean = getApplicationsByQueryLean;