kotlin - initial sqldelight in KMP for wasmJsMain - Stack Overflow

I am working on a KMP (Kotlin Multiplatform) project that targets Android, iOS, Desktop, and JS platfor

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 badges
Add a comment  | 

2 Answers 2

Reset to default 0

You 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742339501a4425332.html

相关推荐

  • kotlin - initial sqldelight in KMP for wasmJsMain - Stack Overflow

    I am working on a KMP (Kotlin Multiplatform) project that targets Android, iOS, Desktop, and JS platfor

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信