Skip to content

Commit

Permalink
npm run format (#35)
Browse files Browse the repository at this point in the history
* .

* .

* .
  • Loading branch information
ldanilek authored Jan 3, 2025
1 parent 44d1d74 commit 1d11e92
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ jobs:
cache: "npm"
- run: npm i
- run: npm ci
- run: npm run lint
- run: npm test

11 changes: 6 additions & 5 deletions compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function compareSameTypeValues<T>(v1: T, v2: T): number {
return 0;
}
if (
typeof v1 === "bigint"
|| typeof v1 === "number"
|| typeof v1 === "boolean"
|| typeof v1 === "string"
typeof v1 === "bigint" ||
typeof v1 === "number" ||
typeof v1 === "boolean" ||
typeof v1 === "string"
) {
return v1 < v2 ? -1 : v1 === v2 ? 0 : 1;
}
Expand Down Expand Up @@ -61,7 +61,8 @@ function makeComparable(v: Value | undefined): [number, any] {
return [2, v];
}
if (typeof v === "number") {
if (isNaN(v)) { // Consider all NaNs to be equal.
if (isNaN(v)) {
// Consider all NaNs to be equal.
return [3.5, 0];
}
return [3, v];
Expand Down
14 changes: 6 additions & 8 deletions convex/argumentsValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import counterSchema from "../counter/component/schema";

const counterModules = import.meta.glob("../counter/component/**/*.ts");


test("query arguments validation", async () => {
const t = convexTest(schema);
await expect(
Expand Down Expand Up @@ -50,25 +49,24 @@ test("optional fields", async () => {

function testWithCounter() {
const t = convexTest(schema);
t.registerComponent(
"counter",
counterSchema,
counterModules
);
t.registerComponent("counter", counterSchema, counterModules);
return t;
}

test("component mutation arguments validation", async () => {
const t = testWithCounter();
expect(
await t.mutation(api.argumentsValidation.componentMutationWithNumberArg, { a: 42 })
await t.mutation(api.argumentsValidation.componentMutationWithNumberArg, {
a: 42,
}),
).toEqual(42);
await expect(
t.mutation(api.argumentsValidation.componentMutationWithNumberArg, {
a: "bad" as any,
}),
).rejects.toThrowError(/Validator error/);
expect(await t.mutation(api.argumentsValidation.componentMutationWithNumberArg, {
expect(
await t.mutation(api.argumentsValidation.componentMutationWithNumberArg, {
a: Number.POSITIVE_INFINITY,
}),
).toEqual(Number.POSITIVE_INFINITY);
Expand Down
9 changes: 6 additions & 3 deletions convex/argumentsValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ export const queryWithOptionalArgs = query({
export const componentMutationWithNumberArg = mutation({
args: { a: v.any() },
handler: (ctx, args) => {
const result = ctx.runMutation(components.counter.public.mutationWithNumberArg, {
a: args.a,
});
const result = ctx.runMutation(
components.counter.public.mutationWithNumberArg,
{
a: args.a,
},
);
return result;
},
});
10 changes: 7 additions & 3 deletions convex/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ export const directCall = internalMutation({
export const mutationWithNestedQuery = internalMutation({
args: {},
handler: async (ctx, _args): Promise<number> => {
return await ctx.runMutation(components.counter.public.mutationWithNestedQuery);
return await ctx.runMutation(
components.counter.public.mutationWithNestedQuery,
);
},
});

Expand All @@ -42,7 +44,9 @@ export const directCall2 = internalQuery({
export const schedule = internalMutation({
args: {},
handler: async (ctx, _args) => {
await ctx.runMutation(components.counter.public.schedule, { name: "beans" });
await ctx.runMutation(components.counter.public.schedule, {
name: "beans",
});
},
});

Expand Down Expand Up @@ -80,4 +84,4 @@ export const scheduleHandle = internalMutation({
{ name: "beans", count: 3 },
);
},
});
});
6 changes: 1 addition & 5 deletions convex/components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ const counterModules = import.meta.glob("../counter/component/**/*.ts");

function testWithCounter() {
const t = convexTest(schema);
t.registerComponent(
"counter",
counterSchema,
counterModules
);
t.registerComponent("counter", counterSchema, counterModules);
return t;
}

Expand Down
10 changes: 6 additions & 4 deletions convex/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ test("all types serde", async () => {
expect(byGet).not.toBeNull();
expectBodiesEq(byGet!.body, body);
// Indexed db.query
const byIndex = await ctx.db.query("messages")
.withIndex("body", q=>q.eq("body", body))
const byIndex = await ctx.db
.query("messages")
.withIndex("body", (q) => q.eq("body", body))
.unique();
expect(byIndex).not.toBeNull();
expectBodiesEq(byIndex!.body, body);
// Filtered db.query
const byFilter = await ctx.db.query("messages")
.filter(q=>q.eq(q.field("body"), body))
const byFilter = await ctx.db
.query("messages")
.filter((q) => q.eq(q.field("body"), body))
.unique();
expect(byFilter).not.toBeNull();
expectBodiesEq(byFilter!.body, body);
Expand Down
11 changes: 8 additions & 3 deletions convex/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ test("patch undefined", async () => {
expect(embedding).toBeUndefined();
});


test("replace after insert", async () => {
const t = convexTest(schema);
const messages = await t.run(async (ctx) => {
Expand Down Expand Up @@ -169,12 +168,18 @@ test("subtransaction commit then rollback parent", async () => {
// Regression test, making sure we merge writes in the correct order.
test("insert then patch in subtransaction", async () => {
const t = convexTest(schema);
const result = await t.mutation(api.mutations.insertThenPatchInSubtransaction, {});
const result = await t.mutation(
api.mutations.insertThenPatchInSubtransaction,
{},
);
expect(result).toEqual(["hi"]);
});

test("insert then delete in subtransaction", async () => {
const t = convexTest(schema);
const result = await t.mutation(api.mutations.insertThenDeleteInSubtransaction, {});
const result = await t.mutation(
api.mutations.insertThenDeleteInSubtransaction,
{},
);
expect(result).toEqual([]);
});
36 changes: 27 additions & 9 deletions convex/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export const rolledBackSubtransaction = mutation({
handler: async (ctx) => {
await ctx.db.insert("messages", { body: "hello", author: "sarah" });
try {
await ctx.runMutation(api.mutations.throws, { body: "hello", author: "lee" });
await ctx.runMutation(api.mutations.throws, {
body: "hello",
author: "lee",
});
} catch (e) {
// ignore
}
Expand All @@ -85,8 +88,14 @@ export const rolledBackSubtransaction = mutation({
export const subtransactionCommitThenRollbackParent = mutation({
args: {},
handler: async (ctx) => {
await ctx.runMutation(api.mutations.insert, { body: "hello", author: "sarah" });
await ctx.runMutation(api.mutations.throws, { body: "hello", author: "lee" });
await ctx.runMutation(api.mutations.insert, {
body: "hello",
author: "sarah",
});
await ctx.runMutation(api.mutations.throws, {
body: "hello",
author: "lee",
});
},
});

Expand All @@ -95,14 +104,20 @@ export const patchAndRead = mutation({
handler: async (ctx, { id, body }): Promise<string[]> => {
await ctx.db.patch(id, { body });
return (await ctx.db.query("messages").collect()).map(({ body }) => body);
}
},
});

export const insertThenPatchInSubtransaction = mutation({
args: {},
handler: async (ctx): Promise<string[]> => {
const id = await ctx.db.insert("messages", { body: "hello", author: "sarah" });
return await ctx.runMutation(api.mutations.patchAndRead, { id, body: "hi" });
const id = await ctx.db.insert("messages", {
body: "hello",
author: "sarah",
});
return await ctx.runMutation(api.mutations.patchAndRead, {
id,
body: "hi",
});
},
});

Expand All @@ -111,13 +126,16 @@ export const deleteAndRead = mutation({
handler: async (ctx, { id }): Promise<string[]> => {
await ctx.db.delete(id);
return (await ctx.db.query("messages").collect()).map(({ body }) => body);
}
},
});

export const insertThenDeleteInSubtransaction = mutation({
args: {},
handler: async (ctx): Promise<string[]> => {
const id = await ctx.db.insert("messages", { body: "hello", author: "sarah" });
const id = await ctx.db.insert("messages", {
body: "hello",
author: "sarah",
});
return await ctx.runMutation(api.mutations.deleteAndRead, { id });
},
});
});
6 changes: 5 additions & 1 deletion convex/pagination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ test("paginate", async () => {
{ author: "sarah", body: "hello2" },
]);
expect(isDone).toEqual(false);
const { continueCursor: continueCursor2, isDone: isDone2, page: page2 } = await t.query(api.pagination.list, {
const {
continueCursor: continueCursor2,
isDone: isDone2,
page: page2,
} = await t.query(api.pagination.list, {
author: "sarah",
paginationOptions: {
cursor: continueCursor,
Expand Down
8 changes: 4 additions & 4 deletions convex/scheduler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ test("self-scheduling mutation", async () => {
const t = convexTest(schema);
await t.mutation(api.scheduler.selfSchedulingMutation, {});

await expect(t.finishAllScheduledFunctions(vi.runAllTimers))
.rejects
.toThrowError(/Check for infinitely recursive scheduled functions/);
await expect(
t.finishAllScheduledFunctions(vi.runAllTimers),
).rejects.toThrowError(/Check for infinitely recursive scheduled functions/);

vi.useRealTimers();
});
});
8 changes: 6 additions & 2 deletions convex/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ export const actionSchedulingActionNTimes = action({
export const selfSchedulingMutation = mutation({
args: {},
handler: async (ctx) => {
await ctx.scheduler.runAfter(1000, api.scheduler.selfSchedulingMutation, {});
await ctx.scheduler.runAfter(
1000,
api.scheduler.selfSchedulingMutation,
{},
);
},
});
});
6 changes: 5 additions & 1 deletion counter/component/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export const schedule = mutation({
export const mutationWithNestedQuery = mutation({
args: {},
handler: async (ctx, _args) => {
const id = await ctx.db.insert("counters", { name: "beans", value: 3, shard: 0 });
const id = await ctx.db.insert("counters", {
name: "beans",
value: 3,
shard: 0,
});
await ctx.runQuery(api.public.count, { name: "beans" });
const doc = await ctx.db.get(id);
return doc!.value;
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"dist/*.map"
],
"scripts": {
"format": "prettier --write .",
"prebuild": "npm run lint && npm run test:once",
"build": "tsc && replace-in-file '\"./convex/**/*.*s\"' '\"../../../convex/**/*.*s\"' ./dist/index.js",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0 && prettier --check .",
"test": "vitest",
"test:once": "vitest run",
"test:debug": "vitest --inspect-brk --no-file-parallelism",
Expand Down

0 comments on commit 1d11e92

Please sign in to comment.