Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | 6x 6x 8x 8x 8x 8x 19x 6x 7x 7x 8x 6x | import { z } from "zod";
import { limitSchema, offsetSchema } from "~app/utils/validation";
import type { PeerId } from "~db/ids";
import { queueCallFactory } from "~web/handlers/batch";
import type { AuthorizedContext } from "~web/handlers/context";
import { authProcedure } from "~web/handlers/trpc";
import type { GeneralOutput } from "~web/utils/batch";
import { queueList } from "~web/utils/batch";
const inputSchema = z.strictObject({
cursor: offsetSchema,
limit: limitSchema,
});
type Input = z.infer<typeof inputSchema>;
type Output = GeneralOutput<PeerId> & { count: number };
const fetchPage = async (
{ database, auth }: AuthorizedContext,
input: Input,
) => {
const userPeers = database.selectFrom("peers").where((eb) =>
eb("peers.ownerUserId", "=", auth.userId).and(
"peers.id",
"<>",
// Typesystem doesn't know that we use user id as self peer id;
auth.userId as PeerId,
),
);
const [peers, peersCount] = await Promise.all([
userPeers
.select("peers.id")
// Stable order for peers with the same name
.orderBy("peers.name")
.orderBy("peers.id")
.offset(input.cursor)
.limit(input.limit)
.execute(),
userPeers
.select(database.fn.count<number>("id").as("amount"))
.executeTakeFirstOrThrow(),
]);
return {
count: peersCount.amount,
cursor: input.cursor,
items: peers.map(({ id }) => id),
};
};
const queuePeerList = queueCallFactory<AuthorizedContext, Input, Output>(
(ctx) => async (inputs) =>
queueList<Input, PeerId, Output>(inputs, (values) =>
fetchPage(ctx, values),
),
);
export const procedure = authProcedure
.meta({
title: "Get peers, paged",
description:
"Returns a page of peerIds owned by the current user, excluding the self-peer.",
})
.input(inputSchema)
.query(queuePeerList);
|