私は現在、NodeJS、MSSQL、およびSequilizeをORMとして使用して、会社内の支店と従業員を管理するためのアプリケーションを開発中です。Sequelize:findAllに結合テーブルの属性を含める
要件は、過去の日付に「戻る」ことができ、その特定の時点で会社の状態を確認できるように、特定の変更を追跡することです。そのためには、私たちが気になる変更(ブランチの変更、アクティブブランチ間の従業員の移動)を追跡するために、タイムスタンプ(initDate
とendDate
)を使用しています。レコードが変更されるたびに、endDate
をnow()
に設定し、変更およびendDate = null
という新しいレコードを作成するので、すべてのブランチまたはブランチと従業員の関係の '最新のバージョンはendDate IS NULL
のものです。
我々はSequelizeで今直面している問題は次のとおりです。従業員をする際findAll()
熱心なローディングは(すなわちinclude
はINGの)しながら、どのように我々は、彼らの枝を分岐従業員の関係についてendDate
を指定することができますか?下の例では、私は会社の現在の状態を問い合わせているので、私はendDate: null
を求めていますが、それは実際には私たちが望む時点を指す引数です。
ありがとうございました。どんな洞察も本当に感謝しています!
var BranchModel = db.define<BranchInstance, Branch>('Branch', {
IdBranch: {
primaryKey: true,
type: DataTypes.INTEGER
},
Name: DataTypes.STRING,
// ... other fields ...
initDate: DataTypes.DATE,
endDate: DataTypes.DATE
}, {
timestamps: false,
classMethods: {
associate: function (models) {
Branch.belongsToMany(models.model('Employee'), {
through: {
model: models.model('Branch_Employee')
},
as: 'Employees',
foreignKey: 'branchId'
});
// ... other associations ..
}
}
});
従業員のモデル定義:
var EmployeeModel = db.define<EmployeeInstance, Employee>('Employee', {
idEmployee: {
primaryKey: true,
type: DataTypes.INTEGER
},
Name: DataTypes.STRING,
// ... other fields ...
active: DataTypes.BOOLEAN
}, {
timestamps: false,
defaultScope: {
where: {
active: true
}
},
classMethods: {
associate: function (models) {
EmployeeModel.belongsToMany(models.model('Branch'), {
through: {
model: models.model('Branch_Employee')
},
foreignKey: 'employeeId'
});
// ... other associations ...
}
}
});
参加テーブル定義:
は---ここで私は、モデルの定義とクエリ---
支店モデルの定義が含まれます
var Branch_Employee = db.define<BranchEmployeeInstance, BranchEmployee>('Branch_Employee', {
branchId: DataTypes.INTEGER,
employeeId: DataTypes.INTEGER
// ... some other attributes of the relation ...
initDate: DataTypes.DATE,
endDate: DataTypes.DATE,
}, {
timestamps: false,
classMethods: {
associate: function(models) {
Branch_Employee.belongsTo(models.model('Employee'), {
as: 'Employee',
foreignKey: 'employeeId'
});
Branch_Employee.belongsTo(models.model('Branch'), {
as: 'Branch',
foreignKey: 'branchId'
});
}
},
instanceMethods: {
// ... some instance methods ...
}
});
すべてのアクティブな従業員を取得するには
Sequelizeクエリ、それらが関連しているために枝をロードする熱心:
let employees = await EmployeeModel.findAll({
logging: true,
include: [{
model: Branch,
as: 'Branches',
where: {
endDate: null, // this would be [Branches].[endDate] IS NULL
// i also need the generated query to include:
// [Branches.Branch_Employee].[endDate] IS NULL
},
required: false
}]
});
上記のクエリのために生成されたSQLは、次のようになります。
SELECT [Employee].[idEmployee]
,[Employee].[Name]
,[Employee].[active]
,[Branches].[IdBranch] AS [Branches.IdBranch]
,[Branches].[Name] AS [Branches.Name]
,[Branches].[initDate] AS [Branches.initDate]
,[Branches].[endDate] AS [Branches.endDate]
,[Branches.Branch_Employee].[initDate] AS Branches.Branch_Employee.initDate]
,[Branches.Branch_Employee].[endDate] AS [Branches.Branch_Employee.endDate]
,[Branches.Branch_Employee].[branchId] AS Branches.Branch_Employee.branchId]
,[Branches.Branch_Employee].[employeeId] AS [Branches.Branch_Employee.employeeId]
FROM [Employee] AS [Employee]
LEFT OUTER JOIN (
[Branch_Employee] AS [Branches.Branch_Employee]
INNER JOIN [Branch] AS [Branches]
ON [Branches].[IdBranch] = [Branches.Branch_Employee].[branchId]
) ON [Employee].[idEmployee] = [Branches.Branch_Employee].[employeeId]
AND [Branches].[endDate] IS NULL
WHERE [Employee].[active] = 1;
しかし、実際にはI結果セットを「現在の」ブランチ - 従業員関係だけを含めるようにさらに制限する必要があります。
[Branches.Branch_Employee].[endDate] IS NULL