mirror of
https://github.com/versia-pub/server.git
synced 2025-12-06 16:38:19 +01:00
refactor(database): ♻️ Use new Drizzle count API
This commit is contained in:
parent
7f17074d16
commit
ce781f3336
|
|
@ -1,7 +1,7 @@
|
||||||
import { apiRoute, applyConfig, auth, idValidator } from "@/api";
|
import { apiRoute, applyConfig, auth, idValidator } from "@/api";
|
||||||
import { createRoute } from "@hono/zod-openapi";
|
import { createRoute } from "@hono/zod-openapi";
|
||||||
import type { Marker as ApiMarker } from "@versia/client/types";
|
import type { Marker as ApiMarker } from "@versia/client/types";
|
||||||
import { and, count, eq } from "drizzle-orm";
|
import { and, eq } from "drizzle-orm";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { db } from "~/drizzle/db";
|
import { db } from "~/drizzle/db";
|
||||||
import { Markers, RolePermissions } from "~/drizzle/schema";
|
import { Markers, RolePermissions } from "~/drizzle/schema";
|
||||||
|
|
@ -140,22 +140,15 @@ export default apiRoute((app) => {
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
const totalCount = await db
|
const totalCount = await db.$count(
|
||||||
.select({
|
Markers,
|
||||||
count: count(),
|
and(eq(Markers.userId, user.id), eq(Markers.timeline, "home")),
|
||||||
})
|
|
||||||
.from(Markers)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(Markers.userId, user.id),
|
|
||||||
eq(Markers.timeline, "home"),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (found?.noteId) {
|
if (found?.noteId) {
|
||||||
markers.home = {
|
markers.home = {
|
||||||
last_read_id: found.noteId,
|
last_read_id: found.noteId,
|
||||||
version: totalCount[0].count,
|
version: totalCount,
|
||||||
updated_at: new Date(found.createdAt).toISOString(),
|
updated_at: new Date(found.createdAt).toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -170,12 +163,8 @@ export default apiRoute((app) => {
|
||||||
),
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
const totalCount = await db
|
const totalCount = await db.$count(
|
||||||
.select({
|
Markers,
|
||||||
count: count(),
|
|
||||||
})
|
|
||||||
.from(Markers)
|
|
||||||
.where(
|
|
||||||
and(
|
and(
|
||||||
eq(Markers.userId, user.id),
|
eq(Markers.userId, user.id),
|
||||||
eq(Markers.timeline, "notifications"),
|
eq(Markers.timeline, "notifications"),
|
||||||
|
|
@ -185,7 +174,7 @@ export default apiRoute((app) => {
|
||||||
if (found?.notificationId) {
|
if (found?.notificationId) {
|
||||||
markers.notifications = {
|
markers.notifications = {
|
||||||
last_read_id: found.notificationId,
|
last_read_id: found.notificationId,
|
||||||
version: totalCount[0].count,
|
version: totalCount,
|
||||||
updated_at: new Date(found.createdAt).toISOString(),
|
updated_at: new Date(found.createdAt).toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -222,21 +211,14 @@ export default apiRoute((app) => {
|
||||||
.returning()
|
.returning()
|
||||||
)[0];
|
)[0];
|
||||||
|
|
||||||
const totalCount = await db
|
const totalCount = await db.$count(
|
||||||
.select({
|
Markers,
|
||||||
count: count(),
|
and(eq(Markers.userId, user.id), eq(Markers.timeline, "home")),
|
||||||
})
|
|
||||||
.from(Markers)
|
|
||||||
.where(
|
|
||||||
and(
|
|
||||||
eq(Markers.userId, user.id),
|
|
||||||
eq(Markers.timeline, "home"),
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
markers.home = {
|
markers.home = {
|
||||||
last_read_id: homeId,
|
last_read_id: homeId,
|
||||||
version: totalCount[0].count,
|
version: totalCount,
|
||||||
updated_at: new Date(insertedMarker.createdAt).toISOString(),
|
updated_at: new Date(insertedMarker.createdAt).toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -253,12 +235,8 @@ export default apiRoute((app) => {
|
||||||
.returning()
|
.returning()
|
||||||
)[0];
|
)[0];
|
||||||
|
|
||||||
const totalCount = await db
|
const totalCount = await db.$count(
|
||||||
.select({
|
Markers,
|
||||||
count: count(),
|
|
||||||
})
|
|
||||||
.from(Markers)
|
|
||||||
.where(
|
|
||||||
and(
|
and(
|
||||||
eq(Markers.userId, user.id),
|
eq(Markers.userId, user.id),
|
||||||
eq(Markers.timeline, "notifications"),
|
eq(Markers.timeline, "notifications"),
|
||||||
|
|
@ -267,7 +245,7 @@ export default apiRoute((app) => {
|
||||||
|
|
||||||
markers.notifications = {
|
markers.notifications = {
|
||||||
last_read_id: notificationsId,
|
last_read_id: notificationsId,
|
||||||
version: totalCount[0].count,
|
version: totalCount,
|
||||||
updated_at: new Date(insertedMarker.createdAt).toISOString(),
|
updated_at: new Date(insertedMarker.createdAt).toISOString(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import {
|
||||||
Collection as CollectionSchema,
|
Collection as CollectionSchema,
|
||||||
Note as NoteSchema,
|
Note as NoteSchema,
|
||||||
} from "@versia/federation/schemas";
|
} from "@versia/federation/schemas";
|
||||||
import { and, count, eq, inArray } from "drizzle-orm";
|
import { and, eq, inArray } from "drizzle-orm";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { db } from "~/drizzle/db";
|
import { db } from "~/drizzle/db";
|
||||||
import { Notes } from "~/drizzle/schema";
|
import { Notes } from "~/drizzle/schema";
|
||||||
|
|
@ -102,19 +102,13 @@ export default apiRoute((app) =>
|
||||||
NOTES_PER_PAGE * (pageNumber - 1),
|
NOTES_PER_PAGE * (pageNumber - 1),
|
||||||
);
|
);
|
||||||
|
|
||||||
const totalNotes = (
|
const totalNotes = await db.$count(
|
||||||
await db
|
Notes,
|
||||||
.select({
|
|
||||||
count: count(),
|
|
||||||
})
|
|
||||||
.from(Notes)
|
|
||||||
.where(
|
|
||||||
and(
|
and(
|
||||||
eq(Notes.authorId, uuid),
|
eq(Notes.authorId, uuid),
|
||||||
inArray(Notes.visibility, ["public", "unlisted"]),
|
inArray(Notes.visibility, ["public", "unlisted"]),
|
||||||
),
|
),
|
||||||
)
|
);
|
||||||
)[0].count;
|
|
||||||
|
|
||||||
const json = {
|
const json = {
|
||||||
first: new URL(
|
first: new URL(
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import {
|
||||||
type InferInsertModel,
|
type InferInsertModel,
|
||||||
type InferSelectModel,
|
type InferSelectModel,
|
||||||
type SQL,
|
type SQL,
|
||||||
count,
|
|
||||||
desc,
|
desc,
|
||||||
eq,
|
eq,
|
||||||
inArray,
|
inArray,
|
||||||
|
|
@ -340,13 +339,7 @@ export class Instance extends BaseInterface<typeof Instances> {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getCount() {
|
static getCount(): Promise<number> {
|
||||||
return (
|
return db.$count(Instances);
|
||||||
await db
|
|
||||||
.select({
|
|
||||||
count: count(),
|
|
||||||
})
|
|
||||||
.from(Instances)
|
|
||||||
)[0].count;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ import {
|
||||||
type InferInsertModel,
|
type InferInsertModel,
|
||||||
type SQL,
|
type SQL,
|
||||||
and,
|
and,
|
||||||
count,
|
|
||||||
desc,
|
desc,
|
||||||
eq,
|
eq,
|
||||||
inArray,
|
inArray,
|
||||||
|
|
@ -349,16 +348,10 @@ export class Note extends BaseInterface<typeof Notes, StatusWithRelations> {
|
||||||
* @returns The number of notes in the database
|
* @returns The number of notes in the database
|
||||||
*/
|
*/
|
||||||
static async getCount(): Promise<number> {
|
static async getCount(): Promise<number> {
|
||||||
return (
|
return await db.$count(
|
||||||
await db
|
Notes,
|
||||||
.select({
|
|
||||||
count: count(),
|
|
||||||
})
|
|
||||||
.from(Notes)
|
|
||||||
.where(
|
|
||||||
sql`EXISTS (SELECT 1 FROM "Users" WHERE "Users"."id" = ${Notes.authorId} AND "Users"."instanceId" IS NULL)`,
|
sql`EXISTS (SELECT 1 FROM "Users" WHERE "Users"."id" = ${Notes.authorId} AND "Users"."instanceId" IS NULL)`,
|
||||||
)
|
);
|
||||||
)[0].count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,6 @@ import {
|
||||||
type InferInsertModel,
|
type InferInsertModel,
|
||||||
type SQL,
|
type SQL,
|
||||||
and,
|
and,
|
||||||
count,
|
|
||||||
countDistinct,
|
countDistinct,
|
||||||
desc,
|
desc,
|
||||||
eq,
|
eq,
|
||||||
|
|
@ -327,15 +326,8 @@ export class User extends BaseInterface<typeof Users, UserWithRelations> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getCount() {
|
static getCount(): Promise<number> {
|
||||||
return (
|
return db.$count(Users, isNull(Users.instanceId));
|
||||||
await db
|
|
||||||
.select({
|
|
||||||
count: count(),
|
|
||||||
})
|
|
||||||
.from(Users)
|
|
||||||
.where(isNull(Users.instanceId))
|
|
||||||
)[0].count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getActiveInPeriod(milliseconds: number) {
|
static async getActiveInPeriod(milliseconds: number) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue