https://kotlinlang.org logo
#compose-web
Title
# compose-web
r

Rahul Rawat

02/13/2022, 2:26 PM
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

Big Chungus

02/13/2022, 2:34 PM
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

hfhbd

02/13/2022, 4:30 PM
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

Big Chungus

02/13/2022, 4:32 PM
Wait what? How does it connect to db straight from the browser sandbox?
h

hfhbd

02/13/2022, 4:33 PM
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

Big Chungus

02/13/2022, 4:35 PM
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

hfhbd

02/13/2022, 4:37 PM
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

Rahul Rawat

02/14/2022, 8:20 AM
@hfhbd What about the issue I am facing?
b

Big Chungus

02/14/2022, 8:23 AM
It's solved in the app he linked
h

hfhbd

02/14/2022, 8:24 AM
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

Big Chungus

02/14/2022, 8:25 AM
Well not solved. More like masked as he's just stubbing fs implementation so anything actually using that module will error when invoked
r

Rahul Rawat

02/14/2022, 8:26 AM
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

Big Chungus

02/14/2022, 8:26 AM
I'd avoid any sql attempts on the browser clients.
b

Big Chungus

02/14/2022, 8:30 AM
But it's not the nodejs fs module that the driver expects. Also its a sandboxed and quite restricted file system.
h

hfhbd

02/14/2022, 8:32 AM
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

Big Chungus

02/14/2022, 8:32 AM
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

hfhbd

02/14/2022, 8:36 AM
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

Big Chungus

02/14/2022, 8:37 AM

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

Wasn't aware of WebSQL spec. Lucky gem!
h

hfhbd

02/14/2022, 8:40 AM
It is deprecated -.-
🤦 1
177 Views