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

  • 2.0.0-alpha4
  • Source
  • npm
  • Socket score

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

Object Relational Mapping

Build Status

Install

npm install orm@2.0.0-alpha4

Despite the alpha tag, this is the recommended version for new applications.

DBMS Support

  • MySQL
  • PostgreSQL
  • SQLite

Introduction

This is a node.js object relational mapping module.

Here is an example on how to use it:

var orm = require('orm');

orm.connect("mysql://username:password@host/database", function (err, db) {
	if (err) throw err;

	var Person = db.define('person', {
		name      : String,
		surname   : String,
		age       : Number,
		male      : Boolean,
		continent : [ 'Europe', 'America', 'Asia', 'Africa', 'Australia', 'Antartica' ], // ENUM type
		photo     : Buffer, // BLOB/BINARY
		data      : Object // JSON encoded
	});

	Person.find({ surname: "Doe" }, function (err, people) {
		// SQL: "SELECT * FROM person WHERE surname = 'Doe'"

		console.log("People found: %d", people.length);
		console.log("First person: %s", people[0].name);
	});
});

Models

A Model is a structure binded to one or more tables, depending on the associations. The model name is assumed to be the table name. After defining a model you can use it to manipulate the table.

Finding Items

After defining a Model you can get a specific element or find one or more based on some conditions.

Person.find({ name: "John", surname: "Doe" }, 3, function (err, people) {
	// finds people with name='John' AND surname='Doe' and returns the first 3
});

Or if you know the ID of the item (called Instance):

Person.get(123, function (err, person) {
	// finds person with id = 123
});

If you need to sort the results because you're limiting or just because you want them sorted do:

Person.find({ surname: "Doe" }, "name", function (err, people) {
	// finds people with surname='Doe' and returns sorted by name ascending
});
Person.find({ surname: "Doe" }, [ "name", "Z" ], function (err, people) {
	// finds people with surname='Doe' and returns sorted by name descending
	// ('Z' means DESC; 'A' means ASC - default)
});

There are more options that you can pass to find something. These options are passed in a second object:

Person.find({ surname: "Doe" }, { offset: 2 }, function (err, people) {
	// finds people with surname='Doe', skips the first 2 and returns the others
});

The order of the parameters is not fixed. You can pass the callback first if you like or mix the other paramenters. The only parameter that needs to be in order is when you pass 2 objects. The first one is for conditions (although it can be empty) and the second one is for options.

Associations

An association is a relation between one or more tables.

hasOne vs. hasMany Associations

If you have a relation of 1 to 0 or 1 to 1, you should use hasOne association. This assumes a column in the model that has the id of the other end of the relation.

var Person = db.define('person', {
	name : String
});
var Animal = db.define('animal', {
	name : String
});
Animal.hasOne("owner", Person); // assumes column 'owner_id' in 'animal' table

// get animal with id = 123
Animal.get(123, function (err, Foo) {
	// Foo is the animal model instance, if found
	Foo.getOwner(function (err, John) {
		// if Foo animal has really an owner, John points to it
	});
});

For relations of 1 to many you have to use hasMany associations. This assumes another table that has 2 columns, one for each table in the association.

var Person = db.define('person', {
	name : String
});
Person.hasMany("friends"); // omitting the other Model, it will assume self model

Person.get(123, function (err, John) {
	Person.getFriends(function (err, friends) {
		// assumes table person_friends with columns person_id and friends_id
	});
});

FAQs

Package last updated on 06 Dec 2012

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