@chevre/domain
Version:
Chevre Domain Library for Node.js
89 lines (88 loc) • 3.59 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const factory = require("../factory");
const accountTitle_1 = require("./mongoose/model/accountTitle");
/**
* Mongoリポジトリー
*/
class MongoRepository {
constructor(connection) {
this.accountTitleModel = connection.model(accountTitle_1.default.modelName);
}
static CREATE_MONGO_CONDITIONS(params) {
// MongoDB検索条件
const andConditions = [
{ typeOf: 'AccountTitle' }
];
if (params.identifier !== undefined) {
andConditions.push({ identifier: params.identifier });
}
return andConditions;
}
save(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.accountTitleModel.findOneAndUpdate({
identifier: params.identifier
}, params, { upsert: true }).exec();
});
}
findMovieByIdentifier(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.accountTitleModel.findOne({
identifier: params.identifier
}, {
__v: 0,
createdAt: 0,
updatedAt: 0
}).exec();
if (doc === null) {
throw new factory.errors.NotFound('Movie');
}
return doc.toObject();
});
}
count(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
return this.accountTitleModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 })
.exec();
});
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
const query = this.accountTitleModel.find({ $and: conditions }, {
__v: 0,
createdAt: 0,
updatedAt: 0
});
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.limit !== undefined && params.page !== undefined) {
query.limit(params.limit).skip(params.limit * (params.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: 10000 }).exec().then((docs) => docs.map((doc) => doc.toObject()));
});
}
deleteByIdentifier(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.accountTitleModel.findOneAndRemove({
identifier: params.identifier
}).exec();
});
}
}
exports.MongoRepository = MongoRepository;
;