mongoose-lean-virtuals
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -0,1 +1,5 @@ | ||
0.9.0 / 2021-10-14 | ||
================== | ||
* fix: set populate virtuals to `null` or `[]` if no results based on `justOne` for Mongoose 6 Automattic/mongoose#10816 | ||
0.8.1 / 2021-09-18 | ||
@@ -2,0 +6,0 @@ ================== |
15
index.js
@@ -195,2 +195,3 @@ 'use strict'; | ||
} | ||
const virtualType = schema.virtuals[virtual]; | ||
const sp = Array.isArray(virtual) ? virtual : virtual.split('.'); | ||
@@ -202,4 +203,16 @@ let cur = doc; | ||
} | ||
cur[sp[sp.length - 1]] = schema.virtuals[virtual].applyGetters(cur[sp[sp.length - 1]], doc); | ||
let val = virtualType.applyGetters(cur[sp[sp.length - 1]], doc); | ||
if (isPopulateVirtual(virtualType) && val === undefined) { | ||
if (virtualType.options.justOne) { | ||
val = null; | ||
} else { | ||
val = []; | ||
} | ||
} | ||
cur[sp[sp.length - 1]] = val; | ||
} | ||
} | ||
function isPopulateVirtual(virtualType) { | ||
return virtualType.options && (virtualType.options.ref || virtualType.options.refPath); | ||
} |
{ | ||
"name": "mongoose-lean-virtuals", | ||
"version": "0.8.1", | ||
"version": "0.9.0", | ||
"description": "Attach virtuals to the results of mongoose queries when using `.lean()`", | ||
@@ -35,3 +35,3 @@ "main": "index.js", | ||
"mocha": "5.2.x", | ||
"mongoose": "^5.13.9" | ||
"mongoose": "6.x" | ||
}, | ||
@@ -38,0 +38,0 @@ "peerDependencies": { |
27894
189