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: allow array getters to return non-arrays #38

Open
wants to merge 2 commits into
base: master
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
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,15 @@ function applyGettersToDoc(schema, doc, fields, prefix) {
const pathExists = mpath.has(path, doc);
if (pathExists) {
if (schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
// A getter may return a non-array
const got = schematype.applyGetters(mpath.get(path, doc), doc, true);
const val = Array.isArray(got) ? got.map(subdoc => {
return schematype.caster.applyGetters(subdoc, doc);
}) : schematype.caster.applyGetters(got, doc);

mpath.set(
path,
schematype.applyGetters(mpath.get(path, doc), doc, true).map(subdoc => {
return schematype.caster.applyGetters(subdoc, doc);
}),
val,
doc
);
} else {
Expand Down
34 changes: 34 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,38 @@ describe('mongoose-lean-getters', function() {
assert.equal(typeof doc.field, 'string');
assert.strictEqual(doc.field, '1337');
});

it('should should allow array getters to return non-arrays', async function() {
const userSchema = new mongoose.Schema({
emails: {
type: [String],
// returns undefined, string, or array
get: (val) => !val || !val.length ? undefined : (val.length === 1 ? val[0] : val),
set: (val) => typeof val === 'string' ? [val] : val,
}
});
userSchema.plugin(mongooseLeanGetters);
const User = mongoose.model('gh-37-transform-arrays', userSchema);

const variants = [
{ sourceVal: 'foo', expectedVal: 'foo' },
{ sourceVal: ['foo'], expectedVal: 'foo' },
{ sourceVal: ['foo', 'bar'], expectedVal: ['foo', 'bar'] },
{ sourceVal: [], expectedVal: undefined },
{ sourceVal: null, expectedVal: undefined },
{ sourceVal: undefined, expectedVal: undefined },
];

await Promise.all(
variants.map(async({ sourceVal, expectedVal }) => {
const user = new User({ emails: sourceVal });
await user.save();

const foundUser = await User.findById(user._id).lean({ getters: true });
const stringified = JSON.stringify({ sourceVal, expectedVal });
assert.deepStrictEqual(user.emails, expectedVal, `user did not have expected value ${stringified}`);
assert.deepStrictEqual(foundUser.emails, expectedVal, `foundUser did not have expected value ${stringified}`);
})
);
});
});
Loading