WiseLibs / better-sqlite3

The fastest and simplest library for SQLite3 in Node.js.
MIT License
5.4k stars 393 forks source link

How to directly use the better-sqlite3.node file in the Electron node process #1260

Closed Super-Badmen-Viper closed 3 days ago

Super-Badmen-Viper commented 5 days ago

I want to directly call the recompiled better-sqlite3.node in the Electron node process (such as main. js/ts) and specify the database path to start using it: This is my code: image

const betterSqlite3 = require(path.resolve('resources/better_sqlite3.node'));
const db = new betterSqlite3(navidrome_db);

But this line of code, It will report an error:

const db = new betterSqlite3(navidrome_db); // TypeError: betterSqlite3 is not a constructor:

How to directly use the better-sqlite3.node file in the Electron node process and specify the database path to start using it ?

Super-Badmen-Viper commented 5 days ago

Here are the reasons why I want to directly use the better-sqlite3.node file in the Electron node process: I have been using better-sqlite3 to develop my open source project for 9 months now. Previously, I used better-sqlite3 in the rendering process of Electron (because it is aimed at individual users, for the sake of development efficiency and personal preferences (typescript development paradigm is more comfortable than nodejs), I chose to disable security policies directly and set the rendering layer to directly access nodes), which is very comfortable to use without any problems But I am using better-sqlite3 in the Electron node process, and I cannot directly pass const db=require ('beter-sqlite3 ') (navidrome_deb); To call it, this will result in an error:

→ E:\0_XiangCheng_WorkSpace\NSMusicS\build\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\build\Debug\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\build\Release\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\out\Debug\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\Debug\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\out\Release\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\Release\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\build\default\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\compiled\20.17.0\win32\x64\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\addon-build\release\install-root\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\addon-build\debug\install-root\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\addon-build\default\install-root\better_sqlite3.node
→ E:\0_XiangCheng_WorkSpace\NSMusicS\lib\binding\node-v128-win32-x64\better_sqlite3.node
at bindings (E:\0_XiangCheng_WorkSpace\NSMusicS\dist\background.js:38619:13)
at new Database (E:\0_XiangCheng_WorkSpace\NSMusicS\dist\background.js:39234:69)
at Database (E:\0_XiangCheng_WorkSpace\NSMusicS\dist\background.js:39196:16)
at Set_ReadLocalMusicInfo_Add_LocalSqlite 

The method I used to solve the above error was to place the recompiled better-sqlite3.node in the build folder of the project, but this allowed me to use it personally without any problems. When I packaged my application and uploaded it to GitHub, I found that my packaging program, Electron Builder, would not package the beta_Sqlite3.node that I manually configured (possibly because it had already packaged better-sqlite3 for the Electron rendering layer to use, instead of packaging a duplicate copy for the node layer). This would cause other users to install my program, and they would not be able to use the better-sqlite3.node in the Electron node process to use the local version I wrote. Music library import function

Super-Badmen-Viper commented 3 days ago

I have already self reviewed the source code for this issue: https://github.com/WiseLibs/better-sqlite3/blob/master/lib/database.js image

Resolve by viewing API documentation: https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md#new-databasepath-options image

Just specify the options option and can use better-sqlite3.node directly

const Database = require('better-sqlite3');
const db = new Database(navidrome_db, {
    nativeBinding: path.resolve('resources/better_sqlite3.node')
});

If someone is using Electron for development and has enabled webPreferences: nodeIntegration: true at the node layer,. So it is very likely that better sqlite3 cannot be used in the node layer and can only be used in the rendering layer. If there is a need to mix better-sqlite3 in the Electron rendering layer and node layer, you can meet your needs by specifying the nativeBinding option in the above code