@chevre/domain
Version:
Chevre Domain Library for Node.js
190 lines (189 loc) • 9.42 kB
JavaScript
"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.NoteRepo = void 0;
const factory = require("../factory");
const settings_1 = require("../settings");
const note_1 = require("./mongoose/schemas/note");
const AVAILABLE_PROJECT_FIELDS = [
'identifier',
'text',
'project',
'provider',
'about',
'dateCreated',
'dateModified',
'creator',
'editor',
'version',
'typeOf'
];
/**
* メモリポジトリ
*/
class NoteRepo {
constructor(connection) {
this.noteModel = connection.model(note_1.modelName, (0, note_1.createSchema)());
}
static CREATE_MONGO_CONDITIONS(params) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
const andConditions = [];
const idIn = (_a = params.id) === null || _a === void 0 ? void 0 : _a.$in;
if (Array.isArray(idIn)) {
andConditions.push({ _id: { $in: idIn } });
}
const projectIdEq = (_c = (_b = params.project) === null || _b === void 0 ? void 0 : _b.id) === null || _c === void 0 ? void 0 : _c.$eq;
if (typeof projectIdEq === 'string') {
andConditions.push({ 'project.id': { $eq: projectIdEq } });
}
const providerIdEq = (_e = (_d = params.provider) === null || _d === void 0 ? void 0 : _d.id) === null || _e === void 0 ? void 0 : _e.$eq;
if (typeof providerIdEq === 'string') {
andConditions.push({ 'provider.id': { $eq: providerIdEq } });
}
const aboutIdEq = (_g = (_f = params.about) === null || _f === void 0 ? void 0 : _f.id) === null || _g === void 0 ? void 0 : _g.$eq;
if (typeof aboutIdEq === 'string') {
andConditions.push({ 'about.id': { $eq: aboutIdEq } });
}
const aboutIdIn = (_j = (_h = params.about) === null || _h === void 0 ? void 0 : _h.id) === null || _j === void 0 ? void 0 : _j.$in;
if (Array.isArray(aboutIdIn)) {
andConditions.push({ 'about.id': { $in: aboutIdIn } });
}
const aboutOrderNumberEq = (_l = (_k = params.about) === null || _k === void 0 ? void 0 : _k.orderNumber) === null || _l === void 0 ? void 0 : _l.$eq;
if (typeof aboutOrderNumberEq === 'string') {
andConditions.push({ 'about.orderNumber': { $exists: true, $eq: aboutOrderNumberEq } });
}
const aboutOrderNumberIn = (_o = (_m = params.about) === null || _m === void 0 ? void 0 : _m.orderNumber) === null || _o === void 0 ? void 0 : _o.$in;
if (Array.isArray(aboutOrderNumberIn)) {
andConditions.push({ 'about.orderNumber': { $exists: true, $in: aboutOrderNumberIn } });
}
const identifierEq = (_p = params.identifier) === null || _p === void 0 ? void 0 : _p.$eq;
if (typeof identifierEq === 'string') {
andConditions.push({ identifier: { $eq: identifierEq } });
}
const identifierIn = (_q = params.identifier) === null || _q === void 0 ? void 0 : _q.$in;
if (Array.isArray(identifierIn)) {
andConditions.push({ identifier: { $in: identifierIn } });
}
return andConditions;
}
/**
* 検索
*/
projectFields(params, inclusion) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const conditions = NoteRepo.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.noteModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
if (typeof ((_a = params.sort) === null || _a === void 0 ? void 0 : _a.dateCreated) === 'number') {
query.sort({ dateCreated: params.sort.dateCreated });
}
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));
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean() // 2024-09-19~
.exec();
});
}
/**
* コードをキーにしてなければ作成する(複数対応)
*/
upsertByIdentifier(params, options) {
return __awaiter(this, void 0, void 0, function* () {
const now = new Date();
const bulkWriteOps = [];
if (Array.isArray(params)) {
params.forEach((creatingNoteParams) => {
const { about, creator, identifier, project, provider, text, version } = creatingNoteParams;
if (typeof identifier !== 'string' || identifier.length === 0) {
throw new factory.errors.ArgumentNull('identifier');
}
if (typeof version !== 'string' || version.length === 0) {
throw new factory.errors.ArgumentNull('version');
}
if (typeof text !== 'string') {
throw new factory.errors.ArgumentNull('text');
}
// リソースのユニークネスを保証するfilter
const filter = {
// typeOf: { $eq: factory.creativeWorkType.NoteDigitalDocument },
'project.id': { $eq: project.id },
'about.id': { $eq: about.id },
identifier: { $eq: identifier },
version: { $eq: version }
};
const setOnInsert = Object.assign({ about, creator, identifier, project, provider, version, dateCreated: now, typeOf: factory.creativeWorkType.NoteDigitalDocument }, (!options.overwrite) ? { text } : undefined // overwriteでない場合はinsert時にtextをセット
);
// 変更可能な属性のみ上書き
const setFields = {
dateModified: now,
editor: creator,
text
};
const updateFilter = Object.assign({ $setOnInsert: setOnInsert }, (options.overwrite) ? { $set: setFields } : undefined);
const updateOne = {
filter,
update: updateFilter,
upsert: true
};
bulkWriteOps.push({ updateOne });
});
}
if (bulkWriteOps.length > 0) {
return this.noteModel.bulkWrite(bulkWriteOps, { ordered: false });
}
});
}
updateById(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const updateFields = {
$set: Object.assign(Object.assign({ dateModified: new Date() }, (typeof params.attributes.text === 'string') ? { text: params.attributes.text } : undefined), (typeof ((_a = params.attributes.editor) === null || _a === void 0 ? void 0 : _a.id) === 'string') ? { editor: params.attributes.editor } : undefined)
};
const doc = yield this.noteModel.findOneAndUpdate({ _id: { $eq: params.id } }, updateFields, {
upsert: false,
new: true,
projection: { _id: 1 }
})
.lean()
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.noteModel.modelName);
}
});
}
deleteManyByAbout(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.noteModel.deleteMany({
'about.id': { $eq: String(params.about.id) },
'about.typeOf': { $eq: String(params.about.typeOf) }
})
.exec();
});
}
unsetUnnecessaryFields(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.noteModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
.exec();
});
}
}
exports.NoteRepo = NoteRepo;