fix: init scope value when all data is null value (#3674)
This commit is contained in:
		
							parent
							
								
									f9567d711b
								
							
						
					
					
						commit
						3da2a8af92
					
				@ -90,6 +90,41 @@ describe('string field', () => {
 | 
				
			|||||||
    console.log(end - begin);
 | 
					    console.log(end - begin);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  it('should init sorted value with null scopeValue', async () => {
 | 
				
			||||||
 | 
					    const Test = db.collection({
 | 
				
			||||||
 | 
					      name: 'tests',
 | 
				
			||||||
 | 
					      fields: [
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          type: 'string',
 | 
				
			||||||
 | 
					          name: 'name',
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          type: 'string',
 | 
				
			||||||
 | 
					          name: 'group',
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await db.sync();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await Test.repository.create({
 | 
				
			||||||
 | 
					      values: [
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          group: null,
 | 
				
			||||||
 | 
					          name: 'r5',
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          group: null,
 | 
				
			||||||
 | 
					          name: 'r6',
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Test.setField('sort', { type: 'sort', scopeKey: 'group' });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await db.sync();
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  it('should init sorted value with scopeKey', async () => {
 | 
					  it('should init sorted value with scopeKey', async () => {
 | 
				
			||||||
    const Test = db.collection({
 | 
					    const Test = db.collection({
 | 
				
			||||||
      name: 'tests',
 | 
					      name: 'tests',
 | 
				
			||||||
 | 
				
			|||||||
@ -99,11 +99,19 @@ export class SortField extends Field {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
      const whereClause =
 | 
					      const whereClause =
 | 
				
			||||||
        scopeKey && scopeValue
 | 
					        scopeKey && scopeValue
 | 
				
			||||||
          ? `
 | 
					          ? (() => {
 | 
				
			||||||
  WHERE ${queryInterface.quoteIdentifier(scopeKey)} IN (${scopeValue
 | 
					              const filteredScopeValue = scopeValue.filter((v) => v !== null);
 | 
				
			||||||
    .filter((v) => v !== null)
 | 
					              if (filteredScopeValue.length === 0) {
 | 
				
			||||||
    .map((v) => `'${v}'`)
 | 
					                return '';
 | 
				
			||||||
    .join(', ')})${scopeValue.includes(null) ? ` OR ${queryInterface.quoteIdentifier(scopeKey)} IS NULL` : ''}`
 | 
					              }
 | 
				
			||||||
 | 
					              const initialClause = `
 | 
				
			||||||
 | 
					  WHERE ${queryInterface.quoteIdentifier(scopeKey)} IN (${filteredScopeValue.map((v) => `'${v}'`).join(', ')})`;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					              const nullCheck = scopeValue.includes(null)
 | 
				
			||||||
 | 
					                ? ` OR ${queryInterface.quoteIdentifier(scopeKey)} IS NULL`
 | 
				
			||||||
 | 
					                : '';
 | 
				
			||||||
 | 
					              return initialClause + nullCheck;
 | 
				
			||||||
 | 
					            })()
 | 
				
			||||||
          : '';
 | 
					          : '';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (this.collection.db.inDialect('postgres')) {
 | 
					      if (this.collection.db.inDialect('postgres')) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user