Skip to content

Commit

Permalink
Merge branch 'master' of github.com:moleculerjs/moleculer-db
Browse files Browse the repository at this point in the history
  • Loading branch information
icebob committed Dec 17, 2022
2 parents 9a33b27 + 1405c0e commit c2c6149
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
36 changes: 18 additions & 18 deletions packages/moleculer-db/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,41 +566,41 @@ module.exports = {
/**
* Authorize the required field list. Remove fields which is not exist in the `this.settings.fields`
*
* @param {Array} fields
* @param {Array} askedFields
* @returns {Array}
*/
authorizeFields(fields) {
authorizeFields(askedFields) {
if (this.settings.fields && this.settings.fields.length > 0) {
let res = [];
if (Array.isArray(fields) && fields.length > 0) {
fields.forEach(f => {
if (this.settings.fields.indexOf(f) !== -1) {
res.push(f);
let allowedFields = [];
if (Array.isArray(askedFields) && askedFields.length > 0) {
askedFields.forEach(askedField => {
if (this.settings.fields.indexOf(askedField) !== -1) {
allowedFields.push(askedField);
return;
}

if (f.indexOf(".") !== -1) {
let parts = f.split(".");
if (askedField.indexOf(".") !== -1) {
let parts = askedField.split(".");
while (parts.length > 1) {
parts.pop();
if (this.settings.fields.indexOf(parts.join(".")) !== -1) {
res.push(f);
break;
allowedFields.push(askedField);
return;
}
}
}

let nestedFields = this.settings.fields.filter(prop => prop.indexOf(f + ".") !== -1);
let nestedFields = this.settings.fields.filter(settingField => settingField.startsWith(askedField + "."));
if (nestedFields.length > 0) {
res = res.concat(nestedFields);
allowedFields = allowedFields.concat(nestedFields);
}
});
//return _.intersection(f, this.settings.fields);
}
return res;
return allowedFields;
}

return fields;
return askedFields;
},

/**
Expand Down Expand Up @@ -832,10 +832,10 @@ module.exports = {
return this.beforeEntityChange("create", entity, ctx)
.then((entity)=>this.validateEntity(entity))
// Apply idField
.then(entity =>
.then(entity =>
this.adapter.beforeSaveTransformID(entity, this.settings.idField)
)
.then(entity =>
.then(entity =>
this.adapter.insert(entity)
)
.then(doc => this.transformDocuments(ctx, {}, doc))
Expand Down Expand Up @@ -939,7 +939,7 @@ module.exports = {
*/
_update(ctx, params) {
let id;

return Promise.resolve()
.then(()=>this.beforeEntityChange("update", params, ctx))
.then((params)=>{
Expand Down
7 changes: 6 additions & 1 deletion packages/moleculer-db/test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ describe("Test authorizeFields method", () => {
name: "store",
adapter: mockAdapter,
settings: {
fields: ["id", "name", "address", "bio.body"]
fields: ["id", "name", "address", "bio.body", "mobile.carrier.name"]
}
});

Expand All @@ -654,6 +654,11 @@ describe("Test authorizeFields method", () => {
const res = service.authorizeFields(["id", "name", "bio.body.height", "bio.male", "bio.dob.year", "bio.body.hair.color"]);
expect(res).toEqual(["id", "name", "bio.body.height", "bio.body.hair.color"]);
});

it("should return empty", () => {
const res = service.authorizeFields(["carrier"]);
expect(res).toEqual([]);
});
});

describe("Test with enabled nested fields", () => {
Expand Down

0 comments on commit c2c6149

Please sign in to comment.