Skip to content
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

feat: start production agent even if some customizations apply to missing collections #1126

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -212,7 +212,7 @@ export default class Agent<S extends TSchema = TSchema> extends FrameworkMounter
this.nocodeCustomizer.addDataSource(this.customizer.getFactory());
this.nocodeCustomizer.use(this.customizationService.addCustomizations);

const dataSource = await this.nocodeCustomizer.getDataSource(logger);
const dataSource = await this.nocodeCustomizer.getDataSource(logger, isProduction);
const [router] = await Promise.all([
this.getRouter(dataSource),
this.sendSchema(dataSource),
Expand Down
6 changes: 3 additions & 3 deletions packages/datasource-customizer/src/datasource-customizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ export default class DataSourceCustomizer<S extends TSchema = TSchema> {
return this;
}

async getDataSource(logger: Logger): Promise<DataSource> {
await this.stack.applyQueuedCustomizations(logger);
async getDataSource(logger: Logger, isProduction = false): Promise<DataSource> {
await this.stack.applyQueuedCustomizations(logger, isProduction);

return this.stack.dataSource;
}

getFactory(): DataSourceFactory {
return async (logger: Logger) => this.getDataSource(logger);
return async (logger: Logger, isProduction = false) => this.getDataSource(logger, isProduction);
}

async updateTypesOnFileSystem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
DataSourceSchema,
} from '@forestadmin/datasource-toolkit';

import { MissingCollectionError } from '../errors';

export default class CompositeDatasource<T extends Collection = Collection>
implements DataSource<T>
{
Expand All @@ -28,7 +30,7 @@ export default class CompositeDatasource<T extends Collection = Collection>
}
}

throw new Error(
throw new MissingCollectionError(
`Collection '${name}' not found. List of available collections: ${this.collections
.map(c => c.name)
.sort()
Expand Down
15 changes: 12 additions & 3 deletions packages/datasource-customizer/src/decorators/decorators-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import SegmentCollectionDecorator from './segment/collection';
import SortEmulateCollectionDecorator from './sort-emulate/collection';
import ValidationCollectionDecorator from './validation/collection';
import WriteDataSourceDecorator from './write/datasource';
import { MissingCollectionError } from '../errors';

export default class DecoratorsStack {
action: DataSourceDecorator<ActionCollectionDecorator>;
Expand Down Expand Up @@ -96,13 +97,21 @@ export default class DecoratorsStack {
* This method will be called recursively and clears the queue at each recursion to ensure
* that all customizations are applied in the right order.
*/
async applyQueuedCustomizations(logger: Logger): Promise<void> {
async applyQueuedCustomizations(logger: Logger, isProduction = false): Promise<void> {
const queuedCustomizations = this.customizations.slice();
this.customizations.length = 0;

while (queuedCustomizations.length) {
await queuedCustomizations.shift()(logger); // eslint-disable-line no-await-in-loop
await this.applyQueuedCustomizations(logger); // eslint-disable-line no-await-in-loop
try {
await queuedCustomizations.shift()(logger); // eslint-disable-line no-await-in-loop
await this.applyQueuedCustomizations(logger); // eslint-disable-line no-await-in-loop
} catch (e) {
if (e instanceof MissingCollectionError && isProduction) {
logger('Warn', e.message);
} else {
throw e;
}
}
}
}
}
2 changes: 2 additions & 0 deletions packages/datasource-customizer/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export class MissingCollectionError extends Error {}
Loading