-
-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
question: .function API with user defiend async function/Promise ? #319
Comments
I realised this is hard in better-sqlite3, since it uses sync API. However I still want to see some ideas on this. |
You should never keep SQLite3 transactions open across event loop ticks in Node.js. SQLite3 serializes all statements/transactions. This means, if you try to run an asynchronous function within an SQL statement, you'll block all other statements/transactions across your entire program. Instead, you should gather all information required for your transaction (reading files, waiting on sockets, etc.) BEFORE you start the transaction. Then, when everything is ready, run the transaction synchronously, within a single event loop tick. |
We might get to jail for this if the JavaScript police catches us, but I'm taking the risk. If you absolutely know what you are doing and there is absolutely no other (proper) way, then you could use https://github.com/ForbesLindesay/sync-rpc which essentially abuses This has very limited use-cases but it can be useful. For example I would never recommend this at all in a server side application with multiple clients. However, in my case it's an Electron app and the user of said app can do cool things with SQL. On their own computer. In their private space. No JavaScript police in sight. There's a dedicated process/thread with a readonly SQLite client for these user-issued queries (shout outs to #311). So in this case performance is not a key concern. The fact that the thing you're doing is possible at all is the main concern. It doesn't matter much if it takes 10ms or 10s. You have been warned. |
To whoever it may concern: spawning a process for every single call is not a great idea. Shockingly, I know. https://github.com/mmhunter/node-sync-ipc does a similar thing but uses synchronous pipe operations to communicate with another process. This is not a proper benchmark but the order of magnitude roughly looks like this (using a
So depending on your use-case these might be acceptable numbers or not. |
Looks like someone found a way to use Worker Threads instead of sub processes. Seems to perform reasonably well if you really need to use an async API and you have no alternative https://github.com/un-ts/synckit |
when a github comment starts like this, you know it's gonna be good 😎 |
Can I make sqlite3 work with user defined async function ?
sth. like
This will result in
TypeError: User-defined function foo() returned an invalid value
for now. Any work around for this ?The text was updated successfully, but these errors were encountered: