eventsourcingdb 0.1.0-beta.1
eventsourcingdb: ^0.1.0-beta.1 copied to clipboard
Community Dart client for EventSourcingDB with typed HTTP and NDJSON APIs.
// Run against a local EventSourcingDB instance, e.g.:
// docker run -it --rm -p 3000:3000 thenativeweb/eventsourcingdb:preview \
// run --api-token secret --data-directory-temporary \
// --http-enabled --https-enabled=false
//
// Then:
// dart run example/example.dart
import 'package:eventsourcingdb/eventsourcingdb.dart';
void main() async {
final client = Client(Uri.parse('http://localhost:3000'), 'secret');
// Ping does not require authentication, so it may succeed even if the API
// token is invalid.
await client.ping();
print('Server is reachable.');
await client.verifyApiToken();
print('API token is valid.');
final writtenEvents = await client.writeEvents([
EventCandidate(
source: 'https://www.eventsourcingdb.io',
subject: '/books/42',
type: 'io.eventsourcingdb.library.book-acquired',
data: {'title': 'Dune', 'author': 'Frank Herbert'},
),
]);
print('Wrote event with id ${writtenEvents.single.id}.');
final events = await client
.readEvents('/books/42', const ReadEventsOptions(recursive: false))
.toList();
for (final event in events) {
final data = event.getData(
(json) => (json as Map<String, Object?>)['title'],
);
print('Read event ${event.id}: $data');
}
final rows = await client
.runEventQlQuery(
'FROM e IN events '
'WHERE e.type == "io.eventsourcingdb.library.book-acquired" '
'PROJECT INTO COUNT()',
)
.toList();
print('Books acquired: ${rows.single}.');
client.close();
}