Skip to content

Commit be49f6a

Browse files
committed
Docs
1 parent a74ec87 commit be49f6a

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

packages/powersync_core/lib/src/database/powersync_db_mixin.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,13 @@ mixin PowerSyncDatabaseMixin implements SqliteConnection {
297297
params: params,
298298
);
299299

300+
if (schema.rawTables.isNotEmpty &&
301+
resolvedOptions.source.syncImplementation !=
302+
SyncClientImplementation.rust) {
303+
throw UnsupportedError(
304+
'Raw tables are only supported by the Rust client.');
305+
}
306+
300307
// ignore: deprecated_member_use_from_same_package
301308
clientParams = params;
302309
var thisConnectAborter = AbortController();

packages/powersync_core/lib/src/schema.dart

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,18 @@ import 'schema_logic.dart';
77
/// No migrations are required on the client.
88
class Schema {
99
/// List of tables in the schema.
10+
///
11+
/// When opening a PowerSync database, these tables will be created and
12+
/// migrated automatically.
1013
final List<Table> tables;
14+
15+
/// A list of [RawTable]s in addition to PowerSync-managed [tables].
16+
///
17+
/// Raw tables give users full control over the SQLite tables, but that
18+
/// includes the responsibility to create those tables and to write migrations
19+
/// for them.
20+
///
21+
/// For more information on raw tables, see [RawTable] and [the documentation](https://docs.powersync.com/usage/use-case-examples/raw-tables).
1122
final List<RawTable> rawTables;
1223

1324
const Schema(this.tables, {this.rawTables = const []});
@@ -316,9 +327,45 @@ class Column {
316327
Map<String, dynamic> toJson() => {'name': name, 'type': type.sqlite};
317328
}
318329

330+
/// A raw table, defined by the user instead of being managed by PowerSync.
331+
///
332+
/// Any ordinary SQLite table can be defined as a raw table, which enables:
333+
///
334+
/// - More performant queries, since data is stored in typed rows instead of the
335+
/// schemaless JSON view PowerSync uses by default.
336+
/// - More control over the table, since custom column constraints can be used
337+
/// in its definition.
338+
///
339+
/// PowerSync doesn't know anything about the internal structure of raw tables -
340+
/// instead, it relies on user-defined [put] and [delete] statements to sync
341+
/// data into them.
342+
///
343+
/// When using raw tables, you are responsible for creating and migrating them
344+
/// when they've changed. Further, triggers are necessary to collect local
345+
/// writes to those tables. For more information, see
346+
/// [the documentation](https://docs.powersync.com/usage/use-case-examples/raw-tables).
347+
///
348+
/// Note that raw tables are only supported by the Rust sync client, which needs
349+
/// to be enabled when connecting with raw tables.
319350
final class RawTable {
351+
/// The name of the table as used by the sync service.
352+
///
353+
/// This doesn't necessarily have to match the name of the SQLite table that
354+
/// [put] and [delete] write to. Instead, it's used by the sync client to
355+
/// identify which statements to use when it encounters sync operations for
356+
/// this table.
320357
final String name;
358+
359+
/// A statement responsible for inserting or updating a row in this raw table
360+
/// based on data from the sync service.
361+
///
362+
/// See [PendingStatement] for details.
321363
final PendingStatement put;
364+
365+
/// A statement responsible for deleting a row based on its PowerSync id.
366+
///
367+
/// See [PendingStatement] for details. Note that [PendingStatementValue]s
368+
/// used here must all be [PendingStatementValue.id].
322369
final PendingStatement delete;
323370

324371
const RawTable({
@@ -334,8 +381,22 @@ final class RawTable {
334381
};
335382
}
336383

384+
/// An SQL statement to be run by the sync client against raw tables.
385+
///
386+
/// Since raw tables are managed by the user, PowerSync can't know how to apply
387+
/// serverside changes to them. These statements bridge raw tables and PowerSync
388+
/// by providing upserts and delete statements.
389+
///
390+
/// For more information, see [the documentation](https://docs.powersync.com/usage/use-case-examples/raw-tables)
337391
final class PendingStatement {
392+
/// The SQL statement to run to upsert or delete data from a raw table.
338393
final String sql;
394+
395+
/// A list of value identifiers for parameters in [sql].
396+
///
397+
/// Put statements can use both [PendingStatementValue.id] and
398+
/// [PendingStatementValue.column], whereas delete statements can only use
399+
/// [PendingStatementValue.id].
339400
final List<PendingStatementValue> params;
340401

341402
PendingStatement({required this.sql, required this.params});
@@ -346,8 +407,14 @@ final class PendingStatement {
346407
};
347408
}
348409

410+
/// A description of a value that will be resolved in the sync client when
411+
/// running a [PendingStatement] for a [RawTable].
349412
sealed class PendingStatementValue {
413+
/// A value that is bound to the textual id used in the PowerSync protocol.
350414
factory PendingStatementValue.id() = _PendingStmtValueId;
415+
416+
/// A value that is bound to the value of a column in a replace (`PUT`)
417+
/// operation of the PowerSync protocol.
351418
factory PendingStatementValue.column(String column) = _PendingStmtValueColumn;
352419

353420
dynamic toJson();

0 commit comments

Comments
 (0)