UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

296 lines (295 loc) 13.3 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()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PriceSpecificationRepo = void 0; const factory = require("../factory"); const priceSpecification_1 = require("./mongoose/schemas/priceSpecification"); const settings_1 = require("../settings"); /** * 価格仕様リポジトリ */ class PriceSpecificationRepo { constructor(connection) { this.priceSpecificationModel = connection.model(priceSpecification_1.modelName, (0, priceSpecification_1.createSchema)()); } // tslint:disable-next-line:cyclomatic-complexity max-func-body-length static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f, _g; const andConditions = []; const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq; if (typeof projectIdEq === 'string') { andConditions.push({ 'project.id': { $eq: projectIdEq } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.id !== undefined && params.id !== null) { if (typeof params.id.$eq === 'string') { andConditions.push({ _id: { $eq: params.id.$eq } }); } } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.typeOf !== undefined) { andConditions.push({ typeOf: params.typeOf }); } if (params.appliesToCategoryCode !== undefined && params.appliesToCategoryCode !== null) { if (params.appliesToCategoryCode.$elemMatch !== undefined && params.appliesToCategoryCode.$elemMatch !== null) { const categoryCodeElemMatch = params.appliesToCategoryCode.$elemMatch; andConditions.push({ appliesToCategoryCode: { $exists: true, $elemMatch: categoryCodeElemMatch } }); } if (params.appliesToCategoryCode.codeValue !== undefined && params.appliesToCategoryCode.codeValue !== null) { if (typeof params.appliesToCategoryCode.codeValue.$eq === 'string') { andConditions.push({ 'appliesToCategoryCode.codeValue': { $exists: true, $eq: params.appliesToCategoryCode.codeValue.$eq } }); } if (Array.isArray(params.appliesToCategoryCode.codeValue.$in)) { andConditions.push({ 'appliesToCategoryCode.codeValue': { $exists: true, $in: params.appliesToCategoryCode.codeValue.$in } }); } } if (params.appliesToCategoryCode.inCodeSet !== undefined && params.appliesToCategoryCode.inCodeSet !== null) { if (params.appliesToCategoryCode.inCodeSet.identifier !== undefined && params.appliesToCategoryCode.inCodeSet.identifier !== null) { if (typeof params.appliesToCategoryCode.inCodeSet.identifier.$eq === 'string') { andConditions.push({ 'appliesToCategoryCode.inCodeSet.identifier': { $exists: true, $eq: params.appliesToCategoryCode.inCodeSet.identifier.$eq } }); } if (Array.isArray(params.appliesToCategoryCode.inCodeSet.identifier.$in)) { andConditions.push({ 'appliesToCategoryCode.inCodeSet.identifier': { $exists: true, $in: params.appliesToCategoryCode.inCodeSet.identifier.$in } }); } } } } if (Array.isArray(params.appliesToVideoFormats)) { andConditions.push({ appliesToVideoFormat: { $exists: true, $in: params.appliesToVideoFormats } }); } const appliesToMovieTicketServiceTypeIn = (_c = params.appliesToMovieTicket) === null || _c === void 0 ? void 0 : _c.serviceTypes; if (Array.isArray(appliesToMovieTicketServiceTypeIn)) { andConditions.push({ 'appliesToMovieTicket.serviceType': { $exists: true, $in: appliesToMovieTicketServiceTypeIn } }); } const appliesToMovieTicketServiceOutputTypeOfEq = (_f = (_e = (_d = params.appliesToMovieTicket) === null || _d === void 0 ? void 0 : _d.serviceOutput) === null || _e === void 0 ? void 0 : _e.typeOf) === null || _f === void 0 ? void 0 : _f.$eq; if (typeof appliesToMovieTicketServiceOutputTypeOfEq === 'string') { andConditions.push({ 'appliesToMovieTicket.serviceOutput.typeOf': { $exists: true, $eq: appliesToMovieTicketServiceOutputTypeOfEq } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.validFrom !== undefined) { andConditions.push({ validThrough: { $exists: true, $gte: params.validFrom } }); } // tslint:disable-next-line:no-single-line-block-comment /* istanbul ignore else */ if (params.validThrough !== undefined) { andConditions.push({ validFrom: { $exists: true, $lte: params.validThrough } }); } const priceEq = (_g = params.price) === null || _g === void 0 ? void 0 : _g.$eq; if (typeof priceEq === 'number') { andConditions.push({ price: { $eq: priceEq } }); } return andConditions; } count(params) { return __awaiter(this, void 0, void 0, function* () { const conditions = PriceSpecificationRepo.CREATE_MONGO_CONDITIONS(params); return this.priceSpecificationModel.countDocuments((conditions.length > 0) ? { $and: conditions } : {}) .setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec(); }); } search(params) { return __awaiter(this, void 0, void 0, function* () { var _a; const conditions = PriceSpecificationRepo.CREATE_MONGO_CONDITIONS(params); const query = this.priceSpecificationModel.find((conditions.length > 0) ? { $and: conditions } : {}, { _id: 0, id: { $toString: '$_id' }, project: 1, typeOf: 1, name: 1, price: 1, priceCurrency: 1, validFrom: 1, validThrough: 1, valueAddedTaxIncluded: 1, appliesToCategoryCode: 1, appliesToMovieTicket: 1, appliesToVideoFormat: 1 // __v: 0, // createdAt: 0, // updatedAt: 0 }); 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 (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.price) !== undefined) { query.sort({ price: params.sort.price }); } // const explainResult = await (<any>query).explain(); // console.log(explainResult[0].executionStats.allPlansExecution.map((e: any) => e.executionStages.inputStage)); // return; return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // lean(2024-08-06~) .exec(); }); } findById(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.priceSpecificationModel.findOne({ _id: { $eq: params.id } }, { _id: 0, id: { $toString: '$_id' }, project: 1, typeOf: 1, name: 1, price: 1, priceCurrency: 1, validFrom: 1, validThrough: 1, valueAddedTaxIncluded: 1, appliesToCategoryCode: 1, appliesToMovieTicket: 1, appliesToVideoFormat: 1 // __v: 0, // createdAt: 0, // updatedAt: 0 }) .lean() // lean(2024-08-06~) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.priceSpecificationModel.modelName); } return doc; }); } save(params) { return __awaiter(this, void 0, void 0, function* () { var _a; let doc; if (typeof params.id !== 'string') { const _b = params.attributes, { $unset, id } = _b, creatingDoc = __rest(_b, ["$unset", "id"]); doc = yield this.priceSpecificationModel.create(creatingDoc); } else { // 上書き禁止属性を除外(2022-08-24~) const _c = params.attributes, { $unset, id, project, typeOf } = _c, updateFields = __rest(_c, ["$unset", "id", "project", "typeOf"]); doc = yield this.priceSpecificationModel.findOneAndUpdate({ _id: { $eq: params.id } }, { $set: updateFields }, { upsert: false, new: true, projection: { _id: 1 } }) .lean() // lean(2024-08-06~) .exec(); } if (doc === null) { throw new factory.errors.NotFound(this.priceSpecificationModel.modelName); } return { id: (_a = doc._id) === null || _a === void 0 ? void 0 : _a.toString() }; }); } /** * 削除する */ deleteById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.priceSpecificationModel.findOneAndDelete({ _id: { $eq: params.id } }, { projection: { _id: 1 } }) .exec(); }); } /** * プロジェクト指定で削除する */ deleteByProject(params) { return __awaiter(this, void 0, void 0, function* () { yield this.priceSpecificationModel.deleteMany({ 'project.id': { $eq: params.project.id } }) .exec(); }); } getCursor(conditions, projection) { return this.priceSpecificationModel.find(conditions, projection) .sort({ price: factory.sortType.Ascending }) .cursor(); } reIndex() { return __awaiter(this, void 0, void 0, function* () { // return this.priceSpecificationModel.collection.name; return this.priceSpecificationModel.db.db.command({ reIndex: this.priceSpecificationModel.collection.name }); }); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.priceSpecificationModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } } exports.PriceSpecificationRepo = PriceSpecificationRepo;