Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

Commit

Permalink
fix(query): not found relationship are null or empty array (#19)
Browse files Browse the repository at this point in the history
* fix(query): not found relationship are null or empty array
  • Loading branch information
gjuchault committed Apr 8, 2017
1 parent c7b4d7f commit be79cfe
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/model/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/model/query/belongsTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions lib/model/query/belongsToMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) {
})
._query
.coerceTo('array')
.default([])

let embedQuery = new Query(q, RelModel, requelize)

Expand Down
1 change: 1 addition & 0 deletions lib/model/query/hasMany.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
158 changes: 158 additions & 0 deletions test/test.unknownRelation.js
Original file line number Diff line number Diff line change
@@ -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()
})
})

0 comments on commit be79cfe

Please sign in to comment.