strata 0.1.0
strata: ^0.1.0 copied to clipboard
An Ecto-inspired data mapping and changeset validation library for Dart.
Strata #
An Ecto-inspired data mapping and changeset validation library for Dart.
strata provides a robust, type-safe, and extensible API for interacting with your database. It is inspired by the best patterns from Elixir's Ecto, focusing on explicit data validation through changesets and a composable query API.
Features #
- Ecto-style Changesets: A powerful
Changesetclass for validating and casting untrusted data before it ever hits your database. - Repo & Adapter Pattern: The main
StrataRepoclass provides a single, clean API for all database operations. It delegates the database-specific work to aStrataAdapter, so your application logic remains decoupled. - Codegen for Queries: Define your data models using
@StrataSchemaannotations. A code generator creates type-safeQueryandChangesetclasses for you, eliminating "magic strings" and providing editor autocompletion. - Migration Support: A simple
Migrationclass is built-in, which adapters can use to manage database schema versions.
The Adapter Model #
strata is a "bring-your-own-database" library. This core package (strata) contains no database-specific code.
It only defines the contracts (like StrataAdapter) and the core logic (like Changeset and StrataRepo).
To use strata with a real database, you must use a specific adapter package.
- strata_sqlite (Coming Soon)
- strata_postgres (Future)
- ...or create your own!
This model keeps the core library lightweight and allows you to swap out your data layer without rewriting your application logic.
Getting Started #
-
Add
strataas a dependency andbuild_runneras a dev dependency to yourpubspec.yaml.name: my_app description: A new Dart application. environment: sdk: ^3.0.0 dependencies: strata: ^1.0.0 # Or the latest version dev_dependencies: build_runner: ^2.0.0 strata_builder: ^1.0.0 # This would be the name of your generator package lints: ^3.0.0 test: ^1.25.0(Note: The
strata_builderpackage is hypothetical based on yourschema_generator.dart. You would need to publish this generator as a separate package, or include it instrata'sdev_dependenciesif it's meant to be used alongsidebuild_runner.) -
Create your
build.yamlfile to configure the code generator.targets: $default: builders: strata|schema: enabled: true
Usage #
Here is a short example of defining a schema and using the Repo with a Changeset.
1. Define Your Schema #
Create a file like lib/account.dart. The @StrataSchema annotation and Schema mixin "tag" this class for the code generator.
// lib/account.dart
import 'package:strata/strata.dart';
part 'account.strata.dart';
@StrataSchema(table: 'accounts')
class Account with Schema {
final int id;
final String username;
Account({required this.id, required this.username});
@override
String toString() => 'Account(id: $id, username: $username)';
}