mirror of
https://gitee.com/nocobase/nocobase.git
synced 2025-05-06 05:59: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.
|
* 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',
|
||||||
@ -333,3 +355,27 @@ describe('sort field', () => {
|
|||||||
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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -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 });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user