fix: init sort value in sort field with scopeKey (#1626)
This commit is contained in:
parent
b015fb769a
commit
2f8e7a8087
packages/core/database/src
@ -17,6 +17,52 @@ describe('string field', () => {
|
|||||||
await db.close();
|
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 () => {
|
it('should init sorted value by createdAt when primaryKey not exists', async () => {
|
||||||
const Test = db.collection({
|
const Test = db.collection({
|
||||||
autoGenId: false,
|
autoGenId: false,
|
||||||
|
@ -42,50 +42,75 @@ export class SortField extends Field {
|
|||||||
};
|
};
|
||||||
|
|
||||||
initRecordsSortValue = async ({ transaction }) => {
|
initRecordsSortValue = async ({ transaction }) => {
|
||||||
const totalCount = await this.collection.repository.count({
|
const doInit = async (scopeKey = null, scopeValue = null) => {
|
||||||
transaction,
|
const filter = {};
|
||||||
});
|
if (scopeKey && scopeValue) {
|
||||||
|
filter[scopeKey] = scopeValue;
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`can not find order key for collection ${this.collection.name}`);
|
const totalCount = await this.collection.repository.count({
|
||||||
})();
|
filter,
|
||||||
|
|
||||||
if (emptyCount === totalCount && emptyCount > 0) {
|
|
||||||
const records = await this.collection.repository.find({
|
|
||||||
order: [orderKey],
|
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
let start = 1;
|
const emptyCount = await this.collection.repository.count({
|
||||||
for (const record of records) {
|
filter: {
|
||||||
await record.update(
|
[this.name]: null,
|
||||||
{
|
...filter,
|
||||||
sort: start,
|
},
|
||||||
},
|
transaction,
|
||||||
{
|
});
|
||||||
hooks: false,
|
|
||||||
transaction,
|
|
||||||
silent: true,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
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();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ export function getConfigByEnv() {
|
|||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
port: process.env.DB_PORT,
|
port: process.env.DB_PORT,
|
||||||
dialect: process.env.DB_DIALECT || 'sqlite',
|
dialect: process.env.DB_DIALECT || 'sqlite',
|
||||||
logging: process.env.DB_LOGGING === 'on' ? console.log : false,
|
logging: process.env.DB_LOGGING === 'on' ? customLogger : false,
|
||||||
storage:
|
storage:
|
||||||
process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:'
|
process.env.DB_STORAGE && process.env.DB_STORAGE !== ':memory:'
|
||||||
? resolve(process.cwd(), process.env.DB_STORAGE)
|
? 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 {
|
export function mockDatabase(options: IDatabaseOptions = {}): MockDatabase {
|
||||||
const dbOptions = merge(getConfigByEnv(), options) as any;
|
const dbOptions = merge(getConfigByEnv(), options) as any;
|
||||||
return new MockDatabase(dbOptions);
|
return new MockDatabase(dbOptions);
|
||||||
|
Loading…
Reference in New Issue
Block a user