I’ve been trying to add sqldelight to my jetpack c...
# compose-web
r
I’ve been trying to add sqldelight to my jetpack compose web’s project and it’s giving me the following error
Copy code
Module not found: Error: Can't resolve 'fs' in '/Users/rahulrawat/Desktop/Projects/web/Address/build/js/node_modules/sql.js/dist'
Has anyone faced this and been able to resolve?
b
You cannot connect to db from the browser environment. It's not compose-web specific limitation. Sqldelight for kotlin js is meant to be used in nodejs server environment.
👍 2
h
Thats not true, you can use sqldelight from the browser, see https://github.com/hfhbd/ComposeTodo/tree/main/web
But its not a persistent db!
b
Wait what? How does it connect to db straight from the browser sandbox?
h
It is a in memory db. Yes, you dont have direct access to an external db, because there is no JDBC (or similar api), you often can't access it (eg db behind a firewall/internal network) and it is not safe to store the db credentials in JS code.
b
Can you point me to a line of code where sqldelight is actually used in that project? Couldn't find it myself. Also I'm still at a loss as to how is this better than LocalStorage?
What am I missing here? 😀
h
I use sqldelight as the client db, so it is defined in clients module: https://github.com/hfhbd/ComposeTodo/tree/main/clients This allows you to use the same shared repository in each client, Android and iOS with a persistent db, web with an in memory. LocalStorage is persistent, but you need a new web based implementation.
r
@hfhbd What about the issue I am facing?
b
It's solved in the app he linked
h
Gradle:
Copy code
implementation("com.squareup.sqldelight:sqljs-driver:1.5.3")
implementation(devNpm("copy-webpack-plugin", "9.1.0"))
Webpack: https://github.com/hfhbd/ComposeTodo/tree/main/web/webpack.config.d
b
Well not solved. More like masked as he's just stubbing fs implementation so anything actually using that module will error when invoked
r
So should I also remove my sqljs driver from the multiplatform module? Or should I just include the aforementioned gradle dependencies in the client?
b
I'd avoid any sql attempts on the browser clients.
b
But it's not the nodejs fs module that the driver expects. Also its a sandboxed and quite restricted file system.
h
True, but it works. The question is, whether you want to implement a low level "db"/repo for your web app or share an (existing) repo for all clients, but use another frameworks/extra db 🤷
b
Don't get me wrong, I'm impressed how much you CAN do in the browser. All I'm saying is that generally doing sql in the browser is simply not recommended.
Sqldelight should provide a module for browser kotlin based on IndexedDB somehow. That way you at least avoid having to pull sql wasm module each page load...
h
Do you know WebSQL? 😄 Yeah, persistence is a big problem! Sqldelight 2.0 wants to support it by using a wrapper, which persists the sqlite db file into indexedDB... so a db inside a db
😆 2
b

https://c.tenor.com/EYPJjOVJOHYAAAAM/inception-deeper.gif

Wasn't aware of WebSQL spec. Lucky gem!
h
It is deprecated -.-
🤦 1
286 Views