Effect CommunityEC
Effect Community16mo ago
1 reply
phischer

Fixing `@effect/sql-sqlite-node` for Next.js Compatibility with ESM Patch

Was trying to use @effect/sql with @effect/sql-sqlite-node for the first time. Running some examples with tsx works, but trying to get it to work in Next.js I needed to patch the SqliteClient.js:

diff --git a/dist/esm/SqliteClient.js b/dist/esm/SqliteClient.js
index 7123c9baa0435414c5cd65e161fd8943e7960aef..8577204cc9e2ddc44b5bd7a9b9cd6de1cecba3c0 100644
--- a/dist/esm/SqliteClient.js
+++ b/dist/esm/SqliteClient.js
@@ -34,7 +34,8 @@ export const make = options => Effect.gen(function* () {
   const makeConnection = Effect.gen(function* () {
     const scope = yield* Effect.scope;
     const db = new Sqlite(options.filename, {
-      readonly: options.readonly ?? false
+      readonly: options.readonly ?? false,
+      nativeBinding: require('better-sqlite3/build/Release/better_sqlite3.node')
     });
     yield* Scope.addFinalizer(scope, Effect.sync(() => db.close()));
     if (options.disableWAL !== true) {


I don't get why the ESM version is being used.

Without the patch I would get this error:

⨯ (FiberFailure) TypeError: Cannot read properties of undefined (reading 'indexOf')
    at Function.getFileName (webpack-internal:///(rsc)/./node_modules/.pnpm/bindings@1.5.0/node_modules/bindings/bindings.js:178:16)
    at bindings (webpack-internal:///(rsc)/./node_modules/.pnpm/bindings@1.5.0/node_modules/bindings/bindings.js:82:48)
    at new Database (webpack-internal:///(rsc)/./node_modules/.pnpm/better-sqlite3@11.3.0/node_modules/better-sqlite3/lib/database.js:48:159)
    at eval (webpack-internal:///(rsc)/./node_modules/.pnpm/@effect+sql-sqlite-node@0.12.6_@effect+platform@0.65.5_@effect+schema@0.73.4_effect@3.8.3__ef_ldn4dagsglbnyizugrwxntnomi/node_modules/@effect/sql-sqlite-node/dist/esm/SqliteClient.js:61:16)


Anyone got an idea?
Was this page helpful?