UNPKG

@brontosaurus/db

Version:
337 lines (336 loc) 14 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.getGroupsByQueryLean = exports.getGroupsByQuery = exports.getAllGroupsByPageLean = exports.getAllGroupsByPage = exports.getAllActiveGroupsByPageLean = exports.getAllActiveGroupsByPage = exports.getGroupsByPageLean = exports.getGroupsByPage = exports.getActiveGroupsByPageLean = exports.getActiveGroupsByPage = exports.getAllGroupsLean = exports.getAllGroups = exports.getAllActiveGroupsLean = exports.getAllActiveGroups = exports.getSelectedGroupsByPageLean = exports.getSelectedGroupsByPage = exports.getSelectedActiveGroupsByPageLean = exports.getSelectedActiveGroupsByPage = exports.getGroupPagesByKeyword = exports.getActiveGroupPagesByKeyword = exports.getTotalActiveGroupPages = exports.getTotalGroupPages = exports.getSelectedActiveGroupPages = exports.getSelectedGroupPages = exports.isGroupDuplicatedById = exports.isGroupDuplicatedByName = exports.getGroupByNamesLean = exports.getGroupByNames = exports.getGroupIdsByNames = exports.getGroupIdByName = exports.getGroupByNameLean = exports.getGroupByName = exports.getGroupsByIdsLean = exports.getGroupsByIds = exports.getGroupByIdLean = exports.getGroupById = exports.createUnsavedGroup = void 0; const common_1 = require("../data/common"); const group_1 = require("../model/group"); const createUnsavedGroup = (name, description) => { const anchor = common_1.fitAnchor(name); const config = { anchor, name, description, }; return new group_1.GroupModel(config); }; exports.createUnsavedGroup = createUnsavedGroup; const getGroupById = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.findOne({ _id: id, }); }); exports.getGroupById = getGroupById; const getGroupByIdLean = (id) => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.findOne({ _id: id, }).lean(); }); exports.getGroupByIdLean = getGroupByIdLean; const getGroupsByIds = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.find({ _id: { $in: ids, }, }); }); exports.getGroupsByIds = getGroupsByIds; const getGroupsByIdsLean = (ids) => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.find({ _id: { $in: ids, }, }).lean(); }); exports.getGroupsByIdsLean = getGroupsByIdsLean; const getGroupByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); return yield group_1.GroupModel.findOne({ anchor, }); }); exports.getGroupByName = getGroupByName; const getGroupByNameLean = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); return yield group_1.GroupModel.findOne({ anchor, }).lean(); }); exports.getGroupByNameLean = getGroupByNameLean; const getGroupIdByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(name); const group = yield group_1.GroupModel.findOne({ anchor, }, { _id: 1, }); if (group) { return group._id; } return null; }); exports.getGroupIdByName = getGroupIdByName; const getGroupIdsByNames = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); const groups = yield group_1.GroupModel.find({ anchor: { $in: anchors, }, }, { _id: 1, }); return groups.map((group) => group._id); }); exports.getGroupIdsByNames = getGroupIdsByNames; const getGroupByNames = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); return yield group_1.GroupModel.find({ anchor: { $in: anchors, }, }); }); exports.getGroupByNames = getGroupByNames; const getGroupByNamesLean = (names) => __awaiter(void 0, void 0, void 0, function* () { const anchors = names.map((name) => common_1.fitAnchor(name)); return yield group_1.GroupModel.find({ anchor: { $in: anchors, }, }).lean(); }); exports.getGroupByNamesLean = getGroupByNamesLean; const isGroupDuplicatedByName = (name) => __awaiter(void 0, void 0, void 0, function* () { const group = yield exports.getGroupByName(name); return Boolean(group); }); exports.isGroupDuplicatedByName = isGroupDuplicatedByName; const isGroupDuplicatedById = (id) => __awaiter(void 0, void 0, void 0, function* () { const group = yield exports.getGroupById(id); return Boolean(group); }); exports.isGroupDuplicatedById = isGroupDuplicatedById; const getSelectedGroupPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getGroupPagesByKeyword(limit, keyword); } return yield exports.getTotalGroupPages(limit); }); exports.getSelectedGroupPages = getSelectedGroupPages; const getSelectedActiveGroupPages = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveGroupPagesByKeyword(limit, keyword); } return yield exports.getTotalActiveGroupPages(limit); }); exports.getSelectedActiveGroupPages = getSelectedActiveGroupPages; const getTotalGroupPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield group_1.GroupModel.estimatedDocumentCount({})) / limit; }); exports.getTotalGroupPages = getTotalGroupPages; const getTotalActiveGroupPages = (limit) => __awaiter(void 0, void 0, void 0, function* () { return (yield group_1.GroupModel.countDocuments({ active: true, })) / limit; }); exports.getTotalActiveGroupPages = getTotalActiveGroupPages; const getActiveGroupPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield group_1.GroupModel.countDocuments({ anchor: { $regex: regexp, }, active: true, })) / limit; }); exports.getActiveGroupPagesByKeyword = getActiveGroupPagesByKeyword; const getGroupPagesByKeyword = (limit, keyword) => __awaiter(void 0, void 0, void 0, function* () { const anchor = common_1.fitAnchor(keyword); const regexp = new RegExp(anchor, 'i'); return (yield group_1.GroupModel.countDocuments({ anchor: { $regex: regexp, }, })) / limit; }); exports.getGroupPagesByKeyword = getGroupPagesByKeyword; const getSelectedActiveGroupsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveGroupsByPage(keyword, limit, page); } return yield exports.getAllActiveGroupsByPage(limit, page); }); exports.getSelectedActiveGroupsByPage = getSelectedActiveGroupsByPage; const getSelectedActiveGroupsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getActiveGroupsByPageLean(keyword, limit, page); } return yield exports.getAllActiveGroupsByPageLean(limit, page); }); exports.getSelectedActiveGroupsByPageLean = getSelectedActiveGroupsByPageLean; const getSelectedGroupsByPage = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getGroupsByPage(keyword, limit, page); } return yield exports.getAllGroupsByPage(limit, page); }); exports.getSelectedGroupsByPage = getSelectedGroupsByPage; const getSelectedGroupsByPageLean = (limit, page, keyword) => __awaiter(void 0, void 0, void 0, function* () { if (keyword) { return yield exports.getGroupsByPageLean(keyword, limit, page); } return yield exports.getAllGroupsByPageLean(limit, page); }); exports.getSelectedGroupsByPageLean = getSelectedGroupsByPageLean; const getAllActiveGroups = () => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.find({ active: true, }); }); exports.getAllActiveGroups = getAllActiveGroups; const getAllActiveGroupsLean = () => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.find({ active: true, }).lean(); }); exports.getAllActiveGroupsLean = getAllActiveGroupsLean; const getAllGroups = () => __awaiter(void 0, void 0, void 0, function* () { return group_1.GroupModel.find({}); }); exports.getAllGroups = getAllGroups; const getAllGroupsLean = () => __awaiter(void 0, void 0, void 0, function* () { return group_1.GroupModel.find({}).lean(); }); exports.getAllGroupsLean = getAllGroupsLean; const getActiveGroupsByPage = (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 groups = yield group_1.GroupModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return groups; }); exports.getActiveGroupsByPage = getActiveGroupsByPage; const getActiveGroupsByPageLean = (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 groups = yield group_1.GroupModel.find({ anchor: { $regex: regexp, }, active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return groups; }); exports.getActiveGroupsByPageLean = getActiveGroupsByPageLean; const getGroupsByPage = (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 groups = yield group_1.GroupModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return groups; }); exports.getGroupsByPage = getGroupsByPage; const getGroupsByPageLean = (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 groups = yield group_1.GroupModel.find({ anchor: { $regex: regexp, }, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return groups; }); exports.getGroupsByPageLean = getGroupsByPageLean; const getAllActiveGroupsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const groups = yield group_1.GroupModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }); return groups; }); exports.getAllActiveGroupsByPage = getAllActiveGroupsByPage; const getAllActiveGroupsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const groups = yield group_1.GroupModel.find({ active: true, }).skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return groups; }); exports.getAllActiveGroupsByPageLean = getAllActiveGroupsByPageLean; const getAllGroupsByPage = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const groups = yield group_1.GroupModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }); return groups; }); exports.getAllGroupsByPage = getAllGroupsByPage; const getAllGroupsByPageLean = (limit, page) => __awaiter(void 0, void 0, void 0, function* () { if (page < 0) { return []; } if (limit < 1) { return []; } const groups = yield group_1.GroupModel.find({}) .skip(page * limit).limit(limit).sort({ _id: -1 }).lean(); return groups; }); exports.getAllGroupsByPageLean = getAllGroupsByPageLean; const getGroupsByQuery = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.find(query); }); exports.getGroupsByQuery = getGroupsByQuery; const getGroupsByQueryLean = (query) => __awaiter(void 0, void 0, void 0, function* () { return yield group_1.GroupModel.find(query).lean(); }); exports.getGroupsByQueryLean = getGroupsByQueryLean;