Giter Club home page Giter Club logo

dartorm's Introduction

Build Status Coverage Status Gitter

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.

Any feedback is greatly appreciated.

Feel free to contribute!

Features

Annotations

Annotations could be used in-place:

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;
}

Or one can provide a target class for DBTable annotation. In such way one can store third-party classes in database without changing the original class definition.

// somelibrary.dart
class User {
  int id;
  String name;
}

// your code
import 'somelibrary.dart' as lib;

@ORM.Table('users', lib.User)
class DBUser {
  @ORM.DBField()
  int id;
  
  @ORM.DBField()
  String name;
}

// now User instances could be used like this:
lib.User u = new lib.User();
u.name = 'Name';
await ORM.insert(u);

// Note that DBUser is used only for annotation purposes and should not be used directly.

Types support

Any simple Dart type could be used: int/double/String/bool/DateTime.

Lists are supported and could be used as any other type just by annotating a property:

@ORM.DBTable('users')
class User {
  @ORM.DBField()
  List<String> emails;
}

References to other tables are not supported, but are in progress. Stay tuned!

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'
);

Queries

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

Constructors receive 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

Server-side adapters:

https://github.com/ustims/DartORM-PostgreSQL

https://github.com/ustims/DartORM-MySQL

https://github.com/ustims/DartORM-MongoDB

To use an adapter install it with pub and do this:

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

main() {
  orm.AnnotationsParser.initialize();

  String connectionString =
      'postgres://<username>:<password>@localhost:5432/<dbname>';
      
  orm.DBAdapter postgresAdapter = new PostgresqlDBAdapter(connectionString);
  await postgresAdapter.connect();
  
  orm.addAdapter('postgres', postgresAdapter);
  orm.setDefaultAdapter('postgres');
  
  await orm.Migrator.migrate();
}

DartORM could also be used on client side with indexedDB:

https://github.com/ustims/DartORM-IndexedDB

Roadmap

  • model relations (in progress)
  • migration system

dartorm's People

Contributors

kevmoo avatar luisvargastije avatar ustims avatar vgordievskiy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dartorm's Issues

Test script does not exit correctly

When you directly run dart test/mysql_integration_test.dart, the process does not exit.
I suppose it's because the connection to database is not closed when test ends.
However, connection.close() could not be placed to tearDown because it will run after each of the testcases.

@kevmoo Do you know any way to run something after all tests? Maybe we could wrap everything in one group()?

Upgrade tests to 'test' package

'unittest' package was deprecated, so we should use 'test' package instead.

The problem is that is could not be used as a drop-in replacement because of our tests structure.

First of all, either 'unittest' or 'test' packages fail to detect when the tests are finished, but 'unittest' package has SimpleConfiguration.onDone which helps (see line 133 here). I suppose it would be better to find a reason for such behaviour and fix it.

Second: test package do not detects any tests when run by pub test. This should be investigated, I suppose it's because the main test.dart file actually do not makes any calls to 'group' or 'test' functions.

DartORM-SQLite ?

anyone has started building an SQLite adapter for DartORM ?

DartORM-SQLite ?

Migrations

Ideas/implementation details and discussion thread.

I would like to create something similar to that shown in this video starting from 4:30 : https://msdn.microsoft.com/en-us/data/jj193542

For those who are familiar with python: you probably know about Django framework and it's orm and migrations technique used in that orm.
https://docs.djangoproject.com/es/1.9/topics/migrations/

So the key concepts are:

  • do not do any database alters automatically
  • all database versions should be stored as migration files in migrations directory
  • when model code is changed, orm should detect that and ask for creating new migrations file
  • orm should be able to generate migration files by doing a diff between schema stored in database and schema got from the code
  • need to come up with a solition for running migrations. Right now the only way to do that is using Isolates. Need to check if that is possible.

Map class fields to orm records in a new way

Right now the only way to store objects in database is to write annotations on classes. That's fine for simple architectures, but often you need to store objects that come from other services/libraries, or you simply want to have clean classes and separate file that maps class properties to database record fields.

That means that we need to add another way to annotate classes.
First thing that came to my mind is to have something like this:

// user.dart
class User {
  int id;
}

// user_orm.dart
@ORM.ClassAnnotation(User)
@ORM.Table('users')
class UserORMMap {
  @ORM.Field('id')
  @ORM.PrimaryKeyField()
  int id;
}

That's just an idea, I will start working on this soon and provide more details.

Database records vs model instances

User u1 = new User();
u1.name = 'Sergey';
await u1.save(); // this will cause INSERT query and new row id will be set to 'u1' User instance.

ORM.FindOne f = new ORM.FindOne(User)
..whereEquals('id', u1.id); // now lets find a row with that id

User u2 = await f.execute();
// so here we have two 'User' instances representing the same database record.

u2.name = 'Alexander';
u2.save();

//u1.name = ??

So the question is: what should happen to u1 when we change u2 and save it? Should we a strict rule to allow only one row instance in memory? Or is it ok that some instances will contain old data?

Build failing in CI system

Just noticed the drone.io build is failing. Someone was asking about an ORM solution for Dart, and I found this package. But, will wait to share until we see the build succeeding again. First impressions and all that :)

Thanks for working on this! Lots of people ask about what ORM options there are for Dart.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.