Skip to content

Latest commit

History

163 Commits

Folders and files

NameName
Last commit message
Last commit date

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.

NameDescriptionMySQL TypePostgreSQL TypeMongoDB Type
stringSmall textVARCHAR(255)VARCHAR(255)String
textBig textTEXTTEXTString
int, integer, num, numberSigned integerINTINTEGERInt
floatFloating point numberFLOATREALFloat
bool, booleanTrue or false valueTINYINT(1) (true=1, false=0)BOOLEANBoolean
dateDate/time value (seconds precision)DATETIMETIMESTAMP?
dataBinary dataBLOBBYTEAString
enumEnumerated valueENUMENUMString
struct, objectGeneric (and simple) objectTEXT (saved as JSON)TEXT (saved as JSON)Object

About

Object-Relational Mapping for NodeJS

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages