UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

133 lines (132 loc) 6.46 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.OfferItemConditionRepo = void 0; const factory = require("../factory"); const settings_1 = require("../settings"); const offerItemCondition_1 = require("./mongoose/schemas/offerItemCondition"); const AVAILABLE_PROJECT_FIELDS = [ 'project', 'identifier', 'typeOf', 'name', 'itemOffered' ]; /** * アイテムコンディションリポジトリ */ class OfferItemConditionRepo { constructor(connection) { this.offerItemConditionModel = connection.model(offerItemCondition_1.modelName, (0, offerItemCondition_1.createSchema)()); } static CREATE_MONGO_CONDITIONS(params) { var _a, _b, _c, _d, _e, _f; 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 } }); } const idEq = (_c = params.id) === null || _c === void 0 ? void 0 : _c.$eq; if (typeof idEq === 'string') { andConditions.push({ _id: { $eq: idEq } }); } const idIn = (_d = params.id) === null || _d === void 0 ? void 0 : _d.$in; if (Array.isArray(idIn)) { andConditions.push({ _id: { $in: idIn } }); } const identifierEq = (_e = params.identifier) === null || _e === void 0 ? void 0 : _e.$eq; if (typeof identifierEq === 'string') { andConditions.push({ identifier: { $eq: identifierEq } }); } const identifierIn = (_f = params.identifier) === null || _f === void 0 ? void 0 : _f.$in; if (Array.isArray(identifierIn)) { andConditions.push({ identifier: { $in: identifierIn } }); } return andConditions; } projectFields(params, // projection?: IProjection inclusion) { return __awaiter(this, void 0, void 0, function* () { var _a; const conditions = OfferItemConditionRepo.CREATE_MONGO_CONDITIONS(params); // const positiveProjectionExists: boolean = (projection !== undefined && projection !== null) // ? Object.values(projection) // .some((value) => value !== 0) // : false; let positiveProjectionFields = AVAILABLE_PROJECT_FIELDS; if (Array.isArray(inclusion) && inclusion.length > 0) { positiveProjectionFields = inclusion.filter((key) => AVAILABLE_PROJECT_FIELDS.includes(key)); } else { throw new factory.errors.ArgumentNull('inclusion', 'inclusion must be specified'); } const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1])))); const query = this.offerItemConditionModel.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)); } if (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.identifier) !== undefined) { query.sort({ identifier: params.sort.identifier }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .lean() // lean(2024-09-24~) .exec(); }); } save(params) { return __awaiter(this, void 0, void 0, function* () { let savedId; let doc; if (typeof params.id !== 'string' || params.id === '') { const { id } = params, creatingParams = __rest(params, ["id"]); // omit id(2024-09-12~) doc = yield this.offerItemConditionModel.create(creatingParams); savedId = doc.id; } else { const { id, identifier, project, typeOf } = params, updateFields = __rest(params, ["id", "identifier", "project", "typeOf"]); doc = yield this.offerItemConditionModel.findOneAndUpdate({ _id: { $eq: id } }, updateFields, { upsert: false, new: true, projection: { _id: 1 } }) .exec(); savedId = id; } if (doc === null) { throw new factory.errors.NotFound(this.offerItemConditionModel.modelName); } return { id: savedId }; }); } deleteById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.offerItemConditionModel.findOneAndDelete({ _id: { $eq: params.id } }, { projection: { _id: 1 } }) .exec(); }); } unsetUnnecessaryFields(params) { return __awaiter(this, void 0, void 0, function* () { return this.offerItemConditionModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false }) .exec(); }); } } exports.OfferItemConditionRepo = OfferItemConditionRepo;