Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion about Field.get Method #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ module.exports = {
```

### `get`: \<Function\> _(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
Expand Down
3 changes: 2 additions & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down
27 changes: 26 additions & 1 deletion test/integration/transform.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,13 +36,31 @@ 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" },
phone: { type: "string", permission: "admin" }
}
},

methods: {
getNameLength ({ entity }){
return entity.name.length
}
},

async started() {
const adapter = await this.getAdapter();

Expand Down Expand Up @@ -76,6 +95,8 @@ module.exports = (getAdapter, adapterType) => {
const res = await svc.createEntity(ctx, {
name: "John Doe",
upperName: "Nothing",
lowerName: "Nothing",
lengthName: 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are readonly fields, why do you send them in create?

email: "[email protected]",
phone: "+1-555-1234",
password: "johnDoe1234",
Expand All @@ -87,6 +108,8 @@ module.exports = (getAdapter, adapterType) => {
myID: expectedID,
name: "John Doe",
upperName: "JOHN DOE",
lowerName: "john doe",
lengthName: 8,
email: "[email protected]",
phone: "+1-555-1234"
});
Expand All @@ -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,
});
});

Expand Down