@chevre/domain
Version:
Chevre Domain Library for Node.js
117 lines (116 loc) • 3.29 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose = require("mongoose");
const safe = { j: true, w: 'majority', wtimeout: 10000 };
const objectSchema = new mongoose.Schema({}, {
id: false,
_id: false,
strict: false
});
const resultSchema = new mongoose.Schema({}, {
id: false,
_id: false,
strict: false
});
const agentSchema = new mongoose.Schema({}, {
id: false,
_id: false,
strict: false
});
const recipientSchema = new mongoose.Schema({}, {
id: false,
_id: false,
strict: false
});
const errorSchema = new mongoose.Schema({}, {
id: false,
_id: false,
strict: false
});
const potentialActionsSchema = new mongoose.Schema({}, {
id: false,
_id: false,
strict: false
});
/**
* 取引スキーマ
*/
const schema = new mongoose.Schema({
status: String,
typeOf: String,
agent: agentSchema,
recipient: recipientSchema,
error: errorSchema,
result: resultSchema,
object: objectSchema,
expires: Date,
startDate: Date,
endDate: Date,
tasksExportedAt: Date,
tasksExportationStatus: String,
potentialActions: potentialActionsSchema
}, {
collection: 'transactions',
id: true,
read: 'primaryPreferred',
safe: safe,
strict: true,
useNestedStrict: true,
timestamps: {
createdAt: 'createdAt',
updatedAt: 'updatedAt'
},
toJSON: { getters: true },
toObject: { getters: true }
});
schema.index({ createdAt: 1 }, { name: 'searchByCreatedAt' });
schema.index({ updatedAt: 1 }, { name: 'searchByUpdatedAt' });
schema.index({ typeOf: 1 }, { name: 'searchByTypeOf' });
schema.index({ status: 1 }, { name: 'searchByStatus' });
schema.index({ agent: 1 }, { name: 'searchByAgent' });
schema.index({ startDate: 1 }, { name: 'searchByStartDate' });
schema.index({ endDate: 1 }, {
name: 'searchByEndDate',
partialFilterExpression: {
endDate: { $exists: true }
}
});
schema.index({ expires: 1 }, { name: 'searchByExpires' });
schema.index({ tasksExportationStatus: 1 }, { name: 'searchByTasksExportationStatus' });
schema.index({ tasksExportedAt: 1 }, {
name: 'searchByTasksExportedAt',
partialFilterExpression: {
tasksExportedAt: { $exists: true }
}
});
// タスクエクスポート時の検索で使用
schema.index({ tasksExportationStatus: 1, status: 1 });
// 取引期限切れ確認等に使用
schema.index({ status: 1, expires: 1 });
// 実行中タスクエクスポート監視に使用
schema.index({ tasksExportationStatus: 1, updatedAt: 1 });
// 取引進行中は、基本的にIDとステータスで参照する
schema.index({ status: 1, typeOf: 1, _id: 1 });
// 許可証でユニークに
schema.index({
'object.passportToken': 1
}, {
unique: true,
partialFilterExpression: {
'object.passportToken': { $exists: true }
}
});
// 取引タイプ指定で取得する場合に使用
schema.index({
typeOf: 1,
_id: 1
});
exports.default = mongoose.model('Transaction', schema).on('index',
// tslint:disable-next-line:no-single-line-block-comment
/* istanbul ignore next */
(error) => {
if (error !== undefined) {
// tslint:disable-next-line:no-console
console.error(error);
}
});