admin管理员组文章数量:1332319
I am working on a KMP (Kotlin Multiplatform) project that targets Android, iOS, Desktop, and JS platforms. I have integrated SQLDelight as an ORM for my project by following the official documentation.
In the Common module's build.gradle
file, I added the plugin and configured the database as follows:
plugins {
id("app.cash.sqldelight") version "2.0.2"
}
sqldelight {
databases {
create("Database") {
packageName.set("ir.dorantech.kmp.data.database")
}
}
}
However, when building the project, I encounter the following error:
:composeApp:wasmJsMain: Could not resolve app.cash.sqldelight:runtime:2.0.2.
Required by: project :composeApp
Possible solution:
- Declare repository providing the artifact, see the documentation at .html
In an attempt to fix the issue, I found a solution for JS and updated my configuration as follows:
sqldelight {
databases {
create("Database") {
packageName.set("ir.dorantech.kmp.data.database")
generateAsync.set(true)
}
}
}
Unfortunately, the error remains unresolved.
Has anyone encountered this issue with SQLDelight in a KMP project targeting JS? Any suggestions or solutions would be greatly appreciated.
I am working on a KMP (Kotlin Multiplatform) project that targets Android, iOS, Desktop, and JS platforms. I have integrated SQLDelight as an ORM for my project by following the official documentation.
In the Common module's build.gradle
file, I added the plugin and configured the database as follows:
plugins {
id("app.cash.sqldelight") version "2.0.2"
}
sqldelight {
databases {
create("Database") {
packageName.set("ir.dorantech.kmp.data.database")
}
}
}
However, when building the project, I encounter the following error:
:composeApp:wasmJsMain: Could not resolve app.cash.sqldelight:runtime:2.0.2.
Required by: project :composeApp
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle./current/userguide/declaring_repositories.html
In an attempt to fix the issue, I found a solution for JS and updated my configuration as follows:
sqldelight {
databases {
create("Database") {
packageName.set("ir.dorantech.kmp.data.database")
generateAsync.set(true)
}
}
}
Unfortunately, the error remains unresolved.
Has anyone encountered this issue with SQLDelight in a KMP project targeting JS? Any suggestions or solutions would be greatly appreciated.
Share Improve this question asked Nov 20, 2024 at 17:54 Ali DoranAli Doran 5663 silver badges20 bronze badges2 Answers
Reset to default 0You are using KMP's experimental wasmJs
target, which is currently not supported by sqldelight
(as of version 2.0.2). wasmJs
is one of the two javascript targets that KMP currently supports.
Alternatively to wasmJs
, you can use KMP's js
target with compose and sqldelight, which is supported by sqldelight
and experimentally supported via the CanvasBasedWindow
.
Please note that the web worker driver:
- is only compatible with the browser targets (so not in a library)
- the provided SQL.js worker does not provide persistence, it's memory only
To set up a KMP js
target with compose
and sqldelight
, you need to
1. setup the js
target in your apps build.gradle.kts
:
kotlin {
...
js {
moduleName = "composeApp"
browser {
commonWebpackConfig {
outputFileName = "composeApp.js"
}
}
binaries.executable()
useEsModules()
}
...
}
2. add the correct dependencies for the driver including the worker to your sourceset, see docs here and here
sourceSets.jsMain.dependencies {
implementation "app.cash.sqldelight:sqljs-driver:2.0.2"
implementation npm("sql.js", "1.12.0")
implementation devNpm("copy-webpack-plugin", "9.1.0")
implementation(npm("@cashapp/sqldelight-sqljs-worker", "2.0.2"))
}
3. add the copy webpack for sqljs configuration to your project directory (e.g., composeApp/webpack.config.d/sqljs.js), see documentation
// {project}/webpack.config.d/sqljs.js
config.resolve = {
fallback: {
fs: false,
path: false,
crypto: false,
}
};
const CopyWebpackPlugin = require('copy-webpack-plugin');
config.plugins.push(
new CopyWebpackPlugin({
patterns: [
'../../node_modules/sql.js/dist/sql-wasm.wasm'
]
})
);
4. use the generateAsync
flag when creating the database, see documentation
sqldelight {
databases {
create("Database") {
packageName.set("com.example")
generateAsync.set(true)
}
}
}
5. use the async driver for JS, see documentation
actual suspend fun provideDbDriver(
schema: SqlSchema<QueryResult.AsyncValue<Unit>>
): SqlDriver {
return WebWorkerDriver(
Worker(
js("""new URL("@cashapp/sqldelight-sqljs-worker/sqljs.worker.js", import.meta.url)""")
)
).also { schema.create(it).await() }
}
6. use the experimental CanvasBasedWindow
to instantiate the compose app:
// jsMain Main.kt entry point for your App
suspend fun main() {
onWasmReady {
CanvasBasedWindow("composeApp") {
App()
}
}
}
SQLDelight is a widely used KMP SQL ORM that supports platforms such as JVM, Android, iOS, and JS. However, native support for WASM is currently unavailable, though it may be added in the future.
本文标签: kotlininitial sqldelight in KMP for wasmJsMainStack Overflow
版权声明:本文标题:kotlin - initial sqldelight in KMP for wasmJsMain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742339500a2456308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论