-
Notifications
You must be signed in to change notification settings - Fork 50
Raw tables #654
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
Merged
Merged
Raw tables #654
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
'@powersync/common': minor | ||
'@powersync/node': minor | ||
'@powersync/web': minor | ||
'@powersync/react-native': minor | ||
--- | ||
|
||
Add experimental support for raw tables, giving you full control over the table structure to sync into. | ||
While PowerSync manages tables as JSON views by default, raw tables have to be created by the application | ||
developer. Also, the upsert and delete statements for raw tables needs to be specified in the app schema: | ||
|
||
```JavaScript | ||
const customSchema = new Schema({}); | ||
customSchema.withRawTables({ | ||
lists: { | ||
put: { | ||
sql: 'INSERT OR REPLACE INTO lists (id, name) VALUES (?, ?)', | ||
// put statements can use `Id` and extracted columns to bind parameters. | ||
params: ['Id', { Column: 'name' }] | ||
}, | ||
delete: { | ||
sql: 'DELETE FROM lists WHERE id = ?', | ||
// delete statements can only use the id (but a CTE querying existing rows by id could | ||
// be used as a workaround). | ||
params: ['Id'] | ||
} | ||
} | ||
}); | ||
|
||
const powersync = // open powersync database; | ||
await powersync.execute('CREATE TABLE lists (id TEXT NOT NULL PRIMARY KEY, name TEXT);'); | ||
|
||
// Ready to sync into your custom table at this point | ||
``` | ||
|
||
The main benefit of raw tables is better query performance (since SQLite doesn't have to | ||
extract rows from JSON) and more control (allowing the use of e.g. column and table constraints). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* A pending variant of a {@link RawTable} that doesn't have a name (because it would be inferred when creating the | ||
* schema). | ||
*/ | ||
export type RawTableType = { | ||
/** | ||
* The statement to run when PowerSync detects that a row needs to be inserted or updated. | ||
*/ | ||
put: PendingStatement; | ||
/** | ||
* The statement to run when PowerSync detects that a row needs to be deleted. | ||
*/ | ||
delete: PendingStatement; | ||
}; | ||
|
||
/** | ||
* A parameter to use as part of {@link PendingStatement}. | ||
* | ||
* For delete statements, only the `"Id"` value is supported - the sync client will replace it with the id of the row to | ||
* be synced. | ||
* | ||
* For insert and replace operations, the values of columns in the table are available as parameters through | ||
* `{Column: 'name'}`. | ||
*/ | ||
export type PendingStatementParameter = 'Id' | { Column: string }; | ||
stevensJourney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* A statement that the PowerSync client should use to insert or delete data into a table managed by the user. | ||
*/ | ||
export type PendingStatement = { | ||
sql: string; | ||
params: PendingStatementParameter[]; | ||
}; | ||
|
||
/** | ||
* Instructs PowerSync to sync data into a "raw" table. | ||
* | ||
* Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow | ||
* using client-side table and column constraints. | ||
* | ||
* Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client. | ||
* | ||
* @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or | ||
* stability guarantees. | ||
*/ | ||
export class RawTable implements RawTableType { | ||
/** | ||
* The name of the table. | ||
* | ||
* This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use | ||
* another table. Instead, this name is used by the sync client to recognize that operations on this table (as it | ||
* appears in the source / backend database) are to be handled specially. | ||
*/ | ||
name: string; | ||
put: PendingStatement; | ||
delete: PendingStatement; | ||
|
||
constructor(name: string, type: RawTableType) { | ||
this.name = name; | ||
this.put = type.put; | ||
this.delete = type.delete; | ||
} | ||
} |
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
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.