fix: sort field init (#2520)
* fix: sort field init * chore: sort field name
This commit is contained in:
parent
b0630005d9
commit
0f9d946f09
@ -17,6 +17,34 @@ describe('string field', () => {
|
|||||||
await db.close();
|
await db.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should init with camelCase scope key', async () => {
|
||||||
|
const Test = db.collection({
|
||||||
|
name: 'tests',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
name: 'name',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: 'string',
|
||||||
|
name: 'scopeKey',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.sync();
|
||||||
|
|
||||||
|
await Test.repository.create({
|
||||||
|
values: {
|
||||||
|
name: 't1',
|
||||||
|
scopeKey: 'a',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
Test.setField('scopeKeySort', { type: 'sort', scopeKey: 'scopeKey' });
|
||||||
|
await db.sync();
|
||||||
|
});
|
||||||
|
|
||||||
it('should init sorted value with thousand records', async () => {
|
it('should init sorted value with thousand records', async () => {
|
||||||
const Test = db.collection({
|
const Test = db.collection({
|
||||||
name: 'tests',
|
name: 'tests',
|
||||||
|
@ -44,9 +44,11 @@ export class SortField extends Field {
|
|||||||
initRecordsSortValue = async ({ transaction }) => {
|
initRecordsSortValue = async ({ transaction }) => {
|
||||||
const orderField = (() => {
|
const orderField = (() => {
|
||||||
const model = this.collection.model;
|
const model = this.collection.model;
|
||||||
|
|
||||||
if (model.primaryKeyAttribute) {
|
if (model.primaryKeyAttribute) {
|
||||||
return model.primaryKeyAttribute;
|
return model.primaryKeyAttribute;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model.rawAttributes['createdAt']) {
|
if (model.rawAttributes['createdAt']) {
|
||||||
return model.rawAttributes['createdAt'].field;
|
return model.rawAttributes['createdAt'].field;
|
||||||
}
|
}
|
||||||
@ -79,8 +81,19 @@ export class SortField extends Field {
|
|||||||
const doInit = async (scopeKey = null, scopeValue = null) => {
|
const doInit = async (scopeKey = null, scopeValue = null) => {
|
||||||
const queryInterface = this.collection.db.sequelize.getQueryInterface();
|
const queryInterface = this.collection.db.sequelize.getQueryInterface();
|
||||||
|
|
||||||
|
if (scopeKey) {
|
||||||
|
const scopeField = this.collection.getField(scopeKey);
|
||||||
|
if (!scopeField) {
|
||||||
|
throw new Error(`can not find scope field ${scopeKey} for collection ${this.collection.name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
scopeKey = this.collection.model.rawAttributes[scopeKey].field;
|
||||||
|
}
|
||||||
|
|
||||||
const quotedOrderField = queryInterface.quoteIdentifier(orderField);
|
const quotedOrderField = queryInterface.quoteIdentifier(orderField);
|
||||||
|
|
||||||
|
const sortColumnName = this.collection.model.rawAttributes[this.name].field;
|
||||||
|
|
||||||
const sql = `
|
const sql = `
|
||||||
WITH ordered_table AS (
|
WITH ordered_table AS (
|
||||||
SELECT *, ROW_NUMBER() OVER (${
|
SELECT *, ROW_NUMBER() OVER (${
|
||||||
@ -105,12 +118,12 @@ export class SortField extends Field {
|
|||||||
this.collection.db.inDialect('mysql')
|
this.collection.db.inDialect('mysql')
|
||||||
? `
|
? `
|
||||||
UPDATE ${this.collection.quotedTableName()}, ordered_table
|
UPDATE ${this.collection.quotedTableName()}, ordered_table
|
||||||
SET ${this.collection.quotedTableName()}.${this.name} = ordered_table.new_sequence_number
|
SET ${this.collection.quotedTableName()}.${sortColumnName} = ordered_table.new_sequence_number
|
||||||
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
|
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
|
||||||
`
|
`
|
||||||
: `
|
: `
|
||||||
UPDATE ${this.collection.quotedTableName()}
|
UPDATE ${this.collection.quotedTableName()}
|
||||||
SET ${queryInterface.quoteIdentifier(this.name)} = ordered_table.new_sequence_number
|
SET ${queryInterface.quoteIdentifier(sortColumnName)} = ordered_table.new_sequence_number
|
||||||
FROM ordered_table
|
FROM ordered_table
|
||||||
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ${queryInterface.quoteIdentifier(
|
WHERE ${this.collection.quotedTableName()}.${quotedOrderField} = ${queryInterface.quoteIdentifier(
|
||||||
'ordered_table',
|
'ordered_table',
|
||||||
|
Loading…
Reference in New Issue
Block a user