forked from cashapp/sqldelight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Getting Started docs for sqljs driver (cashapp#2261)
* Add getting started docs for sqljs driver Fix dokka issue with Kotlin/JS modules * Add multiplatform guidance for SqlJs driver Remove "resources" page for Js docs * Add required webpack config
- Loading branch information
Showing
14 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/coroutines-multiplatform.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/custom_projections.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/gradle-multiplatform.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/grouping_statements.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Getting started on Kotlin JS with SQLDelight | ||
|
||
{% include 'common/index_gradle_database.md' %} | ||
|
||
{% include 'common/index_schema.md' %} | ||
|
||
```groovy | ||
kotlin { | ||
sourceSets.jsMain.dependencies { | ||
implementation "com.squareup.sqldelight:sqljs-driver:{{ versions.sqldelight }}" | ||
} | ||
} | ||
``` | ||
Unlike on other platforms, the SqlJs driver can not be instantiated directly. | ||
The driver must be loaded asynchronously by calling the `initSqlDriver` function which returns a `Promise<SqlDriver>`. | ||
```kotlin | ||
// As a Promise | ||
val promise: Promise<SqlDriver> = initSqlDriver(Database.Schema) | ||
promise.then { driver -> /* ... */ } | ||
|
||
// In a coroutine | ||
suspend fun createDriver() { | ||
val driver: SqlDriver = initSqlDriver(Database.Schema).await() | ||
/* ... */ | ||
} | ||
``` | ||
|
||
If building for browsers, some additional webpack configuration is also required. | ||
```js | ||
// project/webpack.conf.d/fs.js | ||
config.node = { | ||
fs: 'empty' | ||
}; | ||
|
||
// project/webpack.conf.d/wasm.js | ||
var CopyWebpackPlugin = require('copy-webpack-plugin'); | ||
config.plugins.push( | ||
new CopyWebpackPlugin([ | ||
{ from: '../../node_modules/sql.js/dist/sql-wasm.wasm', | ||
to: '../../../{your project}/build/distributions' } | ||
]) | ||
); | ||
|
||
``` | ||
|
||
{% include 'common/index_queries.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/intellij_plugin.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/migrations.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# Multiplatform setup with the SqlJs Driver | ||
|
||
{% include 'common/index_gradle_database.md' %} | ||
|
||
{% include 'common/index_schema.md' %} | ||
|
||
```groovy | ||
kotlin { | ||
// The drivers needed will change depending on what platforms you target: | ||
sourceSets.androidMain.dependencies { | ||
implementation "com.squareup.sqldelight:android-driver:{{ versions.sqldelight }}" | ||
} | ||
// or sourceSets.iosMain, sourceSets.windowsMain, etc. | ||
sourceSets.nativeMain.dependencies { | ||
implementation "com.squareup.sqldelight:native-driver:{{ versions.sqldelight }}" | ||
} | ||
sourceSets.jvmMain.dependencies { | ||
implementation "com.squareup.sqldelight:sqlite-driver:{{ versions.sqldelight }}" | ||
} | ||
sourceSets.jsMain.dependencies { | ||
implementation "com.squareup.sqldelight:sqljs-driver:{{ versions.sqldelight }}" | ||
} | ||
} | ||
``` | ||
|
||
Because the SqlJs driver must be initialized asynchronously, the drivers for other platforms must be initialized in a compatible way to be usable in a common source set. | ||
|
||
The drivers can be initialized in a coroutine, and a higher-order function can be used to ensure that the driver is initialized before executing a block of code that requires the database: | ||
|
||
```kotlin | ||
// in src/commonMain/kotlin | ||
expect suspend fun provideDbDriver(schema: SqlDriver.Schema): SqlDriver | ||
|
||
class SharedDatabase( | ||
private val driverProvider: suspend (SqlDriver.Schema) -> SqlDriver | ||
) { | ||
private var database: Database? = null | ||
|
||
suspend fun initDatabase() { | ||
if (database == null) { | ||
database = driverProvider(Database.Schema).createDatabase() | ||
} | ||
} | ||
|
||
suspend operator fun <R> invoke(block: suspend (Database) -> R): R { | ||
initDatabase() | ||
return block(database!!) | ||
} | ||
|
||
private fun SqlDriver.createDatabase(): Database { /* ... */ } | ||
} | ||
|
||
val sharedDb = SharedDatabase(::createTestDbDriver) | ||
class DataRepository( | ||
private val withDatabase: SharedDatabase = sharedDb | ||
) { | ||
suspend fun getData() = withDatabase { database -> | ||
/* Do something with the database */ | ||
} | ||
} | ||
|
||
// in src/jsMain/kotlin | ||
actual suspend fun provideDbDriver(schema: SqlDriver.Schema): SqlDriver { | ||
return initSqlDriver(schema).await() | ||
} | ||
|
||
// in src/nativeMain/kotlin | ||
actual suspend fun provideDbDriver(schema: SqlDriver.Schema): SqlDriver { | ||
return NativeSqliteDriver(schema, "test.db") | ||
} | ||
|
||
// in src/jvmMain/kotlin | ||
actual suspend fun provideDbDriver(schema: SqlDriver.Schema): SqlDriver { | ||
return JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY).also { driver -> | ||
schema.create(driver) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/query_arguments_sqlite.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% include 'common/transactions.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{% include 'common/types_sqlite.md' %} | ||
|
||
{% include 'common/custom_column_types.md' %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters