CLI Tool
Bavard comes with a built-in CLI tool to help you scaffold your models and pivot tables quickly. The CLI features colored output for better readability and supports generating complete, ready-to-use code.
Setup
To use the Bavard CLI in your project, you need to expose it through a script in your bin/ directory. This allows you to run it using dart run bavard.
Create the CLI Wrapper
Create a file at bin/bavard.dart in your project root:
import 'package:bavard_cli/bavard_cli.dart';
import 'package:bavard_migration/bavard_migration.dart';
void main(List<String> args) {
// Register standard CLI commands along with migration commands
CliRunner([
MakeMigrationCommand(),
MigrateCommand(),
RollbackCommand(),
]).run(args);
}Configure Database for Migrations
Commands that interact with the database (like migrate) need to know how to connect to it. By convention, they look for a getDatabaseAdapter() function in lib/config/database.dart.
Create lib/config/database.dart:
import 'package:bavard/bavard.dart';
/// This function is used by the CLI to obtain a database connection.
Future<DatabaseAdapter> getDatabaseAdapter() async {
// Return your configured adapter
return SQLiteAdapter('database.db');
}Usage
Run the CLI using dart run:
dart run bavard <command> [arguments]Commands
make:model
Creates a new Model class.
Syntax:
dart run bavard make:model <ModelName> [options]Options:
| Option | Description | Example |
|---|---|---|
--columns | Comma-separated list of name:type. Supported types: string, int, double, bool, datetime, json. | --columns=name:string,age:int |
--table | Explicitly set the table name. Defaults to the snake_case plural of the model name. | --table=my_users |
--path | Specify the output directory. | --path=lib/data/models |
--force | Overwrite the file if it already exists. | --force |
--help | Show detailed usage information and examples. | --help |
Examples
1. Basic Model Creates a bare-bones model with just table and fromMap.
dart run bavard make:model Product2. Model with Schema & Accessors (Recommended) Creates a full model with static const schema, typed getters/setters, and casts.
dart run bavard make:model User --columns=name:string,age:int,is_active:bool,metadata:json3. Custom Table & Path Creates a model in a specific directory with a custom table name.
dart run bavard make:model Category --table=product_categories --path=lib/features/shop/modelsThis generates:
class User extends Model {
@override
String get table => 'users';
User([super.attributes]);
@override
User fromMap(Map<String, dynamic> map) => User(map);
// SCHEMA
static const schema = (
name: TextColumn('name'),
age: IntColumn('age'),
isActive: BoolColumn('is_active'),
metadata: JsonColumn('metadata'),
);
// ACCESSORS
String? get name => getAttribute<String>('name');
set name(String? value) => setAttribute('name', value);
int? get age => getAttribute<int>('age');
set age(int? value) => setAttribute('age', value);
// ... (bool and json accessors)
// CASTS
@override
Map<String, String> get casts => {
'age': 'int',
'is_active': 'bool',
'metadata': 'json',
};
}make:pivot
Creates a new Pivot class for Many-to-Many relationships. This generates a single, self-contained file with the class, schema, and typed accessors.
Syntax:
dart run bavard make:pivot <PivotName> [options]Options:
| Option | Description | Example |
|---|---|---|
--columns | Comma-separated list of name:type. | --columns=is_admin:bool |
--model | Optional path to the parent model to import. Defaults to checking the sibling file. | --model=./user.dart |
--path | Specify the output directory. | --path=lib/models |
--force | Overwrite the file if it already exists. | --force |
Examples
1. Basic Pivot
dart run bavard make:pivot UserRole --columns=is_active:bool,created_at:datetimeThis generates:
import 'package:bavard/bavard.dart';
import 'package:bavard/schema.dart';
class UserRole extends Pivot {
UserRole(super.attributes);
static const schema = (
isActive: BoolColumn('is_active'),
createdAt: DateTimeColumn('created_at'),
);
/// Accessor for [isActive] (DB: is_active)
bool get isActive => get(UserRole.schema.isActive);
set isActive(bool value) => set(UserRole.schema.isActive, value);
/// Accessor for [createdAt] (DB: created_at)
DateTime get createdAt => get(UserRole.schema.createdAt);
set createdAt(DateTime value) => set(UserRole.schema.createdAt, value);
static List<Column> get columns => [
UserRole.schema.isActive,
UserRole.schema.createdAt,
];
}make:migration
Creates a new migration file in database/migrations.
Syntax:
dart run bavard make:migration <MigrationName>Example:
dart run bavard make:migration create_users_tablemigrate
Executes all outstanding migrations.
Syntax:
dart run bavard migratemigrate:rollback
Rolls back the last batch of migrations.
Syntax:
dart run bavard migrate:rollback