fix: appends belongs to association (#1893)
* fix: merge stage in eager loading tree * chore: test
This commit is contained in:
		
							parent
							
								
									cb2feb304d
								
							
						
					
					
						commit
						ccdc05b30b
					
				@ -54,6 +54,7 @@ describe('repository.find', () => {
 | 
			
		||||
  let User: Collection;
 | 
			
		||||
  let Post: Collection;
 | 
			
		||||
  let Comment: Collection;
 | 
			
		||||
  let Tag: Collection;
 | 
			
		||||
 | 
			
		||||
  beforeEach(async () => {
 | 
			
		||||
    db = mockDatabase();
 | 
			
		||||
@ -70,8 +71,18 @@ describe('repository.find', () => {
 | 
			
		||||
        { type: 'string', name: 'name' },
 | 
			
		||||
        { type: 'belongsTo', name: 'user' },
 | 
			
		||||
        { type: 'hasMany', name: 'comments' },
 | 
			
		||||
        { type: 'belongsToMany', name: 'tags' },
 | 
			
		||||
      ],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    Tag = db.collection({
 | 
			
		||||
      name: 'tags',
 | 
			
		||||
      fields: [
 | 
			
		||||
        { type: 'string', name: 'name' },
 | 
			
		||||
        { type: 'belongsToMany', name: 'posts' },
 | 
			
		||||
      ],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    Comment = db.collection({
 | 
			
		||||
      name: 'comments',
 | 
			
		||||
      fields: [
 | 
			
		||||
@ -80,6 +91,10 @@ describe('repository.find', () => {
 | 
			
		||||
      ],
 | 
			
		||||
    });
 | 
			
		||||
    await db.sync();
 | 
			
		||||
 | 
			
		||||
    const tags = await Tag.repository.create({
 | 
			
		||||
      values: [{ name: 't1' }, { name: 't2' }],
 | 
			
		||||
    });
 | 
			
		||||
    await User.repository.createMany({
 | 
			
		||||
      records: [
 | 
			
		||||
        {
 | 
			
		||||
@ -88,18 +103,22 @@ describe('repository.find', () => {
 | 
			
		||||
            {
 | 
			
		||||
              name: 'post11',
 | 
			
		||||
              comments: [{ name: 'comment111' }, { name: 'comment112' }, { name: 'comment113' }],
 | 
			
		||||
              tags: [{ id: tags[0].get('id') }],
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
              name: 'post12',
 | 
			
		||||
              comments: [{ name: 'comment121' }, { name: 'comment122' }, { name: 'comment123' }],
 | 
			
		||||
              tags: [{ id: tags[1].get('id') }, { id: tags[0].get('id') }],
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
              name: 'post13',
 | 
			
		||||
              comments: [{ name: 'comment131' }, { name: 'comment132' }, { name: 'comment133' }],
 | 
			
		||||
              tags: [{ id: tags[0].get('id') }],
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
              name: 'post14',
 | 
			
		||||
              comments: [{ name: 'comment141' }, { name: 'comment142' }, { name: 'comment143' }],
 | 
			
		||||
              tags: [{ id: tags[1].get('id') }],
 | 
			
		||||
            },
 | 
			
		||||
          ],
 | 
			
		||||
        },
 | 
			
		||||
@ -109,6 +128,7 @@ describe('repository.find', () => {
 | 
			
		||||
            {
 | 
			
		||||
              name: 'post21',
 | 
			
		||||
              comments: [{ name: 'comment211' }, { name: 'comment212' }, { name: 'comment213' }],
 | 
			
		||||
              tags: [{ id: tags[0].get('id') }, { id: tags[1].get('id') }],
 | 
			
		||||
            },
 | 
			
		||||
            {
 | 
			
		||||
              name: 'post22',
 | 
			
		||||
@ -144,6 +164,16 @@ describe('repository.find', () => {
 | 
			
		||||
    await db.close();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should appends with belongs to association', async () => {
 | 
			
		||||
    const posts = await Post.repository.find({
 | 
			
		||||
      appends: ['user'],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    posts.forEach((post) => {
 | 
			
		||||
      expect(post.get('user')).toBeDefined();
 | 
			
		||||
    });
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  test('find pk with filter', async () => {
 | 
			
		||||
    const Test = db.collection({
 | 
			
		||||
      name: 'tests',
 | 
			
		||||
 | 
			
		||||
@ -224,11 +224,11 @@ export class EagerLoadingTree {
 | 
			
		||||
          const targetKey = association.targetKey;
 | 
			
		||||
 | 
			
		||||
          for (const instance of node.instances) {
 | 
			
		||||
            const parentInstance = node.parent.instances.find(
 | 
			
		||||
            const parentInstances = node.parent.instances.filter(
 | 
			
		||||
              (parentInstance) => parentInstance.get(foreignKey) == instance.get(targetKey),
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
            if (parentInstance) {
 | 
			
		||||
            for (const parentInstance of parentInstances) {
 | 
			
		||||
              parentInstance.setDataValue(association.as, instance);
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user