Skip to content

Generating ORM for simple table

Ravi Teja Gudapati edited this page Mar 29, 2019 · 7 revisions

Lets generate Bean for this simple model:

user.dart

// ... imports ...

part 'user.jorm.dart';

class User {
  @PrimaryKey(auto: true, isNullable: false)
  int id;

  String name;

  int age;

  User({this.id, this.name, this.age});
}

The field id is declared as non-nullable, auto-incrementing primary key using Column spec annotation. For information about more Column specification annotations, visit the following wiki page.

Now, let us request Jaguar to generate bean logic for our model:

/// Bean logic is generated by Jaguar ORM
@GenBean()
class UserBean extends Bean<User> with _UserBean {
  UserBean(Adapter adapter) : super(adapter);

  String get tableName => 'simple_user';
}

The GenBean annotation triggers the generation of bean login in class _UserBean. Make sure you specify the table name using tableName field.

If you haven't already, add these dependencies to pubspec.yaml:

dependencies:
  jaguar_query: 
  jaguar_orm:
  jaguar_query_postgres:

dev_dependencies:
  jaguar_orm_gen:
  build_runner:

Now its time to generate the bean logic. Execute build_runner using the following command:

pub run build_runner build

Jaguar should have now generated the bean login in class _UserBean in the file user.jorm.dart. Add part 'user.jorm.dart'; in user.dart.

What's next?

In the next article, we will learn how to use the generated bean logic to perform CRUD operations on the simple_user table.

Clone this wiki locally