1+ import { TableOrRawTableOptions } from './Table.js' ;
2+
13/**
2- * A pending variant of a {@link RawTable} that doesn't have a name (because it would be inferred when creating the
3- * schema).
4+ * Instructs PowerSync to sync data into a "raw" table.
5+ *
6+ * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
7+ * using client-side table and column constraints.
8+ *
9+ * To collect local writes to raw tables with PowerSync, custom triggers are required. See
10+ * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
11+ * using raw tables.
12+ *
13+ * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
14+ *
15+ * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
16+ * stability guarantees.
417 */
5- export type RawTableType = {
18+ export type RawTableType = RawTableTypeWithStatements | InferredRawTableType ;
19+
20+ interface RawTableTypeWithStatements {
621 /**
722 * The statement to run when PowerSync detects that a row needs to be inserted or updated.
823 */
@@ -11,7 +26,44 @@ export type RawTableType = {
1126 * The statement to run when PowerSync detects that a row needs to be deleted.
1227 */
1328 delete : PendingStatement ;
14- } ;
29+
30+ /**
31+ * An optional statement to run when `disconnectAndClear()` is called on a PowerSync database.
32+ */
33+ clear ?: string ;
34+ }
35+
36+ /**
37+ * The schema of a {@link RawTableType} in the local database.
38+ *
39+ * This information is optional when declaring raw tables. However, providing it allows the sync client to infer `put`
40+ * and `delete` statements automatically.
41+ */
42+ interface RawTableSchema extends TableOrRawTableOptions {
43+ /**
44+ * The actual name of the raw table in the local schema.
45+ *
46+ * Unlike {@link RawTable.name}, which describes the name of synced tables to match, this reflects the SQLite table
47+ * name. This is used to infer {@link RawTableType.put} and {@link RawTableType.delete} statements for the sync
48+ * client. It can also be used to auto-generate triggers forwarding writes on raw tables into the CRUD upload queue
49+ * (using the `powersync_create_raw_table_crud_trigger` SQL function).
50+ *
51+ * When absent, defaults to {@link RawTable.name}.
52+ */
53+ tableName ?: string ;
54+
55+ /**
56+ * An optional filter of columns that should be synced.
57+ *
58+ * By default, all columns in a raw table are considered for sync. If a filter is specified, PowerSync treats
59+ * unmatched columns as local-only and will not attempt to sync them.
60+ */
61+ syncedColumns ?: string [ ] ;
62+ }
63+
64+ interface InferredRawTableType extends Partial < RawTableTypeWithStatements > {
65+ schema : RawTableSchema ;
66+ }
1567
1668/**
1769 * A parameter to use as part of {@link PendingStatement}.
@@ -21,8 +73,10 @@ export type RawTableType = {
2173 *
2274 * For insert and replace operations, the values of columns in the table are available as parameters through
2375 * `{Column: 'name'}`.
76+ * The `"Rest"` parameter gets resolved to a JSON object covering all values from the synced row that haven't been
77+ * covered by a `Column` parameter.
2478 */
25- export type PendingStatementParameter = 'Id' | { Column : string } ;
79+ export type PendingStatementParameter = 'Id' | { Column : string } | 'Rest' ;
2680
2781/**
2882 * A statement that the PowerSync client should use to insert or delete data into a table managed by the user.
@@ -33,35 +87,16 @@ export type PendingStatement = {
3387} ;
3488
3589/**
36- * Instructs PowerSync to sync data into a "raw" table.
37- *
38- * Since raw tables are not backed by JSON, running complex queries on them may be more efficient. Further, they allow
39- * using client-side table and column constraints.
40- *
41- * To collect local writes to raw tables with PowerSync, custom triggers are required. See
42- * {@link https://docs.powersync.com/usage/use-case-examples/raw-tables the documentation} for details and an example on
43- * using raw tables.
44- *
45- * Note that raw tables are only supported when using the new `SyncClientImplementation.rust` sync client.
46- *
47- * @experimental Please note that this feature is experimental at the moment, and not covered by PowerSync semver or
48- * stability guarantees.
90+ * @internal
4991 */
50- export class RawTable implements RawTableType {
92+ export type RawTable < T extends RawTableType = RawTableType > = T & {
5193 /**
5294 * The name of the table.
5395 *
54- * This does not have to match the actual table name in the schema - {@link put} and {@link delete} are free to use
55- * another table. Instead, this name is used by the sync client to recognize that operations on this table (as it
56- * appears in the source / backend database) are to be handled specially.
96+ * This does not have to match the actual table name in the schema - {@link RawTableType.put} and
97+ * {@link RawTableType.delete} are free to use another table. Instead, this name is used by the sync client to
98+ * recognize that operations on this table (as it appears in the source / backend database) are to be handled
99+ * specially.
57100 */
58101 name : string ;
59- put : PendingStatement ;
60- delete : PendingStatement ;
61-
62- constructor ( name : string , type : RawTableType ) {
63- this . name = name ;
64- this . put = type . put ;
65- this . delete = type . delete ;
66- }
67- }
102+ } ;
0 commit comments