diff --git a/examples/demo/README.md b/examples/demo/README.md index ddbf7cb..4c83d30 100644 --- a/examples/demo/README.md +++ b/examples/demo/README.md @@ -10,8 +10,7 @@ deno task start This will watch the project directory and restart as necessary. - ### Testing locally -Swap out "x-pentagon" and "pentagon" in the `import_map.json` to switch between testing the local library or the latest -release. +Swap out "x-pentagon" and "pentagon" in the `import_map.json` to switch between +testing the local library or the latest release. diff --git a/examples/demo/routes/index.tsx b/examples/demo/routes/index.tsx index afb2f14..28639be 100644 --- a/examples/demo/routes/index.tsx +++ b/examples/demo/routes/index.tsx @@ -1,8 +1,8 @@ -import {Head} from "$fresh/runtime.ts"; -import {Handlers, PageProps} from "$fresh/server.ts"; -import {z} from "zod"; +import { Head } from "$fresh/runtime.ts"; +import { Handlers, PageProps } from "$fresh/server.ts"; +import { z } from "zod"; -import {db, TodoTask, User} from "../lib/db.ts"; +import { db, TodoTask, User } from "../lib/db.ts"; import Input from "../components/ui/input.tsx"; import Button from "../components/ui/button.tsx"; diff --git a/test/regression_test.ts b/test/regression_test.ts index 68be4ad..3136e22 100644 --- a/test/regression_test.ts +++ b/test/regression_test.ts @@ -2,11 +2,12 @@ import { assert, assertEquals, } from "https://deno.land/std@0.186.0/testing/asserts.ts"; -import { clearMocks, createMockDatabase, User } from "./util.ts"; -import { removeVersionstamp } from "../src/util.ts"; +import { clearMocks, createMockDatabase, Post, User } from "./util.ts"; +import { removeVersionstamp, removeVersionstamps } from "../src/util.ts"; import { createPentagon } from "../mod.ts"; import { z } from "../deps.ts"; import { schemaToKeys } from "../src/keys.ts"; +import { ZodSchema } from "https://deno.land/x/zod@v3.21.4/types.ts"; Deno.test("include", async (t) => { const db = createMockDatabase(); @@ -295,3 +296,60 @@ Deno.test("regression #33", async () => { assertEquals(typeof meeting.updatedAt, "string"); kv.close(); }); + +Deno.test("regression #44", async () => { + const kv = await Deno.openKv(); + + const userWithSecondaryIndex = User.extend({ + // @todo: this should work with `index` too?? + name: z.string().describe("unique"), + }); + + const postWithUniqueIndex = Post.extend({ + title: z.string().describe("unique"), + }); + + const db = createPentagon(kv, { + users: { + schema: userWithSecondaryIndex, + }, + posts: { + schema: postWithUniqueIndex, + }, + }); + + await db.users.deleteMany({}); + await db.posts.deleteMany({}); + await clearMocks(); + + const userId = crypto.randomUUID(); + const createdAt = new Date(); + + await db.users.create({ + data: { + id: userId, + createdAt, + name: "John Doe", + }, + }); + + // Update by secondary index + await db.users.update({ + where: { name: "John Doe" }, + data: { + name: "Doe John", + }, + }); + + // Check that user got updated when updating by secondary index + const updatedUsersList = await db.users.findMany({}); + assertEquals(removeVersionstamps(updatedUsersList), [ + { + id: userId, + createdAt, + name: "Doe John", + }, + ]); + + kv.close(); +}); diff --git a/test/util.ts b/test/util.ts index 58222c3..49425ce 100644 --- a/test/util.ts +++ b/test/util.ts @@ -161,6 +161,9 @@ export async function clearMocks() { for await (const x of kv.list({ prefix: ["users"] })) { await kv.delete(x.key); } + for await (const x of kv.list({ prefix: ["posts"] })) { + await kv.delete(x.key); + } for await (const x of kv.list({ prefix: ["orders"] })) { await kv.delete(x.key); }