Skip to content

shine-on/node-orm

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NodeJS ORM

Call for testers

  • please test-report configurable constraints on MySQL;

About

Node-ORM is a multi-database Object-Relational Mapping for NodeJS.

Features

  • validators;
  • beforeSave/afterSave hooks;

Installing

npm install orm

Connecting to a database

var orm = require("orm");
var dsn = "mysql://username:password@hostname/database";

orm.connect(dsn, function (success, db) {
    if (!success) {
        console.log("Could not connect to database!");
        return;
    }
    
    // you can now use db variable to define models
});

Connecting to a database via raw API

var orm = require("orm");
var mysql = require("mysql");
var client = mysql.createClient({
    user: 'root',
    password: 'root'
});

orm.connect("mysql", client, function (success, db) {
    // same as above...
});

Defining a model

var Person = db.define("person", {
    "name"   : { "type": "string" },
    "surname": { "type": "string", "default": "" },
    "age"    : { "type": "int" }
}, {
    "methods" : {
        "fullName" :function () {
            return this.name + " " + this.surname;
        }
    }
});

Creating a model

Person.sync();

Using a model

var John = new Person({
	"name"		: "John",
	"surname"	: "Doe",
	"age"		: 20
});
console.log("Hello, my name is " + John.fullName() + " and I'm " + John.age + " years old");

Saving a model

John.save(function (err, JohnCopy) {
	if (!err) {
		console.log("Saved! ID=" + John.id); // you can use John or JohnCopy
	} else {
		console.log("Something went wrong...");
		console.dir(err);
	}
});

Adding associations

Person.hasOne("father", Person);
// or just
Person.hasOne("mother"); // defaults to same model

// this will create "person_friends" table with "person_id" and "friend_id"
Person.hasMany("friends", Person, "friend");

Changing associations

John.setFather(Jeremy, function () {
	John.setMother(Jane, function () {
		John.addFriends(Jeremy, Jane, function () {
			console.log("Jeremy and Jane (John's parents) are now his friends too");
		});
	});
});

John.getFather(function (JohnFather) {
	console.log("John's father is " + JohnFather.name);
});
John.unsetMother(function () {
	console.log("John has no mother now!");
});
John.removeFriends(Jeremy, Jane, function () {
	console.log("John has no friends now!");
});
// or just don't send any, all will be removed
John.removeFriends(function () {
	console.log("John has no friends now!");
});

API

Check out API for more.

Supported database types

Currently supported database types are:

Supported data types

Currently, following data types affect only .sync() method (i.e. CREATE TABLE). Others, like .save() method (i.e. UPDATE), does not perform any data type checks.

Name Description MySQL Type PostgreSQL Type MongoDB Type
string Small text VARCHAR(255) VARCHAR(255) String
text Big text TEXT TEXT String
int, integer, num, number Signed integer INT INTEGER Int
float Floating point number FLOAT REAL Float
bool, boolean True or false value TINYINT(1) (true=1, false=0) BOOLEAN Boolean
date Date/time value (seconds precision) DATETIME TIMESTAMP ?
data Binary data BLOB BYTEA String
enum Enumerated value ENUM ENUM String
struct, object Generic (and simple) object TEXT (saved as JSON) TEXT (saved as JSON) Object

About

Object-Relational Mapping for NodeJS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%