UNPKG

@brontosaurus/db

Version:
337 lines (336 loc) 15 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.getDecoratorsByQueryLean = exports.getDecoratorsByQuery = exports.getAllDecoratorsByPageLean = exports.getAllDecoratorsByPage = exports.getAllActiveDecoratorsByPageLean = exports.getAllActiveDecoratorsByPage = exports.getDecoratorsByPageLean = exports.getDecoratorsByPage = exports.getActiveDecoratorsByPageLean = exports.getActiveDecoratorsByPage = exports.getAllDecoratorsLean = exports.getAllDecorators = exports.getAllActiveDecoratorsLean = exports.getAllActiveDecorators = exports.getSelectedDecoratorsByPageLean = exports.getSelectedDecoratorsByPage = exports.getSelectedActiveDecoratorsByPageLean = exports.getSelectedActiveDecoratorsByPage = exports.getDecoratorPagesByKeyword = exports.getActiveDecoratorPagesByKeyword = exports.getTotalActiveDecoratorPages = exports.getTotalDecoratorPages = exports.getSelectedActiveDecoratorPages = exports.getSelectedDecoratorPages = exports.isDecoratorDuplicatedById = exports.isDecoratorDuplicatedByName = exports.getDecoratorByNamesLean = exports.getDecoratorByNames = exports.getDecoratorByNameLean = exports.getDecoratorByName = exports.getDecoratorsByIdsLean = exports.getDecoratorsByIds = exports.getDecoratorByIdLean = exports.getDecoratorIdsByNames = exports.getDecoratorIdByName = exports.getDecoratorById = exports.createUnsavedDecorator = void 0; const common_1 = require("../data/common"); const decorator_1 = require("../model/decorator"); const createUnsavedDecorator = (name, description) => { const anchor = common_1.fitAnchor(name); const config = { anchor, name, description, }; return new decorator_1.DecoratorModel(config); }; exports.createUnsavedDecorator = createUnsavedDecorator; const getDecoratorById = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.findOne({ _id: id, }); }); exports.getDecoratorById = getDecoratorById; const getDecoratorIdByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); const decorator = yield decorator_1.DecoratorModel.findOne({ anchor, }, { _id: 1, }); if (decorator) { return decorator._id; } return null; }); exports.getDecoratorIdByName = getDecoratorIdByName; const getDecoratorIdsByNames = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); const decorators = yield decorator_1.DecoratorModel.find({ anchor: { $in: anchors, }, }, { _id: 1, }); return decorators.map((decorator) => decorator._id); }); exports.getDecoratorIdsByNames = getDecoratorIdsByNames; const getDecoratorByIdLean = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.findOne({ _id: id, }).lean(); }); exports.getDecoratorByIdLean = getDecoratorByIdLean; const getDecoratorsByIds = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.find({ _id: { $in: ids, }, }); }); exports.getDecoratorsByIds = getDecoratorsByIds; const getDecoratorsByIdsLean = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.find({ _id: { $in: ids, }, }).lean(); }); exports.getDecoratorsByIdsLean = getDecoratorsByIdsLean; const getDecoratorByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); return yield decorator_1.DecoratorModel.findOne({ anchor, }); }); exports.getDecoratorByName = getDecoratorByName; const getDecoratorByNameLean = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); return yield decorator_1.DecoratorModel.findOne({ anchor, }).lean(); }); exports.getDecoratorByNameLean = getDecoratorByNameLean; const getDecoratorByNames = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); return yield decorator_1.DecoratorModel.find({ anchor: { $in: anchors, }, }); }); exports.getDecoratorByNames = getDecoratorByNames; const getDecoratorByNamesLean = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); return yield decorator_1.DecoratorModel.find({ anchor: { $in: anchors, }, }).lean(); }); exports.getDecoratorByNamesLean = getDecoratorByNamesLean; const isDecoratorDuplicatedByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const decorator = yield exports.getDecoratorByName(name); return Boolean(decorator); }); exports.isDecoratorDuplicatedByName = isDecoratorDuplicatedByName; const isDecoratorDuplicatedById = (id) => __awaiter(void 0, void 0, void 0, function* () { const decorator = yield exports.getDecoratorById(id); return Boolean(decorator); }); exports.isDecoratorDuplicatedById = isDecoratorDuplicatedById; const getSelectedDecoratorPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getDecoratorPagesByKeyword(limit, keyword); } return yield exports.getTotalDecoratorPages(limit); }); exports.getSelectedDecoratorPages = getSelectedDecoratorPages; const getSelectedActiveDecoratorPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveDecoratorPagesByKeyword(limit, keyword); } return yield exports.getTotalActiveDecoratorPages(limit); }); exports.getSelectedActiveDecoratorPages = getSelectedActiveDecoratorPages; const getTotalDecoratorPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield decorator_1.DecoratorModel.estimatedDocumentCount({})) / limit; }); exports.getTotalDecoratorPages = getTotalDecoratorPages; const getTotalActiveDecoratorPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield decorator_1.DecoratorModel.countDocuments({ active: true, })) / limit; }); exports.getTotalActiveDecoratorPages = getTotalActiveDecoratorPages; const getActiveDecoratorPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield decorator_1.DecoratorModel.countDocuments({ anchor: { $regex: regexp, }, active: true, })) / limit; }); exports.getActiveDecoratorPagesByKeyword = getActiveDecoratorPagesByKeyword; const getDecoratorPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield decorator_1.DecoratorModel.countDocuments({ anchor: { $regex: regexp, }, })) / limit; }); exports.getDecoratorPagesByKeyword = getDecoratorPagesByKeyword; const getSelectedActiveDecoratorsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveDecoratorsByPage(keyword, limit, page); } return yield exports.getAllActiveDecoratorsByPage(limit, page); }); exports.getSelectedActiveDecoratorsByPage = getSelectedActiveDecoratorsByPage; const getSelectedActiveDecoratorsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveDecoratorsByPageLean(keyword, limit, page); } return yield exports.getAllActiveDecoratorsByPageLean(limit, page); }); exports.getSelectedActiveDecoratorsByPageLean = getSelectedActiveDecoratorsByPageLean; const getSelectedDecoratorsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getDecoratorsByPage(keyword, limit, page); } return yield exports.getAllDecoratorsByPage(limit, page); }); exports.getSelectedDecoratorsByPage = getSelectedDecoratorsByPage; const getSelectedDecoratorsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getDecoratorsByPageLean(keyword, limit, page); } return yield exports.getAllDecoratorsByPageLean(limit, page); }); exports.getSelectedDecoratorsByPageLean = getSelectedDecoratorsByPageLean; const getAllActiveDecorators = () => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.find({ active: true, }); }); exports.getAllActiveDecorators = getAllActiveDecorators; const getAllActiveDecoratorsLean = () => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.find({ active: true, }).lean(); }); exports.getAllActiveDecoratorsLean = getAllActiveDecoratorsLean; const getAllDecorators = () => __awaiter(void 0, void 0, void 0, function* () { return decorator_1.DecoratorModel.find({}); }); exports.getAllDecorators = getAllDecorators; const getAllDecoratorsLean = () => __awaiter(void 0, void 0, void 0, function* () { return decorator_1.DecoratorModel.find({}).lean(); }); exports.getAllDecoratorsLean = getAllDecoratorsLean; const getActiveDecoratorsByPage = (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(anchor, 'i'); const decorators = yield decorator_1.DecoratorModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return decorators; }); exports.getActiveDecoratorsByPage = getActiveDecoratorsByPage; const getActiveDecoratorsByPageLean = (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(anchor, 'i'); const decorators = yield decorator_1.DecoratorModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return decorators; }); exports.getActiveDecoratorsByPageLean = getActiveDecoratorsByPageLean; const getDecoratorsByPage = (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(anchor, 'i'); const decorators = yield decorator_1.DecoratorModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return decorators; }); exports.getDecoratorsByPage = getDecoratorsByPage; const getDecoratorsByPageLean = (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(anchor, 'i'); const decorators = yield decorator_1.DecoratorModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return decorators; }); exports.getDecoratorsByPageLean = getDecoratorsByPageLean; const getAllActiveDecoratorsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const decorators = yield decorator_1.DecoratorModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return decorators; }); exports.getAllActiveDecoratorsByPage = getAllActiveDecoratorsByPage; const getAllActiveDecoratorsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const decorators = yield decorator_1.DecoratorModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return decorators; }); exports.getAllActiveDecoratorsByPageLean = getAllActiveDecoratorsByPageLean; const getAllDecoratorsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const decorators = yield decorator_1.DecoratorModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }); return decorators; }); exports.getAllDecoratorsByPage = getAllDecoratorsByPage; const getAllDecoratorsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const decorators = yield decorator_1.DecoratorModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return decorators; }); exports.getAllDecoratorsByPageLean = getAllDecoratorsByPageLean; const getDecoratorsByQuery = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.find(query); }); exports.getDecoratorsByQuery = getDecoratorsByQuery; const getDecoratorsByQueryLean = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield decorator_1.DecoratorModel.find(query).lean(); }); exports.getDecoratorsByQueryLean = getDecoratorsByQueryLean;