Skip to content

Commit

Permalink
Use js SQL driver (#452)
Browse files Browse the repository at this point in the history
Co-authored-by: hfhbd <[email protected]>
  • Loading branch information
hfhbd and hfhbd authored Jan 15, 2022
1 parent 2810bf6 commit ef69352
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package app.softwork.composetodo

import android.content.*
import app.softwork.composetodo.repository.*
import app.softwork.composetodo.repository.TodoRepository.Companion.createDatabase
import app.softwork.composetodo.viewmodels.*
import com.squareup.sqldelight.android.*
import com.squareup.sqldelight.db.*
import io.ktor.client.*
import io.ktor.client.engine.android.*
import io.ktor.client.features.*
Expand All @@ -13,7 +13,7 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

class Container(applicationContext: Context, override val scope: CoroutineScope) : AppContainer {
private val driver: SqlDriver = AndroidSqliteDriver(ComposeTodoDB.Schema, applicationContext, "composetodo.db")
private val db = createDatabase(AndroidSqliteDriver(ComposeTodoDB.Schema, applicationContext, "composetodo.db"))

override val client = HttpClient(Android) {
defaultRequest {
Expand All @@ -34,5 +34,6 @@ class Container(applicationContext: Context, override val scope: CoroutineScope)
this.api.value = it
}

override fun todoViewModel(api: API.LoggedIn) = TodoViewModel(scope, TodoRepository(api = api, driver = driver))
override fun todoViewModel(api: API.LoggedIn) =
TodoViewModel(scope, TodoRepository(api = api, dao = db.schemaQueries))
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@ interface TodoRepository {
companion object {
operator fun invoke(
api: API.LoggedIn,
driver: SqlDriver,
dao: SchemaQueries = createDatabase(driver).schemaQueries,
onCreate: (SqlDriver.Schema) -> Unit = { }
dao: SchemaQueries,
) = object : TodoRepository {
init {
onCreate(ComposeTodoDB.Schema)
}

override val todos: Flow<List<Todo>> = dao.all().asFlow().mapToList()

override suspend fun delete(todo: Todo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IosContainer(
protocol: URLProtocol,
host: String
) : AppContainer {
private val driver: SqlDriver = NativeSqliteDriver(ComposeTodoDB.Schema, "composetodo.db")
private val db = TodoRepository.createDatabase(NativeSqliteDriver(ComposeTodoDB.Schema, "composetodo.db"))

constructor() : this(scope = MainScope(), protocol = URLProtocol.HTTPS, host = "api.todo.softwork.app")

Expand Down Expand Up @@ -46,7 +46,8 @@ class IosContainer(
this.api.value = it
}

override fun todoViewModel(api: API.LoggedIn) = TodoViewModel(scope = scope, repo = TodoRepository(api, driver))
override fun todoViewModel(api: API.LoggedIn) =
TodoViewModel(scope = scope, repo = TodoRepository(api, db.schemaQueries))

override fun registerViewModel(api: API.LoggedOut) = RegisterViewModel(scope, api) {
this.api.value = it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.softwork.composetodo

import app.softwork.composetodo.repository.*
import app.softwork.composetodo.repository.TodoRepository.Companion.createDatabase
import app.softwork.composetodo.viewmodels.*
import com.squareup.sqldelight.db.*
import com.squareup.sqldelight.sqlite.driver.*
Expand All @@ -12,9 +13,14 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

class DesktopContainer(override val scope: CoroutineScope) : AppContainer {
private val driver: SqlDriver = JdbcSqliteDriver("jdbc:sqlite:composetodo.db")
private val db: ComposeTodoDB
init {
val driver: SqlDriver = JdbcSqliteDriver("jdbc:sqlite:composetodo.db")
ComposeTodoDB.Schema.create(driver)
db = createDatabase(driver)
}
override fun todoViewModel(api: API.LoggedIn): TodoViewModel =
TodoViewModel(scope, TodoRepository(api, driver) { schema -> schema.create(driver) })
TodoViewModel(scope, TodoRepository(api, db.schemaQueries))

override fun loginViewModel(api: API.LoggedOut) = LoginViewModel(scope, api) {
this.api.value = it
Expand Down
56 changes: 54 additions & 2 deletions kotlin-js-store/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,18 @@ cookie@~0.4.1:
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.4.1.tgz#afd713fe26ebd21ba95ceb61f9a8116e50a537d1"
integrity sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==

[email protected]:
version "9.1.0"
resolved "https://registry.yarnpkg.com/copy-webpack-plugin/-/copy-webpack-plugin-9.1.0.tgz#2d2c460c4c4695ec0a58afb2801a1205256c4e6b"
integrity sha512-rxnR7PaGigJzhqETHGmAcxKnLZSR5u1Y3/bcIv/1FnqXedcL/E2ewK7ZCNrArJKCiSv8yVXhTqetJh8inDvfsA==
dependencies:
fast-glob "^3.2.7"
glob-parent "^6.0.1"
globby "^11.0.3"
normalize-path "^3.0.0"
schema-utils "^3.1.1"
serialize-javascript "^6.0.0"

core-util-is@~1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.3.tgz#a6042d3634c2b27e9328f837b965fac83808db85"
Expand Down Expand Up @@ -1076,6 +1088,17 @@ fast-glob@^3.1.1:
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-glob@^3.2.7, fast-glob@^3.2.9:
version "3.2.11"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.11.tgz#a1172ad95ceb8a16e20caa5c5e56480e5129c1d9"
integrity sha512-xrO3+1bxSo3ZVHAnqzyuewYT6aMFHRAd4Kcs92MAonjwQZLsK9d0SF1IyQ3k5PoirxTW0Oe/RqFgMQ6TcNE5Ew==
dependencies:
"@nodelib/fs.stat" "^2.0.2"
"@nodelib/fs.walk" "^1.2.3"
glob-parent "^5.1.2"
merge2 "^1.3.0"
micromatch "^4.0.4"

fast-json-stable-stringify@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
Expand Down Expand Up @@ -1221,6 +1244,13 @@ glob-parent@^5.1.2, glob-parent@~5.1.2:
dependencies:
is-glob "^4.0.1"

glob-parent@^6.0.1:
version "6.0.2"
resolved "https://registry.yarnpkg.com/glob-parent/-/glob-parent-6.0.2.tgz#6d237d99083950c79290f24c7642a3de9a28f9e3"
integrity sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==
dependencies:
is-glob "^4.0.3"

glob-to-regexp@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
Expand Down Expand Up @@ -1262,6 +1292,18 @@ globby@^11.0.1:
merge2 "^1.3.0"
slash "^3.0.0"

globby@^11.0.3:
version "11.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==
dependencies:
array-union "^2.1.0"
dir-glob "^3.0.1"
fast-glob "^3.2.9"
ignore "^5.2.0"
merge2 "^1.4.1"
slash "^3.0.0"

[email protected]:
version "3.12.2"
resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.12.2.tgz#50ce9f9b6281235724eb243d6a83e969a2176e53"
Expand Down Expand Up @@ -1423,6 +1465,11 @@ ignore@^5.1.4:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.9.tgz#9ec1a5cbe8e1446ec60d4420060d43aa6e7382fb"
integrity sha512-2zeMQpbKz5dhZ9IwL0gbxSW5w0NK/MSAMtNuhgIHEPmaU3vPdKPL0UdvUCXs5SS4JAwsBxysK5sFMW8ocFiVjQ==

ignore@^5.2.0:
version "5.2.0"
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.2.0.tgz#6d3bac8fa7fe0d45d9f9be7bac2fc279577e345a"
integrity sha512-CmxgYGiEPCLhfLnpPp1MoRmifwEIOgjcHXxOBjv7mY96c+eWScsOP9c112ZyLdWHi0FxHjI+4uVhKYp/gcdRmQ==

import-local@^3.0.2:
version "3.0.3"
resolved "https://registry.yarnpkg.com/import-local/-/import-local-3.0.3.tgz#4d51c2c495ca9393da259ec66b62e022920211e0"
Expand Down Expand Up @@ -1533,7 +1580,7 @@ is-fullwidth-code-point@^3.0.0:
resolved "https://registry.yarnpkg.com/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz#f116f8064fe90b3f7844a38997c0b75051269f1d"
integrity sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==

is-glob@^4.0.1, is-glob@~4.0.1:
is-glob@^4.0.1, is-glob@^4.0.3, is-glob@~4.0.1:
version "4.0.3"
resolved "https://registry.yarnpkg.com/is-glob/-/is-glob-4.0.3.tgz#64f61e42cbbb2eec2071a9dac0b28ba1e65d5084"
integrity sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==
Expand Down Expand Up @@ -1791,7 +1838,7 @@ merge-stream@^2.0.0:
resolved "https://registry.yarnpkg.com/merge-stream/-/merge-stream-2.0.0.tgz#52823629a14dd00c9770fb6ad47dc6310f2c1f60"
integrity sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==

merge2@^1.3.0:
merge2@^1.3.0, merge2@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae"
integrity sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==
Expand Down Expand Up @@ -2571,6 +2618,11 @@ spdy@^4.0.2:
select-hose "^2.0.0"
spdy-transport "^3.0.0"

[email protected]:
version "1.6.2"
resolved "https://registry.yarnpkg.com/sql.js/-/sql.js-1.6.2.tgz#7fb70ff68089434826a39b8f5afb2170d682eb3f"
integrity sha512-9iucI5fXQa+Gspeqf/BNB20PxJIn5LhXDt4mjXoFPqXdR+NqtFs15SdKpSIJ6s529aGL9zFR9p2eSCIEiMsNGA==

"statuses@>= 1.4.0 < 2", "statuses@>= 1.5.0 < 2", statuses@~1.5.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/statuses/-/statuses-1.5.0.tgz#161c7dac177659fd9811f43771fa99381478628c"
Expand Down
2 changes: 2 additions & 0 deletions web/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ kotlin {
implementation("app.softwork:routing-compose:0.1.5")
implementation(compose.web.core)
implementation(compose.runtime)
implementation("com.squareup.sqldelight:sqljs-driver:1.5.3")
implementation(devNpm("copy-webpack-plugin", "9.1.0"))
}
}

Expand Down
48 changes: 0 additions & 48 deletions web/src/jsMain/kotlin/app/softwork/composetodo/OnlineRepository.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app.softwork.composetodo

import app.softwork.composetodo.repository.*
import app.softwork.composetodo.viewmodels.*
import com.squareup.sqldelight.db.*
import io.ktor.client.*
import io.ktor.client.engine.js.*
import io.ktor.client.features.*
Expand All @@ -9,7 +11,8 @@ import io.ktor.http.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

class Container(override val scope: CoroutineScope) : AppContainer {
class WebContainer(override val scope: CoroutineScope, driver: SqlDriver) : AppContainer {
private val db = TodoRepository.createDatabase(driver)
override fun loginViewModel(api: API.LoggedOut) = LoginViewModel(scope, api = api) {
this.api.value = it
}
Expand All @@ -18,7 +21,7 @@ class Container(override val scope: CoroutineScope) : AppContainer {
this.api.value = it
}

override fun todoViewModel(api: API.LoggedIn) = TodoViewModel(scope, OnlineRepository(api = api))
override fun todoViewModel(api: API.LoggedIn) = TodoViewModel(scope, TodoRepository(api = api, db.schemaQueries))

override val client = HttpClient(Js) {
install(HttpCookies)
Expand Down
6 changes: 4 additions & 2 deletions web/src/jsMain/kotlin/app/softwork/composetodo/main.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package app.softwork.composetodo

import com.squareup.sqldelight.drivers.sqljs.*
import kotlinx.coroutines.*
import org.jetbrains.compose.web.*

fun main() {
suspend fun main() {
// https://youtrack.jetbrains.com/issue/KTOR-539
js(
"""
Expand All @@ -13,8 +14,9 @@ window.fetch = function (resource, init) {
};
"""
)
val driver = initSqlDriver(ComposeTodoDB.Schema).await()
renderComposable(rootElementId = "root") {
val appContainer = Container(MainScope())
val appContainer = WebContainer(MainScope(), driver)
MainApp(appContainer)
}
}
7 changes: 7 additions & 0 deletions web/webpack.config.d/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
config.resolve = {
fallback: {
"fs": false,
"path": false,
"crypto": false,
}
};
8 changes: 8 additions & 0 deletions web/webpack.config.d/wasm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const CopyWebpackPlugin = require('copy-webpack-plugin');
config.plugins.push(
new CopyWebpackPlugin({
patterns: [
'../../node_modules/sql.js/dist/sql-wasm.wasm'
]
})
);

0 comments on commit ef69352

Please sign in to comment.