UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

153 lines (152 loc) 6.65 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.RoleRepo = void 0; const settings_1 = require("../settings"); const role_1 = require("./mongoose/schemas/role"); const AVAILABLE_PROJECT_FIELDS = [ 'typeOf', 'permissions', 'roleName', 'member', 'memberOf' ]; /** * IAMロールリポジトリ */ class RoleRepo { constructor(connection) { this.roleModel = connection.model(role_1.modelName, (0, role_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f, _g; const andConditions = []; if (typeof ((_a = params.roleName) === null || _a === void 0 ? void 0 : _a.$eq) === 'string') { andConditions.push({ roleName: { $eq: params.roleName.$eq } }); } const roleNameIn = (_b = params.roleName) === null || _b === void 0 ? void 0 : _b.$in; if (Array.isArray(roleNameIn)) { andConditions.push({ roleName: { $in: roleNameIn } }); } const permissionsEq = (_c = params.permissions) === null || _c === void 0 ? void 0 : _c.$eq; if (typeof permissionsEq === 'string') { andConditions.push({ permissions: { $exists: true, $eq: permissionsEq } }); } const memberTypeOfEq = (_e = (_d = params.member) === null || _d === void 0 ? void 0 : _d.typeOf) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof memberTypeOfEq === 'string') { andConditions.push({ 'member.typeOf': { $eq: memberTypeOfEq } }); } const memberOfTypeOfEq = (_g = (_f = params.memberOf) === null || _f === void 0 ? void 0 : _f.typeOf) === null || _g === void 0 ? void 0 : _g.$eq; if (typeof memberOfTypeOfEq === 'string') { andConditions.push({ 'memberOf.typeOf': { $eq: memberOfTypeOfEq } }); } return andConditions; } projectFields(params, inclusion) { return __awaiter(this, void 0, void 0, function* () { const conditions = RoleRepo.CREATE_MONGO_CONDITIONS(params); let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)); } const projection = Object.assign({ _id: 0 }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const query = this.roleModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection); if (typeof params.limit === 'number' && params.limit > 0) { const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; query.limit(params.limit) .skip(params.limit * (page - 1)); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.sort !== undefined) { query.sort(params.sort); } // const explainResult = await (<any>query).explain(); // console.log(explainResult[0].executionStats.allPlansExecution.map((e: any) => e.executionStages.inputStage)); return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // 2024-08-23~ .exec(); }); } aggregatePermissions(params) { return __awaiter(this, void 0, void 0, function* () { const matchStages = [ { $match: { roleName: { $in: params.roleName.$in } } } ]; const aggregate = this.roleModel.aggregate([ // ...(typeof params.sort?.productID === 'number') // ? [{ $sort: { productID: params.sort.productID } }] // : [], ...matchStages, { $unwind: { path: '$permissions' } }, { $project: { _id: 0, permission: '$permissions' } }, { $group: { _id: '$permission' } } ]); // if (typeof params.limit === 'number' && params.limit > 0) { // const page: number = (typeof params.page === 'number' && params.page > 0) ? params.page : 1; // aggregate.limit(params.limit * page) // .skip(params.limit * (page - 1)); // } return aggregate.option({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } addPermissionIfNotExists(params) { return __awaiter(this, void 0, void 0, function* () { return this.roleModel.findOneAndUpdate({ roleName: { $eq: params.roleName.$eq }, permissions: { $ne: params.permission } }, { $push: { permissions: { $each: [params.permission], $sort: 1 } } }, { new: true, projection: { _id: 1 } }) .lean() .exec(); }); } addMember(params) { return __awaiter(this, void 0, void 0, function* () { const { roleName, member, memberOf } = params; return this.roleModel.findOneAndUpdate({ roleName: { $eq: roleName }, 'member.typeOf': { $ne: member.typeOf } }, { $set: { member, memberOf } }, { new: true, projection: { _id: 1 } }) .lean() .exec(); }); } } exports.RoleRepo = RoleRepo;