Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- '0.6'
- '0.8'
- '0.10'
before_script:
Expand Down
3 changes: 1 addition & 2 deletions lib/Associations/Many.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ function extendInstance(Model, Instance, Driver, association, opts, createInstan
var opts = {};
var cb = noOperation;
var run = function () {
var savedAssociations = [];

var savedAssociations = [];
var saveNextAssociation = function () {
if (Associations.length === 0) {
return cb(null, savedAssociations);
Expand Down
23 changes: 13 additions & 10 deletions lib/Drivers/DDL/SQL.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
var _ = require("lodash");
var Sync = require("sql-ddl-sync").Sync;

exports.sync = function (dialect, driver, opts, cb) {
exports.sync = function (opts, cb) {
var sync = new Sync({
dialect : dialect,
db : driver.db,
driver : this,
debug : false//function (text) { console.log(text); }
});

Expand All @@ -14,9 +13,9 @@ exports.sync = function (dialect, driver, opts, cb) {
};
var props = {};

if (driver.customTypes) {
for (var k in driver.customTypes) {
sync.defineType(k, driver.customTypes[k]);
if (this.customTypes) {
for (var k in this.customTypes) {
sync.defineType(k, this.customTypes[k]);
}
}
for (var k in opts.allProperties) {
Expand All @@ -41,24 +40,28 @@ exports.sync = function (dialect, driver, opts, cb) {
}

sync.sync(cb);

return this;
};

exports.drop = function (dialect, driver, opts, cb) {
exports.drop = function (opts, cb) {
var i, queries = [], pending;

queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.table));
queries.push("DROP TABLE IF EXISTS " + this.query.escapeId(opts.table));

for (i = 0; i < opts.many_associations.length; i++) {
queries.push("DROP TABLE IF EXISTS " + driver.query.escapeId(opts.many_associations[i].mergeTable));
queries.push("DROP TABLE IF EXISTS " + this.query.escapeId(opts.many_associations[i].mergeTable));
}

pending = queries.length;

for (i = 0; i < queries.length; i++) {
driver.execQuery(queries[i], function (err) {
this.execQuery(queries[i], function (err) {
if (--pending === 0) {
return cb(err);
}
});
}

return this;
};
4 changes: 2 additions & 2 deletions lib/Drivers/helpers.js → lib/Drivers/DML/_shared.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports.sql = {
module.exports = {
execQuery: function () {
if (arguments.length == 2) {
var query = arguments[0];
Expand All @@ -10,4 +10,4 @@ module.exports.sql = {
}
return this.execSimpleQuery(query, cb);
}
}
};
20 changes: 5 additions & 15 deletions lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
var _ = require("lodash");
var mysql = require("mysql");
var Query = require("sql-query").Query;
var helpers = require("../helpers");
var shared = require("./_shared");
var DDL = require("../DDL/SQL");

exports.Driver = Driver;

function Driver(config, connection, opts) {
this.dialect = 'mysql';
this.config = config || {};
this.opts = opts || {};
this.customTypes = {};

if (!this.config.timezone) {
this.config.timezone = "local";
}
this.query = new Query({ dialect: "mysql", timezone: this.config.timezone });
this.query = new Query({ dialect: this.dialect, timezone: this.config.timezone });

this.reconnect(null, connection);

Expand All @@ -26,19 +28,7 @@ function Driver(config, connection, opts) {
"DISTINCT"];
}

_.extend(Driver.prototype, helpers.sql);

Driver.prototype.sync = function (opts, cb) {
require("../DDL/SQL").sync("mysql", this, opts, cb);

return this;
};

Driver.prototype.drop = function (opts, cb) {
require("../DDL/SQL").drop("mysql", this, opts, cb);

return this;
};
_.extend(Driver.prototype, shared, DDL);

Driver.prototype.ping = function (cb) {
this.db.ping(cb);
Expand Down
20 changes: 5 additions & 15 deletions lib/Drivers/DML/postgres.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var _ = require("lodash");
var pg = require("pg");
var Query = require("sql-query").Query;
var helpers = require("../helpers");
var shared = require("./_shared");
var DDL = require("../DDL/SQL");

exports.Driver = Driver;

Expand Down Expand Up @@ -71,14 +72,15 @@ var switchableFunctions = {
function Driver(config, connection, opts) {
var functions = switchableFunctions.client;

this.dialect = 'postgresql';
this.config = config || {};
this.opts = opts || {};

if (!this.config.timezone) {
this.config.timezone = "local";
}

this.query = new Query({ dialect: "postgresql", timezone: this.config.timezone });
this.query = new Query({ dialect: this.dialect, timezone: this.config.timezone });
this.customTypes = {};

if (connection) {
Expand Down Expand Up @@ -115,19 +117,7 @@ function Driver(config, connection, opts) {
];
}

_.extend(Driver.prototype, helpers.sql);

Driver.prototype.sync = function (opts, cb) {
require("../DDL/SQL").sync("postgresql", this, opts, cb);

return this;
};

Driver.prototype.drop = function (opts, cb) {
require("../DDL/SQL").drop("postgresql", this, opts, cb);

return this;
};
_.extend(Driver.prototype, shared, DDL);

Driver.prototype.ping = function (cb) {
this.execSimpleQuery("SELECT * FROM pg_stat_activity LIMIT 1", function () {
Expand Down
20 changes: 5 additions & 15 deletions lib/Drivers/DML/sqlite.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ var _ = require("lodash");
var util = require("util");
var sqlite3 = require("sqlite3");
var Query = require("sql-query").Query;
var helpers = require("../helpers");
var shared = require("./_shared");
var DDL = require("../DDL/SQL");

exports.Driver = Driver;

function Driver(config, connection, opts) {
this.dialect = 'sqlite';
this.config = config || {};
this.opts = opts || {};

if (!this.config.timezone) {
this.config.timezone = "local";
}

this.query = new Query({ dialect: "sqlite", timezone: this.config.timezone });
this.query = new Query({ dialect: this.dialect, timezone: this.config.timezone });
this.customTypes = {};

if (connection) {
Expand All @@ -37,19 +39,7 @@ function Driver(config, connection, opts) {
"DISTINCT" ];
}

_.extend(Driver.prototype, helpers.sql);

Driver.prototype.sync = function (opts, cb) {
require("../DDL/SQL").sync("sqlite", this, opts, cb);

return this;
};

Driver.prototype.drop = function (opts, cb) {
require("../DDL/SQL").drop("sqlite", this, opts, cb);

return this;
};
_.extend(Driver.prototype, shared, DDL);

Driver.prototype.ping = function (cb) {
process.nextTick(cb);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sqlite",
"mongodb"
],
"version" : "2.1.3",
"version" : "2.1.4",
"license" : "MIT",
"homepage" : "http://dresende.github.io/node-orm2",
"repository" : "http://github.com/dresende/node-orm2.git",
Expand All @@ -38,7 +38,7 @@
"dependencies": {
"enforce" : "0.1.2",
"sql-query" : "0.1.16",
"sql-ddl-sync" : "git://github.com/dresende/node-sql-ddl-sync.git#v0.1.5",
"sql-ddl-sync" : "git://github.com/dresende/node-sql-ddl-sync.git#v0.2.0",
"hat" : "0.0.3",
"lodash" : "2.4.1"
},
Expand Down
69 changes: 33 additions & 36 deletions test/integration/association-hasmany.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
var _ = require('lodash');
var should = require('should');
var helper = require('../support/spec_helper');
var ORM = require('../../');
Expand Down Expand Up @@ -291,61 +292,59 @@ describe("hasMany", function () {
describe("addAccessor", function () {
before(setup());

if (common.protocol() == "mongodb") return;

it("might add duplicates", function (done) {
Pet.find({ name: "Mutt" }, function (err, pets) {
Person.find({ name: "Jane" }, function (err, people) {
should.equal(err, null);

people[0].addPets(pets[0], function (err) {
if (common.protocol() != "mongodb") {
it("might add duplicates", function (done) {
Pet.find({ name: "Mutt" }, function (err, pets) {
Person.find({ name: "Jane" }, function (err, people) {
should.equal(err, null);

people[0].getPets("name", function (err, pets) {
people[0].addPets(pets[0], function (err) {
should.equal(err, null);

should(Array.isArray(pets));
pets.length.should.equal(2);
pets[0].name.should.equal("Mutt");
pets[1].name.should.equal("Mutt");
people[0].getPets("name", function (err, pets) {
should.equal(err, null);

return done();
should(Array.isArray(pets));
pets.length.should.equal(2);
pets[0].name.should.equal("Mutt");
pets[1].name.should.equal("Mutt");

return done();
});
});
});
});
});
});
});

describe("addAccessor", function () {
before(setup());
}

it("should keep associations and add new ones", function (done) {
Pet.find({ name: "Deco" }).first(function (err, Deco) {
Person.find({ name: "Jane" }).first(function (err, Jane) {
should.equal(err, null);

Jane.addPets(Deco, function (err) {
should.equal(err, null);
Jane.getPets(function (err, janesPets) {
should.not.exist(err);

var petsAtStart = janesPets.length;

Jane.getPets("name", function (err, pets) {
Jane.addPets(Deco, function (err) {
should.equal(err, null);

should(Array.isArray(pets));
pets.length.should.equal(2);
pets[0].name.should.equal("Deco");
pets[1].name.should.equal("Mutt");
Jane.getPets("name", function (err, pets) {
should.equal(err, null);

return done();
should(Array.isArray(pets));
pets.length.should.equal(petsAtStart + 1);
pets[0].name.should.equal("Deco");
pets[1].name.should.equal("Mutt");

return done();
});
});
});
});
});
});
});

describe("addAccessor", function () {
before(setup());

it("should accept several arguments as associations", function (done) {
Pet.find(function (err, pets) {
Expand All @@ -369,16 +368,14 @@ describe("hasMany", function () {
});

it("should accept array as list of associations", function (done) {
Pet.find(function (err, pets) {
var petCount = pets.length;

Pet.create([{ name: 'Ruff' }, { name: 'Spotty' }],function (err, pets) {
Person.find({ name: "Justin" }).first(function (err, Justin) {
should.equal(err, null);

Justin.getPets(function (err, justinsPets) {
should.equal(err, null);

should.equal(justinsPets.length, 2);
var petCount = justinsPets.length;

Justin.addPets(pets, function (err) {
should.equal(err, null);
Expand All @@ -387,7 +384,7 @@ describe("hasMany", function () {
should.equal(err, null);

should(Array.isArray(justinsPets));
// We're not checking uniqueness.
// Mongo doesn't like adding duplicates here, so we add new ones.
should.equal(justinsPets.length, petCount + 2);

return done();
Expand Down