The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Neon from "@alchemy.run/neon"; |
| 2 | import * as Prisma from "@alchemy.run/prisma"; |
| 3 | import * as PrismaPostgres from "@alchemy.run/prisma/ORM/Postgres"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Path from "effect/Path"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import * as Result from "effect/Result"; |
| 11 | import * as Schema from "effect/Schema"; |
| 12 | import * as Stream from "effect/Stream"; |
| 13 | import { contract } from "./fixtures/client/contract.ts"; |
| 14 | import { makeDatabase } from "./fixtures/psl/generated/client.ts"; |
| 15 | import { schemas } from "./fixtures/psl/generated/schemas.ts"; |
| 16 | import { makeDatabase as makeVariantDatabase } from "./fixtures/variants/generated/client.ts"; |
| 17 | |
| 18 | const { test } = Test.make({ |
| 19 | providers: Layer.mergeAll(Prisma.providers(), Neon.providers()), |
| 20 | }); |
| 21 | |
| 22 | const HOOK_TIMEOUT = 120_000; |
| 23 | |
| 24 | test.provider( |
| 25 | "PSL-generated client: CRUD, relations, schemas, and rollback", |
| 26 | (stack) => |
| 27 | Effect.gen(function* () { |
| 28 | yield* stack.destroy(); |
| 29 | const path = yield* Path.Path; |
| 30 | const config = yield* path.fromFileUrl( |
| 31 | new URL("./fixtures/psl/prisma.config.ts", import.meta.url), |
| 32 | ); |
| 33 | const { branch } = yield* stack.deploy( |
| 34 | Effect.gen(function* () { |
| 35 | const contract = yield* Prisma.Contract("psl-contract", { config }); |
| 36 | const project = yield* Neon.Project("PrismaPslProject"); |
| 37 | const branch = yield* Neon.Branch("PrismaPslBranch", { project }); |
| 38 | yield* Prisma.Migrate("psl-migrate", { |
| 39 | contract, |
| 40 | url: branch.connectionUri, |
| 41 | }); |
| 42 | return { branch }; |
| 43 | }), |
| 44 | ); |
| 45 | const db = yield* makeDatabase(Effect.succeed(Redacted.make(branch.connectionUri))); |
| 46 | yield* Effect.scoped( |
| 47 | Effect.gen(function* () { |
| 48 | const user = yield* db.orm.public.User.create({ |
| 49 | email: "psl@example.com", |
| 50 | name: null, |
| 51 | }); |
| 52 | const validated = yield* Schema.decodeUnknownEffect(schemas.public.User)(user); |
| 53 | expect(validated.id).toBe(user.id); |
| 54 | yield* db.orm.public.Post.create({ |
| 55 | title: "PSL post", |
| 56 | authorId: user.id, |
| 57 | }); |
| 58 | const loaded = yield* db.orm.public.User.where({ id: user.id }).include("posts").first(); |
| 59 | expect(loaded?.posts.map((post) => post.title)).toEqual(["PSL post"]); |
| 60 | const rollback = yield* db |
| 61 | .transaction((tx) => |
| 62 | Effect.gen(function* () { |
| 63 | yield* tx.orm.public.User.where({ id: user.id }).update({ |
| 64 | name: "rolled back", |
| 65 | }); |
| 66 | return yield* tx.rollback(); |
| 67 | }), |
| 68 | ) |
| 69 | .pipe(Effect.result); |
| 70 | expect(Result.isFailure(rollback)).toBe(true); |
| 71 | expect((yield* db.orm.public.User.where({ id: user.id }).first())?.name).toBeNull(); |
| 72 | yield* db.orm.public.Post.where({ authorId: user.id }).delete(); |
| 73 | yield* db.orm.public.User.where({ id: user.id }).delete(); |
| 74 | expect(yield* db.orm.public.User.all()).toEqual([]); |
| 75 | }), |
| 76 | ); |
| 77 | yield* stack.destroy(); |
| 78 | }), |
| 79 | { |
| 80 | tags: [ |
| 81 | "provider:neon", |
| 82 | "provider:neon:branch", |
| 83 | "provider:neon:project", |
| 84 | "provider:prisma", |
| 85 | "provider:prisma:orm", |
| 86 | "live", |
| 87 | ], |
| 88 | timeout: HOOK_TIMEOUT, |
| 89 | }, |
| 90 | ); |
| 91 | |
| 92 | test.provider( |
| 93 | "polymorphic collections: single-table and multi-table variants", |
| 94 | (stack) => |
| 95 | Effect.gen(function* () { |
| 96 | yield* stack.destroy(); |
| 97 | const path = yield* Path.Path; |
| 98 | const config = yield* path.fromFileUrl( |
| 99 | new URL("./fixtures/variants/prisma.config.ts", import.meta.url), |
| 100 | ); |
| 101 | const { branch } = yield* stack.deploy( |
| 102 | Effect.gen(function* () { |
| 103 | const contract = yield* Prisma.Contract("variant-contract", { |
| 104 | config, |
| 105 | }); |
| 106 | const project = yield* Neon.Project("PrismaVariantProject"); |
| 107 | const branch = yield* Neon.Branch("PrismaVariantBranch", { project }); |
| 108 | yield* Prisma.Migrate("variant-migrate", { |
| 109 | contract, |
| 110 | url: branch.connectionUri, |
| 111 | }); |
| 112 | return { branch }; |
| 113 | }), |
| 114 | ); |
| 115 | const db = yield* makeVariantDatabase(Effect.succeed(Redacted.make(branch.connectionUri))); |
| 116 | yield* Effect.scoped( |
| 117 | Effect.gen(function* () { |
| 118 | const assignee = yield* db.orm.public.Assignee.create({ |
| 119 | name: "Sam", |
| 120 | }); |
| 121 | const bug = yield* db.orm.public.Task.variant("Bug").create({ |
| 122 | title: "Crash", |
| 123 | severity: "critical", |
| 124 | assigneeId: assignee.id, |
| 125 | }); |
| 126 | const feature = yield* db.orm.public.Task.variant("Feature").create({ |
| 127 | title: "Search", |
| 128 | priority: 3, |
| 129 | }); |
| 130 | expect(bug).toMatchObject({ type: "bug", severity: "critical" }); |
| 131 | expect(feature).toMatchObject({ type: "feature", priority: 3 }); |
| 132 | const all = yield* db.orm.public.Task.orderBy((task) => task.id.asc()).all(); |
| 133 | expect(all.map((task) => task.type)).toEqual(["bug", "feature"]); |
| 134 | const narrowed = yield* db.orm.public.Task.variant("Feature") |
| 135 | .where((task) => task.priority.gte(3)) |
| 136 | .orderBy((task) => task.priority.desc()) |
| 137 | .first(); |
| 138 | expect(narrowed).toMatchObject({ id: feature.id, priority: 3 }); |
| 139 | const included = yield* db.orm.public.Task.variant("Bug").include("assignee").first(); |
| 140 | expect(included?.assignee).toEqual(assignee); |
| 141 | const selected = yield* db.orm.public.Task.variant("Bug") |
| 142 | .include("assignee", (person) => person.select("name")) |
| 143 | .select("id", "title") |
| 144 | .first(); |
| 145 | expect(selected).toEqual({ |
| 146 | id: bug.id, |
| 147 | title: "Crash", |
| 148 | assignee: { name: "Sam" }, |
| 149 | }); |
| 150 | const rolledBack = yield* db |
| 151 | .transaction((tx) => |
| 152 | Effect.gen(function* () { |
| 153 | yield* tx.orm.public.Task.variant("Feature").create({ |
| 154 | title: "Undo", |
| 155 | priority: 5, |
| 156 | }); |
| 157 | return yield* tx.rollback(); |
| 158 | }), |
| 159 | ) |
| 160 | .pipe(Effect.result); |
| 161 | expect(Result.isFailure(rolledBack)).toBe(true); |
| 162 | expect(yield* db.orm.public.Task.variant("Feature").all()).toHaveLength(1); |
| 163 | expect(yield* db.orm.public.Task.variant("Feature").deleteAndCount()).toBe(1); |
| 164 | expect(yield* db.orm.public.Task.variant("Bug").deleteAndCount()).toBe(1); |
| 165 | expect(yield* db.orm.public.Task.all()).toEqual([]); |
| 166 | }), |
| 167 | ); |
| 168 | yield* stack.destroy(); |
| 169 | }), |
| 170 | { |
| 171 | tags: [ |
| 172 | "provider:neon", |
| 173 | "provider:neon:branch", |
| 174 | "provider:neon:project", |
| 175 | "provider:prisma", |
| 176 | "provider:prisma:orm", |
| 177 | "live", |
| 178 | ], |
| 179 | timeout: HOOK_TIMEOUT, |
| 180 | }, |
| 181 | ); |
| 182 | |
| 183 | const fixtureConfig = Effect.gen(function* () { |
| 184 | const path = yield* Path.Path; |
| 185 | const self = yield* path.fromFileUrl(new URL(import.meta.url)); |
| 186 | return path.join(path.dirname(self), "fixtures", "client", "prisma.config.ts"); |
| 187 | }); |
| 188 | |
| 189 | /** Deploy the fixture contract onto a fresh Neon branch and hand back a db. */ |
| 190 | const deployDatabase = (stack: { deploy: <A, E, R>(e: Effect.Effect<A, E, R>) => any }) => |
| 191 | Effect.gen(function* () { |
| 192 | const configPath = yield* fixtureConfig; |
| 193 | const { branch } = yield* stack.deploy( |
| 194 | Effect.gen(function* () { |
| 195 | const contract = yield* Prisma.Contract("client-contract", { |
| 196 | config: configPath, |
| 197 | }); |
| 198 | const project = yield* Neon.Project("PrismaClientProject"); |
| 199 | const branch = yield* Neon.Branch("PrismaClientBranch", { project }); |
| 200 | yield* Prisma.Migrate("client-migrate", { |
| 201 | url: branch.connectionUri, |
| 202 | contract, |
| 203 | }); |
| 204 | return { branch }; |
| 205 | }), |
| 206 | ); |
| 207 | return yield* PrismaPostgres.Postgres( |
| 208 | Effect.succeed(Redacted.make(branch.connectionUri as string)), |
| 209 | { contract }, |
| 210 | ); |
| 211 | }); |
| 212 | |
| 213 | test.provider( |
| 214 | "orm lane: typed CRUD, include, select, and where gating", |
| 215 | (stack) => |
| 216 | Effect.gen(function* () { |
| 217 | yield* stack.destroy(); |
| 218 | const db = yield* deployDatabase(stack); |
| 219 | |
| 220 | yield* Effect.scoped( |
| 221 | Effect.gen(function* () { |
| 222 | // create — input + row are contract-typed |
| 223 | const alice = yield* db.orm.public.User.create({ |
| 224 | email: "alice@example.com", |
| 225 | name: "Alice", |
| 226 | }); |
| 227 | expect(alice.id).toBeGreaterThan(0); |
| 228 | expect(alice.name).toEqual("Alice"); |
| 229 | |
| 230 | yield* db.orm.public.Post.create({ |
| 231 | title: "hello", |
| 232 | authorId: alice.id, |
| 233 | }); |
| 234 | |
| 235 | // include — Row gains a typed relation field |
| 236 | const withPosts = yield* db.orm.public.User.where({ id: alice.id }) |
| 237 | .include("posts") |
| 238 | .first(); |
| 239 | expect(withPosts?.posts).toHaveLength(1); |
| 240 | expect(withPosts?.posts[0]?.title).toEqual("hello"); |
| 241 | |
| 242 | // Shorthand filters keep their model field and value types. |
| 243 | // @ts-expect-error unknown fields are not valid filters |
| 244 | db.orm.public.User.where({ missing: "value" }); |
| 245 | // @ts-expect-error User.id is a number |
| 246 | db.orm.public.User.where({ id: "not-a-number" }); |
| 247 | |
| 248 | const bulk = yield* db.orm.public.Post.createAll([ |
| 249 | { title: "bulk-one", authorId: alice.id }, |
| 250 | { title: "bulk-two", authorId: alice.id }, |
| 251 | ]); |
| 252 | expect(bulk.map((post) => post.title)).toEqual(["bulk-one", "bulk-two"]); |
| 253 | const count = yield* db.orm.public.Post.createAndCount([ |
| 254 | { title: "bulk-count", authorId: alice.id }, |
| 255 | ]); |
| 256 | expect(count).toBe(1); |
| 257 | const upserted = yield* db.orm.public.User.upsert({ |
| 258 | create: { email: "alice@example.com", name: "Alice" }, |
| 259 | update: { name: "Alice" }, |
| 260 | conflictOn: { email: "alice@example.com" }, |
| 261 | }); |
| 262 | expect(upserted.id).toBe(alice.id); |
| 263 | |
| 264 | db.orm.public.User.createAll([ |
| 265 | // @ts-expect-error bulk creates accept scalar fields only |
| 266 | { email: "bulk@example.com", posts: () => undefined }, |
| 267 | ]); |
| 268 | db.orm.public.User.createAndCount([ |
| 269 | // @ts-expect-error bulk creates accept scalar fields only |
| 270 | { email: "bulk@example.com", posts: () => undefined }, |
| 271 | ]); |
| 272 | db.orm.public.User.upsert({ |
| 273 | // @ts-expect-error upsert creates accept scalar fields only |
| 274 | create: { email: "bulk@example.com", posts: () => undefined }, |
| 275 | update: {}, |
| 276 | }); |
| 277 | |
| 278 | // where callback form |
| 279 | const byEmail = yield* db.orm.public.User.where((u) => |
| 280 | u.email.eq("alice@example.com"), |
| 281 | ).first(); |
| 282 | expect(byEmail?.id).toEqual(alice.id); |
| 283 | |
| 284 | // select — Row narrows to the projection |
| 285 | const projected = yield* db.orm.public.User.select("id", "email").all(); |
| 286 | expect(projected[0]?.email).toEqual("alice@example.com"); |
| 287 | // @ts-expect-error `name` was not selected — narrowing is real |
| 288 | projected[0]?.name; |
| 289 | |
| 290 | // update requires a prior where (compile-time gate) and works |
| 291 | const renamed = yield* db.orm.public.User.where({ |
| 292 | id: alice.id, |
| 293 | }).update({ name: "Alice II" }); |
| 294 | expect(renamed?.name).toEqual("Alice II"); |
| 295 | |
| 296 | // @ts-expect-error update without where() is a type error |
| 297 | db.orm.public.User.update({ name: "nope" }); |
| 298 | |
| 299 | // laziness: one effect, two evaluations → two rows |
| 300 | const insert = db.orm.public.Post.create({ |
| 301 | title: "repeat", |
| 302 | authorId: alice.id, |
| 303 | }); |
| 304 | yield* insert; |
| 305 | yield* insert; |
| 306 | const repeats = yield* db.orm.public.Post.where({ |
| 307 | title: "repeat", |
| 308 | }).all(); |
| 309 | expect(repeats).toHaveLength(2); |
| 310 | |
| 311 | const refined = yield* db.orm.public.User.select("id") |
| 312 | .include("posts", (posts) => |
| 313 | posts |
| 314 | .select("title") |
| 315 | .orderBy((post) => post.id.desc()) |
| 316 | .limit(2), |
| 317 | ) |
| 318 | .first(); |
| 319 | expect(refined?.posts).toEqual([{ title: "repeat" }, { title: "repeat" }]); |
| 320 | const combined = yield* db.orm.public.User.select("id") |
| 321 | .include("posts", (posts) => |
| 322 | posts.combine({ |
| 323 | recent: posts |
| 324 | .select("title") |
| 325 | .orderBy((post) => post.id.desc()) |
| 326 | .limit(1), |
| 327 | total: posts.count(), |
| 328 | }), |
| 329 | ) |
| 330 | .first(); |
| 331 | expect(combined?.posts).toEqual({ |
| 332 | recent: [{ title: "repeat" }], |
| 333 | total: 6, |
| 334 | }); |
| 335 | const grouped = yield* db.orm.public.Post.groupBy("authorId") |
| 336 | .orderBy((post) => post.authorId.asc()) |
| 337 | .limit(1) |
| 338 | .aggregate((aggregate) => ({ total: aggregate.count() })); |
| 339 | expect(grouped).toEqual([{ authorId: alice.id, total: 6 }]); |
| 340 | const streamedRows = yield* db.orm.public.Post.select("title") |
| 341 | .orderBy((post) => post.id.asc()) |
| 342 | .all() |
| 343 | .stream.pipe(Stream.take(2), Stream.runCollect); |
| 344 | expect([...streamedRows]).toEqual([{ title: "hello" }, { title: "bulk-one" }]); |
| 345 | const page = yield* db.orm.public.Post.orderBy((post) => post.id.asc()) |
| 346 | .cursor({ id: bulk[0]!.id }) |
| 347 | .limit(1) |
| 348 | .all(); |
| 349 | expect(page.map((post) => post.title)).toEqual(["bulk-two"]); |
| 350 | const distinct = yield* db.orm.public.Post.distinct("title").select("title").all(); |
| 351 | expect(distinct).toHaveLength(5); |
| 352 | const distinctOn = yield* db.orm.public.Post.orderBy([ |
| 353 | (post) => post.title.asc(), |
| 354 | (post) => post.id.asc(), |
| 355 | ]) |
| 356 | .distinctOn("title") |
| 357 | .all(); |
| 358 | expect(distinctOn).toHaveLength(5); |
| 359 | expect(distinctOn.find((post) => post.title === "repeat")?.id).toBe(repeats[0]!.id); |
| 360 | expect( |
| 361 | yield* db.orm.public.Post.aggregate((aggregate) => ({ |
| 362 | total: aggregate.count(), |
| 363 | highestId: aggregate.max("id"), |
| 364 | })), |
| 365 | ).toEqual({ total: 6, highestId: repeats[1]!.id }); |
| 366 | expect( |
| 367 | yield* db.orm.public.Post.groupBy("authorId") |
| 368 | .having((aggregate) => aggregate.count().gt(6)) |
| 369 | .aggregate((aggregate) => ({ total: aggregate.count() })), |
| 370 | ).toEqual([]); |
| 371 | expect( |
| 372 | yield* db.orm.public.Post.groupBy("authorId") |
| 373 | .orderBy((post) => post.authorId.asc()) |
| 374 | .offset(1) |
| 375 | .aggregate((aggregate) => ({ total: aggregate.count() })), |
| 376 | ).toEqual([]); |
| 377 | const changed = yield* db.orm.public.Post.where({ |
| 378 | title: "repeat", |
| 379 | }).updateAll({ title: "updated" }); |
| 380 | expect(changed.map((post) => post.title)).toEqual(["updated", "updated"]); |
| 381 | expect( |
| 382 | yield* db.orm.public.Post.where({ |
| 383 | title: "updated", |
| 384 | }).updateAndCount({ title: "counted" }), |
| 385 | ).toBe(2); |
| 386 | const removed = yield* db.orm.public.Post.where({ title: "counted" }) |
| 387 | .deleteAll() |
| 388 | .stream.pipe(Stream.runCollect); |
| 389 | expect(removed).toHaveLength(2); |
| 390 | expect( |
| 391 | yield* db.orm.public.Post.where({ |
| 392 | title: "bulk-count", |
| 393 | }).deleteAndCount(), |
| 394 | ).toBe(1); |
| 395 | expect( |
| 396 | yield* db.orm.public.Post.orderBy((post) => post.id.asc()) |
| 397 | .offset(1) |
| 398 | .limit(1) |
| 399 | .first(), |
| 400 | ).toMatchObject({ title: "bulk-one" }); |
| 401 | |
| 402 | // granular constraint tags: unique violation is its own error |
| 403 | const dup = yield* db.orm.public.User.create({ |
| 404 | email: "alice@example.com", |
| 405 | }).pipe( |
| 406 | Effect.as("created" as const), |
| 407 | Effect.catchTag("Prisma.UniqueViolationError", (error) => |
| 408 | Effect.succeed(error.sqlState === "23505" ? ("unique" as const) : ("other" as const)), |
| 409 | ), |
| 410 | ); |
| 411 | expect(dup).toEqual("unique"); |
| 412 | |
| 413 | // ...and so is a foreign-key violation |
| 414 | const fk = yield* db.orm.public.Post.create({ |
| 415 | title: "orphan", |
| 416 | authorId: 999_999, |
| 417 | }).pipe( |
| 418 | Effect.as("created" as const), |
| 419 | Effect.catchTag("Prisma.ForeignKeyViolationError", () => |
| 420 | Effect.succeed("fk-violation" as const), |
| 421 | ), |
| 422 | ); |
| 423 | expect(fk).toEqual("fk-violation"); |
| 424 | |
| 425 | // ORM misuse surfaces as the ORM category tag with a typed code. |
| 426 | // (`count()` is refinement-only; even prisma's own types reject |
| 427 | // this call, so the cast is deliberate — the test pins the |
| 428 | // *runtime* classification of the thrown ORM.* error.) |
| 429 | const misuse = yield* db |
| 430 | .use((c) => c.orm.public.User.count() as unknown as PromiseLike<unknown>) |
| 431 | .pipe( |
| 432 | Effect.as("ok" as const), |
| 433 | Effect.catchTag("Prisma.OrmError", (error) => Effect.succeed(error.code)), |
| 434 | ); |
| 435 | expect(misuse).toEqual("ORM.INCLUDE_INVALID"); |
| 436 | |
| 437 | // delete gating + row-returning delete |
| 438 | const deleted = yield* db.orm.public.User.where({ |
| 439 | email: "nobody@example.com", |
| 440 | }).delete(); |
| 441 | expect(deleted).toBeNull(); |
| 442 | }), |
| 443 | ); |
| 444 | |
| 445 | yield* stack.destroy(); |
| 446 | }), |
| 447 | { |
| 448 | tags: [ |
| 449 | "provider:neon", |
| 450 | "provider:neon:branch", |
| 451 | "provider:neon:project", |
| 452 | "provider:prisma", |
| 453 | "provider:prisma:orm", |
| 454 | "live", |
| 455 | ], |
| 456 | timeout: HOOK_TIMEOUT, |
| 457 | }, |
| 458 | ); |
| 459 | |
| 460 | test.provider( |
| 461 | "sql lane, streaming, and transactions", |
| 462 | (stack) => |
| 463 | Effect.gen(function* () { |
| 464 | yield* stack.destroy(); |
| 465 | const db = yield* deployDatabase(stack); |
| 466 | |
| 467 | const statement = yield* Effect.scoped( |
| 468 | Effect.gen(function* () { |
| 469 | const bob = yield* db.orm.public.User.create({ |
| 470 | email: "bob@example.com", |
| 471 | name: "Bob", |
| 472 | }); |
| 473 | |
| 474 | // sql builder lane: pure plan, Effect executor, typed rows |
| 475 | const plan = db.sql.public.user.select("id", "email").build(); |
| 476 | const rows = yield* db.execute(plan); |
| 477 | expect(rows.map((r) => r.email)).toContain("bob@example.com"); |
| 478 | |
| 479 | // stream lane (fresh execution per run) |
| 480 | const streamed = yield* Stream.runCollect(db.stream(plan)); |
| 481 | expect([...streamed].length).toBe(rows.length); |
| 482 | |
| 483 | const prepared = yield* db.prepare({ email: "pg/text@1" }, (sql, params) => |
| 484 | sql.public.user |
| 485 | .select("id", "email") |
| 486 | .where((fields, fns) => fns.eq(fields.email, params.email)) |
| 487 | .build(), |
| 488 | ); |
| 489 | const preparedRead = prepared.query({ email: "bob@example.com" }); |
| 490 | expect(yield* preparedRead).toEqual([{ id: bob.id, email: "bob@example.com" }]); |
| 491 | expect(yield* preparedRead).toEqual([{ id: bob.id, email: "bob@example.com" }]); |
| 492 | expect([...(yield* Stream.runCollect(preparedRead.stream))]).toEqual([ |
| 493 | { id: bob.id, email: "bob@example.com" }, |
| 494 | ]); |
| 495 | expect(yield* prepared.query({ email: "missing@example.com" })).toEqual([]); |
| 496 | const rename = yield* db.prepare({ id: "pg/int4@1", name: "pg/text@1" }, (_sql, params) => |
| 497 | db.raw.sql`UPDATE "user" SET name = ${params.name} WHERE id = ${params.id}` |
| 498 | .affectedCount() |
| 499 | .build(), |
| 500 | ); |
| 501 | expect(yield* rename.execute({ id: bob.id, name: "Prepared Bob" })).toEqual({ |
| 502 | affectedRows: 1, |
| 503 | }); |
| 504 | expect((yield* db.orm.public.User.where({ id: bob.id }).first())?.name).toBe( |
| 505 | "Prepared Bob", |
| 506 | ); |
| 507 | const unused = yield* db |
| 508 | .prepare({ unused: "pg/text@1" }, (sql) => sql.public.user.select("id").build()) |
| 509 | .pipe( |
| 510 | Effect.as("unexpected success"), |
| 511 | Effect.catchTag("Prisma.RuntimeError", (error) => Effect.succeed(error.code)), |
| 512 | ); |
| 513 | expect(unused).toBe("RUNTIME.PREPARE_UNUSED_PARAM"); |
| 514 | |
| 515 | // transaction: commit path |
| 516 | const committed = yield* db.transaction((tx) => |
| 517 | Effect.gen(function* () { |
| 518 | const post = yield* tx.orm.public.Post.create({ |
| 519 | title: "tx-post", |
| 520 | authorId: bob.id, |
| 521 | }); |
| 522 | const read = yield* tx.orm.public.Post.where({ |
| 523 | id: post.id, |
| 524 | }).first(); |
| 525 | return read?.title; |
| 526 | }), |
| 527 | ); |
| 528 | expect(committed).toEqual("tx-post"); |
| 529 | const persisted = yield* db.orm.public.Post.where({ |
| 530 | title: "tx-post", |
| 531 | }).all(); |
| 532 | expect(persisted).toHaveLength(1); |
| 533 | |
| 534 | // transaction: explicit rollback is a typed failure and undoes writes |
| 535 | const rolledBack = yield* db |
| 536 | .transaction((tx) => |
| 537 | Effect.gen(function* () { |
| 538 | yield* tx.orm.public.Post.create({ |
| 539 | title: "rollback-post", |
| 540 | authorId: bob.id, |
| 541 | }); |
| 542 | const update = yield* tx.prepare( |
| 543 | { id: "pg/int4@1", name: "pg/text@1" }, |
| 544 | (_sql, params) => |
| 545 | db.raw.sql`UPDATE "user" SET name = ${params.name} WHERE id = ${params.id}` |
| 546 | .affectedCount() |
| 547 | .build(), |
| 548 | ); |
| 549 | expect(yield* update.execute({ id: bob.id, name: "Rolled back" })).toEqual({ |
| 550 | affectedRows: 1, |
| 551 | }); |
| 552 | const read = yield* tx.prepare({ id: "pg/int4@1" }, (sql, params) => |
| 553 | sql.public.user |
| 554 | .select("name") |
| 555 | .where((fields, fns) => fns.eq(fields.id, params.id)) |
| 556 | .build(), |
| 557 | ); |
| 558 | expect(yield* read.query({ id: bob.id })).toEqual([{ name: "Rolled back" }]); |
| 559 | return yield* tx.rollback(); |
| 560 | }), |
| 561 | ) |
| 562 | .pipe( |
| 563 | Effect.as("committed" as const), |
| 564 | Effect.catchTag("Prisma.RollbackError", () => Effect.succeed("rolled-back" as const)), |
| 565 | ); |
| 566 | expect(rolledBack).toEqual("rolled-back"); |
| 567 | expect((yield* db.orm.public.User.where({ id: bob.id }).first())?.name).toBe( |
| 568 | "Prepared Bob", |
| 569 | ); |
| 570 | expect(yield* db.orm.public.Post.where({ title: "rollback-post" }).all()).toHaveLength(0); |
| 571 | |
| 572 | // transaction: a failing effect rolls back too |
| 573 | const failed = yield* Effect.result( |
| 574 | db.transaction((tx) => |
| 575 | Effect.gen(function* () { |
| 576 | yield* tx.orm.public.Post.create({ |
| 577 | title: "failed-post", |
| 578 | authorId: bob.id, |
| 579 | }); |
| 580 | // unique violation aborts the transaction |
| 581 | yield* tx.orm.public.User.create({ |
| 582 | email: "bob@example.com", |
| 583 | }); |
| 584 | }), |
| 585 | ), |
| 586 | ); |
| 587 | expect(Result.isFailure(failed)).toBe(true); |
| 588 | expect(yield* db.orm.public.Post.where({ title: "failed-post" }).all()).toHaveLength(0); |
| 589 | return prepared; |
| 590 | }), |
| 591 | ); |
| 592 | |
| 593 | const later = yield* statement.query({ email: "bob@example.com" }).pipe(Effect.scoped); |
| 594 | expect(later.map((row) => row.email)).toEqual(["bob@example.com"]); |
| 595 | const concurrent = yield* Effect.all( |
| 596 | ["bob@example.com", "missing@example.com"].map((email) => |
| 597 | statement.query({ email }).pipe(Effect.scoped), |
| 598 | ), |
| 599 | { concurrency: 2 }, |
| 600 | ); |
| 601 | expect(concurrent.map((rows) => rows.length)).toEqual([1, 0]); |
| 602 | |
| 603 | yield* stack.destroy(); |
| 604 | }), |
| 605 | { |
| 606 | tags: [ |
| 607 | "provider:neon", |
| 608 | "provider:neon:branch", |
| 609 | "provider:neon:project", |
| 610 | "provider:prisma", |
| 611 | "provider:prisma:orm", |
| 612 | "live", |
| 613 | ], |
| 614 | timeout: HOOK_TIMEOUT, |
| 615 | }, |
| 616 | ); |