diff --git a/packages/core/database/src/__tests__/hooks/hook.test.ts b/packages/core/database/src/__tests__/hooks/hook.test.ts new file mode 100644 index 0000000000..e43f77a9ff --- /dev/null +++ b/packages/core/database/src/__tests__/hooks/hook.test.ts @@ -0,0 +1,67 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { Database } from '../../database'; +import { mockDatabase } from '../'; + +describe('hook', () => { + let db: Database; + + beforeEach(async () => { + db = mockDatabase(); + await db.clean({ drop: true }); + }); + + afterEach(async () => { + await db.close(); + }); + + it('should get hook modelName', async () => { + const Posts = db.collection({ + name: 'posts', + fields: [ + { + type: 'string', + name: 'title', + }, + ], + }); + + expect(Posts.model.options.modelName).toBe('posts'); + const Tags = db.collection({ + name: 'tags', + fields: [ + { + type: 'string', + name: 'name', + }, + ], + }); + + Posts.setField('tags', { + type: 'belongsToMany', + target: 'tags', + through: 'post', + }); + + await db.sync(); + + const throughCollection = db.getCollection('post'); + throughCollection.setField('test', { type: 'string' }); + + const callback = vi.fn(); + db.on('post.afterSync', (options) => { + callback(options); + }); + + await throughCollection.sync(); + + expect(callback).toHaveBeenCalledOnce(); + }); +}); diff --git a/packages/core/database/src/collection.ts b/packages/core/database/src/collection.ts index f6943847c8..6404aa3ea6 100644 --- a/packages/core/database/src/collection.ts +++ b/packages/core/database/src/collection.ts @@ -243,6 +243,8 @@ export class Collection< this.model = class extends M {}; this.model.init(null, this.sequelizeModelOptions()); + this.model.options.modelName = this.options.name; + if (!autoGenId) { this.model.removeAttribute('id'); } diff --git a/packages/core/database/src/fields/sort-field.ts b/packages/core/database/src/fields/sort-field.ts index be29eaf82f..da4a77fd3e 100644 --- a/packages/core/database/src/fields/sort-field.ts +++ b/packages/core/database/src/fields/sort-field.ts @@ -50,7 +50,8 @@ export class SortField extends Field { } }; - initRecordsSortValue = async ({ transaction }) => { + initRecordsSortValue = async (options) => { + const { transaction } = options; const orderField = (() => { const model = this.collection.model; diff --git a/packages/core/database/src/model-hook.ts b/packages/core/database/src/model-hook.ts index dfd0f3c74f..751e8deb24 100644 --- a/packages/core/database/src/model-hook.ts +++ b/packages/core/database/src/model-hook.ts @@ -44,13 +44,10 @@ export class ModelHook { if (arg['model']) { return arg['model'].name; } - const plural = arg?.name?.plural; - if (this.database.sequelize.isDefined(plural)) { - return plural; - } - const singular = arg?.name?.singular; - if (this.database.sequelize.isDefined(singular)) { - return singular; + + const modelName = arg['modelName']; + if (this.database.sequelize.isDefined(modelName)) { + return modelName; } } }