Code Monkey home page Code Monkey logo

node-orm2's Introduction

Object Relational Mapping

Build Status FOSSA Status Flattr this git repo

This package is not actively maintained

If you're starting a new project, consider using one of the following instead as they have a more active community:

Install

npm install orm

Node.js Version Support

Supported: 4.0 +

If using Nodejs >= 14 & Postgres, you must use pg driver >= 8.1. v7 doesn't work correctly (tests time out).

Tests are run on Travis CI If you want you can run tests locally:

npm test

DBMS Support

  • MySQL & MariaDB
  • PostgreSQL
  • Amazon Redshift
  • SQLite
  • MongoDB (beta, node 6 or older, doesn't work with node 8. Also, missing aggregation features)

Features

  • Create Models, sync, drop, bulk create, get, find, remove, count, aggregated functions
  • Create Model associations, find, check, create and remove
  • Define custom validations (several builtin validations, check instance properties before saving - see enforce for details)
  • Model instance caching and integrity (table rows fetched twice are the same object, changes to one change all)
  • Plugins: MySQL FTS , Pagination , Transaction, Timestamps, Migrations

Introduction

This is a node.js object relational mapping module.

An example:

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, // FLOAT
    male      : Boolean,
    continent : [ "Europe", "America", "Asia", "Africa", "Australia", "Antarctica" ], // ENUM type
    photo     : Buffer, // BLOB/BINARY
    data      : Object // JSON encoded
  }, {
    methods: {
      fullName: function () {
        return this.name + ' ' + this.surname;
      }
    },
    validations: {
      age: orm.enforce.ranges.number(18, undefined, "under-age")
    }
  });

  // add the table to the database
  db.sync(function(err) {
    if (err) throw err;

    // add a row to the person table
    Person.create({ id: 1, name: "John", surname: "Doe", age: 27 }, function(err) {
      if (err) throw err;

      // query the person table by surname
      Person.find({ surname: "Doe" }, function (err, people) {
        // SQL: "SELECT * FROM person WHERE surname = 'Doe'"
        if (err) throw err;

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

        people[0].age = 16;
        people[0].save(function (err) {
          // err.msg == "under-age";
        });
      });
    });
  });
});

Express

If you're using Express, you might want to use the simple middleware to integrate more easily.

var express = require('express');
var orm = require('orm');
var app = express();

app.use(orm.express("mysql://username:password@host/database", {
	define: function (db, models, next) {
		models.person = db.define("person", { ... });
		next();
	}
}));
app.listen(80);

app.get("/", function (req, res) {
	// req.models is a reference to models used above in define()
	req.models.person.find(...);
});

You can call orm.express more than once to have multiple database connections. Models defined across connections will be joined together in req.models. Don't forget to use it before app.use(app.router), preferably right after your assets public folder(s).

Examples

See examples/anontxt for an example express based app.

Documentation

Documentation is moving to the wiki.

Settings

See information in the wiki.

Connecting

See information in the wiki.

Models

A Model is an abstraction over one or more database tables. Models support associations (more below). The name of the model is assumed to match the table name.

Models support behaviours for accessing and manipulating table data.

Defining Models

See information in the wiki.

Properties

See information in the wiki.

Instance Methods

Are passed in during model definition.

var Person = db.define('person', {
    name    : String,
    surname : String
}, {
    methods: {
        fullName: function () {
            return this.name + ' ' + this.surname;
        }
    }
});

Person.get(4, function(err, person) {
    console.log( person.fullName() );
})

Model Methods

Are defined directly on the model.

var Person = db.define('person', {
    name    : String,
    height  : { type: 'integer' }
});
Person.tallerThan = function(height, callback) {
    this.find({ height: orm.gt(height) }, callback);
};

Person.tallerThan( 192, function(err, tallPeople) { ... } );

Loading Models

Models can be in separate modules. Simply ensure that the module holding the models uses module.exports to publish a function that accepts the database connection, then load your models however you like.

Note - using this technique you can have cascading loads.

// your main file (after connecting)
db.load("./models", function (err) {
  // loaded!
  var Person = db.models.person;
  var Pet    = db.models.pet;
});

// models.js
module.exports = function (db, cb) {
  db.load("./models-extra", function (err) {
    if (err) {
      return cb(err);
    }

    db.define('person', {
      name : String
    });

    return cb();
  });
};

// models-extra.js
module.exports = function (db, cb) {
  db.define('pet', {
      name : String
  });

  return cb();
};

Synchronizing Models

See information in the wiki.

Dropping Models

See information in the wiki.

Advanced Options

ORM2 allows you some advanced tweaks on your Model definitions. You can configure these via settings or in the call to define when you setup the Model.

For example, each Model instance has a unique ID in the database. This table column is added automatically, and called "id" by default.
If you define your own key: true column, "id" will not be added:

var Person = db.define("person", {
	personId : { type: 'serial', key: true },
	name     : String
});

// You can also change the default "id" property name globally:
db.settings.set("properties.primary_key", "UID");

// ..and then define your Models
var Pet = db.define("pet", {
	name : String
});

Pet model will have 2 columns, an UID and a name.

It's also possible to have composite keys:

var Person = db.define("person", {
	firstname : { type: 'text', key: true },
	lastname  : { type: 'text', key: true }
});

Other options:

  • identityCache : (default: false) Set it to true to enable identity cache (Singletons) or set a timeout value (in seconds);
  • autoSave : (default: false) Set it to true to save an Instance right after changing any property;
  • autoFetch : (default: false) Set it to true to fetch associations when fetching an instance from the database;
  • autoFetchLimit : (default: 1) If autoFetch is enabled this defines how many hoops (associations of associations) you want it to automatically fetch.

Hooks

See information in the wiki.

Finding Items

Model.get(id, [ options ], cb)

To get a specific element from the database use Model.get.

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

Model.find([ conditions ] [, options ] [, limit ] [, order ] [, cb ])

Finding one or more elements has more options, each one can be given in no specific parameter order. Only options has to be after conditions (even if it's an empty object).

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

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

You can also use raw SQL when searching. It's documented in the Chaining section below.

Model.count([ conditions, ] cb)

If you just want to count the number of items that match a condition you can just use .count() instead of finding all of them and counting. This will actually tell the database server to do a count (it won't be done in the node process itself).

Person.count({ surname: "Doe" }, function (err, count) {
	console.log("We have %d Does in our db", count);
});

Model.exists([ conditions, ] cb)

Similar to .count(), this method just checks if the count is greater than zero or not.

Person.exists({ surname: "Doe" }, function (err, exists) {
	console.log("We %s Does in our db", exists ? "have" : "don't have");
});

Aggregating Functions

If you need to get some aggregated values from a Model, you can use Model.aggregate(). Here's an example to better illustrate:

Person.aggregate({ surname: "Doe" }).min("age").max("age").get(function (err, min, max) {
	console.log("The youngest Doe guy has %d years, while the oldest is %d", min, max);
});

An Array of properties can be passed to select only a few properties. An Object is also accepted to define conditions.

Here's an example to illustrate how to use .groupBy():

//The same as "select avg(weight), age from person where country='someCountry' group by age;"
Person.aggregate(["age"], { country: "someCountry" }).avg("weight").groupBy("age").get(function (err, stats) {
  // stats is an Array, each item should have 'age' and 'avg_weight'
});

Base .aggregate() methods

  • .limit(): you can pass a number as a limit, or two numbers as offset and limit respectively
  • .order(): same as Model.find().order()

Additional .aggregate() methods

  • min
  • max
  • avg
  • sum
  • count (there's a shortcut to this - Model.count)

There are more aggregate functions depending on the driver (Math functions for example).

Chaining

If you prefer less complicated syntax you can chain .find() by not giving a callback parameter.

Person.find({ surname: "Doe" }).limit(3).offset(2).only("name", "surname").run(function (err, people) {
    // finds people with surname='Doe', skips first 2 and limits to 3 elements,
    // returning only 'name' and 'surname' properties
});

If you want to skip just one or two properties, you can call .omit() instead of .only.

Chaining allows for more complicated queries. For example, we can search by specifying custom SQL:

Person.find({ age: 18 }).where("LOWER(surname) LIKE ?", ['dea%']).all( ... );

It's bad practice to manually escape SQL parameters as it's error prone and exposes your application to SQL injection. The ? syntax takes care of escaping for you, by safely substituting the question mark in the query with the parameters provided. You can also chain multiple where clauses as needed.

.find, .where & .all do the same thing; they are all interchangeable and chainable.

You can also order or orderRaw:

Person.find({ age: 18 }).order('-name').all( ... );
// see the 'Raw queries' section below for more details
Person.find({ age: 18 }).orderRaw("?? DESC", ['age']).all( ... );

You can also chain and just get the count in the end. In this case, offset, limit and order are ignored.

Person.find({ surname: "Doe" }).count(function (err, people) {
  // people = number of people with surname="Doe"
});

Also available is the option to remove the selected items. Note that a chained remove will not run any hooks.

Person.find({ surname: "Doe" }).remove(function (err) {
  // Does gone..
});

You can also make modifications to your instances using common Array traversal methods and save everything in the end.

Person.find({ surname: "Doe" }).each(function (person) {
	person.surname = "Dean";
}).save(function (err) {
	// done!
});

Person.find({ surname: "Doe" }).each().filter(function (person) {
	return person.age >= 18;
}).sort(function (person1, person2) {
	return person1.age < person2.age;
}).get(function (people) {
	// get all people with at least 18 years, sorted by age
});

Of course you could do this directly on .find(), but for some more complicated tasks this can be very usefull.

Model.find() does not return an Array so you can't just chain directly. To start chaining you have to call .each() (with an optional callback if you want to traverse the list). You can then use the common functions .filter(), .sort() and .forEach() more than once.

In the end (or during the process..) you can call:

  • .count() if you just want to know how many items there are;
  • .get() to retrieve the list;
  • .save() to save all item changes.

Conditions

Conditions are defined as an object where every key is a property (table column). All keys are supposed to be concatenated by the logical AND. Values are considered to match exactly, unless you're passing an Array. In this case it is considered a list to compare the property with.

{ col1: 123, col2: "foo" } // `col1` = 123 AND `col2` = 'foo'
{ col1: [ 1, 3, 5 ] } // `col1` IN (1, 3, 5)

If you need other comparisons, you have to use a special object created by some helper functions. Here are a few examples to describe it:

{ col1: orm.eq(123) } // `col1` = 123 (default)
{ col1: orm.ne(123) } // `col1` <> 123
{ col1: orm.gt(123) } // `col1` > 123
{ col1: orm.gte(123) } // `col1` >= 123
{ col1: orm.lt(123) } // `col1` < 123
{ col1: orm.lte(123) } // `col1` <= 123
{ col1: orm.between(123, 456) } // `col1` BETWEEN 123 AND 456
{ col1: orm.not_between(123, 456) } // `col1` NOT BETWEEN 123 AND 456
{ col1: orm.like(12 + "%") } // `col1` LIKE '12%'
{ col1: orm.not_like(12 + "%") } // `col1` NOT LIKE '12%'
{ col1: orm.not_in([1, 4, 8]) } // `col1` NOT IN (1, 4, 8)

Raw queries

db.driver.execQuery("SELECT id, email FROM user", function (err, data) { ... })

// You can escape identifiers and values.
// For identifier substitution use: ??
// For value substitution use: ?
db.driver.execQuery(
  "SELECT user.??, user.?? FROM user WHERE user.?? LIKE ? AND user.?? > ?",
  ['id', 'name', 'name', 'john', 'id', 55],
  function (err, data) { ... }
)

// Identifiers don't need to be scaped most of the time
db.driver.execQuery(
  "SELECT user.id, user.name FROM user WHERE user.name LIKE ? AND user.id > ?",
  ['john', 55],
  function (err, data) { ... }
)

Identity pattern

You can use the identity pattern (turned off by default). If enabled, multiple different queries will result in the same result - you will get the same object. If you have other systems that can change your database or you need to call some manual SQL queries, you shouldn't use this feature. It is also know to cause some problems with complex autofetch relationships. Use at your own risk.

It can be enabled/disabled per model:

var Person = db.define('person', {
	name          : String
}, {
	identityCache : true
});

and also globally:

orm.connect('...', function(err, db) {
  db.settings.set('instance.identityCache', true);
});

The identity cache can be configured to expire after a period of time by passing in a number instead of a boolean. The number will be considered the cache timeout in seconds (you can use floating point).

Note: One exception about Caching is that it won't be used if an instance is not saved. For example, if you fetch a Person and then change it, while it doesn't get saved it won't be passed from Cache.

Creating Items

Model.create(items, cb)

To insert new elements to the database use Model.create.

Person.create([
	{
		name: "John",
		surname: "Doe",
		age: 25,
		male: true
	},
	{
		name: "Liza",
		surname: "Kollan",
		age: 19,
		male: false
	}
], function (err, items) {
	// err - description of the error or null
	// items - array of inserted items
});

Updating Items

Every item returned has the properties that were defined to the Model and also a couple of methods you can use to change each item.

Person.get(1, function (err, John) {
	John.name = "Joe";
	John.surname = "Doe";
	John.save(function (err) {
		console.log("saved!");
	});
});

Updating and then saving an instance can be done in a single call:

Person.get(1, function (err, John) {
	John.save({ name: "Joe", surname: "Doe" }, function (err) {
		console.log("saved!");
	});
});

If you want to remove an instance, just do:

// you could do this without even fetching it, look at Chaining section above
Person.get(1, function (err, John) {
	John.remove(function (err) {
		console.log("removed!");
	});
});

Validations

See information in the wiki.

Associations

An association is a relation between one or more tables.

hasOne

Is a many to one relationship. It's the same as belongs to.
Eg: Animal.hasOne('owner', Person).
Animal can only have one owner, but Person can have many animals.
Animal will have the owner_id property automatically added.

The following functions will become available:

animal.getOwner(function..)         // Gets owner
animal.setOwner(person, function..) // Sets owner_id
animal.hasOwner(function..)         // Checks if owner exists
animal.removeOwner()                // Sets owner_id to 0

Chain Find

The hasOne association is also chain find compatible. Using the example above, we can do this to access a new instance of a ChainFind object:

Animal.findByOwner({ /* options */ })

Reverse access

Animal.hasOne('owner', Person, {reverse: 'pets'})

will add the following:

// Instance methods
person.getPets(function..)
person.setPets(cat, function..)

// Model methods
Person.findByPets({ /* options */ }) // returns ChainFind object

hasMany

Is a many to many relationship (includes join table).
Eg: Patient.hasMany('doctors', Doctor, { why: String }, { reverse: 'patients', key: true }).
Patient can have many different doctors. Each doctor can have many different patients.

This will create a join table patient_doctors when you call Patient.sync():

column name type
patient_id Integer (composite key)
doctor_id Integer (composite key)
why varchar(255)

The following functions will be available:

patient.getDoctors(function..)           // List of doctors
patient.addDoctors(docs, function...)    // Adds entries to join table
patient.setDoctors(docs, function...)    // Removes existing entries in join table, adds new ones
patient.hasDoctors(docs, function...)    // Checks if patient is associated to specified doctors
patient.removeDoctors(docs, function...) // Removes specified doctors from join table

doctor.getPatients(function..)
etc...

// You can also do:
patient.doctors = [doc1, doc2];
patient.save(...)

To associate a doctor to a patient:

patient.addDoctor(surgeon, {why: "remove appendix"}, function(err) { ... } )

which will add {patient_id: 4, doctor_id: 6, why: "remove appendix"} to the join table.

getAccessor

This accessor in this type of association returns a ChainFind if not passing a callback. This means you can do things like:

patient.getDoctors().order("name").offset(1).run(function (err, doctors), {
	// ... all doctors, ordered by name, excluding first one
});

extendsTo

If you want to split maybe optional properties into different tables or collections. Every extension will be in a new table, where the unique identifier of each row is the main model instance id. For example:

var Person = db.define("person", {
    name : String
});
var PersonAddress = Person.extendsTo("address", {
    street : String,
    number : Number
});

This will create a table person with columns id and name. The extension will create a table person_address with columns person_id, street and number. The methods available in the Person model are similar to an hasOne association. In this example you would be able to call .getAddress(cb), .setAddress(Address, cb), ..

Note: you don't have to save the result from Person.extendsTo. It returns an extended model. You can use it to query directly this extended table (and even find the related model) but that's up to you. If you only want to access it using the original model you can just discard the return.

Examples & options

If you have a relation of 1 to n, you should use hasOne (belongs to) association.

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

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

You can mark the owner_id field as required in the database by specifying the required option:

Animal.hasOne("owner", Person, { required: true });

If a field is not required, but should be validated even if it is not present, then specify the alwaysValidate option. (this can happen, for example when validation of a null field depends on other fields in the record)

Animal.hasOne("owner", Person, { required: false, alwaysValidate: true });

If you prefer to use another name for the field (owner_id) you can change this parameter in the settings.

db.settings.set("properties.association_key", "{field}_{name}"); // {name} will be replaced by 'owner' and {field} will be replaced by 'id' in this case

Note: This has to be done before the association is specified.

The hasMany associations can have additional properties in the association table.

var Person = db.define('person', {
    name : String
});
Person.hasMany("friends", {
  rate : Number
}, {}, { key: true });

Person.get(123, function (err, John) {
  John.getFriends(function (err, friends) {
    // assumes rate is another column on table person_friends
    // you can access it by going to friends[N].extra.rate
  });
});

If you prefer you can activate autoFetch. This way associations are automatically fetched when you get or find instances of a model.

var Person = db.define('person', {
  name : String
});
Person.hasMany("friends", {
  rate : Number
}, {
  key       : true, // Turns the foreign keys in the join table into a composite key
  autoFetch : true
});

Person.get(123, function (err, John) {
    // no need to do John.getFriends() , John already has John.friends Array
});

You can also define this option globally instead of a per association basis.

var Person = db.define('person', {
  name : String
}, {
    autoFetch : true
});
Person.hasMany("friends", {
  rate : Number
}, {
  key: true
});

Associations can make calls to the associated Model by using the reverse option. For example, if you have an association from ModelA to ModelB, you can create an accessor in ModelB to get instances from ModelA. Confusing? Look at the next example.

var Pet = db.define('pet', {
  name : String
});
var Person = db.define('person', {
  name : String
});
Pet.hasOne("owner", Person, {
  reverse : "pets"
});

Person(4).getPets(function (err, pets) {
  // although the association was made on Pet,
  // Person will have an accessor (getPets)
  //
  // In this example, ORM will fetch all pets
  // whose owner_id = 4
});

This makes even more sense when having hasMany associations since you can manage the many to many associations from both sides.

var Pet = db.define('pet', {
  name : String
});
var Person = db.define('person', {
  name : String
});
Person.hasMany("pets", Pet, {
  bought  : Date
}, {
  key     : true,
  reverse : "owners"
});

Person(1).getPets(...);
Pet(2).getOwners(...);

Promise support

ORM supports Promises via bluebird. Most methods which accept a callback have a Promise version whith a Async postfix. Eg:

orm.connectAsync().then().catch();
Person.getAsync(1).then();
Person.find({ age: 18 }).where("LOWER(surname) LIKE ?", ['dea%']).allAsync( ... );
Person.aggregate({ surname: "Doe" }).min("age").max("age").getAsync();

The exception here are hooks, which should return a Promise if they perform asynchronous operations:

beforeCreate: function () {
  return new Promise(function(resolve, reject) {
    doSomeStuff().then(resolve);
  }

Adding external database adapters

To add an external database adapter to orm, call the addAdapter method, passing in the alias to use for connecting with this adapter, along with the constructor for the adapter:

require('orm').addAdapter('cassandra', CassandraAdapter);

See the documentation for creating adapters for more details.

License

FOSSA Status

node-orm2's People

Contributors

anton-locomote avatar belfordz avatar benkitzelman avatar dresende avatar dxg avatar elexy avatar esk525 avatar galeone avatar interlock avatar jogly avatar jpage-godaddy avatar justintnt avatar kapouer avatar kvanwagenen avatar li-qiang avatar m407 avatar markandrus avatar michaelr avatar mike-zenith avatar mithgol avatar moonrailgun avatar nicholasf avatar notheotherben avatar pjoe avatar ramzamzam avatar stephanhoyer avatar stueynz avatar tttmaximttt avatar void-995 avatar zachberry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-orm2's Issues

Feature request: Allow a filter when fetching associations

Take Person.getFriends(function (err, friends) { ... }); from the examples for example. What if I want to filter out only the female friends? Right now one would loop the results and filter them there.

To be more efficient it'd be great to have a syntax like Person.getFriends({'gender': 'female'}, function (err, friends) { ... }); which filters them out in the query (less rows returned from the database).

The same syntax (arguments and arguments order) as Model.find() would be ideal.

Server closed the connection

1: Just noticed when connecting to database provider cleardb as an addon to heroku I run in to a connection lost issue. After connecting approx 2 mins the message of "The server closed the connection" appears in console and the app dies.

It seems my only work around is to manually call db.close()
eg....

app.get('/test', function(req, res) {

orm.connect("mysql://123456789.cleardb.com/heroku_987654321?reconnect=true", function (err, db) {
    if (err) throw err;
    var Artist = db.define("artist", {
        name: String
    });

    Artist.find({}, function(err, artists) {
        res.send('hello world - ' + artists[0].name);
        db.close(); // <-- i need to add this
    });
});

});

2: should i be opening a new connection (and defining models) every time I wish to make a set of db calls within an express route? Or defining once for the whole app is acceptable (which would fail in my instance with cleardb)?

BETWEEN queries

Is it possible to do BETWEEN queries with the current code or would I need to develop new code to do this?

Association search works incorrectly

when I try to query
A.getB, the next sql is generated:
select * from A left join A_B on... where A.id=...

but I need to select B records, not A, so generated query should looks like:
select * from B left join A_B on... where B.id=...

Overriding model getters/setters?

Hey, say I have a model like:

User = db.define('user',{
    email: String
  },{
  validations: {
    email: orm.validators.unique()
  },
  hooks: {
    beforeSave: function() {
      this.email = email.toLowerCase();
    }
  }
});

All is jolly, however:

user1 = db.models.user.new({email: '[email protected]'});
user1.save(...);
...
user2 = db.models.user.new({email: '[email protected]'});
user2.save(...);

We now have two duplicate users.
(I actually have a unique index on the table to prevent this, but it throws an ugly error when saving)

I can think of two ways of solving this:

  1. Add a beforeValidate hook.
  2. Make it possible to add custom setters/getters for model properties.

I imagine it working something like this:

User = db.define('user',{
    email: String
  },{
  setters: {
    email: function(val) {
        return val.toLowerCase();
    }
  }
});

Or maybe even more fancy:

User = db.define('user',{
    email: String
  },{
  methods: {
    email: {
        set: function(val) {
            this.setAttribute('email', val.toLowerCase());
        }
    }
  }
});

where it defaults to the standard getter as one isn't specified, and setAttribute calls the default setter.

What do you think?

Provide access to associations whenever a new model instance is initialized

I think it would be nice to be able to access associations as soon as I initialize an object, not when I fetch an object from the database. Let me give you an example:

Currently, if i want to fetch the orders of a person, whose ID is known to me (coming from a URL or something), I have to fetch the person from scratch, and only then do I have the option to fetch the associated orders to that person. That implies one additional SQL query, which in many cases can be avoided.

wouldn't it be more efficient if I could do:

var me = new models.Person({id:7})
me.getOrders( function(err, myOrders) { .... } ):

Internally, ORM will anyway use the id of the parent entity.

Feature request: Allow limiting of the fields returned when using .find()

Right now all fields defined in the model are returned when using .find() (or .getFriends() from the associations example).

Sometimes only a few of those fields are enough. Right now I fix this by defining a "lite" version of model next to the original model but that's not something I think is quite feasible.

Additionally: sometimes (when using associations) it can happen that the relation table contains an extra field (one that does not exist in either of the two entities). It should also be possible to inject extra fields to be selected.

db.load should follow node 'modules as folders' convention

It's important to support clear separation of model logic and the natural way to do it is via node modules.

The db.load(..) function is how this problem is solved, as it can load model definitions that are in separate modules. The way it locates them, however, is limited. You have to directly specify an index like file that is in the same directory as the file making the db.load call:

orm.connect(env.connectionString, function(err, db){
    db.load("./models/index", function (err) {
        //...
    });
});

It would be nice if db.load could work with the traditional loading sequence described here: http://nodejs.org/api/modules.html#modules_folders_as_modules

In other words, I should be able to do this:

orm.connect(env.connectionString, function(err, db){
    db.load("./models/", function (err) {
      //...
    });
});

Where models/index.js would be loaded if it exists.

empty request should not return error

when table is empty, model.getAssociation returns error: [Error: write after end] but it should return an empty array with no errors as the empty result is correct result

How to set a default value for a column?

var order = db.define("order", {
...
confirmed:{type:"boolean", default: false},
}

The 'default' property was just a wild guess so I don't know if it's supported yet. And by the way, can you please point me to a list of available options to use when creating columns like this?

Thanks.

MongoDB

is there support for MongoDB?

hasMany returning parent as result?

var Artist = db.define("artist", {
name: String,
});
Artist.hasMany("genres", Genre);

var Genre = db.define("genre", {
name: String
});

... calling getGenres doesn't return the many related genre records. Instead returns an object of the parent artist:

db.Artist.get(req.params.id, function (err, artist) {
artist.getGenres(function(err, genres) {

DB reconnect when connection lost occurred

Is it possible to catch and handle 'connection lost' event and try to reconnect to db now?
I use mysql driver and I have wait_timeout set in mysql config, so, if connection is inactive for some time, mysql closes it and node-mysql throws an error.
I tryed to catch it with db.driver.on or db.driver.db.on, but no success.
So, I have two questions:

  1. What should I do in this situation? Should I just ping db every n seconds, or there is another solution?
  2. What is happening when I do db.driver.db.on? As far as I understand, db.driver.on is an instance of node-mysql connection. But, when I do:
var mysql = require('mysql');
var conn = mysql.createConnection(my_params);
conn.connect(my_cb);
conn.on('error', function (e) { ... }); // I'm doing this in node REPL, so error handler is attached after db is connected.

everything works well, and, when I do:

var orm = require('orm');
orm.connect(my_params, function (e, db) { 
  db.driver.db.on('error', function (e) { ... }); //There can be db.driver.on also
});

node-mysql just throws unhandled exception and the whole process ends.
Any help would be appreciated!

db.load loads files relative to orm module directory (2.0.0-alpha10)

Assume I have a main.js file and a models.js file in the same directory (same setup as in the documentation under Loading Models).

If my main.js file calls db.load('./models') I'll get an error Error: Cannot find module './models'

That's because ORM.js's load function simply calls require(file)(this, cb);. This, then, looks for a models.js in ./node_modules/orm/lib/.

Instead I can call db.load("../../../dbmodels") to get it to work.

I'd be willing to submit a fix but I'm a little new to node and not really sure what the best course of action is. Any ideas? Or, if this is expected behavior then I'd like to update the documentation to make note of it.

Date fields in Models

Is this how should date fields be specified in models

var Person = db.define('person', {
        name : String,
        surname : String,
        age : Number,
        registered : Date
    });

hasMany ignores additional params

i have such relation:
table1:{table1_id,val}
table2:{table2_id,val}
table1_table2:{table1_id,table2_id,val}

when I create db.models.table1.hasMany('table2',{val:Number},opts) relation, my "val" field ignored.
So driver sync function part should looks like this:

...
params=opts.many_associations[i].props.map(function(prop){
...create field signature for table creation...
})
...
"CREATE TABLE IF NOT EXISTS " + driver.escapeId(opts.many_associations[i].mergeTable) +
" (" +
params.join(',')+
driver.escapeId(opts.many_associations[i].mergeId) + " INTEGER NOT NULL, " +
driver.escapeId(opts.many_associations[i].mergeAssocId) + " INTEGER NOT NULL" +
")",

Postgres Model.sync creating new index every call

Using Postgres every time Model.sync is called a new index is generated for the id field. As an example, I created the User model on the db database. When I run psql database on the command line and do \d user after restarting the server a few times I get these results.

Indexes:
    "user_id_idx" btree (id)
    "user_id_idx1" btree (id)
    "user_id_idx10" btree (id)
    "user_id_idx2" btree (id)
    "user_id_idx3" btree (id)
    "user_id_idx4" btree (id)
    "user_id_idx5" btree (id)
    "user_id_idx6" btree (id)
    "user_id_idx7" btree (id)
    "user_id_idx8" btree (id)
    "user_id_idx9" btree (id)

hasMany not working

Running though the examples, in 2.0.0-alpha6. It appears hasMany is not extending the instance, so we have no getFirends etc on the instance object. Still digging in.

IS NULL condition

Hello,

is there any way to create "IS NULL" condition. Tried to do

Model.find({field: null}, ...

but that throws exception

500 TypeError: Cannot read property 'orm_special_object' of null

    at Builder.build (.\node_modules\orm\lib\sql\Select.js:130:38)
    at Driver.find(.\node_modules\orm\lib\Drivers\DML\mysql.js:94:34)
    at Object.run (.\node_modules\orm\lib\ChainFind.js:75:16)
    at Function.model.find (.\node_modules\orm\lib\Model.js:242:9)

Thank you for your great work on project.

Example of use with SQLite

When I'm trying to connect to sqlite database:

var orm = require('orm');
orm.connect('db',function (err,db) {

  if (err) {
      throw err;
  }

  res.render('index'); 
});

It gets me an error

500 TypeError: Cannot call method 'replace' of undefined
at Object.exports.connect (D:\Dropbox\sites\myblog\node_modules\orm\lib\ORM.js:49:29)

Data cache leaking?

Consider these models:

var Movie = db.define('movies', {
    id: Number,
    title: String,
    year: Number,
    rating: Number
});

var Genre = db.define('genres', {
    id: Number,
    title: String
});

Movie.hasMany('genres', Genre); // link genres, no autofetch

When calling this piece of code (I'll name it snippet A, as I'll refer back to it later on) ...

Movie.find({}, 100, function(err, movies) {
    // ...
}

... and then outputting movies on screen in the callback you get this dataset for example:

[
    {
        "id": 1,
        "title": "Futurama: Bender's Big Score",
        "year": 2007,
        "rating": 7.6
    },
    {
        "id": 2,
        "title": "Futurama: The Beast with a Billion Backs",
        "year": 2008,
        "rating": 7.2,
    }
]

When then calling a different URL on the app to fetch one single movie with genres ...

Movie.get(id, function(err, movie) {
    movie.getGenres(function(err, genres) {
        movie.genres = genres;
        // ...
    }
});

... we get back this when outputting movie from within the second callback

{
    "id": 1,
    "title": "Futurama: Bender's Big Score",
    "year": 2007,
    "rating": 7.6,
    "genres": [
        {"id": 1, "title: "Animation"},
        {"id": 2, "title: "Action"},
        {"id": 3, "title: "Comedy"}
    ]
}

Now comes the kicker: when now visiting the URL that executes snippet A again, you get back this dataset:

[
    {
        "id": 1,
        "title": "Futurama: Bender's Big Score",
        "year": 2007,
        "rating": 7.6,
        "genres": [
            {"id": 1, "title: "Animation"},
            {"id": 2, "title: "Action"},
            {"id": 3, "title: "Comedy"}
        ]
    },
    {
        "id": 2,
        "title": "Futurama: The Beast with a Billion Backs",
        "year": 2008,
        "rating": 7.2,
    }
]

As you can see the genres mysteriously appear here now, which shouldn't be the case.

65535 limit hit on Blob

I'm uploading images to tables. The closest model data type i found was Buffer. However that refers to Blob which has a limit of 65535. I need LongBlob. Possible in the current implementation?

How to create tables?

Sorry if this is a stupid question, but: How do you create tables with node-orm2?

The original node-orm wiki mentions the .sync() method for models, which would create all the necessary tables...

But this method doesn't seem to exist in node-orm2. Am I supposed to create my tables manually? If so, I need some more information, like what kind of naming convention node-orm2 expects for association tables and foreign key field names, etc. (Or does this documentation already exist somewhere?)

Problems, saving data to DB which already comes with IDs preassigned

I read briefly through the source, and as it seems, you use the id as an indicator of whether to insert or update. In other words, if I am not mistaken, there is not a separate "update()" method, only a context-dependent "save()"

My problem is that in the app that I am writing, ids in one of the models, come from another web service, and are well defined. The problem is that whenever I want to save an item that I got from the web service, I will always do:

var m = new MyModel({id:webServiceResult.id, name:webServiceResult.name});

which will create a problem, since it does not yet exist in my DB, but ORM will consider it worthy for an update, because of the id. As a result, my data won't get saved.

Of course, I can follow your logic and introduce a simple auto-increasing ID, just for ORM, and leave the original ID in the table as "entityID". This will work, but will bring a bit of redundancy, and in general I am not a huge fan of it.

Of course, if there is nothing else I can do (I think overriding the "save" is also not a good idea), I will have to live with it for now. What do you think?

getting association failed when association id field name!='id'

in Many.js Instance[association.mergeId] must be used instead of Instance.id:

                   options.extra_info = {
            table: association.mergeTable,
            id: Instance[association.mergeId],
            id_prop: association.mergeId,
            assoc_prop: association.mergeAssocId
        };

        if (conditions === null) {
            conditions = {};
        }
        conditions[association.mergeTable + "." + association.mergeId] = Instance[association.mergeId];

sync does not create tables on PostgreSQL

sync(function(err) { /*...*/ } in release 2.0.4 does neither produce an error nor create the underlying tables. Reproducable by this snippet on PostgreSQL 9.0:

var orm = require('orm'),
    config = require('../config');

orm.connect('postgresql://' +
    config.database.username + ':' + config.database.password +
    '@' + config.database.host + '/' + config.database.name,
    function(err, db) {
        if(err) console.error(err);

        var Test = db.define('test', {
            val1: String,
            val2: Number,
            val3: Boolean,
            val4: [ 'A', 'B', 'C' ],
            val5: Buffer
        });

        Test.sync(function(err) {
            if(err) console.error(err);
            else console.log('Test synced');
        })

        db.sync(function(err) {
            if(err) console.error(err);
            else console.log('Database synced');
        });
    }
);

Output:

Test synced
Database synced

The database exists and the user does have the rights to access it and create tables.

updating issue

I am trying to update a model after doing get. I am getting the following error and not able to solve the issue. Please guide me in resolving the issue

Error: Cannot enqueue Query after invoking quit.
    at Protocol._validateEnqueue (/home/os/workspace/cronservice/node_modules/mysql/lib/protocol/Protocol.js:115:16)

My observation: This occurs after find. Looks like mysql is closing connection before update.

Here is the snippet of what i am trying to do.

function(updateParams,errCallBack,updateJobCallBack){
    var url = CONFIG.db+"://"+CONFIG.user+":"+CONFIG.password+"@"+CONFIG.server+":"+CONFIG.port+"/"+CONFIG.database;
    orm.connect(url,function(err,db){
            Job = db.define('cron_job', {
                id: String,
                pattern: String,
                url: String,
                identifier: String
            });
            Job.get(updateParams.jobId,function(err,job){
                job.pattern = updateParams.pattern;
                job.url = updateParams.url;
                job.save(function(err2){
                    if(err2) errCallBack(err2);
                });
                updateJobCallBack(job);
                db.close();
            });
        });
    };

Note: I am new to nodejs

Setting associations on object creation

I can't find anything in the documentation about setting associations when you first create an object. Is this supported?

For example...

var Person = db.define('person', { name: String })
var Pet = db.define('pet', { name: String })
Person.hasMany(pet);

var jake = new Person({ name: "Jake" });
jake.save(function(err) {
    var smokey = new Pet({ name: "Smokey" });
    smokey.save(function(err) {
        // Is this logic supported / correct?
        jake.addPet(smokey);
        jake.save();
    });
});

Any clarification would be appreciated. Thanks!

support for more fields

Well, i tried to add support for JSON type, and it needs both conversions - before save and after load. Property.js is global for all engines, so i was looking for engine related place. I think same situation will be with dates or boolean (some engines support bool, mysql not) etc.

Functions convertToValue and convertFromValue could be in:

  1. engine library (engine/mysql for example)
  2. query dialects (sql/*) but in this case, query have to be different for every engine
    (i think it's good because postgre/sqlite/mysql have slightly different language)

I think first one is better.

`beforeCreate` and Model Properties

Hi,

Thanks a lot for this project. I was wondering if I'm doing something wrong here—I expect to be able to modify the properties of a model in beforeCreate, but Postgres shows the following in the database:

 id |        email        | password | salt | created 
----+---------------------+----------+------+---------
  1 | [email protected]      | visible? |      | 
var orm    = require('orm'),
    bcrypt = require('bcrypt');

orm.connect('postgres://localhost/database', function (err, db) {
  var Person = db.define('Person', {
    email: String,
    password: String,
    salt: String,
    created: Date
  }, {
    validations: {
      email: orm.validators.unique()
    },
    hooks: {
      beforeCreate: function () {
        if (!this.salt) {
          console.log('Generating salt...');
          this.salt = bcrypt.genSaltSync(10);
          this.created = new Date();
        }
        if (this.password) {
          console.log('Hashing password...');
          this.password = bcrypt.hashSync(this.password, this.salt);
        }
      }
    }
  });
  // Person.sync(function (err) { console.log(err); });
  Person.create([{
    email: '[email protected]',
    password: 'visible?'
  }], function (err, items) {
    console.log(err);
    // console.log(items);
  });
});

Testing with beforeSave yields different results for me, too: the password gets hashed, but salt and created go unset in the database.

hasMany on same table

Trying to solve tree heirarchy through orm.

CREATE TABLE `category` (
  `id` int(11) NOT NULL,
  `name` varchar(1024) COLLATE utf8_czech_ci NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `category_tree` (
  `predecessor_id` int(11) NOT NULL,
  `successor_id` int(11) NOT NULL,
  `distance` int(11) NOT NULL,
  PRIMARY KEY (`predecessor_id`,`successor_id`),
  KEY `FK_category_tree_successor` (`successor_id`),
  CONSTRAINT `FK_category_tree_predecessor` FOREIGN KEY (`predecessor_id`) REFERENCES `category` (`id`),
  CONSTRAINT `FK_category_tree_successor` FOREIGN KEY (`successor_id`) REFERENCES `category` (`id`)
);

Defined model

var db = config.db;
var Category = db.define('category', {
    //properties
    id : Number,
    name : String
},{
    //options
}); 

Category.hasMany("successors", {distance : Number}, Category, {reverse: 'predecessors', mergeTable: 'category_tree', mergeId: 'predecessor_id', mergeAssocId: 'successor_id'});

Executing this:

Category(6).getSuccessors({distance: 1}, ['name', 'A'], function(err, categories) {
    res.json(categories);
});     

generates:

SELECT * FROM `category` WHERE `distance` = 1 AND `category_tree`.`predecessor_id` = 6 ORDER BY `name` ASC 

hasMany countAccessor

Hello,
is possible to add countAccessor to Many association?

Model

var db = config.db;
var Category = db.define('category', {
    //properties
    id : Number,
    name : String
},{
    //options
}); 
Category.hasMany("successors", {distance : Number}, Category, {reverse: 'predecessors', mergeTable: 'category_tree', mergeId: 'predecessor_id', mergeAssocId: 'successor_id'});

Controller

var Category = require('../models/Category');
exports.countSuccessors = function(req, res, next) {
    category_id = Number(req.params['category']);
    Category(category_id).countSuccessors({distance:1}, function (err, count) {     
        res.json(count);
    });

}

help in development

Hi dresende,

i tried many nodejs libraries (sequelizejs, resourceful, tubbs,..) but this one is most promising i've seen - it's well designed to be engine agnostic, has query builder, model is prepared for bussiness logic (not so tied with orm).

Can i help with some parts to speedup development? plugins, fields, validation, engines.

Andrew

Underlying data changed - not reflected in find results

I have a find call which brings in rows of data just fine. I can modify the data and save it back and all data is perfect.

However if i call the find method > results output > then go and change from the database directly some value in those records > results are the same as the original output.

I can stop the node app and restart it and the new db change comes in. Its like a singleton instance is created and is caching the results.

Note: I have tried adding {cache: false} in my find call. No luck.
Can anyone replicate my issue? I'm running on mysql.

db.close() not allowing any other associated data to be queried

I have multiple chunks of code in separate areas where on any given page request i perform a few db connections. eg auth, page logic etc... I am finding that any related records (hasMany) seem to bomb out once db.close is called in any other separate area. Its like there is a singleton connection hiding in there somewhere.
My error (running mysql): "Error: Cannot enqueue Query after being destroyed."

Some code which I have thrown in to app.js createServer ...

_db.openAndSync(function(db) {
        console.log('1 opened db and performed sync');

        // get artist
        db.Artist.get(1, function (err, artist) {
            if (err) throw err;
            console.log('1 artist from first get: ' + artist.name);
            // get associated artist genres
            artist.getGenre(function(err, artistGenres) {
                if (err) throw err;
                console.log('1 artist: ' + artist.name);
                db.close(); // < close it

                // lets try that again ...
                _db.openAndSync(function(db) {
                    console.log('2 opened db and performed sync');

                    // get artist
                    db.Artist.get(1, function (err, artist) {
                        if (err) throw err;
                        console.log('2 artist from first get: ' + artist.name);
                        // get associated artist genres
                        artist.getGenre(function(err, artistGenres) {
                            if (err) throw err;
                            console.log('2 artist: ' + artist.name);
                            db.close(); // < close it
                        });
                    });

                });

            });
        });

    });

my output to console:

1 opened db and performed sync
1 artist from first get: The Testers1
1 artist: The Testers1
2 opened db and performed sync
2 artist from first get: The Testers1
Error: Cannot enqueue Query after being destroyed.

Note:

  • _db is just a require to a file with your regular run of the mill callback of db "orm.connect(connectionString, function (err, db) {"
  • I even tried updating mysql.js to
    //this.db.end(cb);
    this.db.destroy(cb);

It would help so much if anyone could chime in. I'd like to keep my app having separate connections for each db hit as opposed to 1 long running connection.

SSL support for PostgreSQL

Is there any way to turn on SSL for psql or is it planned?

I noticed that require('pg').native may be required for this.

Model.find using sort array param : "Cannot read property 'length' of null"

First I want to thank you for your work on the project, it helps me a lot. I think that I found a little issue, so there's a report.

Function Model.find throws exception when using sort array param.

code snippet

Product.find({}, {offset: param['offset']}, 10, ['name', 'Z'], function(err, products) {
    res.json(products);
}); 

result

 Cannot read property 'length' of null
    at Function.model.find (.\node_modules\orm\lib\Model.js:206:16)
    at exports.index (.\app\controllers\ProductController.js:6:10)
    at Resource.map (.\node_modules\express-resource\index.js:163:7)
    at callbacks (.\node_modules\express\lib\router\index.js:161:37)
    at param (.\node_modules\express\lib\router\index.js:135:11)
    at param (.\node_modules\express\lib\router\index.js:132:11)
    at pass (.\node_modules\express\lib\router\index.js:142:5)
    at Router._dispatch (.\node_modules\express\lib\router\index.js:170:5)
    at Object.router (.\node_modules\express\lib\router\index.js:33:10)
    at next (.\node_modules\express\node_modules\connect\lib\proto.js:199:15)

solution

file:Model.js
row:206

replace

                        if (order.length > 0) {

with

                        if (order == null || order.length == 0) {

no integer type available in models

Storing all numbers in float format is not always good idea in my opinion. I suggest you to add an optional({type:'int'}) integer type.

I am new for GitHub, how can I contribute this project and suggest you some patches?

Sql builder generating incorrect string if a value is undefined

If a property on a model is not given a value the sql string in the builder is not generated correctly.

ie if no last name is given:

insert into customer (first_name, last_name, age)
values ('bob', , 36)

Would it be correct if I made a change to the code which inserted a null into the missing field. ie:

insert into customer (first_name, last_name, age)
values ('bob', null, 36)

Cannot read property 'timezone' of undefined

Looks like the context of escape is incorrect when this.escape is called at line 120 of lib/sql/Select.js and therefore the timezone can't be read from the config.

Binding the escape function fixed it for the mysql driver, changing this to this:

var escapes = {
    escape   : this.db.escape.bind(this.db),
    escapeId : escapeId
};

Not sure if there's any other issues with that or if you want to take a different approach.

Thanks

Reverse hasMany associations?

I may have a hasMany from Person to Pet. But what about the reverse lookup of all persons associated to a single pet?

Person.getPet

and

Pet.getPersons??

or is this functionality not yet in the build...

Problems connecting to PostgreSQL, under OSX 10.6.8

I am trying to make a small nodejs app run under osx (10.6.8), and connect to my localhost Postgres database. I am using node-orm (https://github.com/dresende/node-orm2) as well as the Postgres bindings (https://github.com/brianc/node-postgres).

For some reason, when I use orm.connect and give it the connection url as a string, it fails with:

Error: getaddrinfo ENOENT at errnoException (dns.js:31:11) at Object.onanswer [as oncomplete] (dns.js:123:16)

If I change localhost with 127.0.0.1. it also dies with a timeout exception.

Using the vanilla postgress bindings to connect to the DB, using the same connection string is successful, and I even managed to get a few test queries running, got results, etc.

I tried to set up the DB client manually using the vanilla postgress api, an pass it on to orm using orm.use. This manages to connect successfully to the DB, but every time I try to execute a query, nothing happens. It does not generate an error, but sinply gets stuck in the QueryQueue and does not call the success callback. I checked the DB logs, and it seems that it has not detected the query either.

What do I do? I am a bit confused

Many association ordering doesn't work after conversion to sql-query

Hi,

Thank you very much for your work on this interresting project. It helps me a lot.

It seems there is a problem with ordering of query through many association after the conversion to sql-query using.

Regarding to #68 model definition following code produced error.

var Category = require('../models/Category');

exports.subcategories = function(req, res, next) {
    Category(Number(req.params['category'])).getSuccessors({distance: 1},['name', 'A'], function(err, categories) {
        res.json(categories);
    });
}

Produced SQL Code

SELECT
  *
FROM 
  `category` AS `t1` 
  JOIN `category_tree` AS `t2` ON `t2`.`successor_id` = `t1`.`id` 
WHERE 
  (`t2`.`predecessor_id` = 7 AND `distance` = 1) 
ORDER BY 
  `category.name` ASC

There is a table name used instead of table alias in order by clause.

With regards,
Kody

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.