更新时间:2023-02-15 15:57:10
我找到了以下解决方案,这可能是开销,但我找不到其他方法...
I found the following solution, it might be an overhead, but I couldn't find another way...
1-创建数据库视图:对于每个用户ID 和练习ID
1 - Create DB View: select only 1 (last) record for each userid and exerciseid
CREATE VIEW "v_athletexerciselog" AS
SELECT q.*
FROM athletexerciselog q
WHERE (
q.id IN (
SELECT max(i.id) AS max
FROM athletexerciselog i
WHERE (i.event IS NULL)
GROUP BY i.userid, i.exerciseid
)
)
2-创建新的LB4模型和存储库使用自定义表格名称(视图名称)扩展原始模型
2 - Create a new LB4 Model and Repository extends original model with custom table name (view name)
@model({
settings: {
postgresql: {schema: 'public', table: 'v_athletexerciselog'}, // custom names
},
})
export class VAthletExerciseLog extends AthletExerciseLog {
constructor(data?: Partial<VAthletExerciseLog>) {
super(data);
}
}
3 - 将练习模型和存储库中的相关模型名称更改为 VAthletExerciseLog(新)
3 - change related model name in Exercise model and repository to VAthletExerciseLog (new)
export class Exercise extends Entity {
...
@hasMany(() => VAthletExerciseLog, {keyTo: 'exerciseId'})
athletExerciseLogsExercise?: VAthletExerciseLog[];
...
}
4-删除订单"和限制"来自查询:
4 - Remove "order" and "limit" from query:
exercise.find({
include: [{
relation: "athletExerciseLogsExercise",
scope: {
where: {
userId: id
},
//order: ['date DESC'],
//limit: 1
}
}, ]
});