@chevre/domain
Version:
Chevre Domain Library for Node.js
159 lines (158 loc) • 7.61 kB
JavaScript
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.AdditionalPropertyRepo = void 0;
const factory = require("../factory");
const settings_1 = require("../settings");
const additionalProperty_1 = require("./mongoose/schemas/additionalProperty");
const AVAILABLE_PROJECT_FIELDS = [
'project',
'typeOf',
'codeValue',
'inCodeSet',
'name'
];
/**
* 追加特性リポジトリ
*/
class AdditionalPropertyRepo {
constructor(connection) {
this.additionalPropertyModel = connection.model(additionalProperty_1.modelName, (0, additionalProperty_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, _h, _j, _k;
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 nameRegex = (_d = params.name) === null || _d === void 0 ? void 0 : _d.$regex;
if (typeof nameRegex === 'string' && nameRegex.length > 0) {
andConditions.push({
$or: [
{ 'name.ja': { $exists: true, $regex: new RegExp(nameRegex) } },
{ 'name.en': { $exists: true, $regex: new RegExp(nameRegex) } }
]
});
}
const codeValueEq = (_e = params.codeValue) === null || _e === void 0 ? void 0 : _e.$eq;
if (typeof codeValueEq === 'string') {
andConditions.push({ codeValue: { $exists: true, $eq: codeValueEq } });
}
const codeValueIn = (_f = params.codeValue) === null || _f === void 0 ? void 0 : _f.$in;
if (Array.isArray(codeValueIn)) {
andConditions.push({ codeValue: { $exists: true, $in: codeValueIn } });
}
const inCodeSetIdentifierEq = (_h = (_g = params.inCodeSet) === null || _g === void 0 ? void 0 : _g.identifier) === null || _h === void 0 ? void 0 : _h.$eq;
if (typeof inCodeSetIdentifierEq === 'string') {
andConditions.push({ 'inCodeSet.identifier': { $exists: true, $eq: inCodeSetIdentifierEq } });
}
const inCodeSetIdentifierIn = (_k = (_j = params.inCodeSet) === null || _j === void 0 ? void 0 : _j.identifier) === null || _k === void 0 ? void 0 : _k.$in;
if (Array.isArray(inCodeSetIdentifierIn)) {
andConditions.push({ 'inCodeSet.identifier': { $exists: true, $in: inCodeSetIdentifierIn } });
}
return andConditions;
}
/**
* 検索
*/
projectFields(params, inclusion) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = AdditionalPropertyRepo.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));
}
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.additionalPropertyModel.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);
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean() // 2024-09-19~
.exec();
});
}
save(params) {
return __awaiter(this, void 0, void 0, function* () {
let savedId;
let doc;
if (typeof params.id !== 'string') {
const _a = params.attributes, { id } = _a, createParams = __rest(_a, ["id"]);
doc = yield this.additionalPropertyModel.create(createParams);
savedId = doc.id;
}
else {
const _b = params.attributes, { id, codeValue, inCodeSet, project, typeOf } = _b, updateFields = __rest(_b, ["id", "codeValue", "inCodeSet", "project", "typeOf"]);
doc = yield this.additionalPropertyModel.findOneAndUpdate({ _id: { $eq: params.id } }, updateFields, { upsert: false, new: true, projection: { _id: 1 } })
.exec();
savedId = params.id;
}
if (doc === null) {
throw new factory.errors.NotFound(this.additionalPropertyModel.modelName);
}
return { id: savedId };
});
}
/**
* 削除する
*/
deleteById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.additionalPropertyModel.findOneAndDelete({ _id: { $eq: params.id } }, { projection: { _id: 1 } })
.exec();
});
}
/**
* プロジェクト指定で削除する
*/
deleteByProject(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.additionalPropertyModel.deleteMany({
'project.id': { $eq: params.project.id }
})
.exec();
});
}
unsetUnnecessaryFields(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.additionalPropertyModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
.exec();
});
}
}
exports.AdditionalPropertyRepo = AdditionalPropertyRepo;
;