Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions example/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export declare const components: {
isDeleted: boolean;
ts: number;
}>;
pageStatus?: null | "SplitRequired" | "SplitRecommended";
splitCursor?: null | string;
}
>;
listHistory: FunctionReference<
Expand All @@ -91,6 +93,8 @@ export declare const components: {
isDeleted: boolean;
ts: number;
}>;
pageStatus?: null | "SplitRequired" | "SplitRecommended";
splitCursor?: null | string;
}
>;
listSnapshot: FunctionReference<
Expand Down Expand Up @@ -118,8 +122,8 @@ export declare const components: {
isDeleted: boolean;
ts: number;
}>;
pageStatus?: "SplitRecommended";
splitCursor?: string;
pageStatus?: null | "SplitRequired" | "SplitRecommended";
splitCursor?: null | string;
}
>;
update: FunctionReference<
Expand Down
4 changes: 4 additions & 0 deletions example/convex/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { paginationOptsValidator } from "convex/server";
import { Triggers } from "convex-helpers/server/triggers";
import { DataModel } from "./_generated/dataModel";
import { customCtx, customMutation } from "convex-helpers/server/customFunctions";
import { PaginatedQueryReference } from "convex/react";
import { api } from "./_generated/api";

const userAuditLog = new TableHistory<DataModel, "users">(components.userAuditLog, {
serializability: "wallclock",
Expand Down Expand Up @@ -65,6 +67,8 @@ export const listDocumentHistory = query({
},
});

const _typeAssertion: PaginatedQueryReference = api.example.listDocumentHistory;

export const listSnapshot = query({
args: {
snapshotTs: v.number(),
Expand Down
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type RunMutationCtx = {
runMutation: GenericMutationCtx<GenericDataModel>["runMutation"];
};

export type OpaqueIds<T> = T extends GenericId<infer _T> | string
export type OpaqueIds<T> = T extends GenericId<infer _T>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw this is a really important part of the change get-convex/templates#38

? string
: T extends (infer U)[]
? OpaqueIds<U>[]
Expand Down
8 changes: 6 additions & 2 deletions src/component/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ export type Mounts = {
isDeleted: boolean;
ts: number;
}>;
pageStatus?: null | "SplitRequired" | "SplitRecommended";
splitCursor?: null | string;
}
>;
listHistory: FunctionReference<
Expand All @@ -79,6 +81,8 @@ export type Mounts = {
isDeleted: boolean;
ts: number;
}>;
pageStatus?: null | "SplitRequired" | "SplitRecommended";
splitCursor?: null | string;
}
>;
listSnapshot: FunctionReference<
Expand Down Expand Up @@ -106,8 +110,8 @@ export type Mounts = {
isDeleted: boolean;
ts: number;
}>;
pageStatus?: "SplitRecommended";
splitCursor?: string;
pageStatus?: null | "SplitRequired" | "SplitRecommended";
splitCursor?: null | string;
}
>;
update: FunctionReference<
Expand Down
36 changes: 17 additions & 19 deletions src/component/lib.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { v, Infer } from "convex/values";
import { v, Infer, Validator } from "convex/values";
import { internalMutation, mutation, query, QueryCtx } from "./_generated/server";
import { paginator } from "convex-helpers/server/pagination";
import schema from "./schema.js";
Expand Down Expand Up @@ -85,16 +85,23 @@ export const update = mutation({
},
});

function paginationResultValidator<T>(itemValidator: Validator<T, "required", string>) {
return v.object({
continueCursor: v.string(),
isDone: v.boolean(),
page: v.array(itemValidator),
pageStatus: v.optional(v.union(v.null(), v.literal("SplitRequired"), v.literal("SplitRecommended"))),
splitCursor: v.optional(v.union(v.null(), v.string())),
});
}
Comment on lines +88 to +96
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be great for convex-helpers!

export type PaginationResult<T> = Infer<ReturnType<typeof paginationResultValidator<T>>>;

export const listHistory = query({
args: {
maxTs: v.number(),
paginationOpts: paginationOptsValidator,
},
returns: v.object({
continueCursor: v.string(),
isDone: v.boolean(),
page: v.array(historyEntryValidator),
}),
returns: paginationResultValidator(historyEntryValidator),
handler: async (ctx, args) => {
const results = await paginator(ctx.db, schema)
.query("history")
Expand Down Expand Up @@ -124,11 +131,7 @@ export const listDocumentHistory = query({
maxTs: v.number(),
paginationOpts: paginationOptsValidator,
},
returns: v.object({
continueCursor: v.string(),
isDone: v.boolean(),
page: v.array(historyEntryValidator),
}),
returns: paginationResultValidator(historyEntryValidator),
handler: async (ctx, args) => {
const results = await paginator(ctx.db, schema)
.query("history")
Expand All @@ -151,13 +154,7 @@ export const listSnapshot = query({
currentTs: v.number(),
paginationOpts: paginationOptsValidator,
},
returns: v.object({
continueCursor: v.string(),
isDone: v.boolean(),
page: v.array(historyEntryValidator),
splitCursor: v.optional(v.string()),
pageStatus: v.optional(v.literal("SplitRecommended")),
}),
returns: paginationResultValidator(historyEntryValidator),
handler: async (ctx, args) => {
const pageSize = args.paginationOpts.numItems;
const page: HistoryEntry[] = [];
Expand Down Expand Up @@ -234,12 +231,13 @@ export const listSnapshot = query({
page.push(extractHistoryEntry(revision));
}
}
return {
const output: PaginationResult<HistoryEntry> = {
continueCursor: allIdsBeforeCurrentTs[allIdsBeforeCurrentTs.length - 1],
isDone: false,
page,
...maybeSplit(allIdsSeen, pageSize),
};
return output;
},
});

Expand Down