-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add possibility to parse/convert property values #8
Comments
@VividVisions const resultSet = [
{ id: 1, json: '{"a": 1}', ... } // possible when using mysql json type
]; and you would want to parse the JSON string to a JSON object using JSON.parse const resultMap = [{
mapId: 'jsonMap',
idProperty: 'id',
properties: [
{
name: 'json',
column: 'json',
parse(value) {
try {
return JSON.parse(value);
} catch (error) {
return value;
}
}
},
...
]
}]; Instead of parsing the list of properties you can pinpoint what property needs to be parsed. @nareshbhatia What do you think about this option? |
Exactly. One of my use cases is converting binary hashes (DB side) to hex representations (API/UI side) and back. Another one is creating "virtual" properties out of a couple of DB-stored ones. Simplified example would be: parse: properties => {
properties.fullName = `${properties.firstName} ${properties.lastName}`;
return properties;
} While your suggestion would be great for the first use case, the second one would not be feasible without traversing the mapped result another time, which I wanted to prevent in the first place. In my case it's also a lot easier to use just one |
@VividVisions |
Good discussion, @VividVisions and @FerrielMelarpis. I agree that Walter's suggestion would be more flexible. Only suggestion from my side is to call this ResultMap property |
First of, thanks for this library, it's great. I thought that I would also need such a transform method to handle the conversion between snake_case and camelCase that differs between my db and app namings. What if I want to hash the value of "id" values returned from the db? Is there an existing way to do this during joinjs.map() or should I do it as an extra step on each object in the resulting mapped collection? |
@Mikou solving this issue might solve your problem after all since basically, transform: props => {
props.hash = hash(props.id);
return props;
} I'll take a look on this when I got time. |
Hi @FerrielMelarpis, Thanks for your answer. You are right, injecting a new field would be a better idea than overriding the id. My use case concerning hashing the id, is simply to "hide" the real database id (that is an auto-increment integer) behind a hashed value using the hashids library. Maybe this is overkilled but what about adding 2 functions to resultMap instead of just one: transform and extend.
This would allow to succinctly describe transformation rules such as db's snake_case to app's camelCase naming convention for all fields like so:
The I have tried to illustrate precisely this in a fork I have created: NB: Currently, without the "transform" function, I solve camelCase/snakeCase problem like so:
|
I needed a way to convert some properties, after fetching them from the DB. And since I wanted to prevent traversing the whole result set (before JoinJS) or object tree (after JoinJS) again, I thought it'd be great, if JoinJS could handle this in one go. So, I played around with something like this in
injectResultInObject
:The reason I don't pass the whole
mappedObject
to theparse
function is, that this way, the ID property and any other properties or functions themappedObject
could have whencreateNew
is used are omitted and the chances of completely messing things up during parsing/converting are lower. ;)What do you think? Would this be something you see within the scope of JoinJS?
The text was updated successfully, but these errors were encountered: