From b7d175da86ba7a92895874c1bd0a8c3debacd332 Mon Sep 17 00:00:00 2001 From: Roni Cruz Date: Sat, 6 May 2023 22:52:31 -0300 Subject: [PATCH 1/4] Field.get Method --- docs/README.md | 2 +- src/transform.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6506401..69745ae 100644 --- a/docs/README.md +++ b/docs/README.md @@ -359,7 +359,7 @@ module.exports = { ``` ### `get`: \ _(Default: `null`)_ -The `get` function is called when transforming entities. With this function, you can modify an entity value before sending it back to the caller or calculate a value from other fields of the entity in virtual fields. +The `get` function is called when transforming entities. With this function, you can modify an entity value before sending it back to the caller or calculate a value from other fields of the entity in virtual fields. If it is a String, it should be a service method name that will be called. _It can be asynchronous._ ### Callback parameters diff --git a/src/transform.js b/src/transform.js index d98d9f1..29b42bb 100644 --- a/src/transform.js +++ b/src/transform.js @@ -139,7 +139,8 @@ module.exports = function (mixinOpts) { } // Virtual or formatted field - if (_.isFunction(field.get)) { + // Syntax: ({ entity }) => `${entity.firstName} ${entity.lastName}` + if (field.get) { values = await Promise.all( values.map(async (value, i) => this._callCustomFunction(field.get, [ From b7dade793aeb4fe8698e68f5cd2529a7835ba3a3 Mon Sep 17 00:00:00 2001 From: Roni Cruz Date: Sun, 21 May 2023 19:03:39 -0300 Subject: [PATCH 2/4] Get Method Test --- test/integration/transform.test.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/test/integration/transform.test.js b/test/integration/transform.test.js index 8eb8ac0..5a5b18e 100644 --- a/test/integration/transform.test.js +++ b/test/integration/transform.test.js @@ -12,6 +12,7 @@ module.exports = (getAdapter, adapterType) => { } describe("Test transformations", () => { + const getLowerName = jest.fn(({ entity }) => (entity.name ? entity.name.toLowerCase() : entity.name)); const broker = new ServiceBroker({ logger: false }); const svc = broker.createService({ name: "users", @@ -35,6 +36,18 @@ module.exports = (getAdapter, adapterType) => { virtual: true, get: ({ entity }) => (entity.name ? entity.name.toUpperCase() : entity.name) }, + lowerName: { + type: "string", + readonly: true, + virtual: true, + get: "getLowerName" + }, + lengthName: { + type: "number", + readonly: true, + virtual: true, + get: "getNameLength" + }, password: { type: "string", hidden: true }, token: { type: "string", hidden: "byDefault" }, email: { type: "string", readPermission: "admin" }, @@ -42,6 +55,12 @@ module.exports = (getAdapter, adapterType) => { } }, + methods: { + getNameLength ({ entity }){ + return entity.name.length + } + }, + async started() { const adapter = await this.getAdapter(); @@ -76,6 +95,7 @@ module.exports = (getAdapter, adapterType) => { const res = await svc.createEntity(ctx, { name: "John Doe", upperName: "Nothing", + lowerName: "Nothing", email: "john.doe@moleculer.services", phone: "+1-555-1234", password: "johnDoe1234", @@ -87,6 +107,8 @@ module.exports = (getAdapter, adapterType) => { myID: expectedID, name: "John Doe", upperName: "JOHN DOE", + lowerName: "john doe", + lengthName: 8, email: "john.doe@moleculer.services", phone: "+1-555-1234" }); @@ -98,7 +120,9 @@ module.exports = (getAdapter, adapterType) => { expect(res).toEqual({ myID: expectedID, name: "John Doe", - upperName: "JOHN DOE" + upperName: "JOHN DOE", + lowerName: "john doe", + lengthName: 8, }); }); From 9b0fa6671a43e2d845244913bd87c47be6722c81 Mon Sep 17 00:00:00 2001 From: Roni Cruz Date: Mon, 22 May 2023 11:49:03 -0300 Subject: [PATCH 3/4] Update Get Method Test --- test/integration/transform.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/integration/transform.test.js b/test/integration/transform.test.js index 5a5b18e..de69285 100644 --- a/test/integration/transform.test.js +++ b/test/integration/transform.test.js @@ -96,6 +96,7 @@ module.exports = (getAdapter, adapterType) => { name: "John Doe", upperName: "Nothing", lowerName: "Nothing", + lengthName: "Nothing", email: "john.doe@moleculer.services", phone: "+1-555-1234", password: "johnDoe1234", From 7632a0623138e1ea4092ab4369c990643644b2ae Mon Sep 17 00:00:00 2001 From: Roni Cruz Date: Mon, 22 May 2023 12:00:20 -0300 Subject: [PATCH 4/4] Update Get Method Test using number --- test/integration/transform.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/transform.test.js b/test/integration/transform.test.js index de69285..3e94ded 100644 --- a/test/integration/transform.test.js +++ b/test/integration/transform.test.js @@ -96,7 +96,7 @@ module.exports = (getAdapter, adapterType) => { name: "John Doe", upperName: "Nothing", lowerName: "Nothing", - lengthName: "Nothing", + lengthName: 0, email: "john.doe@moleculer.services", phone: "+1-555-1234", password: "johnDoe1234",