Conversation
|
|
+1 on this being added. Appreciate it! My use-case for this would be replaying write notifications to the database that occurred in another connection so the OPSqliteConnection the UI and watched queries listen on gets updated. Access to the connection would enable calling |
|
@a11rew Could you expand on your specific use case? Specifically, where would you like to do (1) writes, (2) reads and (3) watched queries, between the We probably won't go ahead with this PR in the current form, since it doesn't solve any actual use case I'm aware of: synchronous writes on the op-sqlite connection have issues regarding write locks (see description above), and synchronous reads can be done directly using op-sqlite, without the changes here (see https://github.com/powersync-community/powersync-react-native-synchronous-operations). |
|
@rkistner use case here is reflecting changes made to the underlying database outside the Powersync connection in the UI. i.e. if a change is made to the sqlite db in a connection outside of the one Powersync establishes, watched queries should still update. In my specific case:
What we're currently doing to work around this is registering a sqlite3_update_hook on that other sqlite connection and then calling tablesUpdated on all the listeners registered on the op-sqlite connection the app is using for UI. This also means we've had do things like re-implement the buffering and flushing OPSQLiteConnection does. Direct access to the/a connection instance would make the ergonomics of working with the connection better. |
|
Thanks, that makes sense. So you're making database updates in native iOS code, and need those to trigger update notifications / watched queries in the PowerSyncDatabase. I think this PR as-is isn't quite suitable for that, since opening another op-sqlite connection wouldn't help you. But adding a way to trigger those update notifications directly on the PowerSyncDatabase instance would help. |
This exposes an additional
OPSqliteOpenFactory.openDirectConnection()method, to get direct access to an op-sqlite DB.This is similar to opening another op-sqlite DB directly, but:
OPSqliteOpenFactory.We do this rather than exposing the existing DB, since that would bypass any existing transaction locking mechanisms, leading to likely race conditions when the DB is used directly.
One specific advantage of this is allowing synchronous queries to be executed, to make it easier to migrate existing applications currently built on synchronous queries. I would not recommend this for other use cases.
Even with this approach, synchronous writes should be wrapped with
PowerSyncDatabase.writeLock(), otherwise it could fail withSQLITE_BUSYif another transaction is busy. We cannot really useBUSY_TIMEOUTfor this, since the synchronous query would block the JS thread, preventing the other lock from being released.Synchronous reads do not have the same restriction, and can run concurrently with any other transaction on the PowerSyncDatabase.
Other changes
BEGIN EXCLUSIVEinstead ofBEGIN: Same as in the web SDK. This reduces the number of lock steps, and avoids triggering SQLITE_BUSY errors later in the transaction.TODOs
OPSQLiteDBAdapter- this can probably be done better.