electricsql 0.2.0 copy "electricsql: ^0.2.0" to clipboard
electricsql: ^0.2.0 copied to clipboard

discontinued
outdated

A Dart implementation for ElectricSQL (electric-sql.com) client.

Tests

pub package pub package pub package

Electric Dart ⚡🎯 #

🛠️ WORK IN PROGRESS 🛠️

For development updates make sure to check out the official ElectricSQL Discord server, as well as the official Javascript client


Unofficial Dart client implementation for Electric.

Client based on the typescript client from the clients/typescript subfolder from electric git repository

Reference implementation: #

  • NPM package.
  • Version v0.6.3
  • Commit: b3fc3f49120186396077bf92c4b81a50ed9ced1d

Run the Todos example #

Instructions

Electric Flutter

Usage #

Instantiate #

You can electrify a drift database or a sqlite3 database.

import 'package:electricsql/electricsql.dart';
import 'package:electricsql_flutter/drivers/drift.dart'; // or sqlite3.dart

// This would be the Drift database
AppDatabase db;

final electric = await electrify<AppDatabase>(
    dbName: '<db_name>',
    db: db,
    // Bundled migrations. This variable is autogenerated using 
    // `dart run electricsql_cli generate_migrations`
    migrations: kElectricMigrations,
    config: ElectricConfig(
        auth: AuthConfig(
            // https://electric-sql.com/docs/usage/auth
            // You can use the functions `insecureAuthToken` or `secureAuthToken` to generate one
            token: '<your JWT>',
        ),
        // logger: LoggerConfig(
        //     level: Level.debug, // in production you can use Logger.off
        // ),
        // url: '<ELECTRIC_SERVICE_URL>',
    ),
);

Sync data #

Sync data into the local database. This only needs to be called once. Shapes

final shape = await electric.syncTables([
    'todos',
])

Read data #

Bind live data to the widgets. This can be possible when using drift + its Stream queries.

AppDatabase db;
// Since we've electrified it, we can now use the original database instance normally.
final Stream<List<Todo>> todosStream = db.select(db.todos).watch();

// Stateful Widget + initState
todosStream.listen((liveTodos) {
    setState(() => todos = liveTodos.toList());
});

// StreamBuilder
StreamBuilder<List<Todo>>(
    stream: todosStream,
    builder: (context, snapshot) {
        ...
    },
);

Write data #

You can use the original database instance normally so you don't need to change your database code at all. The data will be synced automatically, even raw SQL statements.

AppDatabase db;
await db.into(db.todos).insert(TodosCompanion(
    title: Value('My todo'),
    completed: Value(false),
));

// Or raw SQL
await db.customInsert(
    'INSERT INTO todos (title, completed) VALUES (?, ?)', 
    variables: [Variable('My todo'), Variable(false)],
    updates: {db.todos}, // This will notify stream queries to rebuild the widget
);

This automatic reactivity works no matter where the write is made — locally, on another device, by another user, or directly into Postgres.

More about ElectricSQL #

Check out the official docs from ElectricSQL here to look at live demos, API docs and integrations.


Development instructions for maintainers and contributors #

Dart 3.x and Melos required

dart pub global activate melos

Bootstrap the workspace #

melos bs

Generate the Protobuf code #

Install the protoc_plugin Dart package. Version used: ^20.0.1

dart pub global activate protoc_plugin 20.0.1

To generate the code

melos generate_proto

Run the tests #

melos test:all

37
likes
0
points
78
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart implementation for ElectricSQL (electric-sql.com) client.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

ansicolor, collection, crypto, dart_jsonwebtoken, drift, equatable, events_emitter, fixnum, http, logging, meta, platform_info, protobuf, rate_limiter, retry, sqlite3, synchronized, uuid, web_socket_channel

More

Packages that depend on electricsql