wojta / sqldelight-node-sqlite3-driver

Driver for library SQLDelight that supports sqlite3 Node.js module

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BUILD stability-experimental Maven Central

sqldelight-node-sqlite3-driver

Driver for the library SQLDelight that supports sqlite3 Node.js module

Gradle set up

Pretty much it's almost the same as with https://cashapp.github.io/sqldelight/2.0.0/js_sqlite/

Initialization of SQLDelight is needed

plugins {
    kotlin("js") version "1.9.20" // probably would work even with different one
    id("app.cash.sqldelight") version "2.0.0" // for version 0.3.0
}
፧
፧
፧
sqldelight {
    databases {
        create("Database") {
            packageName.set("com.example")
            generateAsync.set(true) // required for this driver!
        }
    }
}

Add dependency to the driver:

kotlin {
    js {
        binaries.executable()
        nodejs {
            dependencies {
                implementation("cz.sazel.sqldelight:node-sqlite3-driver-js:0.3.0")
            }
        }
    }
}

You'll also need to install binary bindings for SQLite3. This can be done by adding a special Gradle task that runs yarn in the sqlite3 node package directory.

val bindingsInstall = tasks.register("sqlite3BindingsInstall") {
    doLast {
        val sqlite3moduleDir = rootProject.buildDir.resolve("js/node_modules/sqlite3")
        if (!sqlite3moduleDir.resolve("lib/binding").exists()) {
            exec {
                workingDir = sqlite3moduleDir
                val commandLine = "${yarn.yarnSetupTaskProvider.get().destination.absolutePath}/bin/yarn"
                commandLine(commandLine)
            }
        }
    }
}.get()
tasks["kotlinNpmInstall"].finalizedBy(bindingsInstall)

Simple example

Queries are written as here - https://cashapp.github.io/sqldelight/2.0.0/js_sqlite/

suspend fun main() {
    val driver = initSqlite3SqlDriver(filename = "test.db", schema = Database.Schema)

    val database = Database(driver)

    val playerQueries: PlayerQueries = database.playerQueries

    println(playerQueries.selectAll().executeSuspendingAsList())
    // Prints [HockeyPlayer(15, "Ryan Getzlaf")]

    playerQueries.insert(player_number = 10, full_name = "Corey Perry")
    println(playerQueries.selectAll().executeSuspendingAsList())
    // Prints [HockeyPlayer(15, "Ryan Getzlaf"), HockeyPlayer(10, "Corey Perry")]

    val player = HockeyPlayer(20, "Ronald McDonald")
    playerQueries.insertFullPlayerObject(player)
}

Note: Please use executeSuspendingAsList() in queries instead of executeAsList() as that API is not suspending and will throw an exception with this driver.

Thanks

To Isuru Rajapakse and the project KStore which is an inspiration for the set-up of publishing in Gradle scripts.

To authors of SQLDelight, implementation is based on the sqljs implementation of the driver which is already included in the library.s.

License

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

About

Driver for library SQLDelight that supports sqlite3 Node.js module

License:Apache License 2.0


Languages

Language:Kotlin 100.0%