Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[how solve] Sequalize + Sqlite + Bun compile single Executable #17994

@kelvinauta

Description

@kelvinauta

Although there are two bugs I will report, this isn't strictly a bug report since Sequelize will be changing Dialects in version 7. Therefore, this is more of a guide on how to solve a problem that arises when compiling a single, standalone executable file with Bun using Sequelize 6 and sqlite3. If anyone else encounters this problem, I hope this Issue helps them.

Problem Description:

Bun has the ability to compile an executable; however, after compiling and running the file, you will get an error from Sequelize asking you to manually install sqlite3. This is caused by two issues. First, I will describe the problems, and then I will create a section on how to solve them.

Note: Installing sqlite3 with any package manager does not solve the problem, nor does installing sqlite3 on the system.

Problem with Sequelize and require(dialectModulePath)

Sequelize 6, in the sequelize/lib/dialects/abstract/connection-manager.js file, attempts to perform a runtime require on a "dialectModulePath" variable. However, since this happens at runtime, the Bun bundler cannot interpret what exactly needs to be required, so Bun does not include the module in the executable. As a result, the require depends on the executable being run from within the project directory. In other words, the executable only runs successfully inside the project folder and fails anywhere else, because within the project it can resolve the require as the sqlite3 library is present in node_modules.
This obviously defeats the purpose of creating an executable file; the idea is for it to be standalone and not dependent on its location or the presence of a node_modules directory. I will explain a simple solution later.

Problem with node-sqlite3

node-sqlite3 is a dependency of Sequelize 6 and presents a problem similar to the previous one, but it relates to importing a NAPI-type binary. I detailed the error in this Issue.

Solutions

1.- SQLite Dialect

The solution to the first point is simple: you must manually pass the sqlite3 module to Sequelize, like this:

import {Sequelize} from "sequelize";
import sqlite3 from "sqlite3"
new Sequelize("/tmp/yourdb.db", {
    dialect: "sqlite",
    dialectModule: sqlite3 // <--- HERE
});

In my opinion, this should be in the documentation here, however, I was able to find this option in the reference at this link under options.dialectModule.

2.- Patch sqlite3

Okay, this solution is more of a hack, but fortunately, Bun provides a native patcher, which allows you to patch code in node_modules. Anyway, I'll give you the patch so you don't have to create it yourself.

The Patch:
In the root of your project, create this file: patches/[email protected]. It can actually be in another folder, but that's the default one. The file must have the same name and version as your dependency in package.json.

Put this in the patch file:

--- a/lib/sqlite3-binding.js
+++ b/lib/sqlite3-binding.js
@@ -1 +1,5 @@
-module.exports = require('bindings')('node_sqlite3.node');
+if(Boolean(globalThis.Bun)){
+    module.exports = require("../build/Release/node_sqlite3.node")
+}else{
+    module.exports = require('bindings')('node_sqlite3.node');
+}

What this patch does is, if the app is being run with Bun, it performs a direct require of the necessary binary, node_sqlite3.node. Fortunately, Bun natively supports importing NAPI binaries.

NOTE: This patch assumes the binary is always located at node_modules/sqlite3/build/Release/node_sqlite3.node. Check your node_modules and ensure your binary is in the same directory, as this binary is created during installation and its path may vary depending on the system where it's compiled.

To Apply the Patch:
Go to your package.json and add "patchedDependencies" to the root of the JSON object.

{  
    "dependencies":{
        "sequelize": "^6.37.5",
        "sqlite3": "^5.1.7",
    },
    "patchedDependencies": {
        "[email protected]": "patches/[email protected]"
    }
}

(You only need to add patchedDependencies; I included dependencies just to show where it goes.)

Finally, simply run bun install, and Bun will apply your patch.

After all these steps, you can now compile:
bun build --compile --outfile myExecutable src/myentrypoint.js

Context:

  • Bun 1.2.23
  • sequelize 6.37.5
  • sqlite3 5.1.7
  • OS Arch Linux

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approvalBug reports that have not been verified yet, or feature requests that have not been accepted yet

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions