|
|
@ -16,24 +16,40 @@ export default class DataSourceManager extends ItemManagerModule<ModuleConfig, D |
|
|
super(em, 'DataSources', new DataSources([], em), DataSourcesEvents); |
|
|
super(em, 'DataSources', new DataSources([], em), DataSourcesEvents); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
getValue(key: string | string[], defValue: any) { |
|
|
/** |
|
|
return get(this.getContext(), key, defValue); |
|
|
* Add new data source. |
|
|
} |
|
|
* @param {Object} props Data source properties. |
|
|
|
|
|
* @returns {[DataSource]} Added data source. |
|
|
|
|
|
* @example |
|
|
|
|
|
* const ds = dsm.add({ |
|
|
|
|
|
* id: 'my_data_source_id', |
|
|
|
|
|
* records: [ |
|
|
|
|
|
* { id: 'id1', name: 'value1' }, |
|
|
|
|
|
* { id: 'id2', name: 'value2' } |
|
|
|
|
|
* ] |
|
|
|
|
|
* }); |
|
|
|
|
|
*/ |
|
|
add(props: DataSourceProps, opts: AddOptions = {}) { |
|
|
add(props: DataSourceProps, opts: AddOptions = {}) { |
|
|
const { all } = this; |
|
|
const { all } = this; |
|
|
props.id = props.id || this._createId(); |
|
|
props.id = props.id || this._createId(); |
|
|
return all.add(props, opts); |
|
|
return all.add(props, opts); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Get data source. |
|
|
|
|
|
* @param {String} id Data source id. |
|
|
|
|
|
* @returns {[DataSource]} Data source. |
|
|
|
|
|
* @example |
|
|
|
|
|
* const ds = dsm.get('my_data_source_id'); |
|
|
|
|
|
*/ |
|
|
get(id: string) { |
|
|
get(id: string) { |
|
|
return this.all.get(id); |
|
|
return this.all.get(id); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Remove data source. |
|
|
* Remove data source. |
|
|
* @param {String|[DataSource]} id Id of the data source |
|
|
* @param {String|[DataSource]} id Id of the data source. |
|
|
* @returns {[DataSource]} Removed data source |
|
|
* @returns {[DataSource]} Removed data source. |
|
|
* @example |
|
|
* @example |
|
|
* const removed = dsm.remove('DS_ID'); |
|
|
* const removed = dsm.remove('DS_ID'); |
|
|
*/ |
|
|
*/ |
|
|
@ -41,6 +57,17 @@ export default class DataSourceManager extends ItemManagerModule<ModuleConfig, D |
|
|
return this.__remove(id, opts); |
|
|
return this.__remove(id, opts); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* Get value from data sources by key |
|
|
|
|
|
* @param {String} key Path to value. |
|
|
|
|
|
* @param {any} defValue |
|
|
|
|
|
* @returns {any} |
|
|
|
|
|
* const value = dsm.getValue('ds_id.record_id.propName', 'defaultValue'); |
|
|
|
|
|
*/ |
|
|
|
|
|
getValue(key: string | string[], defValue: any) { |
|
|
|
|
|
return get(this.getContext(), key, defValue); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
getContext() { |
|
|
getContext() { |
|
|
return this.all.reduce((acc, ds) => { |
|
|
return this.all.reduce((acc, ds) => { |
|
|
acc[ds.id] = ds.records.reduce((accR, dr, i) => { |
|
|
acc[ds.id] = ds.records.reduce((accR, dr, i) => { |
|
|
|