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();
|
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,13 +42,21 @@ export class SortField extends Field {
|
|||||||
};
|
};
|
||||||
|
|
||||||
initRecordsSortValue = async ({ transaction }) => {
|
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({
|
const totalCount = await this.collection.repository.count({
|
||||||
|
filter,
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
const emptyCount = await this.collection.repository.count({
|
const emptyCount = await this.collection.repository.count({
|
||||||
filter: {
|
filter: {
|
||||||
[this.name]: null,
|
[this.name]: null,
|
||||||
|
...filter,
|
||||||
},
|
},
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
@ -68,6 +76,7 @@ export class SortField extends Field {
|
|||||||
if (emptyCount === totalCount && emptyCount > 0) {
|
if (emptyCount === totalCount && emptyCount > 0) {
|
||||||
const records = await this.collection.repository.find({
|
const records = await this.collection.repository.find({
|
||||||
order: [orderKey],
|
order: [orderKey],
|
||||||
|
filter,
|
||||||
transaction,
|
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() {
|
bind() {
|
||||||
super.bind();
|
super.bind();
|
||||||
this.on('afterSync', this.initRecordsSortValue);
|
this.on('afterSync', this.initRecordsSortValue);
|
||||||
|
@ -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