Skip to content

Commit cb30bd6

Browse files
committed
add support for extra populate options on relationships
1 parent 1d00015 commit cb30bd6

File tree

3 files changed

+47
-17
lines changed

3 files changed

+47
-17
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ var JSONAPIMongoParser = require(json-api-mongo-parser);
3636
var jsonApiMongoParser = new JSONAPIMongoParser({
3737
article: {
3838
relationships: { // Declaring relationships with its type
39-
author: 'people',
40-
comments: 'comment',
39+
author: 'people', // can be a string
40+
comments: { // Or an object with extra options for population query
41+
type : 'comment',
42+
options: {
43+
lean: true
44+
}
45+
},
4146
}
4247
},
4348
comment: {

lib/JSONAPIMongoParser.js

+31-14
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,25 @@ module.exports = class JSONAPIMongoParser {
116116
if (lastNode === currentNode) {
117117
// Parse select for include type
118118
const select = this.parseFields(_.get(flatOptions[type], chain.slice(0, j + 1).concat(['type'])), fieldsQuery);
119+
// Get populate options defined on this relationships
120+
const options = _.get(flatOptions[type], chain.slice(0, j + 1).concat(['options']));
121+
122+
// Populate query
119123
const populate = {
120124
path: wantedNode,
121125
populate: [],
122126
};
123127

128+
// Select
124129
if (select) {
125130
populate.select = select;
126131
}
127132

133+
// Extra options
134+
if (options) {
135+
populate.options = options;
136+
}
137+
128138
const newNode = currentNode[k] = populate;
129139
currentNode = newNode.populate;
130140
}
@@ -138,6 +148,15 @@ module.exports = class JSONAPIMongoParser {
138148
return !_.isEmpty(output) ? output : undefined;
139149
}
140150

151+
parse(type, query) {
152+
return {
153+
select: this.parseFields(type, query.fields),
154+
sort: this.parseSort(query.sort),
155+
page: this.parsePage(query.page),
156+
populate: this.parseInclude(type, query.include, query.fields),
157+
};
158+
}
159+
141160
_removeDeepEmptyArray(item) {
142161
const that = this;
143162
_.forOwn(item, (value, key) => {
@@ -151,26 +170,24 @@ module.exports = class JSONAPIMongoParser {
151170
});
152171
}
153172

154-
parse(type, query) {
155-
return {
156-
select: this.parseFields(type, query.fields),
157-
sort: this.parseSort(query.sort),
158-
page: this.parsePage(query.page),
159-
populate: this.parseInclude(type, query.include, query.fields),
160-
};
161-
}
162-
163173
_flattenOptions() {
164174
const flatOptions = {};
165175
const that = this;
166176

167177
const flattenRelationships = function(type) {
168-
const flatRelationships = {
169-
type: type,
170-
};
178+
let flatRelationships;
179+
180+
// Type definition can be a string or an object with extra options for population query
181+
if (_.isString(type)) {
182+
flatRelationships = {
183+
type: type,
184+
};
185+
} else {
186+
flatRelationships = type;
187+
}
171188

172-
if (that.options[type] && that.options[type].relationships) {
173-
_.forOwn(that.options[type].relationships, (value, key) => {
189+
if (that.options[flatRelationships.type] && that.options[flatRelationships.type].relationships) {
190+
_.forOwn(that.options[flatRelationships.type].relationships, (value, key) => {
174191
flatRelationships[key] = flattenRelationships(value);
175192
});
176193
}

test/unit/JSONAPIMongoParser.test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ describe('JSONAPIMongoParser', function() {
325325
article: {
326326
relationships: {
327327
author: 'people',
328-
comments: 'comment',
328+
comments: {
329+
type: 'comment',
330+
options: {
331+
lean: true
332+
}
333+
},
329334
}
330335
},
331336
comment: {
@@ -398,6 +403,9 @@ describe('JSONAPIMongoParser', function() {
398403
body: 1,
399404
author: 1,
400405
tag: 1
406+
},
407+
options: {
408+
lean: true
401409
}
402410
}]
403411
});

0 commit comments

Comments
 (0)