@briswell/bw-domain
Version:
Domain Library for Node.js
66 lines (65 loc) • 2.79 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 Sequelize = require("sequelize");
const section_1 = require("../model/section");
const common_1 = require("../utils/common");
class SectionRepository {
constructor(db) {
this.sectionModel = section_1.default(db);
}
search(params) {
return __awaiter(this, void 0, void 0, function* () {
const findOption = common_1.stripUndefinedField(params);
if (params.sort !== undefined) {
findOption.order = [['id', params.sort]];
}
if (findOption.where !== undefined) {
const where = findOption.where;
if (where.sectionName !== undefined) {
findOption.where = Object.assign({}, where, { [Sequelize.Op.or]: {
sectionName: { [Sequelize.Op.like]: `%${where.sectionName}%` }
} });
delete findOption.where.sectionName;
}
}
if (params.limit !== undefined) {
findOption.limit = params.limit;
}
if (params.offset !== undefined) {
findOption.offset = params.offset;
}
return this.sectionModel.findAndCount(findOption);
});
}
/**
* IDがある場合更新する、ない場合新しい作成する
* @param params データ
*/
upsert(params) {
return __awaiter(this, void 0, void 0, function* () {
params.updatedBy = params.userId;
delete params.userId;
let resultId;
if (params.id !== undefined) { // 編集
yield this.sectionModel.update(params, { where: { id: params.id } });
resultId = params.id;
}
else { // 作成
params.createdBy = params.updatedBy;
const temp = yield this.sectionModel.create(params);
resultId = temp.id;
}
const result = yield this.sectionModel.findById(resultId);
return result;
});
}
}
exports.default = SectionRepository;