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

Commit be79cfe

Browse files
authored
fix(query): not found relationship are null or empty array (#19)
* fix(query): not found relationship are null or empty array
1 parent c7b4d7f commit be79cfe

File tree

6 files changed

+164
-3
lines changed

6 files changed

+164
-3
lines changed

lib/model/parse.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const debug = require('debug')('requelize:model:parse')
1111
*/
1212
module.exports = (obj, opts, Model, requelize) => {
1313
if (!obj) {
14-
return
14+
// return null if null, undefined if undefined
15+
return obj
1516
}
1617

1718
if (Array.isArray(obj)) {

lib/model/query/belongsTo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) {
2323
)
2424

2525
return function (doc) {
26-
const q = relTable.get(doc(join.localKey))
26+
const q = relTable.get(doc(join.localKey)).default(null)
2727

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

lib/model/query/belongsToMany.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) {
4444
})
4545
._query
4646
.coerceTo('array')
47+
.default([])
4748

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

lib/model/query/hasMany.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module.exports = function (Query, requelize, Model, join, tree, key) {
2727
const q = relTable
2828
.getAll(doc(selfPk), { index: join.foreignKey })
2929
.coerceTo('array')
30+
.default([])
3031

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "requelize",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"description": "RethinkDB ORM",
55
"main": "index.js",
66
"repository": "https://github.com/buckless/requelize.git",

test/test.unknownRelation.js

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
const Joi = require('joi')
2+
const { test, requelize, dropDb } = require('./utils')
3+
4+
test('unknown relation - hasOne', (t) => {
5+
t.plan(1)
6+
7+
let Foo
8+
let Bar
9+
let foo
10+
11+
dropDb()
12+
.then(() => {
13+
Foo = requelize.model('foo', {
14+
name: Joi.string()
15+
})
16+
17+
Bar = requelize.model('bar')
18+
19+
Foo.hasOne('bar', 'bar')
20+
Bar.belongsTo('foo', 'foo')
21+
22+
return requelize.sync()
23+
})
24+
.then(() => {
25+
foo = new Foo({ name: 'foo' })
26+
27+
return foo.save()
28+
})
29+
.then((res) => {
30+
return Foo.embed({ bar: true })
31+
})
32+
.then((res) => {
33+
t.equal(null, res[0].bar, 'hasOne relation is null when not found')
34+
})
35+
.catch((err) => {
36+
t.fail(err)
37+
})
38+
.then(() => {
39+
t.end()
40+
})
41+
})
42+
43+
test('unknown relation - belongsTo', (t) => {
44+
t.plan(1)
45+
46+
let Foo
47+
let Bar
48+
let bar
49+
50+
dropDb()
51+
.then(() => {
52+
Foo = requelize.model('foo', {
53+
name: Joi.string()
54+
})
55+
56+
Bar = requelize.model('bar')
57+
58+
Foo.hasOne('bar', 'bar')
59+
Bar.belongsTo('foo', 'foo')
60+
61+
return requelize.sync()
62+
})
63+
.then(() => {
64+
bar = new Bar({ name: 'bar' })
65+
66+
return bar.save()
67+
})
68+
.then((res) => {
69+
return Bar.embed({ foo: true })
70+
})
71+
.then((res) => {
72+
t.equal(null, res[0].foo, 'belongsTo relation is null when not found')
73+
})
74+
.catch((err) => {
75+
t.fail(err)
76+
})
77+
.then(() => {
78+
t.end()
79+
})
80+
})
81+
82+
test('unknown relation - hasMany', (t) => {
83+
t.plan(1)
84+
85+
let Foo
86+
let Bar
87+
let foo
88+
89+
dropDb()
90+
.then(() => {
91+
Foo = requelize.model('foo', {
92+
name: Joi.string()
93+
})
94+
95+
Bar = requelize.model('bar')
96+
97+
Foo.hasMany('bar', 'bars')
98+
Bar.belongsTo('foo', 'foo')
99+
100+
return requelize.sync()
101+
})
102+
.then(() => {
103+
foo = new Foo({ name: 'foo' })
104+
105+
return foo.save()
106+
})
107+
.then((res) => {
108+
return Foo.embed({ bars: true })
109+
})
110+
.then((res) => {
111+
t.equal(0, res[0].bars.length, 'hasMany relation is empty array when not found')
112+
})
113+
.catch((err) => {
114+
t.fail(err)
115+
})
116+
.then(() => {
117+
t.end()
118+
})
119+
})
120+
121+
test('unknown relation - belongsToMany', (t) => {
122+
t.plan(1)
123+
124+
let Foo
125+
let Bar
126+
let foo
127+
128+
dropDb()
129+
.then(() => {
130+
Foo = requelize.model('foo', {
131+
name: Joi.string()
132+
})
133+
134+
Bar = requelize.model('bar')
135+
136+
Foo.belongsToMany('bar', 'bars')
137+
Bar.belongsToMany('foo', 'foo')
138+
139+
return requelize.sync()
140+
})
141+
.then(() => {
142+
foo = new Foo({ name: 'foo' })
143+
144+
return foo.save()
145+
})
146+
.then((res) => {
147+
return Foo.embed({ bars: true })
148+
})
149+
.then((res) => {
150+
t.equal(0, res[0].bars.length, 'hasMany relation is empty array when not found')
151+
})
152+
.catch((err) => {
153+
t.fail(err)
154+
})
155+
.then(() => {
156+
t.end()
157+
})
158+
})

0 commit comments

Comments
 (0)