|
| 1 | +/// A table that is managed by the user instead of being auto-created and migrated by the PowerSync SDK. |
| 2 | +/// |
| 3 | +/// These tables give application developers full control over the table (including table and column constraints). |
| 4 | +/// The ``RawTable/put`` and ``RawTable/delete`` statements used by the sync client to apply |
| 5 | +/// operations to the local database also need to be set explicitly. |
| 6 | +/// |
| 7 | +/// A main benefit of raw tables is that, since they're not backed by JSON views, complex queries on them |
| 8 | +/// can be much more efficient. |
| 9 | +/// However, it's the responsibility of the developer to create these raw tables, migrate them when necessary |
| 10 | +/// and to write triggers detecting local writes. For more information, see [the documentation](https://docs.powersync.com/usage/use-case-examples/raw-tables). |
| 11 | +/// |
| 12 | +/// Note that raw tables are only supported when ``ConnectOptions/newClientImplementation`` |
| 13 | +/// is enabled. |
| 14 | +public struct RawTable: BaseTableProtocol { |
| 15 | + /// The name of the table as it appears in sync rules. |
| 16 | + /// |
| 17 | + /// This doesn't necessarily have to match the statement that ``RawTable/put`` and ``RawTable/delete`` |
| 18 | + /// write into. |
| 19 | + /// Instead, it is used by the sync client to identify which operations need to use which raw table definition. |
| 20 | + public let name: String |
| 21 | + |
| 22 | + /// The statement to run when the sync client has to insert or update a row. |
| 23 | + public let put: PendingStatement |
| 24 | + |
| 25 | + /// The statement to run when the sync client has to delete a row. |
| 26 | + public let delete: PendingStatement |
| 27 | + |
| 28 | + public init(name: String, put: PendingStatement, delete: PendingStatement) { |
| 29 | + self.name = name; |
| 30 | + self.put = put; |
| 31 | + self.delete = delete; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/// A statement to run to sync server-side changes into a local raw table. |
| 36 | +public struct PendingStatement { |
| 37 | + /// The SQL statement to execute. |
| 38 | + public let sql: String |
| 39 | + /// For parameters in the prepared statement, the values to fill in. |
| 40 | + /// |
| 41 | + /// Note that the ``RawTable/delete`` statement can only use ``PendingStatementParameter/id`` - upsert |
| 42 | + /// statements can also use ``PendingStatementParameter/column`` to refer to columns. |
| 43 | + public let parameters: [PendingStatementParameter] |
| 44 | +} |
| 45 | + |
| 46 | +/// A parameter that can be used in a ``PendingStatement``. |
| 47 | +public enum PendingStatementParameter { |
| 48 | + /// A value that resolves to the textual id of the row to insert, update or delete. |
| 49 | + case id |
| 50 | + /// A value that resolves to the value of a column in a `PUT` operation for inserts or updates. |
| 51 | + /// |
| 52 | + /// Note that using this parameter is not allowed for ``RawTable/delete`` statements, which only have access |
| 53 | + /// to the row's ``PendingStatementParameter/id``. |
| 54 | + case column(String) |
| 55 | +} |
0 commit comments