NodeJS ORM
About
Node-ORM is a NodeJS module for multiple databases using Object-Relational Mapping.
Connecting to a Database (MySQL in the example)
var orm = require("orm");
var db = orm.connect("mysql://username:password@hostname/database", function (success, db) {
if (!success) {
console.log("Could not connect to database!");
return;
}
// you can now use db variable to define models
});
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 the model on the database
Person.sync();
Creating and using a record
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 record to database
John.save(function (success) {
if (success) {
console.log("Saved! ID=" + John.id);
} else {
console.log("Something went wrong...");
}
});
Supported Types
This values are still just supported for .sync() (table creations), not for other instance operations like .save() (yet).
Name | Description | MySQL Type |
---|
string | Small text | VARCHAR(255) |
text | Big text | TEXT |
int, integer, num, number | Signed integer | INT |
float | Floating point number | FLOAT |
bool, boolean | True or false value | TINYINT(1) (true=1, false=0) |
date | Date/time value (seconds precision) | DATETIME |
data | Binary data | BLOB |
enum | Enumerated value | ENUM |
struct | Generic (and simple) object | TEXT (saved as JSON) |