Driver for the library SQLDelight that supports sqlite3 Node.js module
Pretty much it's almost the same as with https://sqldelight.github.io/sqldelight/2.4.0/js_sqlite
Initialization of SQLDelight is needed
plugins {
kotlin("js") version "2.3.21" // probably would work even with different one
id("app.cash.sqldelight") version "2.4.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.8.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)Queries are written as here - https://sqldelight.github.io/sqldelight/2.4.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.
Long(SQLiteINTEGER) columns round-trip exactly only up to2^53(JS's safe integer range), because binding and reading a value both pass through a JSNumber. Binding or reading a value beyond2^53throwsSQLite3Exceptioninstead of silently losing precision.
By default a transaction starts with BEGIN TRANSACTION (deferred locking). Pass
beginImmediate = true to initSqlite3SqlDriver(...) to start with BEGIN IMMEDIATE instead,
which acquires the write lock up front instead of failing with SQLITE_BUSY when the transaction
later upgrades from a read lock:
val driver = initSqlite3SqlDriver(filename = "test.db", schema = Database.Schema, beginImmediate = true)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.
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.