Skip to content

Commit

Permalink
feat: start production agent even if some customizations apply to mi
Browse files Browse the repository at this point in the history
ssing collections
  • Loading branch information
Nicolas Moreau committed Jun 13, 2024
1 parent a90d47e commit dcf8d5e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 8 deletions.
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 {}

0 comments on commit dcf8d5e

Please sign in to comment.