Comparing version 0.1.3 to 0.1.4
@@ -5,24 +5,16 @@ var eventEmitter = require('events').EventEmitter; | ||
var self = this; | ||
this.__proto__.__proto__ = modelProto; | ||
//this.__proto__.__proto__ = modelProto; | ||
this.__proto__.model = modelProto; | ||
settings = settings || {}; | ||
this.__proto__.settings = { | ||
this.__proto__.docSettings = { | ||
saved: settings.saved || false | ||
} | ||
/* | ||
for(key in eventEmitter.prototype) { | ||
this[key] = eventEmitter.prototype[key]; | ||
this[key] = eventEmitter.prototype[key].bind(self); | ||
} | ||
*/ | ||
/* | ||
for(key in eventEmitter.prototype) { | ||
this[key] = function() { | ||
eventEmitter.prototype[key].call(self, arguments); | ||
} | ||
for(key in modelProto) { | ||
this[key] = modelProto[key]; | ||
} | ||
*/ | ||
for(key in eventEmitter.prototype) { | ||
this[key] = eventEmitter.prototype[key].bind(self); | ||
} | ||
this.off = this.removeListener; // because `off` is so much more sexy | ||
@@ -36,11 +28,11 @@ | ||
Document.prototype.getModel = function() { | ||
return this.__proto__.__proto__.__proto__; | ||
return this.__proto__.__proto__.model; | ||
} | ||
Document.prototype.getSettings = function() { | ||
return this.__proto__.settings; | ||
Document.prototype.getDocSettings = function() { | ||
return this.__proto__.docSettings; | ||
} | ||
Document.prototype.define = function(name, fn) { | ||
Document.prototype.definePrivate = function(name, fn) { | ||
this.__proto__[name] = fn; | ||
@@ -47,0 +39,0 @@ } |
@@ -125,3 +125,2 @@ var r = require('rethinkdb'); | ||
if (type === Object) { | ||
//console.log(doc[key]); | ||
if (Object.prototype.toString.call(doc[key]) === '[object Object]') { | ||
@@ -275,3 +274,3 @@ result[key] = result[key] || {}; | ||
var query; | ||
var docSettings = self.getSettings() | ||
var docSettings = self.getDocSettings() | ||
@@ -281,14 +280,17 @@ if (docSettings.saved === true) { | ||
//TODO remove the r.expr around self when the driver will be fixed regarding circular reference | ||
r.db(model.thinkyOptions.db).table(model.name).get(self[primaryKey]).update(r.expr(self), {returnVals: true}).run(connection, function(error, result) { | ||
if (error) { | ||
if ((callback) && (typeof callback === 'function')) callback(error, null);; | ||
} | ||
else if ((result) && (result.errors > 0)) { | ||
if ((callback) && (typeof callback === 'function')) callback(result, null); | ||
} | ||
else { | ||
self.replace(result.new_val); | ||
r.db(model.thinkyOptions.db).table(model.name) | ||
.get(self[primaryKey]) | ||
.update(r.expr(self), {returnVals: true}) | ||
.run(connection, function(error, result) { | ||
if (error) { | ||
if ((callback) && (typeof callback === 'function')) callback(error, null);; | ||
} | ||
else if ((result) && (result.errors > 0)) { | ||
if ((callback) && (typeof callback === 'function')) callback(result, null); | ||
} | ||
else { | ||
self.replace(result.new_val); | ||
if ((callback) && (typeof callback === 'function')) callback(error, self); | ||
model.pool.release(connection); | ||
if ((callback) && (typeof callback === 'function')) callback(error, self); | ||
model.pool.release(connection); | ||
self.emit('save', self, result.old_val); | ||
@@ -301,17 +303,19 @@ } | ||
//TODO remove the r.expr around self when the driver will be fixed regarding circular reference | ||
r.db(model.thinkyOptions.db).table(model.name).insert(r.expr(self), {returnVals: true}).run(connection, function(error, result) { | ||
if (error) { | ||
if ((callback) && (typeof callback === 'function')) callback(error, null); | ||
} | ||
else if ((result) && (result.errors > 0)) { | ||
if ((callback) && (typeof callback === 'function')) callback(result, null);; | ||
} | ||
else { | ||
self.replace(result.new_val); | ||
self.getSettings().saved = true; | ||
if ((callback) && (typeof callback === 'function')) callback(null, self); | ||
// We can release the connection because we get back an object | ||
model.pool.release(connection); | ||
self.emit('save', self, result.old_val); | ||
} | ||
r.db(model.thinkyOptions.db).table(model.name) | ||
.insert(r.expr(self), {returnVals: true}) | ||
.run(connection, function(error, result) { | ||
if (error) { | ||
if ((callback) && (typeof callback === 'function')) callback(error, null); | ||
} | ||
else if ((result) && (result.errors > 0)) { | ||
if ((callback) && (typeof callback === 'function')) callback(result, null);; | ||
} | ||
else { | ||
self.replace(result.new_val); | ||
self.getDocSettings().saved = true; | ||
if ((callback) && (typeof callback === 'function')) callback(null, self); | ||
// We can release the connection because we get back an object | ||
model.pool.release(connection); | ||
self.emit('save', self, result.old_val); | ||
} | ||
}); | ||
@@ -324,2 +328,3 @@ | ||
// TODO: Refactor all queries | ||
Model.prototype.get = function(id, callback) { | ||
@@ -326,0 +331,0 @@ var self = this; |
@@ -8,7 +8,36 @@ var thinky = require('./lib/index.js'); | ||
Cat = thinky.createModel('Cat', | ||
{ fieldString: {_type: String} }, | ||
{enforce: {type: true, missing: true, extra: true}}); | ||
Cat = thinky.createModel('Cat', { fieldString: {_type: String} }); | ||
catou = new Cat({fieldString: 'catou'}); | ||
minou = new Cat({}); | ||
Dog = thinky.createModel('Dog', { fieldString2: {_type: String} }); | ||
dog = new Dog({fieldString2: 'dogou'}); | ||
/* | ||
console.log(dog.getModel().name); | ||
console.log(catou.getModel().name); | ||
*/ | ||
minou = new Cat({fieldString: 'minou'}); | ||
/* | ||
console.log(dog.getModel()); | ||
console.log(catou.getModel()); | ||
*/ | ||
/* | ||
console.log(Cat); | ||
console.log(Dog); | ||
console.log(Dog === Cat); | ||
*/ | ||
/* | ||
console.log(dog.getModel().name); | ||
console.log(catou.getModel().name); | ||
*/ | ||
//console.log(catou.getModel()); | ||
catou.save( function(e, r) { | ||
console.log(e); | ||
console.log(r); | ||
}) |
{ | ||
"name": "thinky", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -36,3 +36,3 @@ var thinky = require('../lib/index.js'); | ||
minou = new Cat({name: 'Minou'}); | ||
catou.define('helloDoc', function() { return 'hello, my name is '+this.name; }) | ||
catou.definePrivate('helloDoc', function() { return 'hello, my name is '+this.name; }) | ||
should.equal(catou.helloDoc(), 'hello, my name is Catou'); | ||
@@ -55,3 +55,3 @@ }); | ||
describe('save', function() { | ||
it('should add a field id', function(done){ | ||
it('should add a field id -- Testing document', function(done){ | ||
Cat = thinky.createModel('Cat', { id: String, name: String }); | ||
@@ -58,0 +58,0 @@ catou = new Cat({name: 'Catou'}); |
@@ -28,7 +28,4 @@ var thinky = require('../lib/index.js'); | ||
should(Object.prototype.toString.call(catou) === '[object Object]'); | ||
console.log(catou); | ||
should.equal(catou.catName, 'Catou'); | ||
}); | ||
}); | ||
describe('new', function(){ | ||
it('should create another new instance of the Model', function() { | ||
@@ -39,4 +36,2 @@ minou = new Cat({catName: 'Minou'}); | ||
}); | ||
}); | ||
describe('new', function(){ | ||
it('should not change the previous instances', function() { | ||
@@ -47,8 +42,6 @@ catou = new Cat({catName: 'Catou'}); | ||
}); | ||
it('should be able to create other class instances', function() { | ||
Cat.should.not.equal(Dog) | ||
it('should not interfer with previously created instances of other classes', function() { | ||
dogou = new Dog({dogName: "Dogou"}); | ||
dogou.getModel().should.not.equal(Cat); | ||
should.equal(catou.catName, 'Catou'); | ||
should.equal(dogou.dogName, 'Dogou'); | ||
should(dogou.getModel().name, 'Dog'); | ||
should(catou.getModel().name, 'Cat'); | ||
}); | ||
@@ -311,12 +304,11 @@ }) | ||
it('should save a method', function() { | ||
Cat = thinky.createModel('Cat', { name: String }); | ||
Cat.define('hello', function() { return 'hello, my name is '+this.name; }) | ||
Cat = thinky.createModel('Cat', { catName: String }); | ||
Cat.define('hello', function() { return 'hello, my name is '+this.catName; }) | ||
should.exist(Cat.hello) | ||
}); | ||
it('should define the function for previously created documents', function(){ | ||
catou = new Cat({name: 'Catou'}); | ||
catou = new Cat({catName: 'Catou'}); | ||
should.exist(catou.hello); | ||
should.equal(catou.hello(), 'hello, my name is Catou'); | ||
}); | ||
/* | ||
it('should not create a mehtod for another class', function() { | ||
@@ -329,19 +321,6 @@ Dog = thinky.createModel('Dog', { dogName: String }); | ||
it('should define the function for newly created documents', function(){ | ||
minou = new Cat({name: 'Minou'}); | ||
console.log(catou.getModel()); | ||
console.log('==============================') | ||
console.log(catou.__proto__); | ||
console.log('==============================') | ||
console.log(catou.__proto__.__proto__); | ||
console.log('==============================') | ||
console.log(catou.__proto__.__proto__.__proto__); | ||
console.log('==============================') | ||
console.log(catou.__proto__.__proto__.__proto__.__proto__); | ||
should(catou.getModel(), minou.getModel()); | ||
catou.getModel().should.not.equal(dogou.getModel()); | ||
//should.exist(minou.hello); | ||
//should.equal(minou.hello(), 'hello, my name is Minou'); | ||
minou = new Cat({catName: 'Minou'}); | ||
should.exist(minou.hello); | ||
should.equal(minou.hello(), 'hello, my name is Minou'); | ||
}); | ||
*/ | ||
it('should not create a mehtod for another class', function() { | ||
@@ -358,5 +337,6 @@ Dog = thinky.createModel('Dog', { name: String }); | ||
describe('save', function() { | ||
it('should add a field id', function(done){ | ||
it('should add a field id -- Testing model', function(done){ | ||
Cat = thinky.createModel('Cat', { id: String, name: String }); | ||
catou = new Cat({name: 'Catou'}); | ||
console.log(catou); | ||
catou.save( function(error, result) { | ||
@@ -363,0 +343,0 @@ catouCopy = result; |
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
1260
65050