diff --git a/lib/model/parse.js b/lib/model/parse.js index edd1c8e..5a17149 100644 --- a/lib/model/parse.js +++ b/lib/model/parse.js @@ -11,7 +11,8 @@ const debug = require('debug')('requelize:model:parse') */ module.exports = (obj, opts, Model, requelize) => { if (!obj) { - return + // return null if null, undefined if undefined + return obj } if (Array.isArray(obj)) { diff --git a/lib/model/query/belongsTo.js b/lib/model/query/belongsTo.js index 7682e5c..306b124 100644 --- a/lib/model/query/belongsTo.js +++ b/lib/model/query/belongsTo.js @@ -23,7 +23,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) { ) return function (doc) { - const q = relTable.get(doc(join.localKey)) + const q = relTable.get(doc(join.localKey)).default(null) let embedQuery = new Query(q, RelModel, requelize) diff --git a/lib/model/query/belongsToMany.js b/lib/model/query/belongsToMany.js index c6ea9bd..61f3880 100644 --- a/lib/model/query/belongsToMany.js +++ b/lib/model/query/belongsToMany.js @@ -44,6 +44,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) { }) ._query .coerceTo('array') + .default([]) let embedQuery = new Query(q, RelModel, requelize) diff --git a/lib/model/query/hasMany.js b/lib/model/query/hasMany.js index 8092e17..33c1c7c 100644 --- a/lib/model/query/hasMany.js +++ b/lib/model/query/hasMany.js @@ -27,6 +27,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) { const q = relTable .getAll(doc(selfPk), { index: join.foreignKey }) .coerceTo('array') + .default([]) let embedQuery = new Query(q, RelModel, requelize) diff --git a/package.json b/package.json index f5fbe8b..4f872e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "requelize", - "version": "0.7.1", + "version": "0.7.2", "description": "RethinkDB ORM", "main": "index.js", "repository": "https://github.com/buckless/requelize.git", diff --git a/test/test.unknownRelation.js b/test/test.unknownRelation.js new file mode 100644 index 0000000..4670236 --- /dev/null +++ b/test/test.unknownRelation.js @@ -0,0 +1,158 @@ +const Joi = require('joi') +const { test, requelize, dropDb } = require('./utils') + +test('unknown relation - hasOne', (t) => { + t.plan(1) + + let Foo + let Bar + let foo + + dropDb() + .then(() => { + Foo = requelize.model('foo', { + name: Joi.string() + }) + + Bar = requelize.model('bar') + + Foo.hasOne('bar', 'bar') + Bar.belongsTo('foo', 'foo') + + return requelize.sync() + }) + .then(() => { + foo = new Foo({ name: 'foo' }) + + return foo.save() + }) + .then((res) => { + return Foo.embed({ bar: true }) + }) + .then((res) => { + t.equal(null, res[0].bar, 'hasOne relation is null when not found') + }) + .catch((err) => { + t.fail(err) + }) + .then(() => { + t.end() + }) +}) + +test('unknown relation - belongsTo', (t) => { + t.plan(1) + + let Foo + let Bar + let bar + + dropDb() + .then(() => { + Foo = requelize.model('foo', { + name: Joi.string() + }) + + Bar = requelize.model('bar') + + Foo.hasOne('bar', 'bar') + Bar.belongsTo('foo', 'foo') + + return requelize.sync() + }) + .then(() => { + bar = new Bar({ name: 'bar' }) + + return bar.save() + }) + .then((res) => { + return Bar.embed({ foo: true }) + }) + .then((res) => { + t.equal(null, res[0].foo, 'belongsTo relation is null when not found') + }) + .catch((err) => { + t.fail(err) + }) + .then(() => { + t.end() + }) +}) + +test('unknown relation - hasMany', (t) => { + t.plan(1) + + let Foo + let Bar + let foo + + dropDb() + .then(() => { + Foo = requelize.model('foo', { + name: Joi.string() + }) + + Bar = requelize.model('bar') + + Foo.hasMany('bar', 'bars') + Bar.belongsTo('foo', 'foo') + + return requelize.sync() + }) + .then(() => { + foo = new Foo({ name: 'foo' }) + + return foo.save() + }) + .then((res) => { + return Foo.embed({ bars: true }) + }) + .then((res) => { + t.equal(0, res[0].bars.length, 'hasMany relation is empty array when not found') + }) + .catch((err) => { + t.fail(err) + }) + .then(() => { + t.end() + }) +}) + +test('unknown relation - belongsToMany', (t) => { + t.plan(1) + + let Foo + let Bar + let foo + + dropDb() + .then(() => { + Foo = requelize.model('foo', { + name: Joi.string() + }) + + Bar = requelize.model('bar') + + Foo.belongsToMany('bar', 'bars') + Bar.belongsToMany('foo', 'foo') + + return requelize.sync() + }) + .then(() => { + foo = new Foo({ name: 'foo' }) + + return foo.save() + }) + .then((res) => { + return Foo.embed({ bars: true }) + }) + .then((res) => { + t.equal(0, res[0].bars.length, 'hasMany relation is empty array when not found') + }) + .catch((err) => { + t.fail(err) + }) + .then(() => { + t.end() + }) +})