Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

thinky

Package Overview
Dependencies
Maintainers
1
Versions
129
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thinky - npm Package Compare versions

Comparing version 0.2.10 to 0.2.11

examples/advanced-todo/.npmignore

146

lib/document.js

@@ -102,2 +102,3 @@ var eventEmitter = require('events').EventEmitter;

var join = args.join; // join data
var nextStep = args.nextStep;

@@ -126,3 +127,5 @@ // Define some variables for convenience

else {
doc.merge(result.new_val);
if ((result != null) && (result.new_val != null)) {
doc.merge(result.new_val);
}
if (doc.getDocSettings().saved !== true) {

@@ -136,2 +139,3 @@ doc.getDocSettings().saved = true;

}
/*
else if (joinType === 'hasMany') {

@@ -143,2 +147,3 @@ if (parentDoc[joinClause.leftKey] == null) {

}
*/
}

@@ -148,10 +153,23 @@ }

if (count.saved === count.toSave) {
// All sub documents have been saved
// Let's save the parent
if (parentDoc != null) {
parentDoc.save( {saveJoin: false}, callback);
if (nextStep === 'saveMain') {
parentDoc.save( {saveHasOne: false, saveMain: true, saveHasMany: true}, callback);
}
else if (nextStep === 'saveHasMany') {
// Update all children
var model = doc.getModel();
for(var key in model.joins) {
if (model.joins[key].type === 'hasMany') {
for(var i =0; i<parentDoc[key].length; i++) {
count.toSave++;
parentDoc[key][i][model.joins[key].joinClause.rightKey] = parentDoc[model.joins[key].joinClause.leftKey]
}
}
}
parentDoc.save( {saveHasOne: false, saveMain: false, saveHasMany: true}, callback);
}
else {
// Need to emit other events...
doc.emit('save', doc, result.old_val);
if (result != null) {
doc.emit('save', doc, result.old_val);
}

@@ -196,2 +214,3 @@ if ((callback) && (typeof callback === 'function')) callback(null, doc);

doc.getDocSettings().saved = false;
if (count.deleted === count.toDelete) {

@@ -216,3 +235,3 @@ // Need to emit other events...

if ((options == null) || (options.saveJoin == null)) {
if (options == null) {
options = {

@@ -222,3 +241,16 @@ saveJoin: false

}
else if (options.saveJoin == null) {
options.saveJoin = false
}
if (options.saveHasOne == null) {
options.saveHasOne = options.saveJoin
}
if (options.saveMain == null) {
options.saveMain = true
}
if (options.saveHasMany == null) {
options.saveHasMany = options.saveJoin
}
var self = this; // The document

@@ -234,4 +266,4 @@ var model = this.getModel();

}
if (options.saveJoin === true) {
// We just save the hasOne docs
if (options.saveHasOne === true) {
for(var key in self) {

@@ -243,23 +275,3 @@ if (self.hasOwnProperty(key) === true) {

else if (model.joins.hasOwnProperty(key) === true) {
//TODO add check to make sure we have a valid join
//Array should match hasMany
if (Object.prototype.toString.call(self[key]) === '[object Array]') {
for(var i= 0; i<self[key].length; i++) {
count.toSave++;
if (typeof model.joins[key].joinClause === 'object') {
cb = Document.createCallback({
doc: self[key][i],
callback: callback,
count: count,
parentDoc: self,
join: model.joins[key]
})
}
else {
//TODO Implement join with lambda functions
}
self[key][i].save({saveJoin: true}, cb);
}
}
else if ((typeof self[key] === 'object') && (self[key] != null)) {
if ((model.joins[key].type === 'hasOne') && (typeof self[key] === 'object') && (self[key] != null)) {
count.toSave++;

@@ -272,3 +284,4 @@ if (typeof model.joins[key].joinClause === 'object') {

parentDoc: self,
join: model.joins[key]
join: model.joins[key],
nextStep: 'saveMain'
})

@@ -297,3 +310,3 @@ }

// TODO Use a flag... count.toSave is not safe...
if (count.toSave === 0) {
if ((options.saveMain === true) && (count.toSave === 0)) {
count.toSave++;

@@ -318,11 +331,58 @@

else {
cb = Document.createCallback({
doc: self,
callback: callback,
count: count
})
if (options.saveHasMany === true) {
cb = Document.createCallback({
doc: self,
callback: callback,
count: count,
parentDoc: self,
join: model.joins[key],
nextStep: 'saveHasMany'
})
}
else {
cb = Document.createCallback({
doc: self,
callback: callback,
count: count,
parentDoc: self,
join: model.joins[key],
nextStep: 'saveParent'
})
}
}
this._execute.call(self, query, cb);
}
else if (options.saveHasMany === true) {
for(var key in self) {
//TODO mere the conditions
if (self.hasOwnProperty(key) === true) {
if ((model.joins.hasOwnProperty(key) === true) && (model.joins[key].type === 'hasMany')) {
if (Object.prototype.toString.call(self[key]) === '[object Array]') {
for(var i= 0; i<self[key].length; i++) {
count.toSave++;
if (typeof model.joins[key].joinClause === 'object') {
cb = Document.createCallback({
doc: self[key][i],
callback: callback,
count: count,
nextStep: 'saveParent'
})
}
else {
//TODO Implement join with lambda functions
}
self[key][i].save({saveHasOne: true, saveMain: true, saveHasMany: true}, cb);
}
}
}
}
}
if (count.toSave === 0) {
callback();
//TODO just call the callback
}
}
return this;

@@ -356,3 +416,3 @@ };

}
if (options.deleteJoin === true) {

@@ -370,3 +430,3 @@ for(var key in self) {

for(var i= 0; i<self[key].length; i++) {
self[key][i].delete({deleteJoin: true}, cb, count);
self[key][i].delete({deleteJoin: true}, callback, count);
}

@@ -380,3 +440,3 @@ }

self[key].delete({deleteJoin: true}, cb, count);
self[key].delete({deleteJoin: true}, callback, count);
}

@@ -395,2 +455,3 @@ }

//TODO Move before?
count.toDelete++;

@@ -415,2 +476,7 @@

Document.prototype.update = function(callback ) {
this.getDocSettings().saved = true;
this.save(callback);
};
Document.prototype.off = function(eventKey, listener) {

@@ -417,0 +483,0 @@ if (arguments.length <= 1) {

@@ -448,2 +448,8 @@ var r = require('rethinkdb');

Model.prototype.orderBy = function(field, callback) {
var query = new Query(this);
query = query.orderBy(field, callback);
return query;
}
Model.prototype.getJoin = function(filter, callback) {

@@ -462,2 +468,9 @@ var query = new Query(this);

Model.prototype.run = function(callback) {
var query = new Query(this);
query = query.run(callback);
return query;
}
// Events

@@ -559,3 +572,3 @@ Model.prototype.addListener = function(eventKey, listener) {

}
Model.prototype.hasMany = function(modelArg, fieldName, joinClause) {
Model.prototype.hasMany = function(modelArg, fieldName, joinClause, options) {
//TODO Check arguments

@@ -567,3 +580,4 @@ // joinClause can be an object with two fields (leftKey and rightKey) or a function only

joinClause: joinClause,
type: 'hasMany'
type: 'hasMany',
options: options || {}
}

@@ -570,0 +584,0 @@ }

@@ -97,9 +97,26 @@ var r = require('rethinkdb');

if (Object.prototype.toString.call(field) === '[object Array]') {
var reqlField = [];
for(var i=0; i<field.length; i++) {
if (typeof field[i] === 'string') {
if ((field[i].length > 0) && (field[i][0] === '-')) {
reqlField.push(r.desc(field[i].slice(1)))
}
else {
reqlField.push(field[i])
}
}
}
this.query = this.query.orderBy.apply(this.query, field);
}
else {
this.query = this.query.orderBy(field);
if (typeof field === 'string') {
if ((field.length > 0) && (field[0] === '-')) {
this.query = this.query.orderBy(r.desc(field.slice(1)));
}
else {
this.query = this.query.orderBy(field);
}
}
}
this.type = 'stream';
if (typeof callback === 'function') {

@@ -166,2 +183,3 @@ this.run(callback);

var joinType = model.joins[fieldName].type;
var joinOptions = model.joins[fieldName].options;
if (typeof joinClause === 'object') {

@@ -186,12 +204,23 @@ if (type === 'object') {

else if (joinType === 'hasMany') {
this.query = this.query.do( function(doc) {
return doc(joinClause["leftKey"]).concatMap( function(value) {
// TODO refactor the if/else
if (joinOptions.orderBy != null) {
this.query = this.query.do( function(doc) {
return new Query(otherModel, r.db(otherModel.getModel().thinkyOptions.db).table(otherModel.getModel().name)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}), 'stream').orderBy(joinOptions.orderBy).query.coerceTo('array').do( function(stream) {
return doc.merge(
r.expr([[fieldName, new Query(otherModel, stream, 'stream').getJoin().query]]).coerceTo('object')
)
});
});
}
else {
this.query = this.query.do( function(doc) {
return r.db(otherModel.getModel().thinkyOptions.db).table(otherModel.getModel().name)
.getAll( value, {index: joinClause["rightKey"]})
}).coerceTo('array').do( function(stream) {
return doc.merge(
r.expr([[fieldName, new Query(otherModel, stream, 'stream').getJoin().query]]).coerceTo('object')
)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}).coerceTo('array').do( function(stream) {
return doc.merge(
r.expr([[fieldName, new Query(otherModel, stream, 'stream').getJoin().query]]).coerceTo('object')
)
});
});
});
}
}

@@ -220,15 +249,28 @@ }

else if (joinType === 'hasMany') {
this.query = this.query.map( function(doc) {
return r.branch(
doc.hasFields(joinClause["leftKey"]),
doc(joinClause["leftKey"]).concatMap( function(joinValue) {
return r.db(otherModel.getModel().thinkyOptions.db).table(otherModel.getModel().name)
.getAll( joinValue, {index: joinClause["rightKey"]} ).coerceTo('array')
}).do( function(stream) {
return doc.merge(
r.expr([[fieldName, new Query(otherModel, stream, 'stream').getJoin().query]]).coerceTo('object')
)
}),
doc)
});
if (joinOptions.orderBy != null) {
this.query = this.query.map( function(doc) {
return r.branch(
doc.hasFields(joinClause["leftKey"]),
new Query(otherModel, r.db(otherModel.getModel().thinkyOptions.db).table(otherModel.getModel().name)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}), 'stream').orderBy(joinOptions.orderBy).query.coerceTo('array').do( function(stream) {
return doc.merge(
r.expr([[fieldName, new Query(otherModel, stream, 'stream').getJoin().query]]).coerceTo('object')
)
}),
doc)
});
}
else {
this.query = this.query.map( function(doc) {
return r.branch(
doc.hasFields(joinClause["leftKey"]),
r.db(otherModel.getModel().thinkyOptions.db).table(otherModel.getModel().name)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}).coerceTo('array').do( function(stream) {
return doc.merge(
r.expr([[fieldName, new Query(otherModel, stream, 'stream').getJoin().query]]).coerceTo('object')
)
}),
doc)
});
}
}

@@ -248,3 +290,15 @@ }

Query.prototype.delete = function(callback) {
//TODO Implement a way to delete joins? -- Note: That seems hard to do in one query...
this.query = this.query.delete();
this.type = 'object';
if (typeof callback === 'function') {
this.run(callback);
}
return this;
}
// Execute the query and return instance(s) of the model

@@ -251,0 +305,0 @@ Query.prototype.run = function(callback) {

{
"name": "thinky",
"version": "0.2.10",
"version": "0.2.11",
"description": "RethinkDB ORM for Node.js",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -34,3 +34,3 @@ # Thinky

kitty.hello(); // Log "Hello, I'm Kitty
kitty.save(function(err, result) {
kitty.save({saveJoin: true}, function(err, result) {
if (err) throw err;

@@ -69,4 +69,4 @@ console.log("Kitty and Michel have been saved in the database");

### Roadmap
- Clean/reorganize tests and add more tests.
- Add examples on how to use Thinky.
- Clean/reorganize tests and add more tests -- half done
- Let Thinky cache results so objects so there are no copies of the same document.

@@ -73,0 +73,0 @@ - Joins with lambda functions.

@@ -226,2 +226,3 @@ var thinky = require('../lib/index.js');

});
it('should be able to save the joined doc -- hasOne', function(done) {

@@ -244,2 +245,3 @@ var Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String});

});
it('should keep the references when saving joined documents -- hasOne', function(done) {

@@ -264,2 +266,3 @@ var Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String});

});
it('should marked joined documents that are saved `saved` -- hasOne', function(done) {

@@ -305,5 +308,5 @@ var Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String});

it('should not save the joined docs by default -- hasMany', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -316,3 +319,2 @@ var catou = new Cat({name: "Catou"});

catou.save(function(error, result) {
should.not.exist(catou.taskIds);
should.not.exist(catou.tasks[0].id);

@@ -326,5 +328,5 @@ should.not.exist(catou.tasks[1].id);

it('should be able to save the joined doc -- hasMany', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -337,9 +339,8 @@ var catou = new Cat({name: "Catou"});

catou.save({saveJoin: true}, function(error, result) {
catou.taskIds.should.have.length(3);
should.exist(catou.taskIds[0]);
should.exist(catou.taskIds[1]);
should.exist(catou.taskIds[2]);
should.exist(catou.tasks[0].id);
should.exist(catou.tasks[1].id);
should.exist(catou.tasks[2].id);
should.exist(task1.id);
should.exist(task2.id);
should.exist(task3.id);
should.equal(task1.catId, catou.id);
should.equal(task2.catId, catou.id);
should.equal(task3.catId, catou.id);

@@ -349,3 +350,2 @@ done();

});
});

@@ -435,6 +435,6 @@

it('should not save the joined doc by default -- hasMany', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
it('should not delete the joined doc by default -- hasMany', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -457,6 +457,7 @@ var catou = new Cat({name: "Catou"});

});
it('should be able to delete joined docs -- hasMany', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -469,3 +470,2 @@ var catou = new Cat({name: "Catou"});

catou.save({saveJoin: true}, function(error, result) {
catou.delete({deleteJoin: true}, function(error, result) {

@@ -472,0 +472,0 @@ should.equal(catou.getDocSettings().saved, false);

@@ -488,5 +488,5 @@ var thinky = require('../lib/index.js');

it('should work with hasMany joins', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -501,10 +501,6 @@ var catou = new Cat({name: "Catou"});

result.should.have.length(1);
result[0].taskIds.should.have.length(3);
should.exist(result[0].taskIds[0]);
should.exist(result[0].taskIds[1]);
should.exist(result[0].taskIds[2]);
result[0].tasks.should.have.length(3);
should.exist(result[0].tasks[0].id);
should.exist(result[0].tasks[1].id);
should.exist(result[0].tasks[2].id);
catou.tasks.should.have.length(3);
done();

@@ -514,3 +510,2 @@ })

});
});

@@ -565,5 +560,5 @@

it('should work with a hasMany relation', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -578,6 +573,3 @@ var catou = new Cat({name: "Catou"});

result.should.have.length(1);
result[0].taskIds.should.have.length(3);
should.exist(result[0].taskIds[0]);
should.exist(result[0].taskIds[1]);
should.exist(result[0].taskIds[2]);
result[0].tasks.should.have.length(3);
should.exist(result[0].tasks[0].id);

@@ -870,5 +862,5 @@ should.exist(result[0].tasks[1].id);

it('should add a new key in model.joins', function() {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -883,5 +875,5 @@ should.exist(Cat.getModel().joins['tasks']);

it('get should be able to get joined documents', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -897,6 +889,3 @@ catou = new Cat({name: "Catou"});

Cat.get(catou.id).getJoin().run(function(error, result) {
result.taskIds.should.have.length(3);
should.exist(result.taskIds[0]);
should.exist(result.taskIds[1]);
should.exist(result.taskIds[2]);
result.tasks.should.have.length(3);
should.exist(result.tasks[0].id);

@@ -912,5 +901,5 @@ should.exist(result.tasks[1].id);

it('get should be able to get joined documents -- and they should be `saved`', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
var Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'});

@@ -933,5 +922,45 @@ catou = new Cat({name: "Catou"});

});
it('should work with orderBy', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'}, {orderBy: 'id'});
catou = new Cat({name: "Catou"});
task1 = new Task({task: "Catch the red dot"});
task2 = new Task({task: "Eat"});
task3 = new Task({task: "Sleep"});
catou.tasks = [task1, task2, task3];
catou.save({saveJoin: true}, function(error, firstResult) {
if (error) throw error;
Cat.get(catou.id).getJoin().run(function(error, result) {
should(result.tasks[0].id < result.tasks[1].id);
should(result.tasks[1].id < result.tasks[2].id);
done();
})
})
});
it('should work with orderBy -- desc', function(done) {
var Cat = thinky.createModel('Cat', {id: String, name: String});
var Task = thinky.createModel('Task', {id: String, catId: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'id', rightKey: 'catId'}, {orderBy: '-id'});
catou = new Cat({name: "Catou"});
task1 = new Task({task: "Catch the red dot"});
task2 = new Task({task: "Eat"});
task3 = new Task({task: "Sleep"});
catou.tasks = [task1, task2, task3];
catou.save({saveJoin: true}, function(error, firstResult) {
if (error) throw error;
Cat.get(catou.id).getJoin().run(function(error, result) {
should(result.tasks[0].id > result.tasks[1].id);
should(result.tasks[1].id > result.tasks[2].id);
done();
})
})
});
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc