30 lines
867 B
TypeScript
30 lines
867 B
TypeScript
|
|
import { builder } from "../builder.js";
|
||
|
|
import { StatsType } from "../types/index.js";
|
||
|
|
import { users, videos } from "../../db/schema/index.js";
|
||
|
|
import { eq, count } from "drizzle-orm";
|
||
|
|
|
||
|
|
builder.queryField("stats", (t) =>
|
||
|
|
t.field({
|
||
|
|
type: StatsType,
|
||
|
|
resolve: async (_root, _args, ctx) => {
|
||
|
|
const modelsCount = await ctx.db
|
||
|
|
.select({ count: count() })
|
||
|
|
.from(users)
|
||
|
|
.where(eq(users.role, "model"));
|
||
|
|
const viewersCount = await ctx.db
|
||
|
|
.select({ count: count() })
|
||
|
|
.from(users)
|
||
|
|
.where(eq(users.role, "viewer"));
|
||
|
|
const videosCount = await ctx.db
|
||
|
|
.select({ count: count() })
|
||
|
|
.from(videos);
|
||
|
|
|
||
|
|
return {
|
||
|
|
models_count: modelsCount[0]?.count || 0,
|
||
|
|
viewers_count: viewersCount[0]?.count || 0,
|
||
|
|
videos_count: videosCount[0]?.count || 0,
|
||
|
|
};
|
||
|
|
},
|
||
|
|
}),
|
||
|
|
);
|