mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-05 21:49:25 +08:00
fix(plugin-field-sort): fix field registering for external data source (#6212)
This commit is contained in:
parent
dd7c7f641c
commit
7b715c847d
@ -7,26 +7,48 @@
|
||||
* 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 Plugin from '..';
|
||||
import { SequelizeCollectionManager, SequelizeDataSource } from '@nocobase/data-source-manager';
|
||||
import { uid } from '@nocobase/utils';
|
||||
|
||||
describe('sort field', () => {
|
||||
let app: MockServer;
|
||||
let db: Database;
|
||||
let another: SequelizeDataSource;
|
||||
let anotherDB: Database;
|
||||
|
||||
beforeEach(async () => {
|
||||
app = await createMockServer({
|
||||
plugins: [Plugin, 'data-source-main', 'error-handler'],
|
||||
});
|
||||
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 () => {
|
||||
await app.destroy();
|
||||
});
|
||||
|
||||
describe('main data source', () => {
|
||||
it('should init with camelCase scope key', async () => {
|
||||
const Test = db.collection({
|
||||
name: 'tests',
|
||||
@ -332,4 +354,28 @@ describe('sort field', () => {
|
||||
await t1.reload();
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -20,12 +20,12 @@ export class PluginFieldSortServer extends Plugin {
|
||||
const { lockManager } = this.app;
|
||||
class SortFieldClass extends SortField {}
|
||||
SortFieldClass.lockManager = lockManager;
|
||||
this.app.db.registerFieldTypes({
|
||||
sort: SortFieldClass,
|
||||
});
|
||||
|
||||
this.app.dataSourceManager.beforeAddDataSource((dataSource: DataSource) => {
|
||||
if (dataSource.collectionManager instanceof SequelizeCollectionManager) {
|
||||
dataSource.collectionManager.db.registerFieldTypes({
|
||||
sort: SortFieldClass,
|
||||
});
|
||||
dataSource.resourceManager.registerActionHandlers({ move });
|
||||
}
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user