fix: toJSON with null association (#260)
This commit is contained in:
		
							parent
							
								
									afa807951a
								
							
						
					
					
						commit
						036baaa443
					
				
							
								
								
									
										60
									
								
								packages/database/src/__tests__/repository/update.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								packages/database/src/__tests__/repository/update.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,60 @@
 | 
			
		||||
import { Collection, Database, mockDatabase } from '@nocobase/database';
 | 
			
		||||
 | 
			
		||||
describe('update', () => {
 | 
			
		||||
  let db: Database;
 | 
			
		||||
  let User: Collection;
 | 
			
		||||
  let Tag: Collection;
 | 
			
		||||
  afterEach(async () => {
 | 
			
		||||
    await db.close();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  beforeEach(async () => {
 | 
			
		||||
    db = mockDatabase();
 | 
			
		||||
    User = db.collection({
 | 
			
		||||
      name: 'posts',
 | 
			
		||||
      fields: [
 | 
			
		||||
        { type: 'string', name: 'title' },
 | 
			
		||||
        {
 | 
			
		||||
          type: 'belongsToMany',
 | 
			
		||||
          name: 'tags',
 | 
			
		||||
        },
 | 
			
		||||
      ],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    Tag = db.collection({
 | 
			
		||||
      name: 'tags',
 | 
			
		||||
      fields: [
 | 
			
		||||
        {
 | 
			
		||||
          type: 'string',
 | 
			
		||||
          name: 'name',
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
          type: 'belongsToMany',
 | 
			
		||||
          name: 'posts',
 | 
			
		||||
        },
 | 
			
		||||
      ],
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    await db.sync();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  it('should update tags to null', async () => {
 | 
			
		||||
    await db.getRepository('posts').create({
 | 
			
		||||
      values: {
 | 
			
		||||
        title: 'p1',
 | 
			
		||||
        tags: [{ name: 't1' }],
 | 
			
		||||
      },
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const [p1] = await db.getRepository('posts').update({
 | 
			
		||||
      values: {
 | 
			
		||||
        tags: null,
 | 
			
		||||
      },
 | 
			
		||||
      filter: {
 | 
			
		||||
        title: 'p1',
 | 
			
		||||
      },
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    expect(p1.toJSON()['tags']).toEqual([]);
 | 
			
		||||
  });
 | 
			
		||||
});
 | 
			
		||||
@ -40,7 +40,7 @@ export class Model<TModelAttributes extends {} = any, TCreationAttributes extend
 | 
			
		||||
 | 
			
		||||
    const handleArray = (arrayOfObj, options: JSONTransformerOptions) => {
 | 
			
		||||
      const handles = [this.sortAssociations];
 | 
			
		||||
      return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj);
 | 
			
		||||
      return handles.reduce((carry, fn) => fn.apply(this, [carry, options]), arrayOfObj || []);
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    const opts = {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user