@chevre/domain
Version:
Chevre Domain Library for Node.js
169 lines (168 loc) • 6.17 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 creativeWork_1 = require("./mongoose/model/creativeWork");
const factory = require("../factory");
/**
* 作品抽象リポジトリー
*/
class Repository {
}
exports.Repository = Repository;
/**
* 作品リポジトリー
*/
class MongoRepository {
constructor(connection) {
this.creativeWorkModel = connection.model(creativeWork_1.default.modelName);
}
static CREATE_MONGO_CONDITIONS(params) {
// MongoDB検索条件
const andConditions = [
{
typeOf: factory.creativeWorkType.Movie
}
];
if (params.identifier !== undefined) {
andConditions.push({
identifier: {
$exists: true,
$regex: new RegExp(params.identifier, 'i')
}
});
}
if (params.name !== undefined) {
andConditions.push({
name: {
$exists: true,
$regex: new RegExp(params.name, 'i')
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.datePublishedFrom !== undefined) {
andConditions.push({
datePublished: {
$exists: true,
$gte: params.datePublishedFrom
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.datePublishedThrough !== undefined) {
andConditions.push({
datePublished: {
$exists: true,
$lte: params.datePublishedThrough
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers !== undefined) {
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.availableFrom instanceof Date) {
andConditions.push({
'offers.availabilityEnds': {
$exists: true,
$gt: params.offers.availableFrom
}
});
}
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore else */
if (params.offers.availableThrough instanceof Date) {
andConditions.push({
'offers.availabilityStarts': {
$exists: true,
$lt: params.offers.availableThrough
}
});
}
}
return andConditions;
}
/**
* 映画作品を保管する
*/
saveMovie(movie) {
return __awaiter(this, void 0, void 0, function* () {
yield this.creativeWorkModel.findOneAndUpdate({
identifier: movie.identifier,
typeOf: factory.creativeWorkType.Movie
}, movie, { upsert: true }).exec();
});
}
/**
* 識別子で映画作品を検索する
*/
findMovieByIdentifier(params) {
return __awaiter(this, void 0, void 0, function* () {
const doc = yield this.creativeWorkModel.findOne({
typeOf: factory.creativeWorkType.Movie,
identifier: params.identifier
}, {
__v: 0,
createdAt: 0,
updatedAt: 0
}).exec();
if (doc === null) {
throw new factory.errors.NotFound('Movie');
}
return doc.toObject();
});
}
countMovies(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
return this.creativeWorkModel.countDocuments({ $and: conditions }).setOptions({ maxTimeMS: 10000 })
.exec();
});
}
/**
* 映画作品を検索する
*/
searchMovies(params) {
return __awaiter(this, void 0, void 0, function* () {
const conditions = MongoRepository.CREATE_MONGO_CONDITIONS(params);
const query = this.creativeWorkModel.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()));
});
}
/**
* 映画作品を削除する
*/
deleteMovie(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.creativeWorkModel.findOneAndRemove({
identifier: params.identifier,
typeOf: factory.creativeWorkType.Movie
}).exec();
});
}
}
exports.MongoRepository = MongoRepository;
;