Skip to content
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

fix value serde in patch, comparison of complex values #22

Merged
merged 5 commits into from
Sep 30, 2024
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
85 changes: 85 additions & 0 deletions compare.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Value } from "convex/values";

// Returns -1 if k1 < k2
// Returns 0 if k1 === k2
// Returns 1 if k1 > k2
export function compareValues(k1: Value | undefined, k2: Value | undefined) {
return compareAsTuples(makeComparable(k1), makeComparable(k2));
}

function compareAsTuples<T>(a: [number, T], b: [number, T]): number {
if (a[0] === b[0]) {
return compareSameTypeValues(a[1], b[1]);
} else if (a[0] < b[0]) {
return -1;
}
return 1;
}

function compareSameTypeValues<T>(v1: T, v2: T): number {
if (v1 === undefined || v1 === null) {
return 0;
}
if (
typeof v1 === "bigint"
|| typeof v1 === "number"
|| typeof v1 === "boolean"
|| typeof v1 === "string"
) {
return v1 < v2 ? -1 : v1 === v2 ? 0 : 1;
}
if (!Array.isArray(v1) || !Array.isArray(v2)) {
throw new Error(`Unexpected type ${v1 as any}`);
}
for (let i = 0; i < v1.length && i < v2.length; i++) {
const cmp = compareAsTuples(v1[i], v2[i]);
if (cmp !== 0) {
return cmp;
}
}
if (v1.length < v2.length) {
return -1;
}
if (v1.length > v2.length) {
return 1;
}
return 0;
}

// Returns an array which can be compared to other arrays as if they were tuples.
// For example, [1, null] < [2, 1n] means null sorts before all bigints
// And [3, 5] < [3, 6] means floats sort as expected
// And [7, [[5, "a"]]] < [7, [[5, "a"], [5, "b"]]] means arrays sort as expected
function makeComparable(v: Value | undefined): [number, any] {
if (v === undefined) {
return [0, undefined];
}
if (v === null) {
return [1, null];
}
if (typeof v === "bigint") {
return [2, v];
}
if (typeof v === "number") {
if (isNaN(v)) { // Consider all NaNs to be equal.
return [3.5, 0];
}
return [3, v];
}
if (typeof v === "boolean") {
return [4, v];
}
if (typeof v === "string") {
return [5, v];
}
if (v instanceof ArrayBuffer) {
return [6, Array.from(new Uint8Array(v)).map(makeComparable)];
}
if (Array.isArray(v)) {
return [7, v.map(makeComparable)];
}
// Otherwise, it's an POJO.
const keys = Object.keys(v).sort();
const pojo: Value[] = keys.map((k) => [k, v[k]!]);
return [8, pojo.map(makeComparable)];
}
66 changes: 66 additions & 0 deletions convex/messages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { expect, test, vi } from "vitest";
import { convexTest } from "../index";
import { api } from "./_generated/api";
import schema from "./schema";
import { defineSchema, defineTable } from "convex/server";
import { v } from "convex/values";

test("messages", async () => {
const t = convexTest(schema);
Expand All @@ -27,3 +29,67 @@ test("ai", async () => {
expect(messages).toMatchObject([{ author: "AI", body: "I am the overlord" }]);
vi.unstubAllGlobals();
});

test("all types serde", async () => {
const relaxedSchema = defineSchema({
messages: defineTable({
body: v.optional(v.any()),
}).index("body", ["body"]),
});
const t = convexTest(relaxedSchema);
const expectBodiesEq = (a: any, b: any) => {
if (a === undefined) {
expect(b).toBeUndefined();
} else {
expect(b).toMatchObject(a);
}
};
await t.run(async (ctx) => {
async function testBody(body: any) {
const id = await ctx.db.insert("messages", { body });
// Simple db.get
const byGet = await ctx.db.get(id);
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))
.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))
.unique();
expect(byFilter).not.toBeNull();
expectBodiesEq(byFilter!.body, body);
// Patch
await ctx.db.patch(id, { body });
expectBodiesEq((await ctx.db.get(id))!.body, body);
// Replace
await ctx.db.replace(id, { body });
expectBodiesEq((await ctx.db.get(id))!.body, body);
// Delete
await ctx.db.delete(id);
const byGetAfterDelete = await ctx.db.get(id);
expect(byGetAfterDelete).toBeNull();
}
// NOTE: do it this way instead of with an array so the failed test case
// shows up in the stacktrace.
await testBody("stringValue");
await testBody(undefined);
await testBody(true);
await testBody(35);
await testBody(BigInt(34));
await testBody(null);
await testBody(["a"]);
await testBody([BigInt(34)]);
await testBody({ a: 1 });
await testBody({ a: BigInt(34) });
await testBody(new ArrayBuffer(8));
await testBody(Number.POSITIVE_INFINITY);
await testBody(Number.NEGATIVE_INFINITY);
await testBody(-0.0);
await testBody(NaN);
});
});
104 changes: 23 additions & 81 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
jsonToConvex,
} from "convex/values";
import { createHash } from "crypto";
import { compareValues } from "./compare";

type FilterJson =
| { $eq: [FilterJson, FilterJson] }
Expand Down Expand Up @@ -258,12 +259,11 @@ class DatabaseFake {
}
delete value["_id"];
delete value["_creationTime"];
const convexValue: any = {};
for (const [key, v] of Object.entries(value)) {
if (v['$undefined'] === null) {
value[key] = undefined;
}
convexValue[key] = evaluateValue(v);
}
const merged = { ...fields, ...value };
const merged = { ...fields, ...convexValue };
this._validate(tableNameFromId(_id as string)!, merged);
this._writes[id] = {
newValue: { _id, _creationTime, ...merged },
Expand Down Expand Up @@ -293,10 +293,14 @@ class DatabaseFake {
}
delete value["_id"];
delete value["_creationTime"];
this._validate(tableNameFromId(document._id as string)!, value);
const convexValue: any = {};
for (const [key, v] of Object.entries(value)) {
convexValue[key] = evaluateValue(v);
}
this._validate(tableNameFromId(document._id as string)!, convexValue);
this._writes[id] = {
newValue: {
...value,
...convexValue,
_id: document._id,
_creationTime: document._creationTime,
},
Expand Down Expand Up @@ -586,68 +590,6 @@ function tableNameFromId(id: string) {
return id.split(";")[1];
}

function compareValues(a: Value | undefined, b: Value | undefined) {
if (a === b) {
return 0;
}
if (a === undefined) {
return -1;
}
if (b === undefined) {
return 1;
}
if (a === null) {
return -1;
}
if (b === null) {
return 1;
}
const aType = typeof a;
const bType = typeof b;
if (aType !== bType) {
if (aType === "bigint") {
return -1;
}
if (bType === "bigint") {
return 1;
}
if (aType === "number") {
return -1;
}
if (bType === "number") {
return 1;
}
if (aType === "boolean") {
return -1;
}
if (bType === "boolean") {
return 1;
}
if (aType === "string") {
return -1;
}
if (bType === "string") {
return 1;
}
}
if (aType === "object") {
if (a instanceof ArrayBuffer && !(b instanceof ArrayBuffer)) {
return -1;
}
if (b instanceof ArrayBuffer && !(a instanceof ArrayBuffer)) {
return 1;
}
if (Array.isArray(a) && !Array.isArray(b)) {
return -1;
}
if (Array.isArray(b) && !Array.isArray(a)) {
return 1;
}
}

return a < b ? -1 : 1;
}

function isSimpleObject(value: unknown) {
const isObject = typeof value === "object";
const prototype = Object.getPrototypeOf(value);
Expand Down Expand Up @@ -677,16 +619,16 @@ function evaluateFilter(
filter: any,
): Value | undefined {
if (filter.$eq !== undefined) {
return (
evaluateFilter(document, filter.$eq[0]) ===
return compareValues(
evaluateFilter(document, filter.$eq[0]),
evaluateFilter(document, filter.$eq[1])
);
) === 0;
}
if (filter.$neq !== undefined) {
return (
evaluateFilter(document, filter.$neq[0]) !==
return compareValues(
evaluateFilter(document, filter.$neq[0]),
evaluateFilter(document, filter.$neq[1])
);
) !== 0;
}
if (filter.$and !== undefined) {
return filter.$and.every((child: any) => evaluateFilter(document, child));
Expand Down Expand Up @@ -768,23 +710,23 @@ function evaluateRangeFilter(
const value = evaluateValue(expr.value);
switch (expr.type) {
case "Eq":
return result === value;
return compareValues(result, value) === 0;
case "Gt":
return (result as any) > (value as any);
return compareValues(result, value) > 0;
case "Gte":
return (result as any) >= (value as any);
return compareValues(result, value) >= 0;
case "Lt":
return (result as any) < (value as any);
return compareValues(result, value) < 0;
case "Lte":
return (result as any) <= (value as any);
return compareValues(result, value) <= 0;
}
}

function evaluateValue(value: JSONValue) {
if (typeof value === "object" && value !== null && "$undefined" in value) {
return undefined;
}
return value;
return jsonToConvex(value);
}

function evaluateSearchFilter(
Expand All @@ -794,7 +736,7 @@ function evaluateSearchFilter(
const result = evaluateFieldPath(filter.fieldPath, document);
switch (filter.type) {
case "Eq":
return result === filter.value;
return compareValues(result, filter.value) === 0;
case "Search":
return (result as string)
.split(/\s/)
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["./index.ts"],
"include": ["./*.ts"],
"exclude": ["convex"]
}