jaguar_orm_gen 2.2.18

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 75

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

  • Fixed builder.dart library name

2.2.10

  • fetchBy* bug fix when pivots are empty

2.2.8

  • Immutability
  • Fixes for reflexive relations

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

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.18 Nov 8, 2018 Go to the documentation of jaguar_orm_gen 2.2.18 Download jaguar_orm_gen 2.2.18 archive
2.2.16 Oct 5, 2018 Go to the documentation of jaguar_orm_gen 2.2.16 Download jaguar_orm_gen 2.2.16 archive
2.2.15 Sep 30, 2018 Go to the documentation of jaguar_orm_gen 2.2.15 Download jaguar_orm_gen 2.2.15 archive
2.2.14 Sep 28, 2018 Go to the documentation of jaguar_orm_gen 2.2.14 Download jaguar_orm_gen 2.2.14 archive
2.2.12 Sep 28, 2018 Go to the documentation of jaguar_orm_gen 2.2.12 Download jaguar_orm_gen 2.2.12 archive
2.2.10 Sep 26, 2018 Go to the documentation of jaguar_orm_gen 2.2.10 Download jaguar_orm_gen 2.2.10 archive
2.2.8 Sep 25, 2018 Go to the documentation of jaguar_orm_gen 2.2.8 Download jaguar_orm_gen 2.2.8 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

All 24 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
63
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]
75
Learn more about scoring.

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

  • Dart: 2.0.0
  • pana: 0.12.6

Platforms

Detected platforms: other

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

Health suggestions

Format lib/src/writer/writer.dart.

Run dartfmt to format lib/src/writer/writer.dart.

Maintenance suggestions

The description is too short. (-20 points)

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. (-10 points)

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.33.0 0.33.3
build ^1.0.0 1.0.1
jaguar_orm ^2.2.5 2.2.5
jaguar_query ^2.2.4 2.2.4
logging ^0.11.0 0.11.3+2
meta ^1.1.5 1.1.6
source_gen ^0.9.0 0.9.1+3
Transitive dependencies
args 1.5.1
async 2.0.8
charcode 1.1.2
convert 2.0.2
crypto 2.0.6
csslib 0.14.6
dart_style 1.2.0
front_end 0.1.6+3
glob 1.1.7
html 0.13.3+3
kernel 0.3.6+3
matcher 0.12.4
package_config 1.0.5
plugin 0.2.0+3
quiver 2.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 ^1.1.0
build_test ^0.10.3
collection ^1.14.10 1.14.11
path ^1.6.2 1.6.2
test ^1.3.0