Comparing version 0.2.3 to 0.2.4
@@ -9,3 +9,3 @@ /** | ||
// mongoose setup | ||
// thinky setup | ||
require( './db' ); | ||
@@ -12,0 +12,0 @@ |
@@ -1,12 +0,9 @@ | ||
var mongoose = require( 'mongoose' ); | ||
var Schema = mongoose.Schema; | ||
var thinky = require( 'thinky' ); | ||
var Todo = new Schema({ | ||
thinky.init({}); | ||
var Todo = thinky.createModel('Todo', { | ||
id : String, | ||
user_id : String, | ||
content : String, | ||
updated_at : Date | ||
updated_at : {_type: Number , default: function() { return Date.now() } } | ||
}); | ||
mongoose.model( 'Todo', Todo ); | ||
mongoose.connect( 'mongodb://localhost/express-todo' ); |
@@ -9,4 +9,4 @@ { | ||
"ejs" : "0.8.3", | ||
"mongoose" : "3.3.1" | ||
"thinky" : "0.2.3" | ||
} | ||
} |
@@ -1,3 +0,3 @@ | ||
var mongoose = require( 'mongoose' ); | ||
var Todo = mongoose.model( 'Todo' ); | ||
var thinky = require( 'thinky' ); | ||
var Todo = thinky.models['Todo']; | ||
var utils = require( 'connect' ).utils; | ||
@@ -10,7 +10,7 @@ | ||
Todo. | ||
find({ user_id : user_id }). | ||
sort( '-updated_at' ). | ||
exec( function ( err, todos ){ | ||
filter({ user_id : user_id }). | ||
orderBy( 'updated_at' ). | ||
run( function ( err, todos ){ | ||
if( err ) return next( err ); | ||
console.log(todos); | ||
res.render( 'index', { | ||
@@ -36,3 +36,3 @@ title : 'Express Todo Example', | ||
exports.destroy = function ( req, res, next ){ | ||
Todo.findById( req.params.id, function ( err, todo ){ | ||
Todo.get( req.params.id, null, function ( err, todo ){ | ||
var user_id = req.cookies ? | ||
@@ -45,3 +45,3 @@ req.cookies.user_id : undefined; | ||
todo.remove( function ( err, todo ){ | ||
todo.delete( function ( err, todo ){ | ||
if( err ) return next( err ); | ||
@@ -60,4 +60,4 @@ | ||
find({ user_id : user_id }). | ||
sort( '-updated_at' ). | ||
exec( function ( err, todos ){ | ||
orderBy( 'updated_at' ). | ||
run( function ( err, todos ){ | ||
if( err ) return next( err ); | ||
@@ -74,3 +74,3 @@ | ||
exports.update = function( req, res, next ){ | ||
Todo.findById( req.params.id, function ( err, todo ){ | ||
Todo.get( req.params.id, null, function ( err, todo ){ | ||
var user_id = req.cookies ? | ||
@@ -77,0 +77,0 @@ req.cookies.user_id : undefined; |
@@ -143,2 +143,36 @@ var eventEmitter = require('events').EventEmitter; | ||
Document.createCallbackDelete = function(doc, callback, count, originalDoc, originalCopyDoc, | ||
joinClause, joinType, saveJoin) { | ||
var arg = arguments; | ||
var docSettings = doc.getDocSettings(); | ||
fn = 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 { | ||
count.deleted++; | ||
doc.getDocSettings().saved = false; | ||
if (count.deleted === count.toDelete) { | ||
// All sub document have been saved | ||
// Let's save the parent | ||
if ((originalDoc != null) && (joinClause != null) && (originalDoc !== doc)) { | ||
originalDoc.delete({deleteJoin: false}, callback); | ||
} | ||
else { | ||
if ((callback) && (typeof callback === 'function')) callback(null, doc); | ||
// Need to emit other events... | ||
doc.emit('delete', doc); | ||
} | ||
} | ||
} | ||
} | ||
fn.__wrapped = true; | ||
return fn | ||
} | ||
//TODO We don't need _save. Clean and merge _save and save | ||
@@ -245,2 +279,94 @@ Document.prototype.save = function(callback, options) { | ||
Document.prototype.delete = function(options, callback) { | ||
var self = this; // The document | ||
var model = this.getModel(); | ||
if ((options == null) || (options.saveJoin == null)) { | ||
options = { | ||
deleteJoin: false | ||
} | ||
} | ||
var docSettings = self.getDocSettings(); | ||
var query, cb; | ||
var count = { | ||
toDelete: 0, | ||
deleted: 0 | ||
} | ||
if (options.deleteJoin === true) { | ||
for(var key in self) { | ||
if (self.hasOwnProperty(key) === true) { | ||
if (model.joins.hasOwnProperty(key) === true) { | ||
if (Object.prototype.toString.call(self[key]) === '[object Array]') { | ||
for(var i= 0; i<self[key].length; i++) { | ||
count.toDelete++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallbackDelete(self[key][i], callback, count, self, copyDoc, | ||
model.joins[key].joinClause, 'hasMany', false) | ||
} | ||
else { | ||
cb = Document.createCallbackDelete(self[key][i], callback, count, null, null, | ||
null, 'hasMany') | ||
} | ||
self[key][i].delete(cb, true); | ||
} | ||
} | ||
else if ((typeof self[key] === 'object') && (self[key] != null)) { | ||
count.toDelete++; | ||
if (typeof model.joins[key].joinClause === 'object') { | ||
cb = Document.createCallbackDelete(self[key], callback, count, self, copyDoc, | ||
model.joins[key].joinClause, 'hasOne', false) | ||
} | ||
else { | ||
cb = Document.createCallbackDelete(self[key], callback, count, null, null, | ||
null, 'hasOne') | ||
} | ||
self[key].delete(cb, true); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// If no sub document to save, we directly save things | ||
if (count.toDelete === 0) { | ||
if (docSettings.saved === true) { | ||
count.toDelete++; | ||
query = r.db(model.thinkyOptions.db).table(model.name) | ||
.get(self[model.getPrimaryKey()]) | ||
.delete() | ||
// we have to wrap the callback if the callback has not been wrapped before. | ||
if (callback == null) { | ||
cb = Document.createCallbackDelete(self, callback, count) | ||
} | ||
else if (callback.__wrapped === true) { | ||
cb = callback | ||
} | ||
else { | ||
cb = Document.createCallbackDelete(self, callback, count) | ||
} | ||
this._execute.call(self, query, cb); | ||
} | ||
else { | ||
// we have to wrap the callback if the callback has not been wrapped before. | ||
if (callback == null) { | ||
cb = Document.createCallbackDelete(self, callback, count) | ||
} | ||
else if (callback.__wrapped === true) { | ||
cb = callback | ||
} | ||
else { | ||
cb = Document.createCallbackDelete(self, callback, count) | ||
} | ||
cb(null, {deleted: 1}) | ||
} | ||
} | ||
return this; | ||
} | ||
Document.prototype.off = function(eventKey, listener) { | ||
@@ -247,0 +373,0 @@ if (arguments.length <= 1) { |
{ | ||
"name": "thinky", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "RethinkDB ORM for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -68,3 +68,3 @@ # Thinky | ||
### Roadmap | ||
- Stick closer to the driver -- Things will be chainable | ||
- Clean the code to save/delete -- My eyes are bleeding... | ||
- Add examples on how to use Thinky. | ||
@@ -71,0 +71,0 @@ - Clean/reorganize tests and add more tests. |
@@ -144,4 +144,18 @@ var thinky = require('../lib/index.js'); | ||
}); | ||
describe('delete', function() { | ||
it('should delete the doc', function(done){ | ||
Cat = thinky.createModel('Cat', { id: String, name: String }); | ||
catou = new Cat({name: 'Catou'}); | ||
catou.save( function(error, result) { | ||
should.exist(result.id); | ||
catou.delete( null, function(error, result) { | ||
catou.getDocSettings().saved.should.be.false | ||
done(); | ||
}) | ||
}) | ||
}); | ||
}); | ||
// Test listener | ||
@@ -148,0 +162,0 @@ describe('on', function() { |
@@ -512,3 +512,3 @@ var thinky = require('../lib/index.js'); | ||
it('retrieve documents in the database', function(done){ | ||
Cat.filter(function(doc) { return r.expr([catouCopy.id, minouCopy.id]).contains(doc("id")) }, | ||
Cat.filter(function(doc) { return r.expr([scope.catou.id, scope.minou.id]).contains(doc("id")) }, | ||
null, | ||
@@ -710,2 +710,30 @@ function(error, result) { | ||
}); | ||
it('should be able to delete the joined doc -- nested joins', function(done) { | ||
Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String}); | ||
Human = thinky.createModel('Human', {id: String, ownerName: String, idMom: String}); | ||
Mother = thinky.createModel('Mother', {id: String, motherName: String}); | ||
Human.hasOne(Mother, 'mom', {leftKey: 'idMom', rightKey: 'id'}); | ||
Cat.hasOne(Human, 'owner', {leftKey: 'idHuman', rightKey: 'id'}); | ||
var owner = new Human({ownerName: "Michel"}); | ||
var catou = new Cat({name: "Catou"}); | ||
var mother = new Mother({motherName: "Mom"}); | ||
catou['owner'] = owner; | ||
owner['mom'] = mother; | ||
catou.save( function(error, result) { | ||
should.exist(catou.id); | ||
should.exist(catou.idHuman); | ||
should.exist(catou.owner.id); | ||
catou_id = catou.id; | ||
catou.delete( {deleteJoin: true}, function(error, result) { | ||
catou.getDocSettings().saved.should.be.false | ||
catou.owner.getDocSettings().saved.should.be.false | ||
catou.owner.mom.getDocSettings().saved.should.be.false | ||
done(); | ||
}) | ||
}, {saveJoin: true}); | ||
}); | ||
it('getAll should work -- nested joins', function(done) { | ||
@@ -712,0 +740,0 @@ Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String}); |
Sorry, the diff of this file is not supported yet
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 3 instances in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 3 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
1558390
511
34679
1
3
103
45