jaguar_orm_gen 2.2.6

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 72

jaguar_orm_gen

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.6

  • Fixed autoincrement

2.2.5

  • Reflexive relations

2.1.20

  • IgnoreColumn on getters fix

2.1.17

  • Better exception printing for field parse exceptions

2.1.16

  • Dart2 fixes

2.1.15

  • Relations without associations

2.1.14

  • Generate preloadAll for many-to-many relations
  • Do not generate tableName

2.1.12

  • Added only to update

2.1.10

  • Removed findWhere and removeWhere. They are now in Bean.

2.1.9

  • Added findOneWhere

2.1.8

  • Autoincrement cascading

2.1.6

  • Return insert id for associations

2.1.5

  • Changed NumField to DoubleField

2.1.4

  • Using parseValue for parsing value from Database

2.1.2

  • Fixed bug where BoolField is generated as BitField

2.1.1

  • Dart2 support

Use this package as a library

1. Depend on it

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


dependencies:
  jaguar_orm_gen: ^2.2.6

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub 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_gen/builder.dart';
import 'package:jaguar_orm_gen/jaguar_orm_cli.dart';
  
Version Uploaded Documentation Archive
2.2.6 Sep 20, 2018 Go to the documentation of jaguar_orm_gen 2.2.6 Download jaguar_orm_gen 2.2.6 archive
2.2.5 Sep 20, 2018 Go to the documentation of jaguar_orm_gen 2.2.5 Download jaguar_orm_gen 2.2.5 archive
2.2.4 Sep 7, 2018 Go to the documentation of jaguar_orm_gen 2.2.4 Download jaguar_orm_gen 2.2.4 archive
2.2.2 Aug 13, 2018 Go to the documentation of jaguar_orm_gen 2.2.2 Download jaguar_orm_gen 2.2.2 archive
2.1.20 Jul 24, 2018 Go to the documentation of jaguar_orm_gen 2.1.20 Download jaguar_orm_gen 2.1.20 archive
2.1.18 Jul 24, 2018 Go to the documentation of jaguar_orm_gen 2.1.18 Download jaguar_orm_gen 2.1.18 archive
2.1.16 Jul 24, 2018 Go to the documentation of jaguar_orm_gen 2.1.16 Download jaguar_orm_gen 2.1.16 archive
2.1.15 Jul 24, 2018 Go to the documentation of jaguar_orm_gen 2.1.15 Download jaguar_orm_gen 2.1.15 archive
2.1.14 Jul 17, 2018 Go to the documentation of jaguar_orm_gen 2.1.14 Download jaguar_orm_gen 2.1.14 archive
2.1.12 Jul 7, 2018 Go to the documentation of jaguar_orm_gen 2.1.12 Download jaguar_orm_gen 2.1.12 archive

All 17 versions...

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

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

  • Dart: 2.0.0
  • pana: 0.12.3

Platforms

Detected platforms: other

Platform components identified in package: build, io, isolate, mirrors.

Suggestions

The description is too short.

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Maintain an example.

Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use jaguar_orm_gen.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
analyzer ^0.32.0 0.32.5 0.33.0-alpha.0
build ^0.12.0 0.12.7+3
jaguar_orm ^2.2.1 2.2.4
jaguar_query ^2.2.2 2.2.2
logging ^0.11.0 0.11.3+2
meta ^1.1.5 1.1.6
source_gen ^0.9.0 0.9.1
Transitive dependencies
args 1.5.0
async 2.0.8
charcode 1.1.2
convert 2.0.2
crypto 2.0.6
csslib 0.14.5
dart_style 1.1.3 1.2.0
front_end 0.1.4+1 0.1.5
glob 1.1.7
html 0.13.3+3
kernel 0.3.4+1 0.3.5
matcher 0.12.4
package_config 1.0.5
plugin 0.2.0+3
quiver 2.0.0+1
source_span 1.4.1
stack_trace 1.9.3
string_scanner 1.0.4
typed_data 1.1.6
utf 0.9.0+5
watcher 0.9.7+10
yaml 2.1.15
Dev dependencies
build_runner ^0.10.0
build_test ^0.10.3
collection ^1.14.10 1.14.11
path ^1.6.2 1.6.2
test ^1.3.0