Coder Social home page Coder Social logo

relations about openrecord HOT 3 CLOSED

SampsonCrowley avatar SampsonCrowley commented on May 26, 2024
relations

from openrecord.

Comments (3)

PhilWaldmann avatar PhilWaldmann commented on May 26, 2024

I'm currently working on getting the docs complete.

You could do the following:

Model.join('athlete') //for a left join - or use .leftJoin(), rightJoin(), .innerJoin() or .outerJoin()

if you just want to prefetch relational data, use .include().

The arguments for .join() and .include() could be a:

  • String (relation name)
  • Array with relation names
  • Object where the key is the relation name. The value could by anything the .include() method accepts (String, Array, Object) - sub relations!

See includes-test.js

from openrecord.

SampsonCrowley avatar SampsonCrowley commented on May 26, 2024

so there's no method to fetch an association after the parent is loaded?

e.g. the example above model is 'Account' in the following (obviously unnecessary as is, but shows more of what i'm looking at) example:

Account.get(MY_ID).exec().then((account) => account.athlete.load())

or

Account.get(MY_ID).exec().then((account) => account.relations.athlete.load())

from openrecord.

PhilWaldmann avatar PhilWaldmann commented on May 26, 2024

With a has_many relation, you could do the following:

Account.get(MY_ID).exec()
.then((account) => account.athletes.exec())
.then(athletes => console.log(athletes))

This currently only works with has_many and belongsToMany, because the relation object (empty array) has all conditions at hand. It's a preconfigured query like Athlete.where({account_id: 1234}).
belongs_to relations return null for a record that isn't prefetched.

I've some ideas to solve this problem. But this is a breaking change, so another thing to address in v2

from openrecord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.