fix(plugin-field-sort): fix field registering for external data source (#6212)

This commit is contained in:
Junyi 2025-02-13 22:29:58 +08:00 committed by GitHub
parent dd7c7f641c
commit 7b715c847d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 338 additions and 292 deletions

View File

@ -7,26 +7,48 @@
* For more information, please refer to: https://www.nocobase.com/agreement. * For more information, please refer to: https://www.nocobase.com/agreement.
*/ */
import { Database } from '@nocobase/database'; import { Database, mockDatabase } from '@nocobase/database';
import { createMockServer, MockServer } from '@nocobase/test'; import { createMockServer, MockServer } from '@nocobase/test';
import Plugin from '..'; import Plugin from '..';
import { SequelizeCollectionManager, SequelizeDataSource } from '@nocobase/data-source-manager';
import { uid } from '@nocobase/utils';
describe('sort field', () => { describe('sort field', () => {
let app: MockServer; let app: MockServer;
let db: Database; let db: Database;
let another: SequelizeDataSource;
let anotherDB: Database;
beforeEach(async () => { beforeEach(async () => {
app = await createMockServer({ app = await createMockServer({
plugins: [Plugin, 'data-source-main', 'error-handler'], plugins: [Plugin, 'data-source-main', 'error-handler'],
}); });
db = app.db; db = app.db;
await app.dataSourceManager.add(
new SequelizeDataSource({
name: 'another',
collectionManager: {
database: mockDatabase({
tablePrefix: `t${uid(5)}`,
}),
},
resourceManager: {},
}),
);
another = app.dataSourceManager.dataSources.get('another') as SequelizeDataSource;
anotherDB = (another.collectionManager as SequelizeCollectionManager).db;
another.acl.allow('*', '*', 'loggedIn');
}); });
afterEach(async () => { afterEach(async () => {
await app.destroy(); await app.destroy();
}); });
describe('main data source', () => {
it('should init with camelCase scope key', async () => { it('should init with camelCase scope key', async () => {
const Test = db.collection({ const Test = db.collection({
name: 'tests', name: 'tests',
@ -332,4 +354,28 @@ describe('sort field', () => {
await t1.reload(); await t1.reload();
expect(t1.get('sort')).toBe(3); expect(t1.get('sort')).toBe(3);
}); });
});
describe('external data source', () => {
it('create record', async () => {
anotherDB.collection({
name: 'posts',
fields: [
{ type: 'string', name: 'title' },
{ type: 'sort', name: 'sort' },
],
});
await anotherDB.sync();
const p1 = await anotherDB.getRepository('posts').create({
values: { title: 'p1' },
});
const p2 = await anotherDB.getRepository('posts').create({
values: { title: 'p2' },
});
expect(p1.sort).toBe(1);
expect(p2.sort).toBe(2);
});
});
}); });

View File

@ -20,12 +20,12 @@ export class PluginFieldSortServer extends Plugin {
const { lockManager } = this.app; const { lockManager } = this.app;
class SortFieldClass extends SortField {} class SortFieldClass extends SortField {}
SortFieldClass.lockManager = lockManager; SortFieldClass.lockManager = lockManager;
this.app.db.registerFieldTypes({
sort: SortFieldClass,
});
this.app.dataSourceManager.beforeAddDataSource((dataSource: DataSource) => { this.app.dataSourceManager.beforeAddDataSource((dataSource: DataSource) => {
if (dataSource.collectionManager instanceof SequelizeCollectionManager) { if (dataSource.collectionManager instanceof SequelizeCollectionManager) {
dataSource.collectionManager.db.registerFieldTypes({
sort: SortFieldClass,
});
dataSource.resourceManager.registerActionHandlers({ move }); dataSource.resourceManager.registerActionHandlers({ move });
} }
}); });