local_first_sqlite_storage 0.1.0
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 #
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
watchQueryupdates 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
datacolumn 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).