jaguar_orm 2.2.5

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 91

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

Getting started

Declaring the Model

class User {
  @PrimaryKey()
  String id;

  String name;

  static const String tableName = '_user';

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

Declaring the 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 Bean logic

jaguar_orm use source_gen and jaguar_orm_gen to generate bean logic from bean specification.

Add the following dependencies to dev_dependencies.

  build_runner:
  jaguar_orm_gen:

Run the following command to generate Bean logic:

pub run build_runner build

Connecting to database

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

final PgAdapter _adapter =
    new PgAdapter('example', username: 'postgres', password: 'dart_jaguar');
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 the table

await userBean.drop();

Creating a table

await userBean.createTable();

Inserting a 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();

Examples

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

2.2.4

  • Fixed autoincrement

2.2.2

  • Reflexive relations

example/example.dart

import 'dart:async';
import 'package:jaguar_query/jaguar_query.dart';
import 'package:jaguar_orm/jaguar_orm.dart';

part 'example.jorm.dart';

class CartItem {
  @PrimaryKey(auto: true)
  int id;
  double amount;
  @Column(isNullable: true)
  String product;
  int quantity;
  @BelongsTo(CartBean)
  int cartId;

  CartItem({this.amount, this.product, this.quantity, this.id});
  CartItem.make(this.amount, this.product, this.quantity);

  @override
  String toString() =>
      'id=$id, amount=$amount, quantity=$quantity, product=$product, cartId=$cartId';

  CartItem copy({int id, int quantity, String product, double amount}) {
    return CartItem(
      amount: amount ?? this.amount,
      product: product ?? this.product,
      quantity: quantity ?? this.quantity,
      id: id ?? this.id,
    );
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is CartItem &&
          runtimeType == other.runtimeType &&
          amount == other.amount &&
          product == other.product &&
          quantity == other.quantity;

  @override
  int get hashCode => amount.hashCode ^ product.hashCode ^ quantity.hashCode;
}

@GenBean()
class CartItemBean extends Bean<CartItem> with _CartItemBean {
  CartItemBean(Adapter adapter) : super(adapter);

  CartBean _cartBean;

  @override
  String get tableName => 'cartItem';

  @override
  CartBean get cartBean => _cartBean ??= CartBean(adapter);
}

class Cart {
  @PrimaryKey(auto: true)
  int id;
  @HasMany(CartItemBean)
  List<CartItem> items;
  double amount;

  Cart({this.id, this.amount = 0.0, this.items = const []});

  Cart copy({id, amount, items}) {
    return Cart(
      id: id ?? this.id,
      amount: amount ?? this.amount,
      items: items ?? this.items,
    );
  }

  String get amountLabel {
    //return kCurrencyFormat.format(amount);
  }

  @override
  String toString() => 'id=$id, amount=$amount, items=$items';

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Cart &&
          runtimeType == other.runtimeType &&
          items == other.items &&
          amount == other.amount;

  @override
  int get hashCode => items.hashCode ^ amount.hashCode;
}

@GenBean()
class CartBean extends Bean<Cart> with _CartBean {
  CartBean(Adapter adapter)
      : cartItemBean = CartItemBean(adapter),
        super(adapter);

  @override
  final CartItemBean cartItemBean;

  @override
  String get tableName => 'cart';
}

Use this package as a library

1. Depend on it

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


dependencies:
  jaguar_orm: ^2.2.5

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
2.2.5 Sep 25, 2018 Go to the documentation of jaguar_orm 2.2.5 Download jaguar_orm 2.2.5 archive
2.2.4 Sep 20, 2018 Go to the documentation of jaguar_orm 2.2.4 Download jaguar_orm 2.2.4 archive
2.2.2 Sep 20, 2018 Go to the documentation of jaguar_orm 2.2.2 Download jaguar_orm 2.2.2 archive
2.2.1 Aug 13, 2018 Go to the documentation of jaguar_orm 2.2.1 Download jaguar_orm 2.2.1 archive
2.1.8 Jul 24, 2018 Go to the documentation of jaguar_orm 2.1.8 Download jaguar_orm 2.1.8 archive
2.1.6 Jul 17, 2018 Go to the documentation of jaguar_orm 2.1.6 Download jaguar_orm 2.1.6 archive
2.1.5 Jul 17, 2018 Go to the documentation of jaguar_orm 2.1.5 Download jaguar_orm 2.1.5 archive
2.1.2 Jul 7, 2018 Go to the documentation of jaguar_orm 2.1.2 Download jaguar_orm 2.1.2 archive
2.1.1 Jun 27, 2018 Go to the documentation of jaguar_orm 2.1.1 Download jaguar_orm 2.1.1 archive
0.2.4 Sep 24, 2017 Go to the documentation of jaguar_orm 0.2.4 Download jaguar_orm 0.2.4 archive

All 21 versions...

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

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

  • Dart: 2.1.0
  • pana: 0.12.7

Platforms

Detected platforms: Flutter, web, other

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

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
jaguar_query ^2.2.4 2.2.4
meta ^1.1.6 1.1.6
path ^1.6.0 1.6.2
quiver ^2.0.0 2.0.1
Transitive dependencies
matcher 0.12.4
stack_trace 1.9.3
Dev dependencies
build_runner ^0.10.0
jaguar_orm_gen
jaguar_query_postgres
test ^1.2.0