Is there a way to create a `sqldelight` `DatabaseD...
# multiplatform
c
Is there a way to create a
sqldelight
DatabaseDriver
for Android without requiring a context? I want all of my database work to be handled by shared code and I don't love having to instantiate it all by passing in an Android context
m
I believe SQLDelight docs explain how to use expect/actual for creating the SQLDriver, which would allow you to then write all of your DB code in commonMain.
x
You need context for android, but that doesn’t mean you can create your drivers in shared code
Copy code
expect class DatabaseDriver {
  suspend fun createDriver(schema: Schema, name: String): SqlDriver
}
and on android
Copy code
actual class DatabaseDriver(val context: Context) {
  actual suspend fun createDriver(schema: Schema, name: String): SqlDriver =
    AndroidSqliteDriver(schema, context, name)
}
c
The only thing is that at some point, your Android app has to pass the database a context, so then the rest of the shared code has to wait for an external dependency before it can get access to the database which I feel like makes DI more difficult
m
Should have
applicationContext
&
Application
available from the get-go via DI to instantiate the Driver...