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 93489da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
33 changes: 18 additions & 15 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 All @@ -381,7 +384,7 @@ class MongooseDbAdapter {
json._id = entity._id.toString();
}

if (!useNativeMongooseVirtuals) {
if (!this.useNativeMongooseVirtuals) {
this.mapVirtualsToLocalFields(ctx, json);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ if (process.versions.node.split(".")[0] < 14) {
adapter.init(broker, service);
const ctx = { service, params: {}};
const json = {_id: "xxx", foo: "yyy", bar: "zzz"};
const res = adapter.mapVirtualsToLocalFields(ctx, json);
adapter.mapVirtualsToLocalFields(ctx, json);

expect(json).toEqual({
_id: "xxx",
Expand Down

0 comments on commit 93489da

Please sign in to comment.