Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

orm

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

orm

NodeJS Object-relational mapping

  • 0.1.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
737
increased by21.22%
Maintainers
1
Weekly downloads
 
Created
Source

NodeJS ORM

About

Node-ORM is a NodeJS module for multiple databases using Object-Relational Mapping.

Installing

Install using NPM:

npm install orm

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

Adding associations

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

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

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

Changing associations

I think an example is better to explain.

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

If you want there's also this methods:

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!");
});

Supported Types

This values are still just supported for .sync() (table creations), not for other instance operations like .save() (yet).

NameDescriptionMySQL TypePostgreSQL Type
stringSmall textVARCHAR(255)VARCHAR(255)
textBig textTEXTTEXT
int, integer, num, numberSigned integerINTINTEGER
floatFloating point numberFLOATREAL
bool, booleanTrue or false valueTINYINT(1) (true=1, false=0)BOOLEAN
dateDate/time value (seconds precision)DATETIMETIMESTAMP
dataBinary dataBLOBBYTEA
enumEnumerated valueENUMENUM
structGeneric (and simple) objectTEXT (saved as JSON)TEXT (saved as JSON)

Keywords

FAQs

Package last updated on 06 Dec 2011

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc