chore: remove data source (#3610)

This commit is contained in:
ChengLei Shao 2024-03-05 15:58:45 +08:00 committed by GitHub
parent 3acc574877
commit 89ad88857d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 12 deletions

View File

@ -303,6 +303,45 @@ describe('data source', async () => {
expect(mockDataSource).toBeInstanceOf(MockDataSource); expect(mockDataSource).toBeInstanceOf(MockDataSource);
}); });
it('should destroy data source', async () => {
class MockCollectionManager extends CollectionManager {}
class MockDataSource extends DataSource {
static testConnection(options?: any): Promise<boolean> {
return Promise.resolve(true);
}
async load(): Promise<void> {
await waitSecond(1000);
}
createCollectionManager(options?: any): any {
return new MockCollectionManager();
}
}
app.dataSourceManager.factory.register('mock', MockDataSource);
await app.db.getRepository('dataSources').create({
values: {
key: 'mockInstance1',
type: 'mock',
displayName: 'Mock',
options: {},
},
});
await waitSecond(2000);
expect(app.dataSourceManager.dataSources.get('mockInstance1')).toBeDefined();
await app.agent().resource('dataSources').destroy({
filterByTk: 'mockInstance1',
});
expect(app.dataSourceManager.dataSources.get('mockInstance1')).not.toBeDefined();
});
describe('data source collections', () => { describe('data source collections', () => {
beforeEach(async () => { beforeEach(async () => {
class MockCollectionManager extends CollectionManager {} class MockCollectionManager extends CollectionManager {}

View File

@ -19,18 +19,6 @@ export class DataSourcesFieldModel extends MagicAttributeModel {
const newOptions = mergeOptions(oldField ? oldField.options : {}, options); const newOptions = mergeOptions(oldField ? oldField.options : {}, options);
collection.setField(name, newOptions); collection.setField(name, newOptions);
// const interfaceOption = options.interface;
// if (interfaceOption === 'updatedAt') {
// // @ts-ignore
// collection.model._timestampAttributes.createdAt = this.get('name');
// }
//
// if (interfaceOption === 'createdAt') {
// // @ts-ignore
// collection.model._timestampAttributes.updatedAt = this.get('name');
// }
} }
unload(loadOptions: LoadOptions) { unload(loadOptions: LoadOptions) {
@ -38,7 +26,14 @@ export class DataSourcesFieldModel extends MagicAttributeModel {
const options = this.get(); const options = this.get();
const { collectionName, name, dataSourceKey } = options; const { collectionName, name, dataSourceKey } = options;
const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey); const dataSource = app.dataSourceManager.dataSources.get(dataSourceKey);
if (!dataSource) {
return;
}
const collection = dataSource.collectionManager.getCollection(collectionName); const collection = dataSource.collectionManager.getCollection(collectionName);
if (!collection) {
return;
}
collection.removeField(name); collection.removeField(name);
} }
} }

View File

@ -322,6 +322,10 @@ export class PluginDataSourceManagerServer extends Plugin {
}); });
}); });
this.app.db.on('dataSources.afterDestroy', async (model: DataSourceModel) => {
this.app.dataSourceManager.dataSources.delete(model.get('key'));
});
this.app.on('afterStart', async (app: Application) => { this.app.on('afterStart', async (app: Application) => {
const dataSourcesRecords: DataSourceModel[] = await this.app.db.getRepository('dataSources').find({ const dataSourcesRecords: DataSourceModel[] = await this.app.db.getRepository('dataSources').find({
filter: { filter: {