cqrs 10.0.1 copy "cqrs: ^10.0.1" to clipboard
cqrs: ^10.0.1 copied to clipboard

A library for convenient communication with CQRS-compatible backends, using queries and commands.

cqrs #

cqrs pub.dev badge

A library for convenient communication with CQRS-compatible backends, using queries and commands.

Usage example #

import 'package:cqrs/cqrs.dart';

// You may use json_serializable for cutting on boilerplate code for your queries...
class AllFlowers implements Query<List<Flower>> {
  const AllFlowers({required this.page});

  final int page;

  @override
  String getFullName() => 'AllFlowers';

  @override
  List<Flower> resultFactory(dynamic json) {
    return List<Flower>.of(json as List).map(Flower.fromJson).toList();
  }

  @override
  Map<String, dynamic> toJson() => {'page': page};
}

// ...as well as commands.
class AddFlower implements Command {
  const AddFlower({
    required this.name,
    required this.pretty,
  });

  final String name;
  final bool pretty;

  @override
  String getFullName() => 'AddFlower';

  @override
  Map<String, dynamic> toJson() => {'Name': name, 'Pretty': pretty};
}

// Firstly you need an Uri to which the requests will be sent.
// Remember about the trailing slash as otherwise resolved paths
// may be invalid.
final apiUri = Uri.parse('https://flowers.garden/api/');

// Then construct a CQRS instance using the just created Uri
// and an HTTP client which in most cases will be probably handling
// refreshment of the tokens and their rotation.
final cqrs = Cqrs(loginClient, apiUri);

// Fetch first page of the all flowers query from the CQRS server.
final flowers = await cqrs.get(AllFlowers(page: 1));

// Run a command called add flower which... adds a pretty Daisy.
final result = await cqrs.run(AddFlower(name: 'Daisy', pretty: true));

// You can check the command result for its status, whether it successfully ran.
if (result case CommandSuccess()) {
  print('Added a daisy successfully!');
} else if (result case CommandFailure(isInvalid: true, :final validationErrors)) {
  print('Validation errors occured!');
  handleValidationErrors(validationErrors);
} else {
  print('Error occured');
}
24
likes
150
points
2.45k
downloads

Publisher

verified publisherleancode.co

Weekly Downloads

A library for convenient communication with CQRS-compatible backends, using queries and commands.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

Apache-2.0 (license)

Dependencies

equatable, http, logging

More

Packages that depend on cqrs