Open History to see the last commit
1import { LibsqlClient } from "@effect/sql-libsql"
2import { assert, describe, it, layer } from "@effect/vitest"
3import { Effect, Exit, Layer } from "effect"
4import * as Reactivity from "effect/unstable/reactivity/Reactivity"
5import { LibsqlContainer } from "./util.ts"
6
7const Migrations = Layer.effectDiscard(
8 LibsqlClient.LibsqlClient.pipe(
9 Effect.andThen((sql) =>
10 Effect.acquireRelease(
11 sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`,
12 () => sql`DROP TABLE test;`.pipe(Effect.ignore)
13 )
14 )
15 )
16)
17
18describe("Client", () => {
19 it.effect("keeps transactions isolated between clients", () =>
20 Effect.gen(function*() {
21 const a = yield* LibsqlClient.make({ url: ":memory:" })
22 const b = yield* LibsqlClient.make({ url: ":memory:" })
23
24 yield* a`CREATE TABLE marker (owner TEXT NOT NULL)`
25 yield* b`CREATE TABLE marker (owner TEXT NOT NULL)`
26 yield* a`INSERT INTO marker VALUES ('A')`
27 yield* b`INSERT INTO marker VALUES ('B')`
28
29 const rows = yield* a.withTransaction(b`SELECT owner FROM marker`)
30 assert.deepStrictEqual(rows, [{ owner: "B" }])
31 }).pipe(
32 Effect.scoped,
33 Effect.provide(Reactivity.layer)
34 ))
35
36 it.effect("releases transaction serialization after begin fails", () => {
37 let transactionCalls = 0
38 const transaction = {
39 execute: () => Promise.resolve({ rows: [] }),
40 commit: () => Promise.resolve(),
41 rollback: () => Promise.resolve()
42 }
43 const liveClient = {
44 execute: () => Promise.resolve({ rows: [] }),
45 transaction: () => {
46 transactionCalls++
47 return transactionCalls === 1
48 ? Promise.reject(new Error("transient begin failure"))
49 : Promise.resolve(transaction)
50 }
51 }
52
53 return Effect.gen(function*() {
54 const client = yield* LibsqlClient.make({ liveClient: liveClient as any })
55 const first = yield* Effect.exit(client.withTransaction(Effect.void))
56 assert.isTrue(Exit.isFailure(first))
57
58 yield* Effect.forkChild(client.withTransaction(Effect.void))
59 yield* Effect.yieldNow
60
61 assert.strictEqual(transactionCalls, 2)
62 }).pipe(
63 Effect.scoped,
64 Effect.provide(Reactivity.layer)
65 )
66 })
67
68 // Each test recreates the same tables.
69 layer(LibsqlContainer.layerClient, { timeout: "30 seconds", concurrent: false })("shared tables", (it) => {
70 it.effect("should work", () =>
71 Effect.gen(function*() {
72 const sql = yield* LibsqlClient.LibsqlClient
73 let response = yield* sql`INSERT INTO test (name) VALUES ('hello')`
74 assert.deepStrictEqual(response, [])
75 response = yield* sql`SELECT * FROM test`
76 assert.deepStrictEqual(response, [{ id: 1, name: "hello" }])
77 response = yield* sql`SELECT * FROM test`
78 assert.deepStrictEqual(yield* sql`select * from test`.values, [
79 [1, "hello"]
80 ])
81 }).pipe(Effect.provide(Migrations)))
82
83 it.effect("should work with raw", () =>
84 Effect.gen(function*() {
85 const sql = yield* LibsqlClient.LibsqlClient
86 let response: any
87 response = yield* sql`CREATE TABLE test2 (id INTEGER PRIMARY KEY, name TEXT)`.raw
88 yield* Effect.addFinalizer(() => sql`DROP TABLE test2;`.pipe(Effect.ignore))
89 assert.deepStrictEqual(response.toJSON(), {
90 columnTypes: [],
91 columns: [],
92 lastInsertRowid: null,
93 rows: [],
94 rowsAffected: 0
95 })
96 response = yield* sql`INSERT INTO test (name) VALUES ('hello')`.raw
97 assert.deepStrictEqual(response.toJSON(), {
98 columnTypes: [],
99 columns: [],
100 lastInsertRowid: "1",
101 rows: [],
102 rowsAffected: 1
103 })
104 response = yield* sql`SELECT * FROM test`.raw
105 assert.deepStrictEqual(response.toJSON(), {
106 columnTypes: ["INTEGER", "TEXT"],
107 columns: ["id", "name"],
108 lastInsertRowid: null,
109 rows: [[1, "hello"]],
110 rowsAffected: 0
111 })
112 }).pipe(Effect.provide(Migrations)))
113
114 it.effect("withTransaction", () =>
115 Effect.gen(function*() {
116 const sql = yield* LibsqlClient.LibsqlClient
117 yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`)
118 const rows = yield* sql`SELECT * FROM test`
119 assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }])
120 }).pipe(Effect.provide(Migrations)))
121
122 it.effect("withTransaction rollback", () =>
123 Effect.gen(function*() {
124 const sql = yield* LibsqlClient.LibsqlClient
125 yield* sql`INSERT INTO test (name) VALUES ('hello')`.pipe(
126 Effect.andThen(Effect.fail("boom")),
127 sql.withTransaction,
128 Effect.ignore
129 )
130 const rows = yield* sql`SELECT * FROM test`
131 assert.deepStrictEqual(rows, [])
132 }).pipe(Effect.provide(Migrations)))
133
134 it.effect("withTransaction nested", () =>
135 Effect.gen(function*() {
136 const sql = yield* LibsqlClient.LibsqlClient
137 const stmt = sql`INSERT INTO test (name) VALUES ('hello')`
138
139 yield* stmt.pipe(Effect.andThen(() => stmt.pipe(sql.withTransaction)), sql.withTransaction)
140 const rows = yield* sql<{ total_rows: number }>`select count(*) as total_rows FROM test`
141 assert.deepStrictEqual(rows.at(0)?.total_rows, 2)
142 }).pipe(Effect.provide(Migrations)))
143
144 it.effect("withTransaction nested rollback", () =>
145 Effect.gen(function*() {
146 const sql = yield* LibsqlClient.LibsqlClient
147 const stmt = sql`INSERT INTO test (name) VALUES ('hello')`
148
149 yield* stmt.pipe(
150 Effect.andThen(() => stmt.pipe(Effect.andThen(Effect.fail("boom")), sql.withTransaction, Effect.ignore)),
151 sql.withTransaction
152 )
153 const rows = yield* sql<{ total_rows: number }>`select count(*) as total_rows FROM test`
154 assert.deepStrictEqual(rows.at(0)?.total_rows, 1)
155 }).pipe(Effect.provide(Migrations)))
156 })
157})