Adopt Ormed In An Existing Project
This guide is for teams integrating Ormed into an already running Dart service.
Prerequisites
- Installation
- Familiarity with your current DB schema and deployment flow
What You’ll Learn
- How to add Ormed without rewriting the whole app
- How to scaffold generated runtime config into an existing codebase
- How to choose a safe migration strategy for existing databases
Step Outcome
By the end of this page, you should have:
- Ormed wired into app startup behind a small adapter seam
- One migrated query/write path running through Ormed
- A documented decision on migration ownership
Integration Strategy
Use this sequence:
- Add Ormed packages and generate code.
- Introduce a
DataSourcebootstrap path behind a small adapter layer. - Move one model/query path at a time.
- Adopt migrations only after deciding ownership boundaries.
Safety Rules For Existing Systems
- Keep old and new paths side-by-side during rollout.
- Port read paths before write-heavy paths.
- Add regression tests for each ported endpoint/repository.
- Decide migration ownership once, then document it in-repo.
1. Add Dependencies
Pick your runtime driver and install:
dependencies:
ormed: ^0.2.0
ormed_sqlite: ^0.2.0 # or ormed_postgres / ormed_mysql / ormed_d1
dev_dependencies:
build_runner: ^2.4.0
ormed_cli: ^0.2.0
dart pub get
2. Scaffold Runtime Files
From project root:
dart run ormed_cli:ormed init
This adds lib/src/database/config.dart and lib/src/database/datasource.dart so runtime wiring is code-first by default.
Seeder scaffold is optional and can be added later with ormed init --only=seeders (or by running ormed make:seeder).
3. Add Ormed Models Gradually
Create/annotate model files in lib/src/models/ and generate:
dart run build_runner build --delete-conflicting-outputs
Generated output (*.orm.dart + orm_registry.g.dart) stays alongside your existing code and can be adopted incrementally.
4. Bootstrap DataSource In App Startup
Wire Ormed once during app startup:
import 'src/database/datasource.dart';
Future<void> main() async {
final ds = createDataSource();
await ds.init();
// existing application startup...
}
5. Choose Migration Ownership
For existing databases, pick one approach and document it in your repo:
- Ormed-owned going forward:
- keep historical schema as-is
- add a baseline migration entry representing current state
- use
ormed make:migration/ormed migratefor all new changes
- Externally managed schema:
- continue current migration tool
- use Ormed primarily for models/queries/runtime access
If your schema is already mature, start with externally managed migrations and switch only when your team agrees on ownership and release process.
6. Move Endpoints In Slices
Do not migrate everything at once. Port one endpoint/repository at a time, validate behavior, then proceed.
Recommended order:
- Read-only queries
- Simple writes
- Transaction-heavy paths
Verify Before Expanding Scope
- One endpoint fully moved (query + write + tests)
- Startup/teardown remains stable in all environments
- No schema drift introduced by accidental migration execution