2

What I need is to model Member have a list of followers and followings.

R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
  • Hi. I'm not sure this is something that is possible with redux-orm.. I would go with creating "fake" `Follower` class that would inherit/extend the `Member` class, adding a `following: fk('Member', followers)` (or a many relation if you want). These two class would have a relation with 3rd `User` that would store user info. Dunno if I'm clear enough – guillaumepotier May 17 '18 at 16:05

1 Answers1

1

You define the relation the same way you'd define it for non-self-referencing many-to-many.

class Follower extends Model {
    static modelName = 'Follower';

    static fields = {
        id: attr(),
        name: attr(),
        followers: many('Follower','following')
    };
}

const orm = new ORM();
orm.register(Follower);

const session = orm.session(orm.getEmptyState());

session.Follower.create({id: 1, name: 'f1'});
session.Follower.create({id: 2, name: 'f2'});
session.Follower.create({id: 3, name: 'f3', followers: [1,2]});

// returns ['f1', 'f2']
const f3Followers = session.Follower.withId(3).followers.toRefArray().map(f=>f.name);

// returns ['f3']
const f1Following = session.Follower.withId(1)!.following.toRefArray().map(f=>f.name);