Skip to content

Commit c1aaefb

Browse files
0xh3xtim-smart
andauthored
Add SafeIntegers support to @effect/sql-sqlite-bun client (#6016)
Co-authored-by: Tim Smart <hello@timsmart.co>
1 parent 9a96b87 commit c1aaefb

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

.changeset/nice-boats-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect/sql-sqlite-bun": patch
3+
---
4+
5+
feat(sql-sqlite-bun): add SafeIntegers support

packages/sql-sqlite-bun/examples/Client.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Reactivity } from "@effect/experimental"
22
import { BunFileSystem } from "@effect/platform-bun"
33
import { FileSystem } from "@effect/platform/FileSystem"
44
import { SqliteClient } from "@effect/sql-sqlite-bun"
5+
import * as SqlClient from "@effect/sql/SqlClient"
56
import { describe, expect, test } from "bun:test"
67
import { Effect, pipe } from "effect"
78

@@ -28,4 +29,40 @@ describe("Client", () => {
2829
{ id: 2, name: "world" }
2930
])
3031
}).pipe(Effect.scoped, Effect.runPromise))
32+
33+
test("SafeIntegers returns bigint for large integers", () =>
34+
Effect.gen(function*() {
35+
const sql = yield* makeClient
36+
const big = 9007199254740993n // 2^53 + 1
37+
38+
yield* sql`CREATE TABLE safe_int_test (id BIGINT)`
39+
yield* sql`INSERT INTO safe_int_test (id) VALUES (${big})`
40+
41+
const resultSafe = (yield* sql`SELECT id FROM safe_int_test`.pipe(
42+
Effect.provideService(SqlClient.SafeIntegers, true)
43+
))[0]?.id
44+
expect(typeof resultSafe).toBe("bigint")
45+
expect(resultSafe).toBe(big)
46+
47+
const resultDefault = (yield* sql`SELECT id FROM safe_int_test`)[0]?.id
48+
expect(typeof resultDefault).not.toBe("bigint")
49+
}).pipe(Effect.scoped, Effect.runPromise))
50+
51+
test("SafeIntegers works with values query", () =>
52+
Effect.gen(function*() {
53+
const sql = yield* makeClient
54+
const big = 9007199254740993n // 2^53 + 1
55+
56+
yield* sql`CREATE TABLE safe_int_values_test (id BIGINT)`
57+
yield* sql`INSERT INTO safe_int_values_test (id) VALUES (${big})`
58+
59+
const resultSafe = (yield* sql`SELECT id FROM safe_int_values_test`.values.pipe(
60+
Effect.provideService(SqlClient.SafeIntegers, true)
61+
))[0]?.[0]
62+
expect(typeof resultSafe).toBe("bigint")
63+
expect(resultSafe).toBe(big)
64+
65+
const resultDefault = (yield* sql`SELECT id FROM safe_int_values_test`.values)[0]?.[0]
66+
expect(typeof resultDefault).not.toBe("bigint")
67+
}).pipe(Effect.scoped, Effect.runPromise))
3168
})

packages/sql-sqlite-bun/src/SqliteClient.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,32 @@ export const make = (
102102
sql: string,
103103
params: ReadonlyArray<unknown> = []
104104
) =>
105-
Effect.try({
106-
try: () => (db.query(sql).all(...(params as any)) ?? []) as Array<any>,
107-
catch: (cause) => new SqlError({ cause, message: "Failed to execute statement" })
105+
Effect.withFiberRuntime<Array<any>, SqlError>((fiber) => {
106+
const useSafeIntegers = Context.get(fiber.currentContext, Client.SafeIntegers)
107+
const statement = db.query(sql)
108+
// @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627
109+
statement.safeIntegers(useSafeIntegers)
110+
try {
111+
return Effect.succeed((statement.all(...(params as any)) ?? []) as Array<any>)
112+
} catch (cause) {
113+
return Effect.fail(new SqlError({ cause, message: "Failed to execute statement" }))
114+
}
108115
})
109116

110117
const runValues = (
111118
sql: string,
112119
params: ReadonlyArray<unknown> = []
113120
) =>
114-
Effect.try({
115-
try: () => (db.query(sql).values(...(params as any)) ?? []) as Array<any>,
116-
catch: (cause) => new SqlError({ cause, message: "Failed to execute statement" })
121+
Effect.withFiberRuntime<Array<any>, SqlError>((fiber) => {
122+
const useSafeIntegers = Context.get(fiber.currentContext, Client.SafeIntegers)
123+
const statement = db.query(sql)
124+
// @ts-ignore bun-types missing safeIntegers method, fixed in https://github.com/oven-sh/bun/pull/26627
125+
statement.safeIntegers(useSafeIntegers)
126+
try {
127+
return Effect.succeed((statement.values(...(params as any)) ?? []) as Array<any>)
128+
} catch (cause) {
129+
return Effect.fail(new SqlError({ cause, message: "Failed to execute statement" }))
130+
}
117131
})
118132

119133
return identity<SqliteConnection>({

0 commit comments

Comments
 (0)