Giter Club home page Giter Club logo

Comments (3)

Nedomas avatar Nedomas commented on August 11, 2024

Thanks for asking. I've been thinking a lot on how should we deal with associations, as it is quite common in my production code.

But it is not clear what is the best way to do that. I was thinking of doing something like this:

User.find(1).then(function(user) {
  // some extended array [{ id: 3, user_id: 1, title: 'Hello' }]
  print(user.posts);

  user.posts.create({ title: 'Yo' }).then(function(post) {
    // { user_id: 1, title: 'Yo' }
    print(post);
  });
});

But then there are moments of:
1 - Should we always preload all the associations or have then on demand

user.posts().then(...

2 - How about creating model and giving the association as a property?

Post.create({ user: the_user, title: 'Hello' })

3 - How do you specify the association endpoints on the API? Or should it just load the associations on models by calling .posts on them?

// now we do
var User = new Databound('users');
var Post = new Databound('posts');

// then should we specify the associations alongside
var Post = new Databound('posts');
var User = new Databound('users', post: Post);

// or have it auto-discover the endpoints by sending one request on load 
// or having the first request to the backend be a bit slower than usual

Got any ideas on how to deal with those issues? I would love to work together and implement that into our next release.

Thanks ❤️ ❤️ ❤️

from databound.

Nedomas avatar Nedomas commented on August 11, 2024

Just thought a bit more about that and realised that I am already doing this in the production app I work on.

You can override the as_jsonmethod on the parent model. Something like that:

# in user.rb
def as_json(*)
  super.merge(posts: posts.to_json)
end

# in JS
User.find(1).then(function(user) {
  // [{ id: 15... }]
  print(user.posts)
});

But a much better way is to use Active Model Serializers to do the serialisation and embed the associations there.

The questions now really are:

1 - Should this library start managing serialisation (viewable_columns)? What about the security aspect of it?

2 - Should we provide an API for associations? (think user.posts().then)? Where do you specify the relation permissions then? Just an idea below:

class UsersController < ApplicationController
  databound do
    model :user

    has_many :posts do
      model :post
    end
  end
end

Lets try to come up with a simple solution and implement it only then. Lets not build a monster.

from databound.

drewda avatar drewda commented on August 11, 2024

This sounds great. Yes, probably best to avoid "monsters" like Ember-data or Backbone-relational.

Re your specific questions:

  1. When customization of viewable columns and other more detailed aspects is necessary, I think it's reasonable to have developers create an AMS serializer for the time being.
  2. Looks good. Good that that will support situations where the association has a name different than the model.

from databound.

Related Issues (5)

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.