Skip to content

Commit

Permalink
accept unnested include format
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0uch committed May 3, 2017
1 parent 02c7b77 commit 3f5281e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
4 changes: 4 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog


### 5.2.3 (2017-05-03)
- Improvement: more flexible `include` format, it can accept unnested structures now.


### 5.2.2 (2017-04-23)
- Fix: do not try to call `endTransaction` if none was initiated.

Expand Down
8 changes: 8 additions & 0 deletions lib/dispatch/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,21 @@ module.exports = function include (context) {
if (!idCache[type][id]) idCache[type][id] = true
}

// Cast `include` into an array if it's using shorthand.
if (!Array.isArray(include[0])) include = [ include ]

return Promise.all(map(include, function (fields) {
return new Promise(function (resolve, reject) {
var currentType = type
var currentIds = []
var includeOptions = []
var currentCache, currentOptions, currentField, i, j

// Cast `fields` into an array if it's using shorthand.
if (!Array.isArray(fields) ||
(!Array.isArray(fields[1]) && typeof fields[1] === 'object'))
fields = [ fields ]

for (i = 0, j = fields.length; i < j; i++)
if (Array.isArray(fields[i])) {
includeOptions[i] = fields[i][1]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "fortune",
"description": "Database abstraction layer that implements common features for Node.js and web browsers.",
"version": "5.2.2",
"version": "5.2.3",
"license": "MIT",
"homepage": "http://fortune.js.org",
"repository": {
Expand Down
14 changes: 2 additions & 12 deletions test/integration/methods/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ run((assert, comment) => {
run((assert, comment) => {
comment('get includes')
return findTest({
request: [ 'user', [ 1, 2 ], null, [
[
'ownedPets'
]
] ],
request: [ 'user', [ 1, 2 ], null, 'ownedPets' ],
response: response => {
assert(deepEqual(response.payload.records
.map(record => record[primaryKey]).sort((a, b) => a - b),
Expand All @@ -59,13 +55,7 @@ run((assert, comment) => {
comment('get includes with options')
return findTest({
request: [ 'user', 1, null, [
[
'spouse',
[
'enemies',
{ fields: { name: true } }
]
]
'spouse', [ 'enemies', { fields: { name: true } } ]
] ],
response: response => {
assert(response.payload.include.user.length === 1,
Expand Down

0 comments on commit 3f5281e

Please sign in to comment.