From ecf618783e39c104aa6afb225840b81f89fb2ca9 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Mon, 27 Feb 2023 13:59:17 +0800 Subject: [PATCH] feat: fallback sort field init to createdAt field (#1507) * feat: fallback sort field init to createdAt field when primary key not exists * chore: set belongsToMany through autoCreated table to sortable false --- .../src/__tests__/fields/sort-field.test.ts | 23 ++++++++++++++++++ .../core/database/src/fields/sort-field.ts | 14 ++++++++++- .../__tests__/fields/belongsToMany.test.ts | 24 ++++++++++++++++++- .../hooks/afterCreateForForeignKeyField.ts | 1 + packages/presets/nocobase/src/index.ts | 1 + 5 files changed, 61 insertions(+), 2 deletions(-) diff --git a/packages/core/database/src/__tests__/fields/sort-field.test.ts b/packages/core/database/src/__tests__/fields/sort-field.test.ts index 1a23e4649..dfd36aab7 100644 --- a/packages/core/database/src/__tests__/fields/sort-field.test.ts +++ b/packages/core/database/src/__tests__/fields/sort-field.test.ts @@ -17,6 +17,29 @@ describe('string field', () => { await db.close(); }); + it('should init sorted value by createdAt when primaryKey not exists', async () => { + const Test = db.collection({ + autoGenId: false, + name: 'tests', + }); + + await db.sync(); + await Test.repository.create({ + values: [{}, {}, {}], + }); + + // add sort field + Test.setField('sort', { type: 'sort' }); + + let err; + try { + await db.sync(); + } catch (e) { + err = e; + } + expect(err).toBeFalsy(); + }); + it('sort', async () => { const Test = db.collection({ name: 'tests', diff --git a/packages/core/database/src/fields/sort-field.ts b/packages/core/database/src/fields/sort-field.ts index 7adf643f3..47e7bc873 100644 --- a/packages/core/database/src/fields/sort-field.ts +++ b/packages/core/database/src/fields/sort-field.ts @@ -53,9 +53,21 @@ export class SortField extends Field { 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}`); + })(); + if (emptyCount === totalCount && emptyCount > 0) { const records = await this.collection.repository.find({ - order: [this.collection.model.primaryKeyAttribute], + order: [orderKey], transaction, }); diff --git a/packages/plugins/collection-manager/src/__tests__/fields/belongsToMany.test.ts b/packages/plugins/collection-manager/src/__tests__/fields/belongsToMany.test.ts index 509167c75..5ded8d5af 100644 --- a/packages/plugins/collection-manager/src/__tests__/fields/belongsToMany.test.ts +++ b/packages/plugins/collection-manager/src/__tests__/fields/belongsToMany.test.ts @@ -17,11 +17,14 @@ describe('belongsToMany', () => { values: { name: 'posts', }, + context: {}, }); + await Collection.repository.create({ values: { name: 'tags', }, + context: {}, }); }); @@ -29,5 +32,24 @@ describe('belongsToMany', () => { await app.destroy(); }); - it('should create belongsToMany field', async () => {}); + it('should create belongsToMany field', async () => { + await Field.repository.create({ + values: { + name: 'tags', + type: 'belongsToMany', + collectionName: 'posts', + interface: 'm2m', + through: 'post_tags', + }, + context: {}, + }); + + const throughCollection = await Collection.repository.findOne({ + filter: { + name: 'post_tags', + }, + }); + + expect(throughCollection.get('sortable')).toEqual(false); + }); }); diff --git a/packages/plugins/collection-manager/src/hooks/afterCreateForForeignKeyField.ts b/packages/plugins/collection-manager/src/hooks/afterCreateForForeignKeyField.ts index f1688d872..e722f32cf 100644 --- a/packages/plugins/collection-manager/src/hooks/afterCreateForForeignKeyField.ts +++ b/packages/plugins/collection-manager/src/hooks/afterCreateForForeignKeyField.ts @@ -150,6 +150,7 @@ export function afterCreateForForeignKeyField(db: Database) { hidden: true, autoCreate: true, isThrough: true, + sortable: false, }, transaction, }); diff --git a/packages/presets/nocobase/src/index.ts b/packages/presets/nocobase/src/index.ts index 79e333da3..50a85ed9a 100644 --- a/packages/presets/nocobase/src/index.ts +++ b/packages/presets/nocobase/src/index.ts @@ -80,6 +80,7 @@ export class PresetNocoBase extends Plugin { // } // } }); + this.app.on('beforeUpgrade', async (options) => { const result = await this.app.version.satisfies('<0.8.0-alpha.1'); if (result) {