fix: init sort field with null group

This commit is contained in:
chareice 2023-04-09 12:52:42 +08:00
parent 0c17d609fa
commit f35da4a2a0
2 changed files with 24 additions and 7 deletions

View File

@ -95,6 +95,14 @@ describe('string field', () => {
group: 'b',
name: 'r4',
},
{
group: null,
name: 'r5',
},
{
group: null,
name: 'r6',
},
],
});
@ -105,6 +113,8 @@ describe('string field', () => {
const records = await Test.repository.find({});
const r3 = records.find((r) => r.get('name') === 'r3');
expect(r3.get('sort')).toBe(2);
const r5 = records.find((r) => r.get('name') === 'r5');
expect(r5.get('sort')).toBe(1);
});
it('should init sorted value by createdAt when primaryKey not exists', async () => {

View File

@ -87,11 +87,19 @@ export class SortField extends Field {
scopeKey ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey)}` : ''
} ORDER BY ${quotedOrderField}) AS new_sequence_number
FROM ${this.collection.quotedTableName()}
${
scopeKey
? `WHERE ${queryInterface.quoteIdentifier(scopeKey)} IN (${scopeValue.map((v) => `'${v}'`).join(',')})`
: ''
}
${(() => {
if (scopeKey && scopeValue) {
const hasNull = scopeValue.includes(null);
return `WHERE ${queryInterface.quoteIdentifier(scopeKey)} IN (${scopeValue
.filter((v) => v !== null)
.map((v) => `'${v}'`)
.join(',')}) ${hasNull ? `OR ${queryInterface.quoteIdentifier(scopeKey)} IS NULL` : ''} `;
}
return '';
})()}
)
${
this.collection.db.inDialect('mysql')
@ -99,7 +107,6 @@ export class SortField extends Field {
UPDATE ${this.collection.quotedTableName()}, ordered_table
SET ${this.collection.quotedTableName()}.${this.name} = ordered_table.new_sequence_number
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
${scopeKey ? `AND ${this.collection.quotedTableName()}.${scopeKey} = ordered_table.${scopeKey}` : ''}
`
: `
UPDATE ${this.collection.quotedTableName()}
@ -130,7 +137,7 @@ export class SortField extends Field {
const needInitGroups = [];
for (const group of groups) {
if (await needInit(scopeKey, group[scopeKey])) {
needInitGroups.push(group['group']);
needInitGroups.push(group[scopeKey]);
}
}