Closed servizig closed 10 years ago
Hey @hjarvard, that seems like a pretty fringe case - why exactly would you want to do this?
I'd definitely recommend holding off on big modifications to the code just yet as I'm back in the states and will be working to release the next version which is basically a complete rewrite (much more flexibility) - would hate for your code to be based on old code! I'll explain how the new mapping process works though, and maybe add a proper Wiki for those that want to dive off the deepend modifying it for their own cases.
Hi @kwhitley! I'm using your great library for transforming plain row data that comes from sql query into tree-like complex objects using foreign keys. And as result I wanted to get objects that don't look like they were retrieved from database.
For example, I have table1 w/ columns id, name, fkId and table2 w/ columns id, title in database. fkId references on id in table2. I am using sql query builder, so I don't have to write select statements by hand. Also, I created a mapping file in javascript with definition of what column should be mapped to which property. It looks like this:
table1:
{ columns: [ { name: 'id', property: 'id' }, { name: 'name', property: 'name' }, { name: 'fkId', property: 'fk' } ] }
table2:
{ columns: [ { name: 'id', property: 'id' }, { name: 'title', property: 'title' } }
This gives me the ability to change underlying column names in database without affecting code in javascript. Next, I use query builder that takes my mapping file and creates an sql query to fetch data from both tables. So, I can get an array of objects with properties id, name, fk, fk:id, fk:title
where properties id, name and fk are from table1 and properties fk.* are from table2.
Then I grow
my array and transform it into array of objects like {id: .., name: .., fk: { id: .., title: .. }}
. But property fk
is already defined, so treeize doesn't touch it.
As a workaround, I updated my table1 mapping to this:
{ columns: [ { name: 'id', property: 'id' }, { name: 'name', property: 'name' }, { name: 'fkId', property: 'fkId' } ] }
So query builder now generates query that returns id, name, fkId, fk:id, fk:title
and treeize.grow
works just fine. The only difference is that result object contains extra property fkId:
{ id: .., name: .., fkId: .., fk: { id: .., title: .. } }
So far, this wasn't an issue.
If you think my issue doesn't fit common logic, please feel free to close it. :)
Sort of makes sense... mind sharing which query building module you're using (NPM I assume) so I can look up its documentation?
I'm curious if you need to pull that fk column on its own at all - assuming you're using the query builder to create the final join and running a single query of results through treeize...
Let's see that documentation and I'll take a look :+1:
Glad to get some feedback though, and you're using it exactly for the problem it was designed to address (flat data from SQL -> object graph). The new version I'm working is far more flexible, but I don't think it would [yet] address the problem you're facing...
I use node-sql: https://github.com/brianc/node-sql
Sure, I could select only those fields I need, like .select(table1.id, table1.name, ...)
, but it is more convenient to select all columns: .select(table1.star(), table2.as('fk').star())
.
@hjarvard This has just been addressed in the upcoming version! :+1:
There will be an objectOverwrite
option that will allow what you specify... basically the replacement of existing simple attribute values with incoming objects.
So processing the following two entries (either from one data set or later, from another):
[
{ 'name': 'mike', 'pet': 1 },
{ 'name': 'mike', 'pet:name': 'fido', 'pet:age': 11 }
]
Would result in this after the first row is inserted:
[{ name: 'mike', pet: 1 }]
And this after the second one is added:
[{ name: 'mike', pet: { name: 'fido', age: 11 } }]
That what you're looking for?
Hang in there - as soon as I write up the tests and documentation for this release I think it's about ready to roll out...
Yay! That's exactly what I need! :+1: P.S. Sorry for late response, I've been busy at work.
@hjarvard I'm going to close this for now as it's been addressed on the upcoming feature branch in d997f66f4e38674b55f8f381f7cb6ca0de11d2f9, but I'll ping you when this tagged v2.0.0 release is ready to roll out shortly. It is a complete rewrite with new API, implementation, and a myriad of new capabilities (your request included).
Great, thanks!
@hjarvard This should all be addressed in v2.0.0+
Documentation Treeize.js
Give the new version a try and let me know how it works??
Perfect! I will definitely upgrade in our next product release. Thanks, mate! P.S. The docs became very decent as well.
Currently, if you pass object
then you will get
is it possible to implement overwrite plain properties with objects?
Maybe with config option or something. I checked code, but I'm not sure if I can change it without breaking anything.