Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

localField is not updated after localKeys change for many-to-many relations #533

Open
porlov opened this issue Dec 30, 2018 · 2 comments
Open
Labels

Comments

@porlov
Copy link

porlov commented Dec 30, 2018

Description

For many-to-many relations, which use localKeys, after changing localKeys via record.set localField is not updated. But for relations, which use foreignKey, all works correctly after changing foreignKey.

Environment

  • js-data version: 3.0.5
  • browser version: Google Chrome 69.0
  • operating system: Ubuntu 16.04 LTS

Steps to reproduce

store.defineMapper('user', {
  recordClass: class UserRecord extends JSData.Record {},
  schema: {
    properties: {
      id: {
        type: 'string'
      },
      username: {
        type: 'string'
      },
      post_ids: {
        type: 'array',
        items: 'string'
      }
    }
  },
  relations: {
    hasMany: {
      post: {
        localKeys: 'post_ids',
        localField: 'posts'
      }
    }
  }
});

store.defineMapper('post', {
  recordClass: class PostRecord extends JSData.Record {},
  schema: {
    properties: {
      id: {
        type: 'string'
      },
      title: {
        type: 'string'
      }
    }
  },
  relations: {
    hasMany: {
      user: {
        foreignKeys: 'post_ids',
        localField: 'users'
      }
    }
  }
});

describe('record.set localKeys', () => {
  let posts, user;

  beforeEach(async() => {
    posts = await store.createMany('post', [
      {id: '1', title: 'blog1'},
      {id: '2', title: 'blog2'},
      {id: '3', title: 'blog3'}
    ]);
    user = await store.create('user',
      {id: '1', username: 'user1', post_ids: ['1', '2']}
    );
  });

  afterEach(async() => {
    await store.destroyAll('user');
    await store.destroyAll('post');
  });

  it('created correctly', () => {
    expect(user.post_ids).toEqual(['1', '2']);
    expect(user.posts.length).toEqual(2);
    expect(user.posts[0].toJSON()).toEqual({id: '1', title: 'blog1'});
    expect(user.posts[1].toJSON()).toEqual({id: '2', title: 'blog2'});
  });

  it('should change localField', () => {
    user.set('post_ids', ['3']);

    expect(user.post_ids).toEqual([3]);
    expect(user.posts.length).toEqual(1); // fails
    expect(user.posts[0].toJSON())
      .toEqual({id: '3', title: 'blog3'}); // fails
  });
});

I expect that localField should be updated automatically. Am I doing something wrong?

Test example for foreignKey:

Test example for localKeys

@porlov porlov changed the title localField is not updated after localKeys change localField is not updated after localKeys change for many-to-many relations Dec 30, 2018
@mikeu
Copy link
Contributor

mikeu commented May 15, 2019

@porlov did you find a solution or workaround for this? I've just come up against the same issue.

@porlov
Copy link
Author

porlov commented May 16, 2019

@mikeu I have not investigated yet how to fix the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants