Skip to content

Ian/support count #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ jobs:
- run: npm ci
- run: npm run lint
- run: npm test
- run: npm run build
- run: npx pkg-pr-new publish
10 changes: 10 additions & 0 deletions convex/queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ test("collect", async () => {
]);
});

test("count", async () => {
const t = convexTest(schema);
await t.run(async (ctx) => {
await ctx.db.insert("messages", { author: "sarah", body: "hello1" });
await ctx.db.insert("messages", { author: "sarah", body: "hello2" });
});
const count = await t.query(api.queries.count);
expect(count).toStrictEqual(2);
});

test("withIndex", async () => {
const t = convexTest(schema);
const messages = await t.run(async (ctx) => {
Expand Down
4 changes: 4 additions & 0 deletions convex/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export const list = query(async (ctx) => {
return await ctx.db.query("messages").collect();
});

export const count = query(async (ctx) => {
return await (ctx.db.query("messages") as any).count();
});

/// order

export const lastN = query({
Expand Down
19 changes: 18 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,7 @@ function validateValidator(validator: ValidatorJSON, value: any) {
}
if (!isValid) {
throw new Error(
`Validator error: Expected one of ${validator.value.map((v) => v.type).join(", ")}, got \`${JSON.stringify(value)}\``,
Copy link
Author

Choose a reason for hiding this comment

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

This was a fix to the recent handling of unions - it was barfing trying to JSON.stringify values like BigInt

`Validator error: Expected one of ${validator.value.map((v) => v.type).join(", ")}, got \`${JSON.stringify(convexToJson(value))}\``,
);
}
return;
Expand Down Expand Up @@ -1291,6 +1291,23 @@ function asyncSyscallImpl() {
Math.random();
return JSON.stringify(convexToJson(url));
}
case "1.0/count": {
const { table } = args;
const queryId = db.startQuery({
source: { type: "FullTableScan", tableName: table, order: "asc" },
operators: [],
});
let count = 0;
// eslint-disable-next-line no-constant-condition
while (true) {
const result = db.queryNext(queryId);
if (result.done) {
break;
}
count += 1;
}
return JSON.stringify(count);
}
default: {
throw new Error(
`\`convexTest\` does not support async syscall: "${op}"`,
Expand Down
Loading