Comparing version 2.0.0-alpha4 to 2.0.0-alpha5
var orm = require("../lib/ORM"); | ||
orm.connect("mysql://root:tedua@localhost/orm", function (err, db) { | ||
orm.connect("mysql://root:tedua@localhost/orm?debug=true", function (err, db) { | ||
if (err) { | ||
@@ -20,9 +20,12 @@ throw err; | ||
} | ||
} | ||
}, | ||
autoFetch: true | ||
}); | ||
Person.hasMany("friends", { | ||
rate : Number | ||
}); | ||
Person.get(1, function (err, John) { | ||
console.log(err, John); | ||
console.log(John.fullName()); | ||
Person.get(1, function (err, Jane) { | ||
// console.log(Jane); | ||
}); | ||
}); |
@@ -5,8 +5,28 @@ var InstanceConstructor = require("../Instance").Instance; | ||
exports.prepare = function (Model, associations) { | ||
Model.hasMany = function (name, OtherModel, opts) { | ||
if (typeof OtherModel == "object" && !OtherModel.table) { | ||
opts = OtherModel; | ||
OtherModel = null; | ||
Model.hasMany = function () { | ||
var name; | ||
var OtherModel = Model; | ||
var props = null; | ||
var opts = {}; | ||
for (var i = 0; i < arguments.length; i++) { | ||
switch (typeof arguments[i]) { | ||
case "string": | ||
name = arguments[i]; | ||
break; | ||
case "function": | ||
OtherModel = arguments[i]; | ||
break; | ||
case "object": | ||
if (props === null) { | ||
props = arguments[i]; | ||
} else { | ||
opts = arguments[i]; | ||
} | ||
break; | ||
} | ||
} | ||
opts = opts || {}; | ||
if (props === null) { | ||
props = {}; | ||
} | ||
@@ -19,2 +39,3 @@ var assocName = opts.name || name[0].toUpperCase() + | ||
model : OtherModel || Model, | ||
props : props, | ||
mergeTable : opts.mergeTable || (Model.table + "_" + name), | ||
@@ -55,5 +76,8 @@ mergeId : opts.mergeId || (Model.table + "_id"), | ||
Object.defineProperty(Instance, association.getFunction, { | ||
value: function (cb) { | ||
var conditions = {}, options = {}; | ||
conditions[association.mergeTable + "." + association.mergeId] = Instance.id; | ||
value: function () { | ||
var conditions = {}; | ||
var options = {}; | ||
var limit; | ||
var cb; | ||
options.__merge = { | ||
@@ -63,4 +87,27 @@ from: { table: association.mergeTable, field: association.mergeAssocId }, | ||
}; | ||
options.extra = association.props; | ||
options.extra_info = { | ||
table: association.mergeTable, | ||
id: Instance.id, | ||
id_prop: association.mergeId, | ||
assoc_prop: association.mergeAssocId | ||
}; | ||
association.model.find(conditions, options, cb); | ||
for (var i = 0; i < arguments.length; i++) { | ||
switch (typeof arguments[i]) { | ||
case "function": | ||
cb = arguments[i]; | ||
break; | ||
case "object": | ||
conditions = arguments[i]; | ||
break; | ||
case "number": | ||
limit = arguments[i]; | ||
break; | ||
} | ||
} | ||
conditions[association.mergeTable + "." + association.mergeId] = Instance.id; | ||
association.model.find(conditions, limit, options, cb); | ||
return this; | ||
@@ -118,23 +165,30 @@ }, | ||
Object.defineProperty(Instance, association.addFunction, { | ||
value: function (Association, opts, cb) { | ||
if (typeof opts == "function") { | ||
cb = opts; | ||
opts = {}; | ||
value: function () { | ||
var Associations = []; | ||
var opts = {}; | ||
var cb; | ||
for (var i = 0; i < arguments.length; i++) { | ||
switch (typeof arguments[i]) { | ||
case "function": | ||
cb = arguments[i]; | ||
break; | ||
case "object": | ||
if (arguments[i].isInstance) { | ||
Associations.push(arguments[i]); | ||
} else { | ||
opts = arguments[i]; | ||
} | ||
break; | ||
} | ||
} | ||
opts = opts || {}; | ||
Association.save(function (err) { | ||
if (err) { | ||
return cb(err); | ||
var saveNextAssociation = function () { | ||
if (Associations.length === 0) { | ||
return cb(); | ||
} | ||
var data = {}; | ||
data[association.mergeId] = Instance.id; | ||
data[association.mergeAssocId] = Association.id; | ||
var Association = Associations.pop(); | ||
for (var k in opts) { | ||
data[k] = opts[k]; | ||
} | ||
Driver.insert(association.mergeTable, data, function (err) { | ||
Association.save(function (err) { | ||
if (err) { | ||
@@ -144,5 +198,22 @@ return cb(err); | ||
return cb(null); | ||
var data = {}; | ||
data[association.mergeId] = Instance.id; | ||
data[association.mergeAssocId] = Association.id; | ||
for (var k in opts) { | ||
data[k] = opts[k]; | ||
} | ||
Driver.insert(association.mergeTable, data, function (err) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
return saveNextAssociation(); | ||
}); | ||
}); | ||
}); | ||
}; | ||
saveNextAssociation(); | ||
return this; | ||
@@ -149,0 +220,0 @@ }, |
@@ -5,9 +5,11 @@ var mysql = require("mysql"); | ||
function Driver(opts, connection) { | ||
this.opts = opts || {}; | ||
if (!this.opts.timezone) { | ||
function Driver(config, connection, opts) { | ||
this.config = config || {}; | ||
this.opts = opts || {}; | ||
if (!this.config.timezone) { | ||
// force UTC if not defined, UTC is always better.. | ||
this.opts.timezone = "Z"; | ||
this.config.timezone = "Z"; | ||
} | ||
this.db = (connection ? connection : mysql.createConnection(opts)); | ||
this.db = (connection ? connection : mysql.createConnection(config)); | ||
@@ -60,2 +62,5 @@ var escapes = { | ||
this.db.query(this.QuerySelect.build(), cb); | ||
if (this.opts.debug) { | ||
require("../Debug").sql('mysql', this.QuerySelect.build()); | ||
} | ||
}; | ||
@@ -71,2 +76,5 @@ | ||
if (this.opts.debug) { | ||
require("../Debug").sql('mysql', this.QueryInsert.build()); | ||
} | ||
this.db.query(this.QueryInsert.build(), function (err, info) { | ||
@@ -82,11 +90,16 @@ if (err) { | ||
Driver.prototype.update = function (table, changes, id_prop, id, cb) { | ||
Driver.prototype.update = function (table, changes, conditions, cb) { | ||
this.QueryUpdate | ||
.clear() | ||
.table(table) | ||
.where(id_prop, id); | ||
for (var k in changes) { | ||
.table(table); | ||
for (var k in conditions) { | ||
this.QueryUpdate.where(k, conditions[k]); | ||
} | ||
for (k in changes) { | ||
this.QueryUpdate.set(k, changes[k]); | ||
} | ||
if (this.opts.debug) { | ||
require("../Debug").sql('mysql', this.QueryUpdate.build()); | ||
} | ||
this.db.query(this.QueryUpdate.build(), cb); | ||
@@ -103,2 +116,5 @@ }; | ||
if (this.opts.debug) { | ||
require("../Debug").sql('mysql', this.QueryRemove.build()); | ||
} | ||
this.db.query(this.QueryRemove.build(), cb); | ||
@@ -108,3 +124,7 @@ }; | ||
Driver.prototype.clear = function (table, cb) { | ||
this.db.query("TRUNCATE TABLE " + escapeId(table), cb); | ||
var query = "TRUNCATE TABLE " + escapeId(table); | ||
if (this.opts.debug) { | ||
require("../Debug").sql('mysql', query); | ||
} | ||
this.db.query(query, cb); | ||
}; | ||
@@ -111,0 +131,0 @@ |
@@ -5,5 +5,6 @@ var postgres = require("pg"); | ||
function Driver(opts, connection) { | ||
this.opts = opts || {}; | ||
this.db = (connection ? connection : new postgres.Client(opts)); | ||
function Driver(config, connection, opts) { | ||
this.config = config || {}; | ||
this.opts = opts || {}; | ||
this.db = (connection ? connection : new postgres.Client(config)); | ||
@@ -52,2 +53,5 @@ var escapes = { | ||
if (this.opts.debug) { | ||
require("../Debug").sql('postgres', this.QuerySelect.build()); | ||
} | ||
this.db.query(this.QuerySelect.build(), handleQuery(cb)); | ||
@@ -63,3 +67,5 @@ }; | ||
} | ||
if (this.opts.debug) { | ||
require("../Debug").sql('postgres', this.QueryInsert.build()); | ||
} | ||
this.db.query(this.QueryInsert.build() + " RETURNING *", function (err, result) { | ||
@@ -75,11 +81,15 @@ if (err) { | ||
Driver.prototype.update = function (table, changes, id_prop, id, cb) { | ||
Driver.prototype.update = function (table, changes, conditions, cb) { | ||
this.QueryUpdate | ||
.clear() | ||
.table(table) | ||
.where(id_prop, id); | ||
for (var k in changes) { | ||
.table(table); | ||
for (var k in conditions) { | ||
this.QueryUpdate.where(k, conditions[k]); | ||
} | ||
for (k in changes) { | ||
this.QueryUpdate.set(k, changes[k]); | ||
} | ||
if (this.opts.debug) { | ||
require("../Debug").sql('postgres', this.QueryUpdate.build()); | ||
} | ||
this.db.query(this.QueryUpdate.build(), handleQuery(cb)); | ||
@@ -96,2 +106,5 @@ }; | ||
if (this.opts.debug) { | ||
require("../Debug").sql('postgres', this.QueryRemove.build()); | ||
} | ||
this.db.query(this.QueryRemove.build(), handleQuery(cb)); | ||
@@ -101,3 +114,8 @@ }; | ||
Driver.prototype.clear = function (table, cb) { | ||
this.db.query("TRUNCATE TABLE " + escapeId(table), handleQuery(cb)); | ||
var query = "TRUNCATE TABLE " + escapeId(table); | ||
if (this.opts.debug) { | ||
require("../Debug").sql('postgres', query); | ||
} | ||
this.db.query(query, handleQuery(cb)); | ||
}; | ||
@@ -104,0 +122,0 @@ |
@@ -5,5 +5,6 @@ var sqlite3 = require("sqlite3"); | ||
function Driver(opts, connection) { | ||
this.opts = opts || {}; | ||
this.db = (connection ? connection : new sqlite3.Database(opts.pathname || ':memory:')); | ||
function Driver(config, connection, opts) { | ||
this.config = config || {}; | ||
this.opts = opts || {}; | ||
this.db = (connection ? connection : new sqlite3.Database(config.pathname || ':memory:')); | ||
@@ -56,2 +57,5 @@ var escapes = { | ||
if (this.opts.debug) { | ||
require("../Debug").sql('sqlite', this.QuerySelect.build()); | ||
} | ||
this.db.all(this.QuerySelect.build(), cb); | ||
@@ -68,2 +72,5 @@ }; | ||
if (this.opts.debug) { | ||
require("../Debug").sql('sqlite', this.QueryInsert.build()); | ||
} | ||
this.db.all(this.QueryInsert.build(), function (err, info) { | ||
@@ -84,11 +91,16 @@ if (err) { | ||
Driver.prototype.update = function (table, changes, id_prop, id, cb) { | ||
Driver.prototype.update = function (table, changes, conditions, cb) { | ||
this.QueryUpdate | ||
.clear() | ||
.table(table) | ||
.where(id_prop, id); | ||
for (var k in changes) { | ||
.table(table); | ||
for (var k in conditions) { | ||
this.QueryUpdate.where(k, conditions[k]); | ||
} | ||
for (k in changes) { | ||
this.QueryUpdate.set(k, changes[k]); | ||
} | ||
if (this.opts.debug) { | ||
require("../Debug").sql('sqlite', this.QueryUpdate.build()); | ||
} | ||
this.db.all(this.QueryUpdate.build(), cb); | ||
@@ -105,2 +117,5 @@ }; | ||
if (this.opts.debug) { | ||
require("../Debug").sql('sqlite', this.QueryRemove.build()); | ||
} | ||
this.db.all(this.QueryRemove.build(), cb); | ||
@@ -110,3 +125,8 @@ }; | ||
Driver.prototype.clear = function (table, cb) { | ||
this.db.all("DELETE FROM " + escapeId(table), cb); | ||
var query = "DELETE FROM " + escapeId(table); | ||
if (this.opts.debug) { | ||
require("../Debug").sql('sqlite', query); | ||
} | ||
this.db.all(query, cb); | ||
}; | ||
@@ -113,0 +133,0 @@ |
@@ -9,4 +9,6 @@ var Property = require("./Property"); | ||
opts.data = opts.data || {}; | ||
opts.extra = opts.extra || {}; | ||
opts.id = opts.id || "id"; | ||
opts.changes = []; | ||
opts.extrachanges = []; | ||
@@ -60,3 +62,3 @@ var events = {}; | ||
if (opts.changes.length === 0) { | ||
return cb(null, instance); | ||
return saveInstanceExtra(cb); | ||
} | ||
@@ -100,11 +102,15 @@ | ||
if (typeof cb == "function") { | ||
cb(err, instance); | ||
if (err) { | ||
return cb(err, instance); | ||
} | ||
return saveInstanceExtra(cb); | ||
} | ||
}); | ||
} else { | ||
var changes = {}; | ||
var changes = {}, conditions = {}; | ||
for (var i = 0; i < opts.changes.length; i++) { | ||
changes[opts.changes[i]] = data[opts.changes[i]]; | ||
} | ||
opts.driver.update(opts.table, changes, opts.id, data[opts.id], function (err) { | ||
conditions[opts.id] = data[opts.id]; | ||
opts.driver.update(opts.table, changes, conditions, function (err) { | ||
if (!err) { | ||
@@ -116,3 +122,6 @@ opts.changes.length = 0; | ||
if (typeof cb == "function") { | ||
cb(err, instance); | ||
if (err) { | ||
return cb(err, instance); | ||
} | ||
return saveInstanceExtra(cb); | ||
} | ||
@@ -123,2 +132,21 @@ }); | ||
}; | ||
var saveInstanceExtra = function (cb) { | ||
if (opts.extrachanges.length === 0) { | ||
return cb(null, instance); | ||
} | ||
var data = {}; | ||
var conditions = {}; | ||
for (var i = 0; i < opts.extrachanges.length; i++) { | ||
data[opts.extrachanges[i]] = opts.data[opts.extrachanges[i]]; | ||
} | ||
conditions[opts.extra_info.id_prop] = opts.extra_info.id; | ||
conditions[opts.extra_info.assoc_prop] = opts.data[opts.id]; | ||
opts.driver.update(opts.extra_info.table, data, conditions, function (err) { | ||
return cb(err, instance); | ||
}); | ||
}; | ||
var removeInstance = function (cb) { | ||
@@ -143,3 +171,3 @@ if (!opts.data.hasOwnProperty(opts.id)) { | ||
var saveInstanceProperty = function (key, value) { | ||
var changes = {}; | ||
var changes = {}, conditions = {}; | ||
changes[key] = value; | ||
@@ -154,5 +182,7 @@ | ||
conditions[opts.id] = opts.data[opts.id]; | ||
Hook.trigger(instance, opts.hooks.beforeSave); | ||
opts.driver.update(opts.table, changes, opts.id, opts.data.id, function (err) { | ||
opts.driver.update(opts.table, changes, conditions, function (err) { | ||
if (!err) { | ||
@@ -186,6 +216,32 @@ opts.data[key] = value; | ||
}; | ||
var addInstanceExtraProperty = function (key) { | ||
if (!instance.hasOwnProperty("extra")) { | ||
instance.extra = {}; | ||
} | ||
Object.defineProperty(instance.extra, key, { | ||
get: function () { | ||
return opts.data[key]; | ||
}, | ||
set: function (val) { | ||
opts.data[key] = val; | ||
/*if (opts.autoSave) { | ||
saveInstanceProperty(key, val); | ||
}*/if (opts.extrachanges.indexOf(key) == -1) { | ||
opts.extrachanges.push(key); | ||
} | ||
}, | ||
enumerable: true | ||
}); | ||
}; | ||
for (var k in opts.data) { | ||
if (!opts.data.hasOwnProperty(k)) continue; | ||
if (!opts.properties.hasOwnProperty(k) && k != opts.id && opts.association_properties.indexOf(k) == -1) { | ||
if (!opts.extra.hasOwnProperty(k)) continue; | ||
if (opts.driver.valueToProperty) { | ||
opts.data[k] = opts.driver.valueToProperty(opts.data[k], opts.extra[k]); | ||
} | ||
addInstanceExtraProperty(k); | ||
continue; | ||
@@ -241,2 +297,6 @@ } | ||
}); | ||
Object.defineProperty(instance, "isInstance", { | ||
value: true, | ||
enumerable: false | ||
}); | ||
@@ -243,0 +303,0 @@ if (!opts.data.hasOwnProperty(opts.id)) { |
100
lib/Model.js
@@ -5,2 +5,3 @@ var Instance = require("./Instance").Instance; | ||
var ManyAssociation = require("./Associations/Many"); | ||
var ChainFind = require("./ChainFind"); | ||
@@ -34,6 +35,17 @@ exports.Model = Model; | ||
model.get = function (id, cb) { | ||
model.get = function (id, options, cb) { | ||
var conditions = {}; | ||
conditions[opts.id] = id; | ||
if (typeof options == "function") { | ||
cb = options; | ||
options = {}; | ||
} else { | ||
options = options || {}; | ||
} | ||
if (!options.hasOwnProperty("cache")) { | ||
options.cache = opts.cache; | ||
} | ||
opts.driver.find(opts.table, conditions, { limit: 1 }, function (err, data) { | ||
@@ -46,3 +58,3 @@ if (err) { | ||
} | ||
Singleton.get(opts.table + "/" + id, function (cb) { | ||
Singleton.get(opts.table + "/" + id, { cache: options.cache }, function (cb) { | ||
var instance = new Instance({ | ||
@@ -115,55 +127,51 @@ data : data[0], | ||
if (cb === null) { | ||
throw new Error("Missing Model.find callback"); | ||
if (!options.hasOwnProperty("cache")) { | ||
options.cache = opts.cache; | ||
} | ||
opts.driver.find(opts.table, conditions, { | ||
limit : limit, | ||
order : order, | ||
merge : merge, | ||
offset : options.offset | ||
}, function (err, data) { | ||
if (err) { | ||
return cb(err); | ||
} | ||
if (data.length === 0) { | ||
return cb(null, []); | ||
} | ||
var pending = data.length; | ||
for (var i = 0; i < data.length; i++) { | ||
(function (i) { | ||
Singleton.get(opts.table + (merge ? "+" + merge.from.table : "") + "/" + data[i][opts.id], function (cb) { | ||
var instance = new Instance({ | ||
data : data[i], | ||
autoSave : opts.autoSave, | ||
driver : opts.driver, | ||
table : opts.table, | ||
properties : opts.properties, | ||
hooks : opts.hooks, | ||
methods : opts.methods, | ||
validations : opts.validations, | ||
association_properties : association_properties | ||
}); | ||
OneAssociation.extend(instance, opts.driver, one_associations, { | ||
var chain = new ChainFind({ | ||
table : opts.table, | ||
driver : opts.driver, | ||
conditions : conditions, | ||
limit : limit, | ||
order : order, | ||
merge : merge, | ||
offset : options.offset, | ||
newInstance: function (data, cb) { | ||
Singleton.get(opts.table + (merge ? "+" + merge.from.table : "") + "/" + data[opts.id], | ||
{ cache: options.cache }, | ||
function (cb) { | ||
var instance = new Instance({ | ||
data : data, | ||
extra : options.extra, | ||
extra_info : options.extra_info, | ||
autoSave : opts.autoSave, | ||
driver : opts.driver, | ||
table : opts.table, | ||
properties : opts.properties, | ||
hooks : opts.hooks, | ||
methods : opts.methods, | ||
validations : opts.validations, | ||
association_properties : association_properties | ||
}); | ||
OneAssociation.extend(instance, opts.driver, one_associations, { | ||
autoFetch : opts.autoFetch | ||
}, function () { | ||
ManyAssociation.extend(instance, opts.driver, many_associations, { | ||
autoFetch : opts.autoFetch | ||
}, function () { | ||
ManyAssociation.extend(instance, opts.driver, many_associations, { | ||
autoFetch : opts.autoFetch | ||
}, function () { | ||
return cb(instance); | ||
}); | ||
return cb(instance); | ||
}); | ||
}, function (instance) { | ||
data[i] = instance; | ||
pending -= 1; | ||
if (pending === 0) { | ||
return cb(null, data); | ||
} | ||
}); | ||
})(i); | ||
}, function (instance) { | ||
return cb(null, instance); | ||
}); | ||
} | ||
}); | ||
if (cb === null) { | ||
return chain; | ||
} | ||
chain.run(cb); | ||
return this; | ||
@@ -170,0 +178,0 @@ }; |
@@ -7,8 +7,14 @@ var DriverAliases = require("./Drivers/aliases"); | ||
exports.use = function (connection, proto, cb) { | ||
exports.use = function (connection, proto, opts, cb) { | ||
if (DriverAliases[proto]) { | ||
proto = DriverAliases[proto]; | ||
} | ||
if (typeof opts == "function") { | ||
cb = opts; | ||
opts = {}; | ||
} | ||
var Driver = require("./Drivers/" + proto).Driver; | ||
var driver = new Driver(null, connection); | ||
var driver = new Driver(null, connection, { | ||
debug: (opts.query && opts.query.debug == 'true') | ||
}); | ||
@@ -30,3 +36,3 @@ var ORM = prepare(driver); | ||
} | ||
opts.database = (opts.path ? opts.path.substr(1) : ''); | ||
opts.database = (opts.pathname ? opts.pathname.substr(1) : ''); | ||
@@ -39,3 +45,5 @@ var proto = opts.protocol.substr(0, opts.protocol.length - 1); | ||
var Driver = require("./Drivers/" + proto).Driver; | ||
var driver = new Driver(opts); | ||
var driver = new Driver(opts, null, { | ||
debug: (opts.query && opts.query.debug == 'true') | ||
}); | ||
@@ -69,2 +77,3 @@ driver.connect(function (err) { | ||
properties : properties, | ||
cache : opts.cache, | ||
autoSave : opts.autoSave || false, | ||
@@ -71,0 +80,0 @@ autoFetch : opts.autoFetch || false, |
var map = {}; | ||
exports.get = function (key, createCb, returnCb) { | ||
exports.get = function (key, opts, createCb, returnCb) { | ||
if (opts && opts.cache === false) { | ||
return createCb(returnCb); | ||
} | ||
if (map.hasOwnProperty(key)) { | ||
@@ -5,0 +8,0 @@ return returnCb(map[key]); |
@@ -5,3 +5,3 @@ { | ||
"description": "NodeJS Object-relational mapping", | ||
"version": "2.0.0-alpha4", | ||
"version": "2.0.0-alpha5", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "url": "" |
@@ -8,3 +8,3 @@ ## Object Relational Mapping | ||
```sh | ||
npm install orm@2.0.0-alpha4 | ||
npm install orm@2.0.0-alpha5 | ||
``` | ||
@@ -40,2 +40,11 @@ | ||
data : Object // JSON encoded | ||
}, { | ||
methods: { | ||
fullName: function () { | ||
return this.name + ' ' + this.surname; | ||
} | ||
}, | ||
validations: { | ||
age: orm.validators.rangeNumber(18, undefined, 'under-age') | ||
} | ||
}); | ||
@@ -47,3 +56,8 @@ | ||
console.log("People found: %d", people.length); | ||
console.log("First person: %s", people[0].name); | ||
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'; | ||
}); | ||
}); | ||
@@ -50,0 +64,0 @@ }); |
@@ -99,11 +99,11 @@ var common = exports; | ||
case "postgres": | ||
db.query("CREATE TEMPORARY TABLE " + table + "_" + assoc + " (" + table + "_id BIGINT NOT NULL, " + assoc + "_id BIGINT NOT NULL)", cb); | ||
db.query("CREATE TEMPORARY TABLE " + table + "_" + assoc + " (" + table + "_id BIGINT NOT NULL, " + assoc + "_id BIGINT NOT NULL, extra_field BIGINT)", cb); | ||
break; | ||
case "sqlite": | ||
db.run("DROP TABLE IF EXISTS " + table + "_" + assoc, function () { | ||
db.run("CREATE TABLE " + table + "_" + assoc + " (" + table + "_id INTEGER NOT NULL, " + assoc + "_id INTEGER NOT NULL)", cb); | ||
db.run("CREATE TABLE " + table + "_" + assoc + " (" + table + "_id INTEGER NOT NULL, " + assoc + "_id INTEGER NOT NULL, extra_field INTEGER)", cb); | ||
}); | ||
break; | ||
default: | ||
db.query("CREATE TEMPORARY TABLE " + table + "_" + assoc + " (" + table + "_id BIGINT NOT NULL, " + assoc + "_id BIGINT NOT NULL)", cb); | ||
db.query("CREATE TEMPORARY TABLE " + table + "_" + assoc + " (" + table + "_id BIGINT NOT NULL, " + assoc + "_id BIGINT NOT NULL, extra_field BIGINT)", cb); | ||
break; | ||
@@ -180,2 +180,5 @@ } | ||
for (i = 0; i < data.length; i++) { | ||
if (data[i].length < 3) { | ||
data[i].push(0); | ||
} | ||
query.push(data[i].join(", ")); | ||
@@ -189,2 +192,5 @@ } | ||
for (i = 0; i < data.length; i++) { | ||
if (data[i].length < 3) { | ||
data[i].push(0); | ||
} | ||
db.run("INSERT INTO " + table + " VALUES (" + data[i].join(", ") + ")", function () { | ||
@@ -191,0 +197,0 @@ pending -= 1; |
@@ -14,17 +14,21 @@ var common = require('../common'); | ||
var TestModel = db.define('test_association_hasmany_set', common.getModelProperties()); | ||
TestModel.hasMany("assocs"); | ||
common.insertModelAssocData('test_association_hasmany_set_assocs', db.driver.db, [ | ||
[ 1, 2 ] | ||
], function (err) { | ||
var TestModel = db.define('test_association_hasmany_set', common.getModelProperties()); | ||
TestModel.hasMany("assocs"); | ||
TestModel.get(1, function (err, Test1) { | ||
assert.equal(err, null); | ||
TestModel.get(2, function (err, Test2) { | ||
TestModel.get(1, function (err, Test1) { | ||
assert.equal(err, null); | ||
Test1.setAssocs(Test2, function (err) { | ||
TestModel.get(2, function (err, Test2) { | ||
assert.equal(err, null); | ||
Test1.getAssocs(function (err, Tests) { | ||
Test1.setAssocs(Test2, function (err) { | ||
assert.equal(err, null); | ||
assert.equal(Array.isArray(Tests), true); | ||
assert.equal(Tests.length, 1); | ||
assert.equal(Tests[0].name, Test2.name); | ||
db.close(); | ||
Test1.getAssocs(function (err, Tests) { | ||
assert.equal(err, null); | ||
assert.equal(Array.isArray(Tests), true); | ||
assert.equal(Tests.length, 1); | ||
assert.equal(Tests[0].name, Test2.name); | ||
db.close(); | ||
}); | ||
}); | ||
@@ -31,0 +35,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
98644
71
3097
151