fordth / jinqJs

jinqJs provides a simple way to perform SQL like queries on javaScript arrays, collections and web services using LINQ expressions.
Other
93 stars 29 forks source link

aliases #22

Open tswaters opened 7 years ago

tswaters commented 7 years ago

This is a pretty cool library, looks really awesome.

I'm curious if there is any plan to support alias names? Right now data from multiple joins include the same field names, the result that is emitted for each row has everything clobbered together.

e.g.,


const data = {
  humans: [
    {id: 1, name: 'tyler'}
  ],
  dogs: [
    {dogId: 1, human: 1, name: 'sparky'},
    {dogId: 2, human: 1, name: 'spot'} 
  ]
}

new jinq()
  .from(data.humans)
  .join(data.dogs)
    .on((human, dog) => human.id === dog.human)
  .select()

// [
//   {id: 1, name: 'tyler', human: 1, dogId: 1}, 
//   {id: 1, name: 'tyler', human: 1, dogId: 2}
// ]

It would be really cool if I could do something like the following:

.select([
  {field: 'humans.name', text: 'human'},
  {field: 'dogs.name', text: 'dog'}   
])
// [
//   {human: 'tyler', dog: 'sparky'}
//   {human: 'tyler', dog: 'spot'}
// ]

Of course, if this was possible one should be able to reference these aliases in other clauses as well.

I'm not sure about the API, maybe something like this:

.from(data.humans).as('humans')
.join(data.dogs).as('dogs')

Looking through the code a bit, it wouldn't exactly be a trivial update to implement this, so feel free to close this ticket with extreme prejudice, but ... it would be pretty cool 😄

fordth commented 7 years ago

Hey, great idea! I like it! If I have some time in the future I will look into it. Feel free to branch from Master if you like add send a pull request :). In anyway I will keep it open as a note for me to possibly return back to it.

mcoakley commented 7 years ago

I was looking at this too as I had name collision in some of the joins I was doing. You can get around it today by simply taking a two-step approach...

Using your code as the example...

var dogs = new jinqJs()
  .from(data.dogs)
  .select([{
    field: human
  }, {
    field: name,
    text: dogName
  }]);
  new jinqJs()
    .from(data.humans)
    .join(dogs)
    .on((human, dog) => human.id === dog.human)
    .select()

This should get you what you want (with the added field of dog.human)