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

Commit

Permalink
Merge de57815 into e6f05e7
Browse files Browse the repository at this point in the history
  • Loading branch information
SirbyAlive committed Apr 26, 2016
2 parents e6f05e7 + de57815 commit ce7631d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
8 changes: 2 additions & 6 deletions lib/query.js
Expand Up @@ -112,9 +112,7 @@ Query.prototype.include = function (key, value) {
fields.forEach(function (key) {
self.include(key);
});
}

if (is.object(key)) {
} else if (is.object(key)) {
let fields = key;
let keys = Object.keys(fields);

Expand Down Expand Up @@ -148,9 +146,7 @@ Query.prototype.exclude = function (key, value) {
fields.forEach(function (key) {
self.exclude(key);
});
}

if (is.object(key)) {
} else if (is.object(key)) {
let fields = key;
let keys = Object.keys(fields);

Expand Down
40 changes: 36 additions & 4 deletions test/queries.js
Expand Up @@ -397,10 +397,11 @@ test('populate the response', async t => {
});
});

test('find documents and include only selected fields', async t => {
test('find documents and include only one selected field', async t => {
let post = new Post({
title: 'San Francisco',
featured: true
featured: true,
published: false
});

await post.save();
Expand All @@ -411,17 +412,48 @@ test('find documents and include only selected fields', async t => {
t.same(keys, ['_id', 'title']);
});

test('find documents and exclude selected fields', async t => {
test('find documents and include only two selected fields', async t => {
let post = new Post({
title: 'San Francisco',
featured: true
featured: true,
published: false
});

await post.save();

let posts = await Post.include(['title', 'featured']).find();
let attrs = posts[0].toJSON();
let keys = Object.keys(attrs);
t.same(keys, ['_id', 'title', 'featured']);
});

test('find documents and exclude one selected field', async t => {
let post = new Post({
title: 'San Francisco',
featured: true,
published: false
});

await post.save();

let posts = await Post.exclude('title').find();
let attrs = posts[0].toJSON();
let keys = Object.keys(attrs);
t.same(keys, ['_id', 'featured', 'published', 'created_at', 'updated_at']);
});

test('find documents and exclude two selected fields', async t => {
let post = new Post({
title: 'San Francisco',
featured: true,
published: false
});

await post.save();

let posts = await Post.exclude(['title', 'published']).find();
let attrs = posts[0].toJSON();
let keys = Object.keys(attrs);
t.same(keys, ['_id', 'featured', 'created_at', 'updated_at']);
});

Expand Down

0 comments on commit ce7631d

Please sign in to comment.