@chevre/domain
Version:
Chevre Domain Library for Node.js
87 lines (86 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());
});
};
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.AggregationRepo = void 0;
const factory = require("../factory");
const aggregation_1 = require("./mongoose/schemas/aggregation");
/**
* 集計リポジトリ
*/
class AggregationRepo {
constructor(connection) {
this.aggregationModel = connection.model(aggregation_1.modelName, (0, aggregation_1.createSchema)());
}
saveAggregation(params) {
return __awaiter(this, void 0, void 0, function* () {
const { typeOf, project, aggregateDuration, aggregateStart } = params, setFields = __rest(params, ["typeOf", "project", "aggregateDuration", "aggregateStart"]);
const filter = {
typeOf: { $eq: typeOf },
'project.id': { $eq: project.id },
aggregateStart: { $eq: aggregateStart },
aggregateDuration: { $eq: aggregateDuration }
};
const updateQuery = {
$setOnInsert: { typeOf, project, aggregateDuration, aggregateStart },
$set: setFields
};
return this.aggregationModel.updateOne(filter, updateQuery, { upsert: true })
.exec();
});
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
const limit = Number(params.limit);
const page = Number(params.page);
const filter = Object.assign(Object.assign(Object.assign({}, (typeof ((_a = params.typeOf) === null || _a === void 0 ? void 0 : _a.$eq) === 'string')
? { typeOf: { $eq: params.typeOf.$eq } }
: undefined), (typeof ((_b = params.aggregateDuration) === null || _b === void 0 ? void 0 : _b.$eq) === 'string')
? { aggregateDuration: { $eq: params.aggregateDuration.$eq } }
: undefined), { aggregateStart: Object.assign(Object.assign({}, (((_c = params.aggregateStart) === null || _c === void 0 ? void 0 : _c.$gte) instanceof Date) ? { $gte: params.aggregateStart.$gte } : undefined), (((_d = params.aggregateStart) === null || _d === void 0 ? void 0 : _d.$lte) instanceof Date) ? { $lte: params.aggregateStart.$lte } : undefined) });
return this.aggregationModel.find(filter)
.limit(limit)
.skip(limit * (page - 1))
.sort({ aggregateStart: factory.sortType.Ascending })
.exec();
});
}
/**
* 不要な集計を削除する
*/
deleteAggregateStartPassedCertainPeriod(params) {
return __awaiter(this, void 0, void 0, function* () {
const filter = {
aggregateStart: { $lt: params.aggregateStart.$lt }
};
return this.aggregationModel.deleteMany(filter)
.exec();
});
}
unsetUnnecessaryFields(params) {
return __awaiter(this, void 0, void 0, function* () {
return this.aggregationModel.updateMany(params.filter, { $unset: params.$unset }, { timestamps: false })
.exec();
});
}
}
exports.AggregationRepo = AggregationRepo;
;