Wikodit / js-data-jsonapi-light

JSData adapter which serialize and deserialize JSONApi response and requests.
MIT License
8 stars 4 forks source link

ManyToMany #5

Open Tronix117 opened 7 years ago

Tronix117 commented 7 years ago

ManyToMany needs to be implemented.

Proposed Implementation

User = store.defineMapper('User',{
  endpoint: 'users',
  relations: {
    hasMany: {
      'UserFavorite': {
        localField: 'favorites',
        foreignKey: 'articleId'
      }
    }
  }
});

Article  = store.defineMapper('Article', {
  endpoint: 'articles',
  relations: {
    belongsTo: {
      'User': {
        localField: 'author',
        localKey: 'authorId'
      }
    }
  }
});

UserFavorite = store.defineMapper('UserFavorite', {
  manyToMany: ['User', 'Article']
  relations: {
    belongsTo: {
      'Article': {
        localField: 'article',
        localKey: 'articleId'
      },
      'User': {
        localField: 'user',
        localKey: 'userId'
      }
    }
  }
})
quen09t commented 7 years ago

Class side :

export const Class = store.defineMapper('Class', {
  schema: ClassSchema,
  endpoint: 'classes',
  relations: {
    hasMany: {
      'AppUser': {
        localKeys: 'userIds',
        localField: 'users'
      }
    }
  }
})

User side :

export const AppUser = store.defineMapper('AppUser', {
  schema: AppUserSchema,
  endpoint: 'users',
  relations: {
    hasMany: {
      'Class': {
        foreignKeys: 'userIds',
        localField: 'classes'
      }
    }
  }
})