-
Notifications
You must be signed in to change notification settings - Fork 6
[WIP] GRDB Support #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
stevensJourney
wants to merge
17
commits into
main
Choose a base branch
from
grdb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
245e3d2
wip: grdb connection pool
stevensJourney 8428135
wip: grdb
stevensJourney dbd9c09
Use latest GRDB package. Update tests and queries.
stevensJourney 6f32934
wip: table update hooks
stevensJourney e2681f6
Add test for GRDB updates triggered by GRDB
stevensJourney a79ec5c
add join test
stevensJourney 8a278a1
WIP: Add GRDB demo app
stevensJourney 4002d85
demo improvements
stevensJourney 6f0e630
Table updates from PowerSync side
stevensJourney 41174b1
Use SQLite Session API for PowerSync updates.
stevensJourney 7aae6cf
Update GRDB dependency
stevensJourney e970fd4
Merge remote-tracking branch 'origin/main' into grdb
stevensJourney 76aeb1c
demo update
stevensJourney 281558a
Update README. Cleanup public APIs. WIP WatchOS.
stevensJourney 7beff16
Merge remote-tracking branch 'origin/main' into grdb
stevensJourney a81986e
Update READMEs
stevensJourney 1c1f2bb
Register extension on WatchOS
stevensJourney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
62 changes: 62 additions & 0 deletions
62
Sources/PowerSyncGRDB/Config/Configuration+PowerSync.swift
This file contains hidden or 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,62 @@ | ||
import Foundation | ||
import GRDB | ||
import PowerSync | ||
import SQLite3 | ||
|
||
/// Extension for GRDB `Configuration` to add PowerSync support. | ||
/// | ||
/// Call `configurePowerSync(schema:)` on your existing GRDB `Configuration` to: | ||
/// - Register the PowerSync SQLite core extension (required for PowerSync features). | ||
/// - Add PowerSync schema views to your database schema source. | ||
/// | ||
/// This enables PowerSync replication and view management in your GRDB database. | ||
/// | ||
/// Example usage: | ||
/// ```swift | ||
/// var config = Configuration() | ||
/// config.configurePowerSync(schema: mySchema) | ||
/// let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) | ||
/// ``` | ||
/// | ||
/// - Parameter schema: The PowerSync `Schema` describing your sync views. | ||
public extension Configuration { | ||
mutating func configurePowerSync( | ||
schema: Schema | ||
) { | ||
// Register the PowerSync core extension | ||
prepareDatabase { database in | ||
guard let bundle = Bundle(identifier: "co.powersync.sqlitecore") else { | ||
throw PowerSyncGRDBError.coreBundleNotFound | ||
} | ||
|
||
// Construct the full path to the shared library inside the bundle | ||
let fullPath = bundle.bundlePath + "/powersync-sqlite-core" | ||
|
||
let extensionLoadResult = sqlite3_enable_load_extension(database.sqliteConnection, 1) | ||
if extensionLoadResult != SQLITE_OK { | ||
throw PowerSyncGRDBError.extensionLoadFailed("Could not enable extension loading") | ||
} | ||
var errorMsg: UnsafeMutablePointer<Int8>? | ||
let loadResult = sqlite3_load_extension(database.sqliteConnection, fullPath, "sqlite3_powersync_init", &errorMsg) | ||
if loadResult != SQLITE_OK { | ||
if let errorMsg = errorMsg { | ||
let message = String(cString: errorMsg) | ||
sqlite3_free(errorMsg) | ||
throw PowerSyncGRDBError.extensionLoadFailed(message) | ||
} else { | ||
throw PowerSyncGRDBError.unknownExtensionLoadError | ||
} | ||
} | ||
} | ||
|
||
// Supply the PowerSync views as a SchemaSource | ||
let powerSyncSchemaSource = PowerSyncSchemaSource( | ||
schema: schema | ||
) | ||
if let schemaSource = schemaSource { | ||
self.schemaSource = schemaSource.then(powerSyncSchemaSource) | ||
} else { | ||
schemaSource = powerSyncSchemaSource | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙂 This makes me happy when a feature is used as intended.
I would reverse the chain, though:
powerSyncSchemaSource.then(schemaSource)
. You can, and should, do that becausePowerSyncSchemaSource
is well-behaved: it returns nil for views it does not own.The current code runs the current schema source before
powerSyncSchemaSource
. This is fragile, because this only works if the current schema source is well-behaved. Unfortunately, you do not control it. You can write in your documentation that PowerSync requires a well-behaved schema source... But why bother, when you can chain in the opposite direction, and avoid all problems.That's how
then
rewards the well-behaved schema sources ;-)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! That's a good point. We'll update the order of the chain :)