mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Sql cleanups
This commit is contained in:
parent
69006478cb
commit
4cf67c3247
@ -187,7 +187,7 @@ export const historyH = new Elysia({ tags: ["profiles"] })
|
||||
.select(
|
||||
db
|
||||
.select({
|
||||
profilePk: sql<number>`${profilePk}`,
|
||||
profilePk: sql`${profilePk}`,
|
||||
entryPk: entries.pk,
|
||||
videoPk: videos.pk,
|
||||
percent: sql`hist.percent::integer`,
|
||||
@ -227,6 +227,7 @@ export const historyH = new Elysia({ tags: ["profiles"] })
|
||||
),
|
||||
)
|
||||
.orderBy(nextEntry.showPk, entries.order)
|
||||
.limit(1)
|
||||
.as("nextEntryQ");
|
||||
|
||||
const seenCountQ = db
|
||||
@ -247,8 +248,7 @@ export const historyH = new Elysia({ tags: ["profiles"] })
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.as("seenCountQ");
|
||||
);
|
||||
|
||||
await db
|
||||
.insert(watchlist)
|
||||
@ -260,16 +260,16 @@ export const historyH = new Elysia({ tags: ["profiles"] })
|
||||
status: sql`
|
||||
case
|
||||
when
|
||||
hist.progress >= 95
|
||||
hist.percent::integer >= 95
|
||||
and ${nextEntryQ.pk} is null
|
||||
then 'completed'::watchstatus
|
||||
else 'watching'::watchstatus
|
||||
then 'completed'::watchlist_status
|
||||
else 'watching'::watchlist_status
|
||||
end
|
||||
`,
|
||||
seenCount: sql`
|
||||
case
|
||||
when ${eq(entries.kind, "movie")} then hist.progress::number
|
||||
when hist.progress >= 95 then 1
|
||||
when ${entries.kind} = 'movie' then hist.percent::integer
|
||||
when hist.percent::integer >= 95 then 1
|
||||
else 0
|
||||
end
|
||||
`,
|
||||
@ -282,6 +282,8 @@ export const historyH = new Elysia({ tags: ["profiles"] })
|
||||
else null
|
||||
end
|
||||
`,
|
||||
// see https://github.com/drizzle-team/drizzle-orm/issues/3608
|
||||
updatedAt: sql`now()`,
|
||||
})
|
||||
.from(vals)
|
||||
.leftJoin(
|
||||
@ -297,26 +299,26 @@ export const historyH = new Elysia({ tags: ["profiles"] })
|
||||
),
|
||||
),
|
||||
)
|
||||
.leftLateralJoin(nextEntryQ, sql`true`),
|
||||
.leftJoinLateral(nextEntryQ, sql`true`),
|
||||
)
|
||||
.onConflictDoUpdate({
|
||||
target: [watchlist.profilePk, watchlist.showPk],
|
||||
set: {
|
||||
status: sql`
|
||||
case
|
||||
when ${eq(sql`excluded.status`, "completed")} then excluded.status
|
||||
when ${and(
|
||||
ne(watchlist.status, "completed"),
|
||||
ne(watchlist.status, "rewatching"),
|
||||
)} then excluded.status
|
||||
when excluded.status = 'completed' then excluded.status
|
||||
when
|
||||
${watchlist.status} != 'completed'
|
||||
and ${watchlist.status} != 'rewatching'
|
||||
then excluded.status
|
||||
else ${watchlist.status}
|
||||
end
|
||||
`,
|
||||
seenCount: sql`${seenCountQ.c}`,
|
||||
seenCount: sql`${seenCountQ}`,
|
||||
nextEntry: sql`
|
||||
case
|
||||
when ${eq(watchlist.status, "completed")} then null
|
||||
else excluded.nextEntry
|
||||
when ${watchlist.status} = 'completed' then null
|
||||
else excluded.next_entry
|
||||
end
|
||||
`,
|
||||
completedAt: coalesce(
|
||||
|
@ -95,10 +95,14 @@ export function values(items: Record<string, unknown>[]) {
|
||||
};
|
||||
}
|
||||
|
||||
export const coalesce = <T>(val: SQL<T> | Column, def: SQL<T>) => {
|
||||
export const coalesce = <T>(val: SQL<T> | Column, def: SQL<T> | Column) => {
|
||||
return sql<T>`coalesce(${val}, ${def})`;
|
||||
};
|
||||
|
||||
export const nullif = <T>(val: SQL<T> | Column, eq: SQL<T>) => {
|
||||
return sql<T>`nullif(${val}, ${eq})`;
|
||||
};
|
||||
|
||||
export const jsonbObjectAgg = <T>(key: SQLWrapper, value: SQL<T>) => {
|
||||
return sql<
|
||||
Record<string, T>
|
||||
|
@ -143,7 +143,7 @@ describe("Set & get history", () => {
|
||||
expect(body.items[1].watchStatus).toMatchObject({
|
||||
status: "completed",
|
||||
percent: 100,
|
||||
startedAt: "2025-02-02 00:00:00+00",
|
||||
completedAt: "2025-02-02 00:00:00+00",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user