UNPKG

@chevre/domain

Version:

Chevre Domain Library for Node.js

131 lines (130 loc) 6.05 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.CommentRepo = void 0; const comments_1 = require("./mongoose/schemas/comments"); const factory = require("../factory"); const settings_1 = require("../settings"); /** * コメントリポジトリ */ class CommentRepo { constructor(connection) { this.commentModel = connection.model(comments_1.modelName, (0, comments_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 aboutIdEq = (_d = (_c = params.about) === null || _c === void 0 ? void 0 : _c.id) === null || _d === void 0 ? void 0 : _d.$eq; if (typeof aboutIdEq === 'string') { andConditions.push({ 'about.id': { $exists: true, $eq: aboutIdEq } }); } const aboutIdIn = (_f = (_e = params.about) === null || _e === void 0 ? void 0 : _e.id) === null || _f === void 0 ? void 0 : _f.$in; if (Array.isArray(aboutIdIn)) { andConditions.push({ 'about.id': { $exists: true, $in: aboutIdIn } }); } return andConditions; } /** * 検索 */ search(params) { return __awaiter(this, void 0, void 0, function* () { var _a; let projection = {}; if (Array.isArray(params.inclusion) && params.inclusion.length > 0) { params.inclusion.forEach((field) => { projection[field] = 1; }); } else { projection = { __v: 0, createdAt: 0, updatedAt: 0 }; if (Array.isArray(params.exclusion) && params.exclusion.length > 0) { params.exclusion.forEach((field) => { projection[field] = 0; }); } } const conditions = CommentRepo.CREATE_MONGO_CONDITIONS(params); const query = this.commentModel.find((conditions.length > 0) ? { $and: conditions } : {}) .select(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 (((_a = params.sort) === null || _a === void 0 ? void 0 : _a.dateCreated) !== undefined) { query.sort({ dateCreated: params.sort.dateCreated }); } return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS }) .exec() .then((docs) => docs.map((doc) => doc.toObject())); }); } findById(params) { return __awaiter(this, void 0, void 0, function* () { const doc = yield this.commentModel.findOne({ _id: params.id }, { __v: 0, createdAt: 0, updatedAt: 0 }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.commentModel.modelName); } return doc.toObject(); }); } create(params) { return __awaiter(this, void 0, void 0, function* () { const creatingDoc = Object.assign(Object.assign({ about: params.about, author: params.author, dateCreated: new Date(), project: params.project, text: params.text, typeOf: factory.creativeWorkType.Comment }, (Array.isArray(params.additionalProperty)) ? { additionalProperty: params.additionalProperty } : undefined), (Array.isArray(params.mentions)) ? { mentions: params.mentions } : undefined); const doc = yield this.commentModel.create(creatingDoc); return doc.toObject(); }); } updateById(params) { return __awaiter(this, void 0, void 0, function* () { const updateFields = { $set: Object.assign({ dateModified: new Date() }, (typeof params.attributes.text === 'string') ? { text: params.attributes.text } : undefined) }; const doc = yield this.commentModel.findOneAndUpdate({ _id: params.id }, updateFields, { upsert: false, new: true }) .exec(); if (doc === null) { throw new factory.errors.NotFound(this.commentModel.modelName); } }); } /** * 削除する */ deleteById(params) { return __awaiter(this, void 0, void 0, function* () { yield this.commentModel.findOneAndDelete({ _id: { $eq: params.id } }, { projection: { _id: 1 } }) .exec(); }); } getCursor(conditions, projection) { return this.commentModel.find(conditions, projection) .sort({ codeValue: factory.sortType.Ascending }) .cursor(); } } exports.CommentRepo = CommentRepo;