local_first_sqlite_storage 0.1.0 copy "local_first_sqlite_storage: ^0.1.0" to clipboard
local_first_sqlite_storage: ^0.1.0 copied to clipboard

SQLite adapter for local_first: structured tables, indexes, and filtered queries.

local_first_sqlite_storage #

Flutter Discord

Open Source Love pub package Full tests workflow codecov

Child package of the local_first ecosystem. This SQLite adapter provides structured tables with typed schemas, indexes, and server-like filtering/sorting for offline-first apps.

Why use this adapter? #

  • Structured schema with per-column indexes.
  • Rich query filters (whereIn, comparisons, null checks), sorting, limit/offset.
  • JSON fallback for non-schema fields.
  • Namespaces and metadata storage.
  • Reactive watchQuery updates on data changes.

Installation #

Add the core and the SQLite adapter to your pubspec.yaml:

dependencies:
  local_first: ^0.5.0
  local_first_sqlite_storage: ^0.1.0

Quick start #

import 'package:local_first/local_first.dart';
import 'package:local_first_sqlite_storage/local_first_sqlite_storage.dart';

class Todo with LocalFirstModel {
  Todo({required this.id, required this.title})
      : updatedAt = DateTime.now();

  final String id;
  final String title;
  final DateTime updatedAt;

  @override
  Map<String, dynamic> toJson() => {
        'id': id,
        'title': title,
        'updated_at': updatedAt.toIso8601String(),
      };

  factory Todo.fromJson(Map<String, dynamic> json) => Todo(
        id: json['id'] as String,
        title: json['title'] as String,
      );

  static Todo resolveConflict(Todo local, Todo remote) =>
      local.updatedAt.isAfter(remote.updatedAt) ? local : remote;
}

final todoRepository = LocalFirstRepository<Todo>.create(
  name: 'todo',
  getId: (todo) => todo.id,
  toJson: (todo) => todo.toJson(),
  fromJson: Todo.fromJson,
  onConflict: Todo.resolveConflict,
  schema: const {
    'title': LocalFieldType.text,
    'updated_at': LocalFieldType.datetime,
  },
);

Future<void> main() async {
  final client = LocalFirstClient(
    repositories: [todoRepository],
    localStorage: SqliteLocalFirstStorage(),
    syncStrategies: [
      // Provide your own strategy implementing DataSyncStrategy.
    ],
  );

  await client.initialize();
  await todoRepository.upsert(Todo(id: '1', title: 'Buy milk'));
}

Features #

  • Creates tables and indexes based on provided schema.
  • Query builder with comparisons, IN/NOT IN, null checks, sorting, limit/offset.
  • Stores full JSON in a data column and leverages schema columns for performance.
  • Namespaces and metadata (setMeta / getMeta).
  • Reactive watchQuery.

Testing #

Run tests from this package root:

flutter test

License #

MIT (see LICENSE).

0
likes
160
points
131
downloads

Publisher

verified publishercarda.me

Weekly Downloads

SQLite adapter for local_first: structured tables, indexes, and filtered queries.

Repository (GitHub)
View/report issues
Contributing

Topics

#local-first #sqlite #storage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, local_first, path, sqflite

More

Packages that depend on local_first_sqlite_storage