fix: init sort value in sort field with scopeKey (#1626)

This commit is contained in:
ChengLei Shao 2023-04-01 09:06:20 +08:00 committed by GitHub
parent b015fb769a
commit 2f8e7a8087
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 38 deletions

View File

@ -17,6 +17,52 @@ describe('string field', () => {
await db.close();
});
it('should init sorted value with scopeKey', 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: 'a',
name: 'r1',
},
{
group: 'b',
name: 'r2',
},
{
group: 'a',
name: 'r3',
},
{
group: 'b',
name: 'r4',
},
],
});
Test.setField('sort', { type: 'sort', scopeKey: 'group' });
await db.sync();
const records = await Test.repository.find({});
const r3 = records.find((r) => r.get('name') === 'r3');
expect(r3.get('sort')).toBe(2);
});
it('should init sorted value by createdAt when primaryKey not exists', async () => {
const Test = db.collection({
autoGenId: false,

View File

@ -42,50 +42,75 @@ export class SortField extends Field {
};
initRecordsSortValue = async ({ transaction }) => {
const totalCount = await this.collection.repository.count({
transaction,
});
const emptyCount = await this.collection.repository.count({
filter: {
[this.name]: null,
},
transaction,
});
const orderKey = (() => {
const model = this.collection.model;
if (model.primaryKeyAttribute) {
return model.primaryKeyAttribute;
}
if (model.rawAttributes['createdAt']) {
return 'createdAt';
const doInit = async (scopeKey = null, scopeValue = null) => {
const filter = {};
if (scopeKey && scopeValue) {
filter[scopeKey] = scopeValue;
}
throw new Error(`can not find order key for collection ${this.collection.name}`);
})();
if (emptyCount === totalCount && emptyCount > 0) {
const records = await this.collection.repository.find({
order: [orderKey],
const totalCount = await this.collection.repository.count({
filter,
transaction,
});
let start = 1;
for (const record of records) {
await record.update(
{
sort: start,
},
{
hooks: false,
transaction,
silent: true,
},
);
const emptyCount = await this.collection.repository.count({
filter: {
[this.name]: null,
...filter,
},
transaction,
});
start += 1;
const orderKey = (() => {
const model = this.collection.model;
if (model.primaryKeyAttribute) {
return model.primaryKeyAttribute;
}
if (model.rawAttributes['createdAt']) {
return 'createdAt';
}
throw new Error(`can not find order key for collection ${this.collection.name}`);
})();
if (emptyCount === totalCount && emptyCount > 0) {
const records = await this.collection.repository.find({
order: [orderKey],
filter,
transaction,
});
let start = 1;
for (const record of records) {
await record.update(
{
sort: start,
},
{
hooks: false,
transaction,
silent: true,
},
);
start += 1;
}
}
};
const scopeKey = this.options.scopeKey;
if (scopeKey) {
const groups = await this.collection.repository.find({
attributes: [scopeKey],
group: [scopeKey],
raw: true,
});
for (const group of groups) {
await doInit(scopeKey, group[scopeKey]);
}
} else {
await doInit();
}
};

View File

@ -21,7 +21,7 @@ export function getConfigByEnv() {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
dialect: process.env.DB_DIALECT || 'sqlite',
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
logging: process.env.DB_LOGGING === 'on' ? customLogger : false,
storage:
process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:'
? resolve(process.cwd(), process.env.DB_STORAGE)
@ -39,6 +39,11 @@ export function getConfigByEnv() {
};
}
function customLogger(queryString, queryObject) {
console.log(queryString); // outputs a string
console.log(queryObject.bind); // outputs an array
}
export function mockDatabase(options: IDatabaseOptions = {}): MockDatabase {
const dbOptions = merge(getConfigByEnv(), options) as any;
return new MockDatabase(dbOptions);