jaguar_orm 0.2.4

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 59

jaguar_orm

Source-generated ORM with relations (one-to-one, one-to-many, many-to-many), preloading, cascading, polymorphic relations, etc

Features

  • Relationships
  • Preloads
  • Cascading
    • Cascaded inserts
    • Cascaded updates
    • Cascaded removals
  • Migration
  • Polymorphic relations
  • Composite primary keys
  • Composite foreign keys

Gettings started

Install source generator

jaguar_orm_cli provides orm CLI tool to generate ORM code.

pub global activate jaguar_orm_cli

Project configuration

orm CLI tool reads project configuration from orm.yaml configuration file. The orm property in configuration file must list the paths of all libraries that need ORM generation.

Example orm.yaml file

beans:
  - bin/main.dart
  - lib/models/user.dart
  - lib/models/book.dart

Simple example

Model

class User {
  @PrimaryKey()
  String id;

  String name;

  static const String tableName = '_user';

  String toString() => "User($id, $name)";
}

Bean

A Bean performs database actions on behalf of the model. In this case, UserBean performs actions for User model. Much of the Bean's functionality will be source generated.

@GenBean()
class UserBean extends Bean<User> with _UserBean {
  UserBean(Adapter adapter) : super(adapter);
}

Generating the Bean

Given the configuration and GenBean annotation, orm CLI can magically generate ORM methods for you. Here is the command:

orm build

We will see, how we can use the generated ORM methods to perform basic CRUD operations on User table/model.

Connecting to database

We will use PostgreSQL for this tutorial. PgAdapter is found in the package jaguar_query_postgresql.

PgAdapter _adapter =
    new PgAdapter('postgres://postgres:dart_jaguar@localhost/example');
await _adapter.connect();

Creating instance of bean

Beans internally use jaguar_query's Adapter interface to talk to database. Lets create an instance of UserBean.

final userBean = new UserBean(_adapter);

Dropping table

await userBean.drop();

Creating table

await userBean.createTable();

Inserting new record

await userBean.insert(new User()
    ..id = '1'
    ..name = 'teja');

Fetching record by primary key

User user = await userBean.find('1');

Fetching all records

List<User> users = await userBean.getAll();

Updating a record

User user = await userBean.find('1');
user.name = 'teja hackborn';
await userBean.update(user);

Remove by id

await userBean.remove('1');

Remove all

await userBean.removeAll();

One-To-One example

class User {
  @PrimaryKey()
  String id;

  String name;

  @HasOne(AddressBean)
  Address address;

  static const String tableName = '_user';

  String toString() => "User($id, $name, $address)";
}

class Address {
  @PrimaryKey()
  String id;

  @BelongsTo(UserBean)
  String userId;

  String street;

  static String tableName = 'address';

  String toString() => "Post($id, $userId, $street)";
}

@GenBean()
class UserBean extends Bean<User> with _UserBean {
  UserBean(Adapter adapter)
      : addressBean = new AddressBean(adapter),
        super(adapter);

  final AddressBean addressBean;

  Future createTable() {
    final st = Sql
        .create(tableName)
        .addStr('id', primary: true, length: 50)
        .addStr('name', length: 50);
    return execCreateTable(st);
  }
}

@GenBean()
class AddressBean extends Bean<Address> with _AddressBean {
  AddressBean(Adapter adapter) : super(adapter);

  Future createTable() {
    final st = Sql
        .create(tableName)
        .addStr('id', primary: true, length: 50)
        .addStr('street', length: 150)
        .addStr('user_id', length: 50, foreignTable: '_user', foreignCol: 'id');
    return execCreateTable(st);
  }
}

/// The adapter
PgAdapter _adapter =
    new PgAdapter('postgres://postgres:dart_jaguar@localhost/example');

main() async {
  // Connect to database
  await _adapter.connect();

  // Create beans
  final userBean = new UserBean(_adapter);
  final addressBean = new AddressBean(_adapter);

  // Drop old tables
  await addressBean.drop();
  await userBean.drop();

  // Create new tables
  await userBean.createTable();
  await addressBean.createTable();

  // Cascaded One-To-One insert
  {
    final user = new User()
      ..id = '1'
      ..name = 'Teja'
      ..address = (new Address()
        ..id = '1'
        ..street = 'Stockholm');
    await userBean.insert(user, cascade: true);
  }

  // Fetch One-To-One preloaded
  {
    final user = await userBean.find('1', preload: true);
    print(user);
  }

  // Manual One-To-One insert
  {
    User user = new User()
      ..id = '2'
      ..name = 'Kleak';
    await userBean.insert(user, cascade: true);

    user = await userBean.find('2');

    final address = new Address()
      ..id = '2'
      ..street = 'Stockholm';
    addressBean.associateUser(address, user);
    await addressBean.insert(address);
  }

  // Manual One-To-One preload
  {
    final user = await userBean.find('2');
    print(user);
    user.address = await addressBean.findByUser(user.id);
    print(user);
  }

  // Preload many
  {
    final users = await userBean.getAll();
    print(users);
    await userBean.preloadAll(users);
    print(users);
  }

  // Cascaded One-To-One update
  {
    User user = await userBean.find('1', preload: true);
    user.name = 'Teja Hackborn';
    user.address.street = 'Stockholm, Sweden';
    await userBean.update(user, cascade: true);
  }

  // Fetch One-To-One relationship preloaded
  {
    final user = await userBean.find('1', preload: true);
    print(user);
  }

  // Cascaded removal of One-To-One relation
  await userBean.remove('1', true);

  // Remove addresses belonging to a User
  await addressBean.removeByUser('2');

  exit(0);
}

Changelog

0.2.3

  • Create table

0.2.1

  • Angular like table specification

0.2.0

  • Removed statement generation
  • Moved generator to its own package
  • Relations

0.0.1

  • Initial version, created by Stagehand

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  jaguar_orm: "^0.2.4"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


      import 'package:jaguar_orm/jaguar_orm.dart';
  
Version Uploaded Documentation Archive
0.2.4 Sep 24, 2017 Go to the documentation of jaguar_orm 0.2.4 Download jaguar_orm 0.2.4 archive
0.2.3 Sep 24, 2017 Go to the documentation of jaguar_orm 0.2.3 Download jaguar_orm 0.2.3 archive
0.1.3 Jun 10, 2017 Go to the documentation of jaguar_orm 0.1.3 Download jaguar_orm 0.1.3 archive
0.1.2 Jun 4, 2017 Go to the documentation of jaguar_orm 0.1.2 Download jaguar_orm 0.1.2 archive
0.1.1 Jun 4, 2017 Go to the documentation of jaguar_orm 0.1.1 Download jaguar_orm 0.1.1 archive
0.1.0 Jun 4, 2017 Go to the documentation of jaguar_orm 0.1.0 Download jaguar_orm 0.1.0 archive
0.0.4 Mar 5, 2017 Go to the documentation of jaguar_orm 0.0.4 Download jaguar_orm 0.0.4 archive
0.0.3 Feb 23, 2017 Go to the documentation of jaguar_orm 0.0.3 Download jaguar_orm 0.0.3 archive
0.0.1 Dec 25, 2016 Go to the documentation of jaguar_orm 0.0.1 Download jaguar_orm 0.0.1 archive
0.2.2-dev Sep 24, 2017 Go to the documentation of jaguar_orm 0.2.2-dev Download jaguar_orm 0.2.2-dev archive

All 12 versions...

Analysis

We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
17 / 100
Health:
Code health derived from static analysis. [more]
100 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
59
Learn more about scoring.

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:jaguar_orm/jaguar_orm.dart.

Suggestions

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use jaguar_orm.dart.

  • Use analysis_options.yaml.

    Rename old .analysis_options file to analysis_options.yaml.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <2.0.0
jaguar_query ^0.2.8 0.2.8
quiver ^0.25.0 0.25.0 0.29.0+1
Transitive dependencies
matcher 0.12.3
meta 1.1.5
path 1.6.1
stack_trace 1.9.2
Dev dependencies
jaguar_query_postgresql ^0.2.5
test >=0.12.0 <0.13.0