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

fix: include using parent fields projection #649

Open
wants to merge 1 commit into
base: 5.x
Choose a base branch
from
Open
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
28 changes: 15 additions & 13 deletions lib/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1386,29 +1386,31 @@ MongoDB.prototype.all = function all(modelName, filter, options, callback) {
if (filter.where) {
query = self.buildWhere(modelName, filter.where, options);
}
// Use Object.assign to avoid change filter.fields
// which will cause error when create model from data
let fields = undefined;
if (typeof filter.fields !== 'undefined') {
fields = [];
Object.assign(fields, filter.fields);
}

// Convert custom column names
fields = self.fromPropertyToDatabaseNames(modelName, fields);

options = buildOptions({}, options);

if (fields) {
options.projection = fieldsArrayToObj(fields);
}
this.execute(modelName, 'find', query, options, processResponse);

function processResponse(err, cursor) {
if (err) {
return callback(err);
}

// Use Object.assign to avoid change filter.fields
// which will cause error when create model from data
let fields = undefined;
if (typeof filter.fields !== 'undefined') {
fields = [];
Object.assign(fields, filter.fields);
}

// Convert custom column names
fields = self.fromPropertyToDatabaseNames(modelName, fields);

if (fields) {
cursor.project(fieldsArrayToObj(fields));
}

const collation = options && options.collation;
if (collation) {
cursor.collation(collation);
Expand Down
30 changes: 30 additions & 0 deletions test/mongodb.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2379,6 +2379,36 @@ describe('mongodb connector', function() {
});
});

it('all return should honor filter.fields and not apply them to included relations', function(done) {
const user = new User({name: 'Matt'});
user.save(function(err, user) {
const post = new Post({title: 'b', content: 'BBB', userId: user.id});
post.save(function(err, post) {
db.connector.all(
'Post',
{fields: ['title', 'userId'], where: {title: 'b'}, include: 'user'},
{},
function(err, posts) {
should.not.exist(err);
posts.should.have.lengthOf(1);
post = posts[0];
post.should.have.property('title', 'b');
should.not.exist(post.content);
should.not.exist(post._id);
should.not.exist(post.id);
post.should.have.property('userId');
post.should.have.property('user');
const {user} = post;
user.should.have.property('id');
user.should.have.property('name', 'Matt');

done();
},
);
});
});
});

it('create should convert id from ObjectID to string', function(done) {
const oid = new db.ObjectID();
const sid = oid.toString();
Expand Down