Hey there guys
I am trying to figure out what is the best way to create a “join” dataset
Generally speaking I have two datasets - one is reflecting a players table - for simplicity it has only 2 fields name and country, and a second data set “games” which needs to holds 2 players and a result of the game
My final aim is to represent this in a tournament chart like this

now there are 2 main questions here:
- How should I “join” the players database and the games data base - There isn’t a real “logic key” in the wix code system apart from the hidden “id” column - Should I use it? i.e. - should the automatically generated id on the players table could be inserted into the “game” database as player no1, or player no 2 (alternatively I can manage my own “player id” - but this is much less elegant - because there is not prevention of somebody entering the DB 2 players with the same player id. and I’ll need to build a code to prevent this
- More complicated - lets assume I have 4 games in a first round of a tournament ,and I have created 4 games in my “games” database (or collection to use your terminology)
on the presentation I want to create 4 games exactly as below but instead a link - I want to have buttons (the aim is that a site user will try to predict the winner of the game by clicking the button)
I guess that a dynamic button creation (i.e. - querying the DB - understanding how much games are there and then create 2 buttons for each one ) is not wix style (I might be wrong here)
Lets assume I have created 8 buttons for the 4 games - what is the best path to bind the players data into them?
Naturally I don’t want the code the query for each button the players data with player id.
The 2 “wix - native” structure - of either creating an item page or creating a gallery does not apply here ( I want all 4 games one after another (or maybe I am wrong and gallery is a possible path))
Should I write a module that retrieves a duo - player id, player name and than bind them to the buttons somehow?
Once again - feel free to say that you don’t have the time - and I will seek help in the forums…
1 Like
Hi Kobi,
first of all, take a look at our “coming soon” page - https://www.wix.com/code/home/coming-soon.
it has this exact feature planned (the ability to define references between collections).
- for now, you can implement it yourself using the _id field - that’s the proper way to do it.
- first of all, soon we’ll provide the “repeater” functionality where you can design a “slide” and then repeat it based on data. the slide will be able to host any component, so that way you can repeat buttons (and any other thing) easily based on data.
for now, assuming you know it’s 8 buttons and 4 games, it’s easy to implement what you need.
-
I assume you can create a wix-data query to get the player info for the tournament (basically a query that retrieves the 8 players, in the order of their matches).
-
I also assume your buttons are named button1…button8.
-
each of your buttons should register an onClick() handler (use the same handler for all 8).
-
in that handler, use the ‘event’ argument to get the button that was clicked.
-
from that, get the button number.
-
using the number, you can get the relevant data of the player by looking into the proper index in the wix-data result set you got earlier
hope this helps, and don’t hesitate to ask for more info.
Thanks Ziv - this is great
As for the first question - I tried copy the _id automatically created in the players table and paste it to the “player 1” in the games database (set as string) but it did not paste - is there anything I miss?
2. I did start implementing exactly your suggestion (buttons 1- 8) the problem is that at least for the first round (4 games - each holding 2 players) all buttons should be populated when the page loads - this is forcing me to write 8 queries to the dataset on the page load, which makes it (I guess) extremely slow.
Looking forward to see the repeater functionality when it arrives - as it seems the right solution. - where can I see when new features are launched?
Thanks so much again
Kobi
regarding the paste thing - works for me. you need to double click the cell in order to paste into it.
regarding the 2nd item - not sure why you need 8 queries.
assuming you know the player IDs you need, you can create a single query to retrieve them by OR-ing several queries into one.
see here:
hope this makes sense…
p.s.
you don’t have to use a dataset at all - dataset’s are simply a mechanism on top of wix data to bind data to components.
what I suggested above is to use wix data directly, and then set the data to the relevant components using each component’s API.
Thanks - the pasting seem to work fine
Regarding the or possibility it does sound reasonable - however - I will need to be able to scroll across the items array - I guess I can build a regular JS loop?
I wanted to use a dataset as eventually I want a user to pick a winner in each game and this will update the games table (I guess this is also possible when writing directly to the DB)
another quick question which I struggle with
As the querying of the players DB is repeated - I wanted to build a separate function for it which does something like this:
however it seems that the fullName is pulled correctly (I used a console log to see it) when the “then” scope ends the fullName is back to null and not returned from the function, do you have any idea how I can keep a var from a promise and send it back from a function?
Thanks so much for your help!
OK - the direct approach (non dataset mediated ) is probably the right path - although the main problem still persist (and will have to wait to a join/repeater functionality)
As the number of games in reality would be unknown (or at least unstable) and this means that I need to add multiple “or” phrases which needs to be build dynamically
(BTW - if there was a “in” functionality (instead of or) I could dynamically build relatively easy a string that contains all the required values - however I could find such)
Once again if the select for the “joined” table could have been separated into its own function - it would be less cumbersome (I would loop over the number of games and call the players table each time) - but as the value coming back from this function is disappearing (due to its promise character? - see previous question ) It is also ruled out
Kobi
Hi Kobi,
Regarding extracting the player name to a separate function, since getting the name is an asynchronous action (you set a filter that causes the dataset to go to the collection to get the data), the function return value must also be something asynchronous - in our case, a promise. Then, when using this function, you will get back a promise that will be resolved to the player name.
Here’s the code (notice how I return the setFilter promise, and how I call the function in the last line):

You can read more about promises online, for example here:
Thanks for this Tomer - first time for me with promises - so trying to find my way
Tried this and something bizarre happened (maybe due to the nature of promises) - I have queried my games table and while going over the retrieved items I call the above function - I know that the players no I pass to the function is correct since I inserted a log call
The log first shows all the logs called from the function (which indicates a correct pass of the playerNo parameter,
I also included a log to see what comes back from the function in the calling code, and it seems that always to same player is retrieved by the function…
Any reason for that?

Can you upload an image including the console log line for the player name?
I did the upper lines (trying to retrieve player no x ) is from the function itself
The buttom 6 ( vesely, Jiri * 6) are a log of the returned value (if this is the right terminology in promises…)
See the call below
I think what happens here is that you keep changing the filter without letting the dataset finish getting the results.
Meaning you set the filter to player 6, then immediately to player 5, then immediately to player 4 and so on.
So the dataset doesn’t “get a chance” to get the results for everything, only for the last request.
This is due to how the dataset works internally.
Do you need to change the filter in such way or was it only for testing?
If you do need to change it, you can always wait for the current filter to finish and then run the next filter (meaning calling the getPlayerName function inside the “then” callback of the previous one).
Regardless, we will think about this issue and consider ways to fix this behavior or make it clearer.
Thanks!
Thanks Tomer - I do need the changed filters (once again - I am looping through games each holding 2 different players id’s - which I than want to go to the players DB to get the name.
The reason that I do not put the next() of the dataset in the “then” part of the getPlayerNo is that I already hold all items in one query via the wix data query - I am using a for loop to iterate through the different games
I guess what I really need is that the for loop will continue only when the promise of getPlayerName will be fulfilled (I even tried removing the i++ from the loop and put it in the “then” block - but this has seemed to create an endless loop
Any ideas how to solve it? any idea of how to run such a “suspended” loop? or maybe I can get a the results from the actual_games in a different way that will allow me to call a next function when the promise is fulfilled?
Thanks so much
Kobi
Hi Koby,
how about a different approach?
instead of calling getPlayerName(id) multiple times, why not do a getPlayerNames([id1, id2, …])?
in the implementation you can create a wixdata query using the OR operator to get all names at once.
the result set can be an array of {ID, NAME} records which you can then match to the games data you already got.
should be more performant as well 
I did try this - but bumped into a problem as the number of players is not predefined (some tournaments have 32 players, some 64 and some even 48 or 56 (as the top seed players are exempted from the first round)
Do you see an a possibility of 64 or’s ?
I tried to build a dynamic “or” something like
.setfiler .eq(“playerNo”,6)
if(another required player)
.or (eq.(“playerNo”,7)
but I guess an if between the statements of the filter is not accepted
BTW - if there was an “in” functionality like in SQL - I could easily build a string holding all required players numbers and than use the “in” functionality
Something like
stRequiredPlayers = “6,13,3,4,6,15”
setfilter().in(stRequiredPlayers)
but as far as the documentation shows there isn’t such functionality available
Am I missing something?
Kobi
try this:
//create the basic query, don't run it yet:
let query = wixData.query('players').eq('_id', playerIDs[0]);
//now add more queries to it using OR:
for (let i=1; i < playerIDs.length; i++)
{
query = query.or(wixData.query('players').eq('_id', playerIDs[i]));
}
//now run the query and get results
query.find().then(res =>
{
/*do something with res.items*/
});
make sense?
It worked!!!
Thanks so much Ziv & Tomer 
cheers 
now share a link to a working site…