Skip to content

feat: allow reload agent on datasource #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/agent/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter
* @see {@link https://docs.forestadmin.com/developer-guide-agents-nodejs/data-sources/connection Documentation Link}
*/
addDataSource(factory: DataSourceFactory, options?: DataSourceOptions): this {
this.customizer.addDataSource(factory, options);
this.customizer.addDataSource(factory, options, this.restart.bind(this));

return this;
}
Expand Down
8 changes: 6 additions & 2 deletions packages/datasource-customizer/src/datasource-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ export default class DataSourceCustomizer<S extends TSchema = TSchema> {
* @param factory the datasource to add
* @param options the options
*/
addDataSource(factory: DataSourceFactory, options?: DataSourceOptions): this {
addDataSource(
factory: DataSourceFactory,
options?: DataSourceOptions,
restartAgentFunction?: () => Promise<void>,
): this {
this.stack.queueCustomization(async logger => {
let dataSource = await factory(logger);
let dataSource = await factory(logger, restartAgentFunction);

if (options?.include || options?.exclude) {
const publicationDecorator = new PublicationDataSourceDecorator(dataSource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('DataSourceCustomizer', () => {
const factory = customizer.getFactory();
expect(factory).toBeInstanceOf(Function);

const dataSource = await factory(logger);
const dataSource = await factory(logger, jest.fn());
expect(dataSource.schema).toStrictEqual({ charts: [] });
expect(dataSource.collections).toHaveLength(1);
expect(dataSource.collections[0].name).toBe('foo');
Expand Down
2 changes: 1 addition & 1 deletion packages/datasource-mongo/test/create.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('create', () => {
dataSource: {},
});

return dataSourceFactory(jest.fn());
return dataSourceFactory(jest.fn(), jest.fn());
}

it('should allow to create a new movie', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/datasource-mongo/test/index.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ describe('Datasource Mongo', () => {
},
});

const dataSource = await factory(() => {});
const dataSource = await factory(jest.fn(), jest.fn());

expect(dataSource.collections).toHaveLength(2);
expect(dataSource.collections.map(c => c.name)).toEqual(
Expand Down
2 changes: 1 addition & 1 deletion packages/datasource-mongoose/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('exports', () => {

it('createMongooseDataSource should return a datasource factory', async () => {
const factory = createMongooseDataSource(mongoose.connection, {});
const dataSource = await factory(() => {});
const dataSource = await factory(jest.fn(), jest.fn());

expect(factory).toBeInstanceOf(Function);
expect(dataSource).toBeInstanceOf(MongooseDatasource);
Expand Down
2 changes: 1 addition & 1 deletion packages/datasource-replica/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function createReplicaDataSource(rawOptions: ReplicaDataSourceOptions): DataSour

// Create the sequelize data source and provide it to the source
// (to allow customers to use it when handling requests if they want to)
const sequelizeDs = await createSequelizeDataSource(connection)(logger);
const sequelizeDs = await createSequelizeDataSource(connection)(logger, async () => {});
const publicationDs = new PublicationCollectionDataSourceDecorator(sequelizeDs);
publicationDs.keepCollectionsMatching(null, [
`${options.cacheNamespace}_pending_operations`,
Expand Down
2 changes: 1 addition & 1 deletion packages/datasource-replica/test/integrations/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ export const makeReplicaDataSource = async (
): Promise<DataSource> => {
const replicaFactory = createReplicaDataSource(options ?? {});

return replicaFactory(logger ?? makeLogger());
return replicaFactory(logger ?? makeLogger(), jest.fn());
};
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Count', () => {
{ name: null },
]);

const datasource = await createSequelizeDataSource(sequelize)(undefined as any);
const datasource = await createSequelizeDataSource(sequelize)(jest.fn(), jest.fn());
collection = datasource.getCollection('User') as SequelizeCollection;
}, 30_000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Filter tests on collection', () => {
{ name: null },
]);

const datasource = await createSequelizeDataSource(sequelize)(undefined as any);
const datasource = await createSequelizeDataSource(sequelize)(jest.fn(), jest.fn());
collection = datasource.getCollection('User') as SequelizeCollection;
}, 30_000);

Expand Down Expand Up @@ -439,7 +439,7 @@ describe('Filter tests on collection', () => {
{ tags: null },
]);

const datasource = await createSequelizeDataSource(sequelize)(undefined as any);
const datasource = await createSequelizeDataSource(sequelize)(jest.fn(), jest.fn());
collection = datasource.getCollection('Objects') as SequelizeCollection;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ describe('SqlDataSourceFactory > Integration', () => {
const sqlDs = await createSqlDataSource(
`${connectionDetails.url(databaseName)}?${queryString}`,
{ displaySoftDeleted: ['softDeleted'] },
)(logger);
)(logger, jest.fn());

const collection = sqlDs.getCollection('softDeleted');
const collection2 = sqlDs.getCollection('softDeleted2');
Expand Down Expand Up @@ -493,7 +493,7 @@ describe('SqlDataSourceFactory > Integration', () => {
const sqlDs = await createSqlDataSource(
`${connectionDetails.url(databaseName)}?${queryString}`,
{ displaySoftDeleted: true },
)(logger);
)(logger, jest.fn());

const collection = sqlDs.getCollection('softDeleted');
const collection2 = sqlDs.getCollection('softDeleted2');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ describe('datasource with views', () => {

sequelize = await buildSequelizeInstance(connectionDetails.url(db), jest.fn(), introspection);

datasource = (await createSqlDataSource(connectionDetails.url(db))(logger)) as SqlDatasource;
datasource = (await createSqlDataSource(connectionDetails.url(db))(
logger,
jest.fn(),
)) as SqlDatasource;
});

afterAll(async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/datasource-sql/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('index', () => {
const factory = createSqlDataSource('invalid');
const logger = jest.fn();

await expect(() => factory(logger)).rejects.toThrow(
await expect(() => factory(logger, jest.fn())).rejects.toThrow(
'Connection Uri "invalid" provided to SQL data source is not valid.' +
' Should be <dialect>://<connection>.',
);
Expand All @@ -39,7 +39,7 @@ describe('index', () => {
});
const logger = jest.fn();

await expect(() => factory(logger)).rejects.toThrow(
await expect(() => factory(logger, jest.fn())).rejects.toThrow(
`This version of introspection is newer than this package version. ` +
'Please update @forestadmin/datasource-sql',
);
Expand Down
5 changes: 4 additions & 1 deletion packages/datasource-toolkit/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ export type LoggerLevel = 'Debug' | 'Info' | 'Warn' | 'Error';
export type Logger = (level: LoggerLevel, message: string, error?: Error) => void;

/** Function which generates datasources */
export type DataSourceFactory = (logger: Logger) => Promise<DataSource>;
export type DataSourceFactory = (
logger: Logger,
restartAggent: () => Promise<void>,
) => Promise<DataSource>;
Loading