UNPKG

@brontosaurus/db

Version:
337 lines (336 loc) 13.5 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.getTagsByQueryLean = exports.getTagsByQuery = exports.getAllTagsByPageLean = exports.getAllTagsByPage = exports.getAllActiveTagsByPageLean = exports.getAllActiveTagsByPage = exports.getTagsByPageLean = exports.getTagsByPage = exports.getActiveTagsByPageLean = exports.getActiveTagsByPage = exports.getAllTagsLean = exports.getAllTags = exports.getAllActiveTagsLean = exports.getAllActiveTags = exports.getSelectedTagsByPageLean = exports.getSelectedTagsByPage = exports.getSelectedActiveTagsByPageLean = exports.getSelectedActiveTagsByPage = exports.getTagPagesByKeyword = exports.getActiveTagPagesByKeyword = exports.getTotalActiveTagPages = exports.getTotalTagPages = exports.getSelectedActiveTagPages = exports.getSelectedTagPages = exports.isTagDuplicatedById = exports.isTagDuplicatedByName = exports.getTagByNamesLean = exports.getTagByNames = exports.getTagByNameLean = exports.getTagByName = exports.getTagsByIdsLean = exports.getTagsByIds = exports.getTagIdsByNames = exports.getTagIdByName = exports.getTagByIdLean = exports.getTagById = exports.createUnsavedTag = void 0; const common_1 = require("../data/common"); const tag_1 = require("../model/tag"); const createUnsavedTag = (name, description) => { const anchor = common_1.fitAnchor(name); const config = { anchor, name, description, }; return new tag_1.TagModel(config); }; exports.createUnsavedTag = createUnsavedTag; const getTagById = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.findOne({ _id: id, }); }); exports.getTagById = getTagById; const getTagByIdLean = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.findOne({ _id: id, }).lean(); }); exports.getTagByIdLean = getTagByIdLean; const getTagIdByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); const tag = yield tag_1.TagModel.findOne({ anchor, }, { _id: 1, }); if (tag) { return tag._id; } return null; }); exports.getTagIdByName = getTagIdByName; const getTagIdsByNames = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); const tags = yield tag_1.TagModel.find({ anchor: { $in: anchors, }, }, { _id: 1, }); return tags.map((tag) => tag._id); }); exports.getTagIdsByNames = getTagIdsByNames; const getTagsByIds = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.find({ _id: { $in: ids, }, }); }); exports.getTagsByIds = getTagsByIds; const getTagsByIdsLean = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.find({ _id: { $in: ids, }, }).lean(); }); exports.getTagsByIdsLean = getTagsByIdsLean; const getTagByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); return yield tag_1.TagModel.findOne({ anchor, }); }); exports.getTagByName = getTagByName; const getTagByNameLean = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); return yield tag_1.TagModel.findOne({ anchor, }).lean(); }); exports.getTagByNameLean = getTagByNameLean; const getTagByNames = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); return yield tag_1.TagModel.find({ anchor: { $in: anchors, }, }); }); exports.getTagByNames = getTagByNames; const getTagByNamesLean = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); return yield tag_1.TagModel.find({ anchor: { $in: anchors, }, }).lean(); }); exports.getTagByNamesLean = getTagByNamesLean; const isTagDuplicatedByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const tag = yield exports.getTagByName(name); return Boolean(tag); }); exports.isTagDuplicatedByName = isTagDuplicatedByName; const isTagDuplicatedById = (id) => __awaiter(void 0, void 0, void 0, function* () { const tag = yield exports.getTagById(id); return Boolean(tag); }); exports.isTagDuplicatedById = isTagDuplicatedById; const getSelectedTagPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getTagPagesByKeyword(limit, keyword); } return yield exports.getTotalTagPages(limit); }); exports.getSelectedTagPages = getSelectedTagPages; const getSelectedActiveTagPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveTagPagesByKeyword(limit, keyword); } return yield exports.getTotalActiveTagPages(limit); }); exports.getSelectedActiveTagPages = getSelectedActiveTagPages; const getTotalTagPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield tag_1.TagModel.estimatedDocumentCount({})) / limit; }); exports.getTotalTagPages = getTotalTagPages; const getTotalActiveTagPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield tag_1.TagModel.countDocuments({ active: true, })) / limit; }); exports.getTotalActiveTagPages = getTotalActiveTagPages; const getActiveTagPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield tag_1.TagModel.countDocuments({ anchor: { $regex: regexp, }, active: true, })) / limit; }); exports.getActiveTagPagesByKeyword = getActiveTagPagesByKeyword; const getTagPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield tag_1.TagModel.countDocuments({ anchor: { $regex: regexp, }, })) / limit; }); exports.getTagPagesByKeyword = getTagPagesByKeyword; const getSelectedActiveTagsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveTagsByPage(keyword, limit, page); } return yield exports.getAllActiveTagsByPage(limit, page); }); exports.getSelectedActiveTagsByPage = getSelectedActiveTagsByPage; const getSelectedActiveTagsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveTagsByPageLean(keyword, limit, page); } return yield exports.getAllActiveTagsByPageLean(limit, page); }); exports.getSelectedActiveTagsByPageLean = getSelectedActiveTagsByPageLean; const getSelectedTagsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getTagsByPage(keyword, limit, page); } return yield exports.getAllTagsByPage(limit, page); }); exports.getSelectedTagsByPage = getSelectedTagsByPage; const getSelectedTagsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getTagsByPageLean(keyword, limit, page); } return yield exports.getAllTagsByPageLean(limit, page); }); exports.getSelectedTagsByPageLean = getSelectedTagsByPageLean; const getAllActiveTags = () => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.find({ active: true, }); }); exports.getAllActiveTags = getAllActiveTags; const getAllActiveTagsLean = () => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.find({ active: true, }).lean(); }); exports.getAllActiveTagsLean = getAllActiveTagsLean; const getAllTags = () => __awaiter(void 0, void 0, void 0, function* () { return tag_1.TagModel.find({}); }); exports.getAllTags = getAllTags; const getAllTagsLean = () => __awaiter(void 0, void 0, void 0, function* () { return tag_1.TagModel.find({}).lean(); }); exports.getAllTagsLean = getAllTagsLean; const getActiveTagsByPage = (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 tags = yield tag_1.TagModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return tags; }); exports.getActiveTagsByPage = getActiveTagsByPage; const getActiveTagsByPageLean = (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 tags = yield tag_1.TagModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return tags; }); exports.getActiveTagsByPageLean = getActiveTagsByPageLean; const getTagsByPage = (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 tags = yield tag_1.TagModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return tags; }); exports.getTagsByPage = getTagsByPage; const getTagsByPageLean = (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 tags = yield tag_1.TagModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return tags; }); exports.getTagsByPageLean = getTagsByPageLean; const getAllActiveTagsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const tags = yield tag_1.TagModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return tags; }); exports.getAllActiveTagsByPage = getAllActiveTagsByPage; const getAllActiveTagsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const tags = yield tag_1.TagModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return tags; }); exports.getAllActiveTagsByPageLean = getAllActiveTagsByPageLean; const getAllTagsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const tags = yield tag_1.TagModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }); return tags; }); exports.getAllTagsByPage = getAllTagsByPage; const getAllTagsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const tags = yield tag_1.TagModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return tags; }); exports.getAllTagsByPageLean = getAllTagsByPageLean; const getTagsByQuery = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.find(query); }); exports.getTagsByQuery = getTagsByQuery; const getTagsByQueryLean = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield tag_1.TagModel.find(query).lean(); }); exports.getTagsByQueryLean = getTagsByQueryLean;