mirror of
https://gitee.com/nocobase/nocobase.git
synced 2024-12-05 05:38:23 +08:00
fix: init sort value in sort field with scopeKey (#1626)
This commit is contained in:
parent
b015fb769a
commit
2f8e7a8087
@ -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,
|
||||
|
@ -42,13 +42,21 @@ export class SortField extends Field {
|
||||
};
|
||||
|
||||
initRecordsSortValue = async ({ transaction }) => {
|
||||
const doInit = async (scopeKey = null, scopeValue = null) => {
|
||||
const filter = {};
|
||||
if (scopeKey && scopeValue) {
|
||||
filter[scopeKey] = scopeValue;
|
||||
}
|
||||
|
||||
const totalCount = await this.collection.repository.count({
|
||||
filter,
|
||||
transaction,
|
||||
});
|
||||
|
||||
const emptyCount = await this.collection.repository.count({
|
||||
filter: {
|
||||
[this.name]: null,
|
||||
...filter,
|
||||
},
|
||||
transaction,
|
||||
});
|
||||
@ -68,6 +76,7 @@ export class SortField extends Field {
|
||||
if (emptyCount === totalCount && emptyCount > 0) {
|
||||
const records = await this.collection.repository.find({
|
||||
order: [orderKey],
|
||||
filter,
|
||||
transaction,
|
||||
});
|
||||
|
||||
@ -89,6 +98,22 @@ export class SortField extends Field {
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
};
|
||||
|
||||
bind() {
|
||||
super.bind();
|
||||
this.on('afterSync', this.initRecordsSortValue);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user