sql-js / sql.js

A javascript library to run SQLite on the web.
http://sql.js.org
Other
12.63k stars 1.06k forks source link

missing docs: web worker #595

Open skyqrose opened 1 week ago

skyqrose commented 1 week ago

The README says

If you don't want to run CPU-intensive SQL queries in your main application thread, you can use the more limited WebWorker API.

It gives a couple examples and caveats, but I need a lot more information to use the feature. I need a list of messages I can pass, what the worker will pass back, and a description of what the limitations are. There's no page on the website or GitHub wiki about this.

Other issues:

Because of these missing docs, for my app I'm going to write my own custom web worker using the normal SQL.js API.

I'll also highlight the proposal to remove the whole web worker API, which would be a way to avoid this whole issue (though you'd still want the docs to have some hints about how/why to write a custom web worker): https://github.com/sql-js/sql.js/issues/377

lovasoa commented 1 week ago

Hi ! That would be very useful indeed ! Do you want to write this documentation ?

The underlying worker code is quite simple; it's here: https://github.com/sql-js/sql.js/blob/master/src/worker.js

scola84 commented 4 days ago

Would it be possible to add getRowsModified to the worker?

lovasoa commented 3 days ago

Yes! The code is here: https://github.com/sql-js/sql.js/blob/master/src/worker.js you can open a pull request. Don't forget to add a test for it !

scola84 commented 3 days ago

Alright, I'll look into it next week.

If you look at for example https://github.com/brianc/node-postgres/blob/master/packages/pg/lib/result.js#L13, you can see that the rowCount is part of every query result. That would mean I could also implement something like:

return postMessage({
  id: data["id"],
  results: db.exec(data["sql"], data["params"], config),
  rowsModified: db.getRowsModified()
});

Do you think that is a good idea? (Maybe rowsModified should be in results?) Or would you prefer just a getRowsModified action?

lovasoa commented 3 days ago

I think a separate action, preventing an new wasm call after each request, is better.

Just out of curiosity, do you have very strict performance requirements, or something else that prevents you from just using SQLite's built-in changes sql function?

https://www.sqlite.org/lang_corefunc.html#changes