sqldelight-node-sqlite3-driver

Integration driver for SQLDelight with sqlite3 native bindings, enabling suspending/async query APIs (awaitAsList/executeAsFlow), init helper and Gradle setup for binary bindings.

JS
GitHub stars5
Authorswojta
Open issues1
LicenseApache License 2.0
Creation datealmost 3 years ago

Last activity5 months ago
Latest release0.5.0 (5 months ago)

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://sqldelight.github.io/sqldelight/2.1.0/js_sqlite

Initialization of SQLDelight is needed

plugins {
    kotlin("js") version "2.2.20" // probably would work even with different one
    id("app.cash.sqldelight") version "2.1.0" // for version 0.5.0 and higher
}
፧
፧
፧
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.5.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. It needs to run yarn first to download platform specific binary dependencies.

val bindingsInstall = tasks.register("sqlite3BindingsInstall") {
    doLast {
        val sqlite3moduleDir = layout.buildDirectory.get().dir("js/node_modules/sqlite3").asFile
        if (!sqlite3moduleDir.resolve("build").exists()) {
            exec {
                workingDir = sqlite3moduleDir
                val yarnExecutable = yarn.environment.executable
                val yarnPath = file(yarnExecutable).parent
                val nodePath = file(kotlinNodeJsEnvSpec.executable).parent
                environment(
                    "PATH", System.getenv("PATH") + ":$yarnPath:$nodePath"
                )
                commandLine(yarnExecutable)
            }
        }
    }
}.get()
tasks["kotlinNpmInstall"].finalizedBy(bindingsInstall)

Simple example

Queries are written as here - https://sqldelight.github.io/sqldelight/2.1.0/js_sqlite/#using-queries

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

    val database = Database(driver)

    val playerQueries: PlayerQueries = database.playerQueries

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

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

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

Note: Please use awaitAsList() or executeAsFlow() 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.

JS
GitHub stars5
Authorswojta
Open issues1
LicenseApache License 2.0
Creation datealmost 3 years ago

Last activity5 months ago
Latest release0.5.0 (5 months ago)

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://sqldelight.github.io/sqldelight/2.1.0/js_sqlite

Initialization of SQLDelight is needed

plugins {
    kotlin("js") version "2.2.20" // probably would work even with different one
    id("app.cash.sqldelight") version "2.1.0" // for version 0.5.0 and higher
}
፧
፧
፧
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.5.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. It needs to run yarn first to download platform specific binary dependencies.

val bindingsInstall = tasks.register("sqlite3BindingsInstall") {
    doLast {
        val sqlite3moduleDir = layout.buildDirectory.get().dir("js/node_modules/sqlite3").asFile
        if (!sqlite3moduleDir.resolve("build").exists()) {
            exec {
                workingDir = sqlite3moduleDir
                val yarnExecutable = yarn.environment.executable
                val yarnPath = file(yarnExecutable).parent
                val nodePath = file(kotlinNodeJsEnvSpec.executable).parent
                environment(
                    "PATH", System.getenv("PATH") + ":$yarnPath:$nodePath"
                )
                commandLine(yarnExecutable)
            }
        }
    }
}.get()
tasks["kotlinNpmInstall"].finalizedBy(bindingsInstall)

Simple example

Queries are written as here - https://sqldelight.github.io/sqldelight/2.1.0/js_sqlite/#using-queries

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

    val database = Database(driver)

    val playerQueries: PlayerQueries = database.playerQueries

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

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

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

Note: Please use awaitAsList() or executeAsFlow() 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.