Build Status

Dart ORM

Easy-to-use and easy-to-setup database ORM for dart.

It is in the very beginning stage of development and not ready for production use. Feel free to contribute!

Feature tour

If you want to jump to example code click here: https://github.com/ustims/DartORM/blob/master/example/example.dart

Annotations

import 'package:dart_orm/orm.dart' as ORM;

@ORM.DBTable('users')
class User extends ORM.Model {
  // Every field that needs to be stored in database should be annotated with @DBField
  @ORM.DBField()
  @ORM.DBFieldPrimaryKey()
  int id;

  @ORM.DBField()
  String givenName;

  // column name can be overridden
  @ORM.DBField('family_name')
  String familyName;
}

With such annotated class when you first run ORM.Migrator.migrate()

it will execute such statement:

CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    given_name text,
    family_name text
);

Migrations, schema versions and diffs will be implemented later.

Inserts and updates

Every ORM.Model has .save() method which will update/insert a new row.

If class instance has 'id' field with not-null value, .save() will execute 'UPDATE' statement with 'WHERE id = $id'.

If class instance has null 'id' field, 'INSERT' statement will be executed.

User u = new User();
u.givenName = 'Sergey';
u.familyName = 'Ustimenko';

var saveResult = await u.save();

This statement will be executed on save():

INSERT INTO users (
    given_name,
    family_name)
VALUES (
    'Sergey',
    'Ustimenko'
);

Finding records

ORM has two classes for finding records: Find and FindOne.

Constructors receives a class that extend ORM.Model.


ORM.Find f = new ORM.Find(User)
  ..where(new ORM.LowerThan('id', 3)
    .and(new ORM.Equals('givenName', 'Sergey')
      .or(new ORM.Equals('familyName', 'Ustimenko'))
    )
  )
  ..orderBy('id', 'DESC')
  ..setLimit(10);

List foundUsers = await f.execute();

for(User u in foundUsers){
  print('Found user:');
  print(u);
}

This will result such statement executed on the database:

SELECT *
FROM users
WHERE id < 3 AND (given_name = 'Sergey' OR family_name = 'Ustimenko')
ORDER BY id DESC LIMIT 10

Multiple database adapters support

Postgresql adapter is build-in for now but will be extracted soon.

MongoDB adapter was extracted to separate package: https://pub.dartlang.org/packages/dart_orm_adapter_mongodb

and can be used like this:

import 'package:dart_orm_adapter_mongodb/dart_orm_adapter_mongodb.dart';

...

String mongoUser = 'dart_orm_test_user';
String mongoPass = 'dart_orm_test_user';
String mongoDBName = 'dart_orm_test';

MongoDBAdapter mongoAdapter = new MongoDBAdapter(
  'mongodb://$mongoUser:[email protected]/$mongoDBName');
await mongoAdapter.connect();

ORM.Model.ormAdapter = mongoAdapter;

Roadmap

  • mysql adapter
  • memory & file adapters
  • models relations
  • migration system

Libraries

dart_orm