fix: init scope value when all data is null value (#3674)

This commit is contained in:
ChengLei Shao 2024-03-11 19:37:48 +08:00 committed by GitHub
parent f9567d711b
commit 3da2a8af92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 5 deletions

View File

@ -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',

View File

@ -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')) {