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.1.7 to 0.2.0

22

doc/README.md

@@ -174,3 +174,5 @@ # Documentation for Thinky

options can be {getJoin: Boolean}
__Model.getAll(__ value or [values], indexName, callback, options __)__

@@ -185,4 +187,6 @@

options can be {getJoin: Boolean}
__Model.filter(__ filterFunction, callback, options __)__

@@ -197,4 +201,6 @@

options can be {getJoin: Boolean}
__Model.count(__ __)__

@@ -283,4 +289,14 @@

_Not implemented yet_
Example:
```javascript
Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
catou = new Cat({name: "Catou"});
task1 = new Task({task: "Catch the red dot"});
task2 = new Task({task: "Eat"});
task3 = new Task({task: "Sleep"});
```
#### Document

@@ -315,3 +331,3 @@

__Document.save(__ callback __)__
__Document.save(__ callback, options __)__

@@ -323,3 +339,5 @@ Save the object in the database. Thinky will call insert or update depending

The object `options` can be {saveJoin: Boolean}
__Document.merge(__ newDoc, replace __)__

@@ -326,0 +344,0 @@

16

lib/document.js

@@ -169,16 +169,18 @@ var eventEmitter = require('events').EventEmitter;

if (saveJoin === true) {
for(key in self) {
if (self.hasOwnProperty(key)) {
if (model.joins[key] == null) {
for(var key in self) {
if (self.hasOwnProperty(key) === true) {
if ((!model.joins.hasOwnProperty(key)) || (model.joins[key] == null)) {
copyDoc[key] = self[key];
}
else {
else if (model.joins.hasOwnProperty(key) === true) {
if (Object.prototype.toString.call(self[key]) === '[object Array]') {
for(var i in self[key]) {
for(var i= 0; i<self[key].length; i++) {
count.toSave++;
if (typeof model.joins[key].joinClause === 'object') {
cb = Document.createCallback(self[key][i], callback, count, self, copyDoc, model.joins[key].joinClause, 'hasMany', false)
cb = Document.createCallback(self[key][i], callback, count, self, copyDoc,
model.joins[key].joinClause, 'hasMany', false)
}
else {
cb = Document.createCallback(self[key][i], callback, count, null, null, 'hasMany')
cb = Document.createCallback(self[key][i], callback, count, null, null,
null, 'hasMany')
}

@@ -185,0 +187,0 @@ self[key][i]._save(cb, true);

@@ -69,2 +69,3 @@ var r = require('rethinkdb');

var prefix = prefix || '';
var options = options || {};

@@ -97,3 +98,21 @@ var schema = schema || this.schema;

if ((doc != null) && (doc[joinKey] != null)) {
result[joinKey] = new this.joins[joinKey].model(doc[joinKey], options);
if (this.joins[joinKey].type === 'hasOne') {
if (options.createJoinedDocuments === true) {
result[joinKey] = new this.joins[joinKey].model(doc[joinKey], options);
}
else {
result[joinKey] = doc[joinKey];
}
}
else if (this.joins[joinKey].type === 'hasMany') {
if (options.createJoinedDocuments === true) {
result[joinKey] = [];
for(var i=0; i<doc[joinKey].length; i++) {
result[joinKey].push(new this.joins[joinKey].model(doc[joinKey][i], options));
}
}
else {
result[joinKey] = doc[joinKey];
}
}
}

@@ -336,3 +355,3 @@ }

else {
var doc = new model(result, {saved: true});
var doc = new model(result, {saved: true, createJoinedDocuments: true});
callback(null, doc);

@@ -382,24 +401,8 @@ }

var joinClause = model.joins[fieldName].joinClause;
var joinType = model.joins[fieldName].type;
if (typeof joinClause === 'object') {
if (type === 'object') {
query = query.do( function(doc) {
return r.db(otherModel.thinkyOptions.db).table(otherModel.name)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}).coerceTo('array').do( function(stream) {
return r.branch( stream.count().gt(1),
r.error("Found more than one match"), // TODO Improve error
r.branch( stream.count().eq(0),
r.expr(doc),
doc.merge(
r.expr([[fieldName, otherModel.getJoin(stream.nth(0), otherModel, 'object')]]).coerceTo('object')
)
)
)
});
});
}
else if (type === 'stream') {
query = query.map( function(doc) {
return r.branch(
doc.hasFields(joinClause["leftKey"]),
r.db(otherModel.thinkyOptions.db).table(otherModel.name)
if (joinType === 'hasOne') {
query = query.do( function(doc) {
return r.db(otherModel.thinkyOptions.db).table(otherModel.name)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}).coerceTo('array').do( function(stream) {

@@ -415,5 +418,53 @@ return r.branch( stream.count().gt(1),

)
});
});
}
else if (joinType === 'hasMany') {
query = query.do( function(doc) {
return doc(joinClause["leftKey"]).concatMap( function(value) {
return r.db(otherModel.thinkyOptions.db).table(otherModel.name)
.getAll( value, {index: joinClause["rightKey"]})
}).coerceTo('array').do( function(stream) {
return doc.merge(
r.expr([[fieldName, otherModel.getJoin(stream, otherModel, 'stream')]]).coerceTo('object')
)
});
});
}
}
else if (type === 'stream') {
if (joinType === 'hasOne') {
query = query.map( function(doc) {
return r.branch(
doc.hasFields(joinClause["leftKey"]),
r.db(otherModel.thinkyOptions.db).table(otherModel.name)
.getAll( doc(joinClause["leftKey"]), {index: joinClause["rightKey"]}).coerceTo('array').do( function(stream) {
return r.branch( stream.count().gt(1),
r.error("Found more than one match"), // TODO Improve error
r.branch( stream.count().eq(0),
r.expr(doc),
doc.merge(
r.expr([[fieldName, otherModel.getJoin(stream.nth(0), otherModel, 'object')]]).coerceTo('object')
)
)
)
}),
doc)
});
}
else if (joinType === 'hasMany') {
query = query.map( function(doc) {
return r.branch(
doc.hasFields(joinClause["leftKey"]),
doc(joinClause["leftKey"]).concatMap( function(joinValue) {
return r.db(otherModel.thinkyOptions.db).table(otherModel.name)
.getAll( joinValue, {index: joinClause["rightKey"]} ).coerceTo('array')
}).do( function(stream) {
return doc.merge(
r.expr([[fieldName, otherModel.getJoin(stream, otherModel, 'stream')]]).coerceTo('object')
)
}),
doc)
});
doc)
});
}
}

@@ -436,3 +487,3 @@ }

var args = [];
for(i in id) {
for(var i=0; i<id.length; i++) {
args.push(id[i]);

@@ -567,3 +618,14 @@ }

}
Model.prototype.hasMany = function(modelArg, fieldName, joinClause) {
//TODO Check arguments
// joinClause can be an object with two fields (leftKey and rightKey) or a function only
var model = this.getModel();
model.joins[fieldName] = {
model: modelArg,
joinClause: joinClause,
type: 'hasMany'
}
}
var model = module.exports = exports = Model;

@@ -7,37 +7,26 @@ var thinky = require('./lib/index.js');

Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String});
Cat = thinky.createModel('Cat', { fieldString: {_type: String} });
catou = new Cat({fieldString: 'catou'});
Human = thinky.createModel('Human', {id: String, ownerName: String});
Cat.hasOne(Human, 'owner', {leftKey: 'idHuman', rightKey: 'id'});
Dog = thinky.createModel('Dog', { fieldString2: {_type: String} });
dog = new Dog({fieldString2: 'dogou'});
var owner = new Human({ownerName: "Michel"});
var catou = new Cat({name: "Catou"});
catou['owner'] = owner;
/*
console.log(dog.getModel().name);
console.log(catou.getModel().name);
*/
catou.save( function(e, r) {
if (e) throw e;
console.log('Saved object');
console.log(JSON.stringify(r, null, 2));
console.log('');
minou = new Cat({fieldString: 'minou'});
/*
console.log(dog.getModel());
console.log(catou.getModel());
*/
var catou_id = r.id;
/*
console.log(Cat);
console.log(Dog);
console.log(Dog === Cat);
*/
Cat.get(catou_id, function(error, r) {
console.log('Retrieved object');
console.log(JSON.stringify(r, null, 2));
console.log('');
/*
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);
})
}, {getJoin: true});
}, {saveJoin: true});
{
"name": "thinky",
"version": "0.1.7",
"version": "0.2.0",
"description": "RethinkDB ORM for Node.js",

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

"dependencies":{
"generic-pool": ">= 2.0.3"
"generic-pool": ">= 2.0.3",
"rethinkdb": ">= 1.7.0"
}
}
# Thinky
JavaScript ORM for RethinkDB.
_Note_: Alpha release
_Note_: Beta release

@@ -68,7 +68,8 @@ ### Quick start

### TODO
- Joins with lambda functions
- Decide what to do with null (does it throw when checked against a Number?)
- Joins
- Premises?
- Promises?
- Do not drain the pool when poolMin/poolMax are changed
- Write examples
- Clean tests

@@ -75,0 +76,0 @@ ### About

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

});
describe('define', function(){

@@ -45,4 +46,2 @@ it('should not add the function for other documents', function(){

});
});
describe('define', function(){
it('should not add the function for new documents', function(){

@@ -85,7 +84,2 @@ minou = new Cat({name: 'Minou'});

});
});
describe('merge', function() {
it('should emit the event `change`', function(done){

@@ -115,4 +109,2 @@ Cat = thinky.createModel('Cat', { id: Number, name: String });

});
});
describe('save', function() {
it('should not change the reference of the object', function(done){

@@ -127,4 +119,2 @@ Cat = thinky.createModel('Cat', { id: String, name: String });

});
});
describe('save', function() {
it('should not change the reference of the object', function(done){

@@ -139,6 +129,2 @@ Cat = thinky.createModel('Cat', { id: String, name: String });

});
});
describe('save', function() {
it('should update the document in the database', function(done){

@@ -153,4 +139,2 @@ var value = 'Catouuuuu';

});
});
describe('save', function() {
it('should update the document (in place)', function(done){

@@ -157,0 +141,0 @@ var value = 'Catouuuuu';

@@ -10,4 +10,6 @@ var thinky = require('../lib/index.js');

describe('Model', function(){
var Cat, catou, minou, catou_id, catouCopy, minouCopy, Dog, dogou, Human, owner;
//var Cat, catou, minou, catou_id, catouCopy, minouCopy, Dog, dogou, Human, owner;
var g = {};
describe('createModel', function(){
var Cat, Dog;
it('Create model', function(){

@@ -26,2 +28,7 @@ Cat = thinky.createModel('Cat', { catName: String });

describe('new', function(){
var Cat, catou, minou, Dog, dogou;
it('-- init --', function() {
Cat = thinky.createModel('Cat', { catName: String });
Dog = thinky.createModel('Dog', { dogName: String });
});
it('should create a new instance of the Model', function() {

@@ -52,3 +59,2 @@ catou = new Cat({catName: 'Catou'});

should(catou.getModel().name, 'Cat');
});

@@ -62,54 +68,54 @@ })

it('should save String fields', function() {
Cat = thinky.createModel('Cat', { fieldString: String });
minou = new Cat({fieldString: 'Minou'});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldString: String });
var minou = new Cat({fieldString: 'Minou'});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, 'Minou');
});
it('should save Number fields', function() {
Cat = thinky.createModel('Cat', { fieldNumber: Number });
var Cat = thinky.createModel('Cat', { fieldNumber: Number });
var value = Math.random();
minou = new Cat({fieldNumber: value});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({fieldNumber: value});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldNumber, value);
});
it('should save Boolean fields', function() {
Cat = thinky.createModel('Cat', { fieldBoolean: Boolean });
var Cat = thinky.createModel('Cat', { fieldBoolean: Boolean });
var value = (Math.random > 0.5)? true: false;
minou = new Cat({fieldBoolean: value});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({fieldBoolean: value});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldBoolean, value);
});
it('should save Nested fields', function() {
Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
var Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
var value = "Hello, I'm a nested string field"
minou = new Cat({ nested: { fieldString: value}});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({ nested: { fieldString: value}});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.nested.fieldString, value);
});
it('should save Array fields', function() {
Cat = thinky.createModel('Cat', { arrayOfStrings: [String] });
var Cat = thinky.createModel('Cat', { arrayOfStrings: [String] });
var value = ["a", "b", "c"]
minou = new Cat({ arrayOfStrings: value });
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({ arrayOfStrings: value });
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.arrayOfStrings.join(), value.join());
});
it('should save double nested fields', function() {
Cat = thinky.createModel('Cat', { nested: { nestedBis: {fieldString: String }}});
var Cat = thinky.createModel('Cat', { nested: { nestedBis: {fieldString: String }}});
var value = "Hello, I'm a nested string field"
minou = new Cat({ nested: { nestedBis: {fieldString: value}}});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({ nested: { nestedBis: {fieldString: value}}});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.nested.nestedBis.fieldString, value);
});
it('should save Array or Array fields', function() {
var Cat = thinky.createModel('Cat', { arrayOfStrings: [[String]] });
var value = [["a", "b"], ["c", "d"], ["e"]];
Cat = thinky.createModel('Cat', { arrayOfStrings: [[String]] });
minou = new Cat({ arrayOfStrings: value });
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({ arrayOfStrings: value });
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.arrayOfStrings.join(), value.join());
});
it('should save Array of objects', function() {
var Cat = thinky.createModel('Cat', { arrayOfObjects: [{ key: String}] });
var value = [{key: 'a'}, {key: 'b'}, {key: 'c'}];
Cat = thinky.createModel('Cat', { arrayOfObjects: [{ key: String}] });
minou = new Cat({ arrayOfObjects: value });
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({ arrayOfObjects: value });
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.arrayOfObjects.length, value.length);

@@ -120,37 +126,37 @@ });

it('should by default ignore String fields if not provided', function() {
Cat = thinky.createModel('Cat', { fieldString: String });
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldString: String });
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, undefined);
});
it('should by default ignore Number fields if not provided', function() {
Cat = thinky.createModel('Cat', { fieldNumber: Number });
var Cat = thinky.createModel('Cat', { fieldNumber: Number });
var value = Math.random();
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldNumber, undefined);
});
it('should by default ignore Boolean fields if not provided', function() {
Cat = thinky.createModel('Cat', { fieldBoolean: Boolean });
var Cat = thinky.createModel('Cat', { fieldBoolean: Boolean });
var value = (Math.random > 0.5)? true: false;
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldBoolean, undefined);
});
it('should by default ignore Nested fields if not provided -- at nested level', function() {
Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
minou = new Cat({ nested: {}});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
var minou = new Cat({ nested: {}});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.nested.fieldString, undefined);
});
it('should by default ignore Nested fields if not provided -- at first level', function() {
Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.nested, undefined);
});
it('should by default ignore Array fields if not provided', function() {
Cat = thinky.createModel('Cat', { arrayOfStrings: [String] });
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { arrayOfStrings: [String] });
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.arrayOfStrings, undefined);

@@ -161,7 +167,7 @@ });

it('should by default throw if a String field have another non null type', function() {
Cat = thinky.createModel('Cat', { fieldString: String });
var Cat = thinky.createModel('Cat', { fieldString: String });
(function() { minou = new Cat({fieldString: 1}) }).should.throw('Value for [fieldString] must be a String')
});
it('should by default throw if a Number field have another non null type', function() {
Cat = thinky.createModel('Cat', { fieldNumber: Number });
var Cat = thinky.createModel('Cat', { fieldNumber: Number });
var value = Math.random();

@@ -171,11 +177,11 @@ (function() { minou = new Cat({fieldNumber: 'string'}) }).should.throw('Value for [fieldNumber] must be a Number')

it('should by default throw if a Boolean field have another non null type', function() {
Cat = thinky.createModel('Cat', { fieldBoolean: Boolean });
var Cat = thinky.createModel('Cat', { fieldBoolean: Boolean });
(function() { minou = new Cat({fieldBoolean: 'string'}) }).should.throw('Value for [fieldBoolean] must be a Boolean')
});
it('should by default throw if a Nested field have another non null type -- first level', function() {
Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
var Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
(function() { minou = new Cat({nested: 'string'}) }).should.throw('Value for [nested] must be an Object')
});
it('should by default throw if a Nested field have another non null type -- second level', function() {
Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
var Cat = thinky.createModel('Cat', { nested: { fieldString: String } });
(function() { minou = new Cat({nested: {fieldString: 1}}) }).should.throw('Value for [nested][fieldString] must be a String')

@@ -191,11 +197,11 @@ });

it('should save String fields (schema defined with options)', function() {
Cat = thinky.createModel('Cat', { fieldString: {_type: String }});
minou = new Cat({fieldString: 'Minou'});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldString: {_type: String }});
var minou = new Cat({fieldString: 'Minou'});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, 'Minou');
});
it('should miss the String field (schema defined with options)', function() {
Cat = thinky.createModel('Cat', { fieldString: {_type: String }});
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldString: {_type: String }});
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, undefined);

@@ -205,8 +211,8 @@ });

var value = "noString1";
Cat = thinky.createModel('Cat', { fieldString: {
var Cat = thinky.createModel('Cat', { fieldString: {
_type: String,
default: value
}});
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, value);

@@ -216,24 +222,24 @@ });

var value = "noString2";
Cat = thinky.createModel('Cat', { fieldString: {
var Cat = thinky.createModel('Cat', { fieldString: {
_type: String,
default: function() { return value }
}});
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, value);
});
it('should use the default function -- with the original doc', function() {
var value = "noString3";
Cat = thinky.createModel('Cat', { fieldString: {
var Cat = thinky.createModel('Cat', { fieldString: {
_type: String,
default: function(doc) { return doc.value }
}});
minou = new Cat({value: value});
should(Object.prototype.toString.call(catou) === '[object Object]');
var value = "noString3";
var minou = new Cat({value: value});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldString, value);
});
it('should save Object fields (schema defined with options)', function() {
Cat = thinky.createModel('Cat', { fieldObject: {_type: Object, schema: {fieldString: String} }});
minou = new Cat({fieldObject: {fieldString: 'Minou'}});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldObject: {_type: Object, schema: {fieldString: String} }});
var minou = new Cat({fieldObject: {fieldString: 'Minou'}});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldObject.fieldString, 'Minou');

@@ -243,5 +249,5 @@ });

var value = 'stringDefaultObjectOption';
Cat = thinky.createModel('Cat', { fieldObject: {_type: Object, schema: {fieldString: String}, default: { fieldString: value} }});
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldObject: {_type: Object, schema: {fieldString: String}, default: { fieldString: value} }});
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldObject.fieldString, value);

@@ -251,5 +257,5 @@ });

var value = ["a", "b", "c"];
Cat = thinky.createModel('Cat', { fieldArray: {_type: Array, schema: String }});
minou = new Cat({fieldArray: value});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldArray: {_type: Array, schema: String }});
var minou = new Cat({fieldArray: value});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldArray.join(), value.join());

@@ -259,5 +265,5 @@ });

var value = ["a", "b", "c"];
Cat = thinky.createModel('Cat', { fieldArray: {_type: Array, schema: String, default: value}});
minou = new Cat({});
should(Object.prototype.toString.call(catou) === '[object Object]');
var Cat = thinky.createModel('Cat', { fieldArray: {_type: Array, schema: String, default: value}});
var minou = new Cat({});
should(Object.prototype.toString.call(minou) === '[object Object]');
should.equal(minou.fieldArray.join(), value.join());

@@ -269,39 +275,39 @@ });

it('should throw when a String is missing (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { fieldString: String }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { fieldString: String }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.throw('Value for [fieldString] must be defined')
});
it('should throw when a String is missing (defined with options) (enforce on model level)', function() {
Cat = thinky.createModel('Cat', { fieldString: {_type: String} }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { fieldString: {_type: String} }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.throw('Value for [fieldString] must be defined')
});
it('should throw when an extra field is provided (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { fieldString: String }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { fieldString: String }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({fieldString: 'hello', outOfSchema: 1}) }).should.throw('An extra field `[outOfSchema]` not defined in the schema was found.')
});
it('should throw when a String is missing (defined with options) (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { fieldString: {_type: String} }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { fieldString: {_type: String} }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({fieldString: 'hello', outOfSchema: 1}) }).should.throw('An extra field `[outOfSchema]` not defined in the schema was found.')
});
it('should throw when an Object is missing (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { nested: {fieldString: String} }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { nested: {fieldString: String} }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.throw('Value for [nested] must be defined')
});
it('should throw when an Object is missing (defined with options) (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { nested: {_type: Object, schema: {fieldString: String} }}, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { nested: {_type: Object, schema: {fieldString: String} }}, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.throw('Value for [nested] must be defined')
});
it('should throw when an Array is missing (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { arrayField: [String] }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { arrayField: [String] }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.throw('Value for [arrayField] must be defined')
});
it('should throw when an Array is missing (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { arrayField: {_type: Array, schema: String} }, {enforce: {type: true, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { arrayField: {_type: Array, schema: String} }, {enforce: {type: true, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.throw('Value for [arrayField] must be defined')
});
it('should throw when a field does not have the proper type (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { fieldString: String }, {enforce: {type: false, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { fieldString: String }, {enforce: {type: false, missing: true, extra: true}});
(function() { minou = new Cat({fieldString: 1}) }).should.not.throw();
});
it('should throw when a field is missing and the default value does not have the proper type (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { fieldString: {_type: String, default: 1} }, {enforce: {type: false, missing: true, extra: true}});
var Cat = thinky.createModel('Cat', { fieldString: {_type: String, default: 1} }, {enforce: {type: false, missing: true, extra: true}});
(function() { minou = new Cat({}) }).should.not.throw();

@@ -313,3 +319,3 @@ });

it('should throw when a String is missing (defined with options) (enforce on model leve)', function() {
Cat = thinky.createModel('Cat', { fieldString: {_type: String, enforce: {type: true, missing: true, extra: true}}} );
var Cat = thinky.createModel('Cat', { fieldString: {_type: String, enforce: {type: true, missing: true, extra: true}}} );
(function() { minou = new Cat({}) }).should.throw('Value for [fieldString] must be defined')

@@ -322,4 +328,8 @@ });

describe('define', function() {
var Cat, Dog;
it('-- init --', function() {
Cat = thinky.createModel('Cat', { catName: String });
Dog = thinky.createModel('Dog', { dogName: String });
})
it('should save a method', function() {
Cat = thinky.createModel('Cat', { catName: String });
Cat.define('hello', function() { return 'hello, my name is '+this.catName; })

@@ -329,3 +339,3 @@ should.exist(Cat.hello)

it('should define the function for previously created documents', function(){
catou = new Cat({catName: 'Catou'});
var catou = new Cat({catName: 'Catou'});
should.exist(catou.hello);

@@ -335,8 +345,7 @@ should.equal(catou.hello(), 'hello, my name is Catou');

it('should not create a mehtod for another class', function() {
Dog = thinky.createModel('Dog', { dogName: String });
dogou = new Dog({dogName: "Dogou"});
var dogou = new Dog({dogName: "Dogou"});
should.not.exist(dogou.hello);
});
it('should define the function for newly created documents', function(){
minou = new Cat({catName: 'Minou'});
var minou = new Cat({catName: 'Minou'});
should.exist(minou.hello);

@@ -346,7 +355,7 @@ should.equal(minou.hello(), 'hello, my name is Minou');

it('should throw if the user may overwrite an existing method', function(){
Cat = thinky.createModel('Cat', { catName: String });
var Cat = thinky.createModel('Cat', { catName: String });
(function() { Cat.define('name', function() { return 'hello' }) }).should.throw('A property/method named `name` is already defined. Use Model.define(key, fn, true) to overwrite the function.');
});
it('should throw if the user may overwrite an existing method -- customed method', function(){
Cat = thinky.createModel('Cat', { catName: String });
var Cat = thinky.createModel('Cat', { catName: String });
Cat.define('hello', function() { return 'hello, my name is '+this.catName; });

@@ -356,7 +365,7 @@ (function() { Cat.define('hello', function() { return 'hello' }) }).should.throw('A property/method named `hello` is already defined. Use Model.define(key, fn, true) to overwrite the function.');

it('should throw if the user may overwrite an existing method', function(){
Cat = thinky.createModel('Cat', { catName: String });
var Cat = thinky.createModel('Cat', { catName: String });
(function() { Cat.define('name', function() { return 'hello' }, true) }).should.not.throw();
});
it('should throw if the user may overwrite an existing method -- customed method', function(){
Cat = thinky.createModel('Cat', { catName: String });
var Cat = thinky.createModel('Cat', { catName: String });
Cat.define('hello', function() { return 'hello, my name is '+this.catName; });

@@ -370,5 +379,5 @@ (function() { Cat.define('hello', function() { return 'hello' }, true) }).should.not.throw();

it('should change the schema', function() {
Cat = thinky.createModel('Cat', { catName: String, age: Number });
var Cat = thinky.createModel('Cat', { catName: String, age: Number });
Cat.setSchema({ catName: String, owner: String })
catou = new Cat({ catName: 'Catou', owner: 'Michel', age: 7});
var catou = new Cat({ catName: 'Catou', owner: 'Michel', age: 7});
should.equal(catou.catName, 'Catou');

@@ -383,7 +392,7 @@ should.equal(catou.owner, 'Michel');

it('should return the primary key -- default primary key', function() {
Cat = thinky.createModel('Cat', { catName: String });
var Cat = thinky.createModel('Cat', { catName: String });
should.equal(Cat.getPrimaryKey(), 'id');
});
it('should return the primary key', function() {
Cat = thinky.createModel('Cat -- customed primary key', { catName: String }, {primaryKey: 'test'});
var Cat = thinky.createModel('Cat -- customed primary key', { catName: String }, {primaryKey: 'test'});
should.equal(Cat.getPrimaryKey(), 'test');

@@ -398,9 +407,9 @@ });

it('should add a field id -- Testing model', function(done){
Cat = thinky.createModel('Cat', { id: String, name: String });
var Cat = thinky.createModel('Cat', { id: String, name: String });
catou = new Cat({name: 'Catou'});
catou.save( function(error, result) {
catouCopy = result;
g.catouCopy = result;
should.exist(result.id);
minou = new Cat({name: 'Minou'});
var minou = new Cat({name: 'Minou'});
minou.save( function(error, result) {

@@ -413,6 +422,23 @@ minouCopy = result;

});
describe('get', function() {
var Cat;
var scope = {}
it('-- init --', function(done){
Cat = thinky.createModel('Cat', { id: String, name: String });
var catou = new Cat({name: 'Catou'});
catou.save( function(error, result) {
scope.catou = result;
should.exist(result.id);
var minou = new Cat({name: 'Minou'});
minou.save( function(error, result) {
scope.minou = result;
done();
});
});
});
it('should retrieve a document in the database', function(done){
Cat.get(catouCopy.id, function(error, result) {
should(_.isEqual(result, catouCopy));
Cat.get(scope.catou.id, function(error, result) {
should(_.isEqual(result, scope.catou));
done();

@@ -422,6 +448,6 @@ })

it('should retrieve documents in the database', function(done){
Cat.get([catouCopy.id, minouCopy.id], function(error, result) {
Cat.get([scope.catou.id, scope.minou.id], function(error, result) {
should.not.exists(error);
result.should.have.length(2);
should((result[0].id === catouCopy.id) || (result[0].id === minouCopy.id));
should((result[0].id === scope.catou.id) || (result[0].id === scope.minou.id));
done();

@@ -432,5 +458,21 @@ })

describe('getAll', function() {
var Cat;
var scope = {}
it('-- init --', function(done){
Cat = thinky.createModel('Cat', { id: String, name: String });
var catou = new Cat({name: 'Catou'});
catou.save( function(error, result) {
scope.catou = result;
should.exist(result.id);
var minou = new Cat({name: 'Minou'});
minou.save( function(error, result) {
scope.minou = result;
done();
});
});
});
it('should retrieve some documents in the database -- single values', function(done){
Cat.getAll(catouCopy.name, 'name', function(error, result) {
should.not.exists(error);
Cat.getAll(scope.catou.name, 'name', function(error, result) {
should.not.exist(error);
should(result.length >= 1);

@@ -441,3 +483,3 @@ done();

it('should retrieve some documents in the database -- multiple values', function(done){
Cat.getAll([catouCopy.name, minouCopy.name], 'name', function(error, result) {
Cat.getAll([scope.catou.name, scope.minou.name], 'name', function(error, result) {
should.not.exists(error);

@@ -450,2 +492,18 @@ should(result.length >= 2);

describe('filter', function() {
var Cat;
var scope = {}
it('-- init --', function(done){
Cat = thinky.createModel('Cat', { id: String, name: String });
var catou = new Cat({name: 'Catou'});
catou.save( function(error, result) {
scope.catou = result;
should.exist(result.id);
var minou = new Cat({name: 'Minou'});
minou.save( function(error, result) {
scope.minou = result;
done();
});
});
});
it('retrieve documents in the database', function(done){

@@ -460,4 +518,2 @@ Cat.filter(function(doc) { return r.expr([catouCopy.id, minouCopy.id]).contains(doc("id")) },

});
});
describe('filter', function() {
it('should return many documents', function(done){

@@ -475,2 +531,3 @@ Cat.filter(function(doc) { return true },

it('should return the number of document in the table', function(done){
var Cat = thinky.createModel('Cat', { id: String, name: String });
Cat.filter(function(doc) { return true },

@@ -492,3 +549,3 @@ function(error, result) {

it('should add a listener on the model', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
Cat.on('test', function() { });

@@ -499,3 +556,3 @@ should.exists(Cat.getModel()._listeners['test']);

it('should add a listener on the model', function(done) {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
Cat.on('test', function() { done(); });

@@ -506,3 +563,3 @@ catou = new Cat({name: 'Catou'});

it('should not pollute other/new models', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
should.not.exist(Cat.getModel()._listeners['test']);

@@ -512,3 +569,3 @@ });

var count = 0
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
Cat.on('test', function() {});

@@ -521,3 +578,3 @@ Cat.on('test', function() {});

it('should remove one listener if the event and listener are provided', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
var fn = function() {};

@@ -532,3 +589,3 @@ Cat.on('test', function() { });

it('should remove all the listeners of an event if only the event is provided', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
var fn = function() {};

@@ -542,3 +599,3 @@ Cat.on('test', function() { });

it('should remove all the listeners that match the one provided if only the listener is provided', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
var fn = function() {};

@@ -553,3 +610,3 @@ Cat.on('test', function() { });

it('should remove everything if no argument is provided', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
var fn = function() {};

@@ -567,3 +624,3 @@ Cat.on('test', fn);

it('should return the listeners for the event', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
var fn = function() {};

@@ -580,3 +637,3 @@ Cat.on('test', fn);

it('should return the listeners for the event', function() {
Cat = thinky.createModel('Cat', {name: String});
var Cat = thinky.createModel('Cat', {name: String});
var fn = function() {};

@@ -599,7 +656,6 @@ Cat.on('test', fn);

describe('hasOne', function() {
var Cat, Human, Mother;
it('should add a new key in model.joins', function() {
Cat = thinky.createModel('Cat', {id: String, name: String, idHuman: String});
Human = thinky.createModel('Human', {id: String, ownerName: String});
var catou = new Cat({name: "Catou"});
var owner = new Human({ownerName: "Michel"});
Cat.hasOne(Human, 'owner', {leftKey: 'idHuman', rightKey: 'id'});

@@ -625,4 +681,5 @@ should.exist(Cat.getModel().joins['owner']);

Cat.hasOne(Human, 'owner', {leftKey: 'idHuman', rightKey: 'id'});
var owner = new Human({ownerName: "Michel"});
catou = new Cat({name: "Catou"});
var catou = new Cat({name: "Catou"});
var mother = new Mother({motherName: "Mom"});

@@ -753,7 +810,88 @@ catou['owner'] = owner;

});
})
describe('hasMany', function() {
var Cat, catou, Task, task1, task2, task3;
it('should add a new key in model.joins', function() {
Cat = thinky.createModel('Cat', {id: String, name: String, taskIds: [String]});
Task = thinky.createModel('Task', {id: String, task: String});
Cat.hasMany(Task, 'tasks', {leftKey: 'taskIds', rightKey: 'id'});
should.exist(Cat.getModel().joins['tasks']);
should(Cat.getModel().joins['tasks'].type, 'hasMany');
should(Cat.getModel().joins['tasks'].model, Task);
should(Cat.getModel().joins['tasks'].joinClause.leftKey, 'taskIds');
should(Cat.getModel().joins['tasks'].joinClause.rightKey, 'id');
});
it('should be able to save the joined doc', function(done) {
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( 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);
done();
}, {saveJoin: true});
});
it('get should be able to get joined documents', function(done) {
Cat.get(catou.id, function(error, result) {
result.taskIds.should.have.length(3);
should.exist(result.taskIds[0]);
should.exist(result.taskIds[1]);
should.exist(result.taskIds[2]);
should.exist(result.tasks[0].id);
should.exist(result.tasks[1].id);
should.exist(result.tasks[2].id);
catou.tasks.should.have.length(3);
done();
}, {getJoin: true})
});
it('get should be able to get joined documents -- and they should be `saved`', function(done) {
Cat.get(catou.id, function(error, result) {
should(result.tasks[0].getDocument().docSettings.saved, true);
should(result.tasks[1].getDocument().docSettings.saved, true);
should(result.tasks[2].getDocument().docSettings.saved, true);
done();
}, {getJoin: true})
});
it('getAll should work', function(done) {
Cat.getAll([catou.id], 'id', function(error, result) {
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]);
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();
}, {getJoin: true})
});
it('filter should work', function(done) {
Cat.filter(function(doc) { return doc("id").eq(catou.id) }, function(error, result) {
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]);
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();
}, {getJoin: true})
});
})
})
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