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, [ diff --git a/test/integration/transform.test.js b/test/integration/transform.test.js index 8eb8ac0..3e94ded 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,8 @@ module.exports = (getAdapter, adapterType) => { const res = await svc.createEntity(ctx, { name: "John Doe", upperName: "Nothing", + lowerName: "Nothing", + lengthName: 0, email: "john.doe@moleculer.services", phone: "+1-555-1234", password: "johnDoe1234", @@ -87,6 +108,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 +121,9 @@ module.exports = (getAdapter, adapterType) => { expect(res).toEqual({ myID: expectedID, name: "John Doe", - upperName: "JOHN DOE" + upperName: "JOHN DOE", + lowerName: "john doe", + lengthName: 8, }); });