Skip to content

Commit

Permalink
avoid using _.get() when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Freezystem committed Oct 24, 2023
1 parent 7ab6a52 commit 5b98ac4
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/moleculer-db-adapter-mongoose/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class MongooseDbAdapter {
init(broker, service) {
this.broker = broker;
this.service = service;
this.useNativeMongooseVirtuals = !!service.settings?.useNativeMongooseVirtuals

if (this.service.schema.model) {
this.model = this.service.schema.model;
Expand Down Expand Up @@ -310,29 +311,32 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
getNativeVirtualPopulateQuery(ctx) {
const fieldsToPopulate = _.get(ctx, "params.populate", []);
const fieldsToPopulate = ctx.params?.populate || [];

if (fieldsToPopulate.length === 0) return [];

const virtualFields = Object.entries(_.get(this, "model.schema.virtuals", {}))
const virtualFields = Object.entries( this.model?.schema?.virtuals || {})
.reduce((acc, [path, virtual]) => {
const hasRef = !!(_.get(virtual, "options.ref") || _.get(virtual, "options.refPath"));
const hasMatch = !!_.get(virtual, "options.match");
const hasRef = !!(virtual.options?.ref || virtual.options?.refPath);
const hasMatch = !! virtual.options?.match;
if (hasRef) acc[path] = hasMatch;
return acc;
}, {});
const virtualsToPopulate = _.intersection(fieldsToPopulate, Object.keys(virtualFields));

if (virtualsToPopulate.length === 0) return [];

const getPathOptions = (path) =>
_.get(ctx, `service.settings.virtuals.${path}.options`, {skipInvalidIds: true, lean: true});
function getPathOptions(path) {
return _.get(this, `service.settings.virtuals.${path}.options`, {skipInvalidIds: true, lean: true});
}

const getPathTransform = (path) =>
_.get(ctx, `service.settings.virtuals.${path}.transform`, (doc) => doc._id);
function getPathTransform(path) {
return _.get(this, `service.settings.virtuals.${path}.transform`, (doc) => doc._id);
}

const getPathSelect = (path) =>
_.get(ctx, `service.settings.virtuals.${path}.select`, _.get(virtualFields, path) ? undefined : "_id");
function getPathSelect(path) {
return _.get(this, `service.settings.virtuals.${path}.select`, _.get(virtualFields, path) ? undefined : "_id");
}

return virtualsToPopulate.map((path) => ({
path,
Expand All @@ -352,9 +356,9 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
mapVirtualsToLocalFields(ctx, json) {
Object.entries(_.get(this, "model.schema.virtuals", {}))
Object.entries(this.model?.schema?.virtuals || {})
.forEach(([path, virtual]) => {
const localField = _.get(virtual, "options.localField");
const localField = virtual.options?.localField;
if (localField) json[path] = json[localField];
});
}
Expand All @@ -368,8 +372,7 @@ class MongooseDbAdapter {
* @memberof MongooseDbAdapter
*/
entityToObject(entity, ctx) {
const useNativeMongooseVirtuals = _.get(ctx, "service.settings.useNativeMongooseVirtuals", false);
const populate = useNativeMongooseVirtuals ? this.getNativeVirtualPopulateQuery(ctx) : [];
const populate = this.useNativeMongooseVirtuals ? this.getNativeVirtualPopulateQuery(ctx) : [];

return Promise.resolve(populate.length > 0 ? entity.populate(populate) : entity)
.then(entity => {
Expand Down

0 comments on commit 5b98ac4

Please sign in to comment.