@briswell/bw-domain
Version:
Domain Library for Node.js
57 lines (56 loc) • 2.4 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 customer_1 = require("../model/customer");
const location_1 = require("../model/location");
const common_1 = require("../utils/common");
class LocationRepository {
constructor(db) {
this.locationModel = location_1.default(db);
this.customerModel = customer_1.default(db);
this.locationModel.belongsTo(this.customerModel);
}
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]];
}
findOption.include = [{
model: this.customerModel,
attributes: ['id', 'name']
}];
return this.locationModel.findAll(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.locationModel.update(params, { where: { id: params.id } });
resultId = params.id;
}
else { // 作成
params.createdBy = params.updatedBy;
const temp = yield this.locationModel.create(params);
resultId = temp.id;
}
const result = yield this.locationModel.findById(resultId);
return result;
});
}
}
exports.default = LocationRepository;