Skip to content

Commit

Permalink
Merge pull request #54 from BigAB/add-populate-option-to-get
Browse files Browse the repository at this point in the history
Implement $populate option in params.query on get()
  • Loading branch information
marshallswain committed Feb 12, 2016
2 parents ac9b0ab + 2fe7fb4 commit 5802012
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
42 changes: 24 additions & 18 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Service {
if (filters.$populate){
query.populate(filters.$populate);
}

const executeQuery = total => {
return query.exec().then(data => {
return {
Expand All @@ -72,29 +72,35 @@ class Service {
};
});
};

if(count) {
return this.Model.where(queryParams).count().exec().then(executeQuery);
}

return executeQuery();
}

find(params) {
const paginate = !!this.paginate.default;
const result = this._find(params, paginate, query => filter(query, this.paginate));

if(!paginate) {
return result.then(page => page.data);
}

return result;
}

_get(id) {
return this
_get(id, params) {
let modelQuery = this
.Model
.findById(id)
.findById(id);

if (params && params.query && params.query.$populate) {
modelQuery = modelQuery.populate(params.query.$populate);
}

return modelQuery
.lean(this.lean)
.exec()
.then(data => {
Expand All @@ -106,23 +112,23 @@ class Service {
})
.catch(errorHandler);
}
get(id) {
return this._get(id);

get(id, params) {
return this._get(id, params);
}

_getOrFind(id, params) {
if(id === null) {
return this._find(params).then(page => page.data);
}

return this._get(id);
}

create(data) {
return this.Model.create(data).catch(errorHandler);
}

update(id, data) {
if(id === null) {
return Promise.reject('Not replacing multiple records. Did you mean `patch`?');
Expand All @@ -149,7 +155,7 @@ class Service {
patch(id, data, params) {
params.query = params.query || {};
data = Object.assign({}, data);

// If we are updating multiple records
let multi = id === null;

Expand All @@ -167,10 +173,10 @@ class Service {
.then(() => this._getOrFind(id, params))
.catch(errorHandler);
}

remove(id, params) {
const query = Object.assign({}, params.query);

if (id !== null) {
query[this.id] = id;
}
Expand Down
32 changes: 29 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Feathers Mongoose Service', () => {
new Service();
}
catch(e) {
expect(e).to.not.be.undefined;
expect(e).to.not.be.undefined;
}
});

Expand Down Expand Up @@ -122,7 +122,7 @@ describe('Feathers Mongoose Service', () => {

base(people, _ids, errors, '_id');

it('can $populate', function (done) {
it('can $populate with find', function (done) {
var params = {
query: {
name: 'Doug',
Expand All @@ -135,6 +135,19 @@ describe('Feathers Mongoose Service', () => {
done();
});
});

it('can $populate with get', function (done) {
var params = {
query: {
$populate: ['pets']
}
};

people.get(_ids.Doug, params).then(data => {
expect(data.pets[0].name).to.equal('Rufus');
done();
}).catch(done);
});
});

describe('Lean Services', () => {
Expand All @@ -161,7 +174,7 @@ describe('Feathers Mongoose Service', () => {

base(leanPeople, _ids, errors, '_id');

it('can $populate', function (done) {
it('can $populate with find', function (done) {
var params = {
query: {
name: 'Doug',
Expand All @@ -174,6 +187,19 @@ describe('Feathers Mongoose Service', () => {
done();
});
});

it('can $populate with get', function (done) {
var params = {
query: {
$populate: ['pets']
}
};

leanPeople.get(_ids.Doug, params).then(data => {
expect(data.pets[0].name).to.equal('Rufus');
done();
}).catch(done);
});
});

describe('Mongoose service ORM errors', () => {
Expand Down

0 comments on commit 5802012

Please sign in to comment.