Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement $populate option in params.query on get() #54

Merged
merged 1 commit into from
Feb 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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