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

aliases #22

Open
tswaters opened this issue Oct 1, 2016 · 2 comments
Open

aliases #22

tswaters opened this issue Oct 1, 2016 · 2 comments

Comments

@tswaters
Copy link

tswaters commented Oct 1, 2016

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
Copy link
Owner

fordth commented Oct 2, 2016

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
Copy link
Collaborator

mcoakley commented Oct 4, 2016

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants