Skip to content
Open
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"clsx": "2.1.1",
"convex": "1.46.0",
"convex-svelte": "0.0.12",
"convex-test": "0.0.62",
"convex-test": "https://pkg.pr.new/convex-test@163",
"convex-vue": "0.1.5",
"eslint": "9.39.4",
"eslint-plugin-react": "7.37.5",
Expand Down
16 changes: 16 additions & 0 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type GenericDataModel,
type GenericMutationCtx,
type GenericQueryCtx,
type PaginationOptions,
internalMutationGeneric,
mutationGeneric,
paginationOptsValidator,
Expand Down Expand Up @@ -294,17 +295,27 @@ export class R2 {
*
* @param ctx - A Convex query context.
* @param limit (optional) - The maximum number of documents to return.
* @param cursor (optional) - The cursor to start the page from.
* @param opts (optional) - `endCursor`, `maximumRowsRead` and
* `maximumBytesRead`, as in `PaginationOptions`.
* @returns A promise that resolves to an array of metadata documents.
*/
async listMetadata(
ctx: QueryCtx | MutationCtx | ActionCtx,
limit?: number,
cursor?: string | null,
opts?: Pick<
PaginationOptions,
"endCursor" | "maximumRowsRead" | "maximumBytesRead"
>,
) {
return ctx.runQuery(this.component.lib.listMetadata, {
...this.config,
limit: limit,
cursor: cursor ?? undefined,
endCursor: opts?.endCursor,
maximumRowsRead: opts?.maximumRowsRead,
maximumBytesRead: opts?.maximumBytesRead,
});
}
/**
Expand Down Expand Up @@ -484,6 +495,11 @@ export class R2 {
ctx,
args.paginationOpts.numItems,
args.paginationOpts.cursor,
{
endCursor: args.paginationOpts.endCursor,
maximumRowsRead: args.paginationOpts.maximumRowsRead,
maximumBytesRead: args.paginationOpts.maximumBytesRead,
},
);
},
}),
Expand Down
3 changes: 3 additions & 0 deletions src/component/_generated/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ export type ComponentApi<Name extends string | undefined = string | undefined> =
accessKeyId: string;
bucket: string;
cursor?: string;
endCursor?: string | null;
endpoint: string;
limit?: number;
maximumBytesRead?: number;
maximumRowsRead?: number;
secretAccessKey: string;
},
{
Expand Down
52 changes: 52 additions & 0 deletions src/component/lib.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/// <reference types="vite/client" />
import { convexTest } from "convex-test";
import { describe, expect, it } from "vitest";
import { api } from "./_generated/api.js";
import schema from "./schema.js";

const modules = import.meta.glob("./**/*.ts");

const r2Config = {
bucket: "test-bucket",
endpoint: "https://test.r2.cloudflarestorage.com",
accessKeyId: "test-key",
secretAccessKey: "test-secret",
};

describe("listMetadata", () => {
it("respects endCursor", async () => {
const t = convexTest(schema, modules);
await t.run(async (ctx) => {
for (let i = 0; i < 5; i++) {
await ctx.db.insert("metadata", {
key: `key-${i}`,
bucket: r2Config.bucket,
lastModified: new Date(0).toISOString(),
link: `https://example.com/objects/key-${i}`,
});
}
});
const first = await t.query(api.lib.listMetadata, {
...r2Config,
limit: 2,
});
expect(first.page.map((d) => d.key)).toEqual(["key-0", "key-1"]);
expect(first.isDone).toBe(false);

// With endCursor, the page ends at that cursor regardless of limit.
const refetched = await t.query(api.lib.listMetadata, {
...r2Config,
limit: 1,
endCursor: first.continueCursor,
});
expect(refetched.page.map((d) => d.key)).toEqual(["key-0", "key-1"]);
expect(refetched.continueCursor).toBe(first.continueCursor);

const rest = await t.query(api.lib.listMetadata, {
...r2Config,
cursor: first.continueCursor,
});
expect(rest.page.map((d) => d.key)).toEqual(["key-2", "key-3", "key-4"]);
expect(rest.isDone).toBe(true);
});
});
18 changes: 15 additions & 3 deletions src/component/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type { Doc, TableNames } from "./_generated/dataModel.js";
import { api, components } from "./_generated/api.js";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { asyncMap } from "convex-helpers";
import { paginator } from "convex-helpers/server/pagination";
import { ActionRetrier } from "@convex-dev/action-retrier";
import type { R2Callbacks } from "../client/index.js";

Expand Down Expand Up @@ -89,6 +88,9 @@ export const listMetadata = query({
args: {
limit: v.optional(v.number()),
cursor: v.optional(v.string()),
endCursor: v.optional(v.union(v.string(), v.null())),
maximumRowsRead: v.optional(v.number()),
maximumBytesRead: v.optional(v.number()),
...r2ConfigValidator.fields,
},
returns: paginationReturnValidator(
Expand All @@ -99,14 +101,24 @@ export const listMetadata = query({
}),
),
handler: async (ctx, args) => {
const { limit, cursor, ...r2Config } = args;
const {
limit,
cursor,
endCursor,
maximumRowsRead,
maximumBytesRead,
...r2Config
} = args;
const r2 = createR2Client(r2Config);
const results = await paginator(ctx.db, schema)
const results = await ctx.db
.query("metadata")
.withIndex("bucket", (q) => q.eq("bucket", r2Config.bucket))
.paginate({
numItems: limit ?? DEFAULT_LIST_LIMIT,
cursor: cursor ?? null,
endCursor,
maximumRowsRead,
maximumBytesRead,
});
return {
...results,
Expand Down
Loading