stevendesu / jsindex

1 stars 0 forks source link

Ability to rename columns #5

Closed stevendesu closed 5 years ago

stevendesu commented 5 years ago

Another issue I recently ran into. Suppose I have the following two collections:

traffic:
+----+------+----+---------+------+
| id | from | to | account | mbps |
+----+------+----+---------+------+
|  1 |    1 |  2 |       1 |   30 |
|  2 |    1 |  2 |       2 |   10 |
|  3 |    2 |  1 |       1 |   20 |
|  4 |    2 |  1 |       2 |   50 |
...

datacenters:
+----+------+-----+-----+
| id | name | lat | lng |
+----+------+-----+-----+
|  1 |  abc |  42 | -75 |
|  2 |  xyz |  34 | -118|
+----+------+-----+-----+

These get dumped from the database to CSV, then imported as JavaScript collections, then indexed:

const traffic = Array.load(...).index();
const datacenters = Array.load(...).index();

Now let's say I want to draw lines on a map for the traffic traveling between data centers. This means I need to know the "from_latitude", "from_longitude" for a traffic flow as well as the "to_latitude", "to_longitude"

Currently if I were to try and merge these, I might do something like the following:

traffic.merge(datacenters, {joinOn: {left: "from", right: "id"}});
traffic.merge(datacenters, {joinOn: {left: "to", right: "id"}});

This leads to a collision as the lat and lng columns are added to the traffic collection twice. Now I already have some semblance of a solution for this buried in the merge method. When a duplicate column name exists, _2 is appended (or _3, or _4,... I keep trying until I find a unique key)

So this means my final collection would look like so:

[
    {id: 1, from: 1, to: 2, account: 1, mbps: 30, name: "abc", lat: 42, lng: -75, name_2: "xyz", lat_2: 34, lng_2: -118},
    {id: 2, from: 1, to: 2, account: 2, mbps: 10, name: "abc", lat: 42, lng: -75, name_2: "xyz", lat_2: 34, lng_2: -118},
    ...
]

While this... works... it's not exactly pretty, nor easy to understand. It'd be better if I could rename the keys like so:

traffic.merge(datacenters, {joinOn: {left: "from", right: "id"}});
traffic.rename({"lat": "fromLat", "lng": "fromLng", "name": "fromName"});
traffic.merge(datacenters, {joinOn: {left: "to", right: "id"}});
traffic.rename({"lat": "toLat", "lng": "toLng", "name": "toName"});

As this is a new feature and not a change to the existing API, this would bump the version to 0.1.1 or similar