-
Notifications
You must be signed in to change notification settings - Fork 1
Docs: Datasource
- Introduction
- Installation
- Overview
-
Key Concepts
- Datasource Plugins
- Datasource Components
- Dynamic Datasource Resolvers
-
Core Module
DatasourceModule
-
Key Components
DatasourceFormComponentDatasourceOptionsComponentDatasourceSourceComponentDataDatasourceComponent
-
Key Services
- DatasourcePluginManager
- DatasourceApiService
- DatasourceEvaluator
- Models
- Mocks
-
Usage
- Configure Datasources
- Register Datasource Plugins
- Fetch and Post Data
- API Reference
- Examples
- Testing
The Datasource Angular Library provides tools for managing, rendering, and resolving datasources dynamically within Angular applications. It allows you to interact with data sources via plugins, form-based configurations, and resolvers, offering extensibility through its plugin architecture and reusable components.
Install the library and its dependencies:
npm install @rollthecloudinc/material @rollthecloudinc/plugin @rollthecloudinc/attributes @rollthecloudinc/contentThese dependencies enable plugin-based extensibility, attribute serialization, and seamless data handling.
The Datasource Library integrates datasources such as APIs, attributes, and data objects directly into Angular applications. The library supports:
- Datasource Plugins: Enables dynamic datasource management using custom plugins.
- Component-Based Forms: Provides reusable components for rendering datasource configurations and interacting with data.
-
Dynamic Rendering: Load plugin-specific editors dynamically with directives like
DatasourceRendererHostDirective. - Data Mapping and Serialization: Transform data sources using serialization services.
- Nested Datasource Evaluation: Recursive evaluation of datasources for complex data structures.
A DatasourcePlugin represents a reusable unit for fetching or managing data. It is typically set up with:
- ID: A unique identifier for the plugin.
- Editor Component: UI rendering logic for configuring the plugin.
- Fetch Method: Resolves and retrieves data from a datasource.
Example:
export const dataDatasourcePluginFactory = (attributeSerializer: AttributeSerializerService) => {
return new DatasourcePlugin<string>({
id: 'data',
title: 'Data',
editor: DataDatasourceComponent,
fetch: ({ settings }) =>
of(new Dataset()).pipe(
map(() => attributeSerializer.deserializeAsObject(settings)),
map((s) => new Dataset({ results: JSON.parse(s.data) }))
),
});
};The library provides specialized components to interact with datasources:
-
DatasourceFormComponent: Core form for managing datasource plugins. -
DatasourceOptionsComponent: Form for configuring datasource options like query or mapping fields. -
DataDatasourceComponent: Specialized editor fordatadatasource plugins.
Resolvers facilitate runtime evaluation of datasources and their nested bindings. This enables dynamic resolution of complex datasource relationships.
The DatasourceModule consolidates components, directives, and services required for datasource management. Import it into your application:
import { DatasourceModule } from 'datasource';
@NgModule({
imports: [DatasourceModule],
})
export class AppModule {}The module includes:
- Components:
DatasourceFormComponent,DatasourceOptionsComponent, etc. - Directive:
DatasourceRendererHostDirective. - Services:
DatasourcePluginManager,DatasourceEvaluator.
Here’s the module declaration:
@NgModule({
declarations: [
DatasourceOptionsComponent,
DatasourceFormComponent,
DatasourceRendererHostDirective,
DataDatasourceComponent,
DataSourceFormComponent,
DatasourceSourceComponent,
DatasourceSourceFormComponent,
],
imports: [
CommonModule,
ReactiveFormsModule,
MaterialModule,
DurlModule,
],
exports: [
DatasourceOptionsComponent,
DatasourceFormComponent,
DatasourceRendererHostDirective,
DataDatasourceComponent,
DataSourceFormComponent,
DatasourceSourceComponent,
DatasourceSourceFormComponent,
],
})
export class DatasourceModule {}A dynamic form for managing datasources. It:
- Loads plugin editor components dynamically using the
DatasourceRendererHostDirective. - Handles renderer bindings and plugin-specific settings.
<classifieds-ui-datasource-form [datasource]="datasource"></classifieds-ui-datasource-form>Provides a form-based UI for configuring datasource options such as mappings, multiple selection, and limits.
<classifieds-ui-datasource-options></classifieds-ui-datasource-options>Renders settings and forms for an embedded datasource.
<classifieds-ui-datasource-source [settings]="settings"></classifieds-ui-datasource-source>Manages settings specific to the data plugin.
<classifieds-ui-data-datasource [settings]="settings"></classifieds-ui-data-datasource>Provides methods to fetch and post data to remote endpoints, while caching results for optimized performance.
constructor(private apiService: DatasourceApiService) {}
this.apiService.getData('https://example.com/data').subscribe(data => {
console.log(data);
});Dynamically registers and retrieves datasource plugins.
constructor(private pluginManager: DatasourcePluginManager) {}
pluginManager.getPlugins().subscribe(plugins => {
console.log(plugins);
});Recursively evaluates datasources and their bindings, resolving nested datasource relationships.
constructor(private evaluator: DatasourceEvaluator) {}
this.evaluator.evalDatasource({ datasource, metadata }).subscribe(dataset => {
console.log(dataset.results);
});- DatasourcePlugin: Represents a plugin's settings, editor component, and fetch method.
- Dataset: Structured data received from plugins.
- DatasourceOptions: Configuration options like query, mappings, and limits.
- Datasource: Core datasource model including plugin, renderer settings, and parameters.
The library includes predefined mocks for testing purposes:
Mock Datasource Options Example:
import { mockDatasourceOptions } from 'datasource';
const options = mockDatasourceOptions;You can configure datasources using DatasourceFormComponent.
<classifieds-ui-datasource-form [datasource]="datasource"></classifieds-ui-datasource-form>myDatasource: Datasource = {
plugin: 'data',
settings: [
{ name: 'query', value: 'SELECT * FROM my_table' },
{ name: 'limit', value: '100' },
],
};Register custom datasource plugins using DatasourcePluginManager.
pluginManager.register(
new DatasourcePlugin({
id: 'custom',
title: 'Custom Plugin',
editor: CustomEditorComponent,
fetch: (params) => of(new Dataset({ results: [] })),
})
);DatasourceFormComponentDatasourceOptionsComponentDatasourceSourceComponentDataDatasourceComponent
DatasourceApiServiceDatasourcePluginManagerDatasourceEvaluator
DatasourcePluginDatasourceDatasetDatasourceOptions
<classifieds-ui-datasource-form [datasource]="myDatasource"></classifieds-ui-datasource-form>myDatasource: Datasource = {
plugin: 'data',
settings: [
{ name: 'query', value: 'SELECT * FROM my_table' },
{ name: 'limit', value: '100' },
],
};Unit testing can be performed using Angular's testing utilities. Example test configuration:
describe('DatasourceApiService', () => {
let service: DatasourceApiService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(DatasourceApiService);
});
it('should fetch data', () => {
service.getData('https://test.com/data').subscribe((data) => {
expect(data).toBeTruthy();
});
});
});To execute tests:
npm run testThe Datasource Angular Library simplifies management, rendering, and evaluation of datasources in Angular applications. Its extensible plugin-based framework, reusable components, and powerful evaluators allow developers to build dynamic, scalable, and maintainable applications.
For contributions or feature requests, feel free to reach out!