neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

Documentation for getJoin #406

Open erik-singleton opened 8 years ago

erik-singleton commented 8 years ago

I may be reading it wrong, but it wasn't immediately evident to me that the passed argument to _apply on a getJoin is an actual instance of the model in the documentation https://thinky.io/documentation/api/query/#getjoin .

The first example might be better served by using something like

Users.getJoin({
    accounts: {
        _apply: function(account) {
            return account.orderBy("sold") // Retrieve all the accounts ordered by sold
        }
    },
    company: true // Retrieve the company of the user
})

changing the terminology for the passed argument. I had a static method defined on User to strip out the password, and was trying to find a way to get a joined document with that behavior. Despite looking at the getJoin documentation a few times, I ended up going through query.js to figure out that the argument passed to _apply was an actual instance of the joined model.

Thanks for your work with thinky! I've really been enjoying it so far.

neumino commented 8 years ago

Hello @sasucker -- I'm not sure to understand what bothered you.

account here is a ReQL sequence.

When you write

Users.getJoin({
    accounts: {
        _apply: function(account) {
            return account.fn1(...).fn2(...)
        }
    },
})

It's translated as (or closed to depending the type of relation)

r.table('users').merge(function(user) {
  return {
    accounts: r.table('accounts').getAll(user('id'), {index: ...}).fn1(...).fn2(...)
  }
})

Everything happens server side. Is that what bothered you?

erik-singleton commented 8 years ago

The actual documentation has it as

Users.getJoin({
    accounts: {
        _apply: function(sequence) {
            return sequence.orderBy("sold") // Retrieve all the accounts ordered by sold
        }
    },
    company: true // Retrieve the company of the user
})

which had me believing that _apply was being applied to a set/list of accounts, not necessarily an account model. For example, I had a static method to hide the password on my User model and it was related to an Organization. My initial joins were coming back with the full User model with the password and everything. I was looking for a way to apply the static method to the joined User, but it just wasn't immediately apparent to me that I could do it in the _apply.

Organization.getJoin({ 
  owner: { _apply: owner => owner.getView() },
  users: { _apply: user => user.getView() }
}).then(function(orgs) {
  return res.json(orgs);
});

These lines in particular in the query.js file helped me realize that I could use my own method calls that were defined on the model in _apply

if ((modelToGet[key] != null) && (typeof modelToGet[key]._apply === 'function')) {
  innerQuery = modelToGet[key]._apply(innerQuery);
}
innerQuery = innerQuery.getJoin(modelToGet[key], getAll, gotModel);

I guess I wrongly assumed that I could only use normal ReQL methods in the _apply before digging for the answer.

neumino commented 8 years ago

Oh I see. Yea it works, but I have to admit I didn't really think about it when I implemented it :)

ReQL makes things awesome :)

valayDave commented 7 years ago

@neumino : Can we perform joins on the sequence from the _apply function's arguement. Because when I tried it, It didn't work. I need assistance with performing operations such as map, group and join on the sequence provided in the _apply function

neumino commented 7 years ago

Yes you can do it with _apply. I would need a bit more details than "it didn't work" to be able to help.

valayDave commented 7 years ago

@neumino :

Below is the piece of code that defines the relations with Between the RegionModel , BusinessUnitModel and BusinessUnitGoalsModel.


BusinessUnitModel.hasMany(BusinessUnitGoalsModel,'goals','id','buId');
BusinessUnitGoalsModel.belongsTo(BusinessUnitModel,'businessUnit','buId','id');

RegionModel.hasMany(BusinessUnitGoalsModel,'goals','id','regionId');
BusinessUnitGoalsModel.belongsTo(RegionModel,'regions','id','id');

Now when I perform the following

BusinessUnitModel.getJoin({goals:{
        _apply : function(sequence){
            return sequence.getJoin({region:true});
        }
    }
}).run()

I am not getting the joined documents in the sequence I am trying to join.

But when I run it like this :

BusinessUnitModel.getJoin({goals:{
        _apply : function(sequence){
            return sequence.eqJoin('regionId',r.table('region')).without({right:'id'}).zip();
        }
    }
})

It does the Join and merges the documents.

Can you please provide for more insights on sequence provided in the _apply function. If I want to join the way getJoin does for a sequence, how can it be done?