/
datasource-customizer.ts
189 lines (164 loc) · 5.68 KB
/
datasource-customizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import {
Collection,
DataSource,
DataSourceFactory,
DataSourceSchema,
Logger,
} from '@forestadmin/datasource-toolkit';
import CollectionCustomizer from './collection-customizer';
import { DataSourceChartDefinition } from './decorators/chart/types';
import CompositeDatasource from './decorators/composite-datasource';
import DecoratorsStack from './decorators/decorators-stack';
import PublicationDataSourceDecorator from './decorators/publication/datasource';
import RenameCollectionDataSourceDecorator from './decorators/rename-collection/datasource';
import { TCollectionName, TSchema } from './templates';
import { DataSourceOptions, Plugin } from './types';
import TypingGenerator from './typing-generator';
/**
* Allow to create a new Forest Admin agent from scratch.
* Builds the application by composing and configuring all the collection decorators.
*
* Minimal code to add a datasource
* @example
* new AgentBuilder(options)
* .addDataSource(new SomeDataSource())
* .start();
*/
export default class DataSourceCustomizer<S extends TSchema = TSchema> {
private readonly compositeDataSource: CompositeDatasource<Collection>;
private readonly stack: DecoratorsStack;
/**
* Retrieve schema of the agent
*/
get schema(): DataSourceSchema {
return this.stack.validation.schema;
}
/**
* Get list of customizable collections
*/
get collections(): CollectionCustomizer<S>[] {
return this.stack.validation.collections.map(c =>
this.getCollection(c.name as TCollectionName<S>),
);
}
constructor() {
this.compositeDataSource = new CompositeDatasource<Collection>();
this.stack = new DecoratorsStack(this.compositeDataSource);
}
/**
* Add a datasource
* @param factory the datasource to add
* @param options the options
*/
addDataSource(factory: DataSourceFactory, options?: DataSourceOptions): this {
this.stack.queueCustomization(async logger => {
let dataSource = await factory(logger);
if (options?.include || options?.exclude) {
const publicationDecorator = new PublicationDataSourceDecorator(dataSource);
publicationDecorator.keepCollectionsMatching(options.include, options.exclude);
dataSource = publicationDecorator;
}
if (options?.rename) {
const renamedDecorator = new RenameCollectionDataSourceDecorator(dataSource);
renamedDecorator.renameCollections(options.rename);
dataSource = renamedDecorator;
}
this.compositeDataSource.addDataSource(dataSource);
});
return this;
}
/**
* Create a new API chart
* @param name name of the chart
* @param definition definition of the chart
* @example
* .addChart('numCustomers', {
* type: 'Value',
* render: (context, resultBuilder) => {
* return resultBuilder.value(123);
* }
* })
*/
addChart(name: string, definition: DataSourceChartDefinition<S>): this {
this.stack.queueCustomization(async () => {
this.stack.chart.addChart(name, definition);
});
return this;
}
/**
* Allow to interact with a decorated collection
* @param name the name of the collection to manipulate
* @param handle a function that provide a
* collection builder on the given collection name
* @example
* .customizeCollection('books', books => books.renameField('xx', 'yy'))
*/
customizeCollection<N extends TCollectionName<S>>(
name: N,
handle: (collection: CollectionCustomizer<S, N>) => unknown,
): this {
handle(this.getCollection(name));
return this;
}
/**
* Get given collection by name
* @param name name of the collection
*/
getCollection<N extends TCollectionName<S>>(name: N): CollectionCustomizer<S, N> {
return new CollectionCustomizer<S, N>(this, this.stack, name);
}
/**
* Find a collection by name. Returns undefined if the collection is missing
* @param name name of the collection
*/
findCollection(name: string): CollectionCustomizer<S> | undefined {
if (this.collections.find(collection => collection.name === name)) {
/**
* If the collection is found, we use the getCollection to apply side effects
*/
return this.getCollection(name as TCollectionName<S>);
}
}
/**
* Remove collections from the exported schema (they will still be usable within the agent).
* @param names the collections to remove
* @example
* .removeCollection('aCollectionToRemove', 'anotherCollectionToRemove');
*/
removeCollection(...names: TCollectionName<S>[]): this {
this.stack.queueCustomization(async () => {
this.stack.publication.keepCollectionsMatching(undefined, names);
});
return this;
}
/**
* Load a plugin across all collections
* @param plugin instance of the plugin
* @param options options which need to be passed to the plugin
* @example
* import { advancedExport } from '@forestadmin/plugin-advanced-export';
*
* dataSourceCustomizer.use(advancedExportPlugin, { format: 'xlsx' });
*/
use<Options>(plugin: Plugin<Options>, options?: Options): this {
this.stack.queueCustomization(async () => {
await plugin(this, null, options);
});
return this;
}
async getDataSource(logger: Logger): Promise<DataSource> {
await this.stack.applyQueuedCustomizations(logger);
return this.stack.dataSource;
}
getFactory(): DataSourceFactory {
return async (logger: Logger) => this.getDataSource(logger);
}
async updateTypesOnFileSystem(
typingsPath: string,
typingsMaxDepth: number,
logger?: Logger,
): Promise<void> {
const typingGenerator = new TypingGenerator(logger);
return typingGenerator.updateTypesOnFileSystem(this.stack.hook, typingsPath, typingsMaxDepth);
}
}