@chevre/domain
Version:
Chevre Domain Library for Node.js
99 lines (98 loc) • 4.35 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomerTypeRepo = void 0;
const customerType_1 = require("./mongoose/schemas/customerType");
const settings_1 = require("../settings");
const AVAILABLE_PROJECT_FIELDS = [
'typeOf',
'codeValue',
'name'
];
/**
* カスタマータイプリポジトリ
*/
class CustomerTypeRepo {
constructor(connection) {
this.customerTypeModel = connection.model(customerType_1.modelName, (0, customerType_1.createSchema)());
}
// tslint:disable-next-line:cyclomatic-complexity max-func-body-length
static CREATE_MONGO_CONDITIONS(params) {
var _a, _b;
const andConditions = [];
const codeValueEq = (_a = params.codeValue) === null || _a === void 0 ? void 0 : _a.$eq;
if (typeof codeValueEq === 'string') {
andConditions.push({ codeValue: { $eq: codeValueEq } });
}
const codeValueIn = (_b = params.codeValue) === null || _b === void 0 ? void 0 : _b.$in;
if (Array.isArray(codeValueIn)) {
andConditions.push({ codeValue: { $in: codeValueIn } });
}
return andConditions;
}
/**
* 検索
*/
projectFields(params, inclusion) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = CustomerTypeRepo.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 {
// no op
}
const projection = Object.assign({ _id: 0, id: { $toString: '$_id' } }, Object.fromEntries(positiveProjectionFields.map((key) => ([key, 1]))));
const query = this.customerTypeModel.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 (params.sort !== undefined) {
query.sort(params.sort);
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean()
.exec();
});
}
saveManyByCodeValue(params) {
return __awaiter(this, void 0, void 0, function* () {
const bulkWriteOps = [];
if (Array.isArray(params)) {
params.forEach((p) => {
const $set = Object.assign({}, p.attributes);
if (typeof $set.id === 'string') {
delete $set.id;
}
bulkWriteOps.push({
updateOne: {
filter: {
codeValue: { $eq: p.attributes.codeValue }
},
update: {
$set
// $setOnInsert: {}
},
upsert: (p.upsert !== undefined) ? p.upsert : false
}
});
});
}
if (bulkWriteOps.length > 0) {
return this.customerTypeModel.bulkWrite(bulkWriteOps, { ordered: false });
}
});
}
}
exports.CustomerTypeRepo = CustomerTypeRepo;
;