@chevre/domain
Version:
Chevre Domain Library for Node.js
182 lines (181 loc) • 8.41 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.ProductModelRepo = void 0;
const factory = require("../factory");
const settings_1 = require("../settings");
const productModel_1 = require("./mongoose/schemas/productModel");
/**
* プロダクトモデルリポジトリ
*/
class ProductModelRepo {
constructor(connection) {
this.productModelModel = connection.model(productModel_1.modelName, (0, productModel_1.createSchema)());
}
// tslint:disable-next-line:max-func-body-length
static CREATE_MONGO_CONDITIONS(params) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
// MongoDB検索条件
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 categoryCodeValueIn = (_f = (_e = params.category) === null || _e === void 0 ? void 0 : _e.codeValue) === null || _f === void 0 ? void 0 : _f.$in;
if (Array.isArray(categoryCodeValueIn)) {
andConditions.push({ 'category.codeValue': { $exists: true, $in: categoryCodeValueIn } });
}
const categoryInCodeSetIdentifierEq = (_j = (_h = (_g = params.category) === null || _g === void 0 ? void 0 : _g.inCodeSet) === null || _h === void 0 ? void 0 : _h.identifier) === null || _j === void 0 ? void 0 : _j.$eq;
if (typeof categoryInCodeSetIdentifierEq === 'string') {
andConditions.push({ 'category.inCodeSet.identifier': { $exists: true, $eq: categoryInCodeSetIdentifierEq } });
}
return andConditions;
}
search(conditions, inclusion, exclusion) {
return __awaiter(this, void 0, void 0, function* () {
const andConditions = ProductModelRepo.CREATE_MONGO_CONDITIONS(conditions);
let projection = {};
if (Array.isArray(inclusion) && inclusion.length > 0) {
inclusion.forEach((field) => {
projection[field] = 1;
});
}
else {
projection = {
__v: 0,
createdAt: 0,
updatedAt: 0
};
if (Array.isArray(exclusion) && exclusion.length > 0) {
exclusion.forEach((field) => {
projection[field] = 0;
});
}
}
const query = this.productModelModel.find((andConditions.length > 0) ? { $and: andConditions } : {}, projection);
if (typeof conditions.limit === 'number' && conditions.limit > 0) {
const page = (typeof conditions.page === 'number' && conditions.page > 0) ? conditions.page : 1;
query.limit(conditions.limit)
.skip(conditions.limit * (page - 1));
}
// if (conditions.sort?.productID !== undefined) {
// query.sort({ productID: conditions.sort.productID });
// }
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.exec()
.then((docs) => docs.map((doc) => doc.toObject()));
});
}
deleteById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.productModelModel.findOneAndDelete({ _id: params.id })
.exec();
});
}
/**
* プロダクトを保管する
*/
save(params) {
return __awaiter(this, void 0, void 0, function* () {
let doc;
if (typeof params.id === 'string') {
// 上書き禁止属性を除外
const _a = params.$set, { category, id, project, typeOf } = _a, setFields = __rest(_a, ["category", "id", "project", "typeOf"]);
doc = yield this.productModelModel.findOneAndUpdate({ _id: { $eq: params.id } }, {
$set: setFields
// $unset: params.$unset
}, { upsert: false, new: true, projection: { _id: 1 } })
.exec();
}
else {
const _b = params.$set, { id } = _b, createParams = __rest(_b, ["id"]);
doc = yield this.productModelModel.create(createParams);
}
if (doc === null) {
throw new factory.errors.NotFound(this.productModelModel.modelName);
}
return doc.toObject();
});
}
/**
* 区分から同期する
*/
upsertByCategory(params) {
return __awaiter(this, void 0, void 0, function* () {
const upsertingProductModel = {
project: { id: params.category.project.id, typeOf: factory.organizationType.Project },
typeOf: 'ProductModel',
category: {
codeValue: params.category.codeValue,
inCodeSet: params.category.inCodeSet,
id: params.category.id
},
name: params.category.name,
offers: [{
typeOf: factory.offerType.Offer
}]
};
const { id, project, typeOf, offers, category } = upsertingProductModel, setFields = __rest(upsertingProductModel, ["id", "project", "typeOf", "offers", "category"]);
const doc = yield this.productModelModel.findOneAndUpdate({
'project.id': { $eq: upsertingProductModel.project.id },
'category.id': { $exists: true, $eq: upsertingProductModel.category.id }
}, {
$setOnInsert: { project, typeOf, offers, category },
$set: setFields
}, { new: true, upsert: true, projection: { _id: 1 } })
.exec();
if (doc === null) {
throw new factory.errors.NotFound(this.productModelModel.modelName);
}
return doc.toObject();
});
}
/**
* 区分から削除する
*/
deleteByCategory(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.productModelModel.deleteMany({
'project.id': { $eq: params.category.project.id },
'category.id': { $exists: true, $eq: params.category.id }
})
.exec();
});
}
unsetUnnecessaryFields(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.productModelModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
.exec();
});
}
}
exports.ProductModelRepo = ProductModelRepo;
;