orm

Object-relational mapping library. Can generate classes from database tables. MySQL and SqlLite are supported for now.
https://bitbucket.org/yar3333/haxe-orm

To install, run:

haxelib install orm 2.1.0 

See using Haxelib in Haxelib documentation for more information.

README.md

orm

orm is a object-relational mapping library written on pure haxe with MySQL/SqlLite backends. You can generate classes by existing database and use them in your application.

Installation

haxelib install orm

Add orm library to your project. After that you may connect to your database:

var db = new Db("mysql://user:pass@host/dbname");

Use database directly

var rows : sys.db.ResultSet = db.query("SELECT * FROM users");
for (row in rows)
{
    Lib.println(Reflect.field(row, "login"));
}

db.query("DELETE FROM users WHERE id = " + db.quote(id));

Use database through the ORM system

To generate database table-related classes you need:

haxelib run orm mysql://user:password@host/dbname

After generating you can get models and managers classes. For example, if your database has table "users" then ORM generator create next files:

models/autogenerated/Users.hx file:

// this file is autogenerated
// do not edit
class Users
{
    public var id : Int;
    public var login : String;
    public var password : String;
 
    public function set(login:String, password:String) : Void { ... }
    public function save() : Void { ... }
}

models/Users.hx file:

class Users extends models.autogenerated.Users
{
    // add custom methods here
}

models/autogenerated/UsersManager.hx file:

// this file is autogenerated
// do not edit
class UsersManager
{
    function newModelFromParams(id:Int, login:String, password:String) : models.Users { ... }
    function newModelFromRow(d:Dynamic) : models.Users { ... }
    public function get(id:Int) : models.Users { ... }
    public function create(login:String, password:String) : models.Users { ... }
    public function delete(id:Int) : Void { ... }
    public function getAll(_order:String=null) : Array<models.Users> { ... }
    public function getBySqlOne(sql:String) : models.Users { ... }
    public function getBySqlMany(sql:String) : Array<models.Users> { ... }
     
    // this method generated if "login" field is unique in database
    public function getByLogin(login:String) : models.Users { ... }
}

models/UsersManager.hx file:

class UsersManager extends models.autogenerated.UsersManager
{
    // add custom methods here
} 

Now you may write something like this:

var orm = new Orm(db);

// "id" is autoincremented field, so you don't need to specify it
var adminUser = orm.users.create("admin", "mypass");
Lib.println("Now admin user added to database (id=" + adminUser.id + ")");
 
var users = orm.users.getAll();
for (user in users)
{
    Lib.println(user.login);
}
 
var user = orm.users.getBySqlOne("SELECT * FROM users WHERE login = " + db.quote("admin"));
user.password = "newpass";
user.save(); // update database

var activeUsers = orm.users
                     .where("status", "=", "active")
                     .findMany();

var activeGoogleUser = orm.users
                          .where("email", "=", "support@google.com")
                          .where("status", "=", "active")
                          .findOne();

// to get only specified fields of the record:
var googleUserFields = orm.users
                          .where("email", "=", "support@google.com")
                          .findOneFields({ id:0, name:"" }); // field name and value type are used, not value itself
trace(googleUserFields.id + " / " + googleUserFields.name);

orm.users
   .where("status", "=", "winner")
   .update
    ([ 
         "status" => "done",
         "score" => db.expression("score + 1")
    ]);

orm.users
   .where("status", "=", "toRemove")
   .delete();

Tips

  • TinyInt(1) type in your MySQL database treated as Bool in code.
  • Use db.expression(s) to specify raw SQL.
  • Use db.field(s) to specify table field (became quoted as identifier in SQL).
  • Run haxelib run orm to get help about command-line switches.
Contributors
yar3333
Version
2.1.0
Published
2 years ago
License
LGPL

All libraries are free

Every month, more than thousand developers use haxelib to find, share, and reuse code — and assemble it in powerful new ways. Enjoy Haxe; It is great!

Explore Haxe

Haxe Manual

Haxe Code Cookbook

Haxe API documentation

You can try Haxe in the browser! try.haxe.org

Join us on Github!

Haxe is being developed on GitHub. Feel free to contribute or report issues to our projects.

Haxe on Github