From e8ec6e749e3df4b8ee6a6f0c1eefb94335cb4b6a Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 5 Nov 2024 18:06:57 +0400 Subject: [PATCH] Improve DataSource TS (#6294) * Improve DataSource TS * Datasource types (#6295) * Add datasource types * Refactor types * Remove unused type --------- Co-authored-by: mohamedsalem401 --------- Co-authored-by: mohamedsalem401 --- .../src/data_sources/model/DataRecords.ts | 2 +- .../core/src/data_sources/model/DataSource.ts | 35 ++++++++++++------- packages/core/src/data_sources/types.ts | 17 +++++---- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/packages/core/src/data_sources/model/DataRecords.ts b/packages/core/src/data_sources/model/DataRecords.ts index 87a172dae3..1eadfcc220 100644 --- a/packages/core/src/data_sources/model/DataRecords.ts +++ b/packages/core/src/data_sources/model/DataRecords.ts @@ -3,7 +3,7 @@ import { DataRecordProps } from '../types'; import DataRecord from './DataRecord'; import DataSource from './DataSource'; -export default class DataRecords extends Collection { +export default class DataRecords extends Collection> { dataSource: DataSource; constructor(models: DataRecord[] | DataRecordProps[], options: { dataSource: DataSource }) { diff --git a/packages/core/src/data_sources/model/DataSource.ts b/packages/core/src/data_sources/model/DataSource.ts index 858758be5b..618ee30688 100644 --- a/packages/core/src/data_sources/model/DataSource.ts +++ b/packages/core/src/data_sources/model/DataSource.ts @@ -31,14 +31,18 @@ import { AddOptions, collectionEvents, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common'; import EditorModel from '../../editor/model/Editor'; -import { DataRecordProps, DataSourceProps, DataSourceTransformers } from '../types'; +import { DataSourceProps } from '../types'; +import { DataSourceTransformers, DataSourceType, SingleRecordType } from '../types'; import DataRecord from './DataRecord'; import DataRecords from './DataRecords'; import DataSources from './DataSources'; interface DataSourceOptions extends CombinedModelConstructorOptions<{ em: EditorModel }, DataSource> {} -export default class DataSource extends Model { +export default class DataSource< + DS extends DataSourceType = DataSourceType, + DR extends SingleRecordType = SingleRecordType, +> extends Model { transformers: DataSourceTransformers; /** @@ -52,7 +56,7 @@ export default class DataSource extends Model { return { records: [], transformers: {}, - }; + } as unknown as Partial; } /** @@ -64,13 +68,19 @@ export default class DataSource extends Model { * @param {DataSourceOptions} opts - Options to initialize the data source. * @name constructor */ - constructor(props: DataSourceProps, opts: DataSourceOptions) { - super(props, opts); + constructor(props: DataSourceProps, opts: DataSourceOptions) { + super( + { + ...props, + records: [], + } as unknown as DS, + opts, + ); const { records, transformers } = props; this.transformers = transformers || {}; if (!(records instanceof DataRecords)) { - this.set({ records: new DataRecords(records!, { dataSource: this }) }); + this.set({ records: new DataRecords(records!, { dataSource: this }) } as Partial); } this.listenTo(this.records, 'add', this.onAdd); @@ -84,7 +94,7 @@ export default class DataSource extends Model { * @name records */ get records() { - return this.attributes.records as DataRecords; + return this.attributes.records as NonNullable; } /** @@ -117,7 +127,7 @@ export default class DataSource extends Model { * @returns {DataRecord} The added data record. * @name addRecord */ - addRecord(record: DataRecordProps, opts?: AddOptions) { + addRecord(record: DR, opts?: AddOptions) { return this.records.add(record, opts); } @@ -128,9 +138,8 @@ export default class DataSource extends Model { * @returns {DataRecord | undefined} The data record, or `undefined` if no record is found with the given ID. * @name getRecord */ - getRecord(id: string | number): DataRecord | undefined { - const record = this.records.get(id); - return record; + getRecord(id: string | number) { + return this.records.get(id) as DR | undefined; } /** @@ -141,7 +150,7 @@ export default class DataSource extends Model { * @name getRecords */ getRecords() { - return [...this.records.models].map((record) => this.getRecord(record.id)); + return [...this.records.models].map((record) => this.getRecord(record.id)!); } /** @@ -168,7 +177,7 @@ export default class DataSource extends Model { * @returns {Array} An array of the added data records. * @name setRecords */ - setRecords(records: Array) { + setRecords(records: DR[]) { this.records.reset([], { silent: true }); records.forEach((record) => { diff --git a/packages/core/src/data_sources/types.ts b/packages/core/src/data_sources/types.ts index f1873fd3ec..ec9508c7a9 100644 --- a/packages/core/src/data_sources/types.ts +++ b/packages/core/src/data_sources/types.ts @@ -1,4 +1,4 @@ -import { ObjectAny } from '../common'; +import { Collection, ObjectAny } from '../common'; import ComponentDataVariable from './model/ComponentDataVariable'; import DataRecord from './model/DataRecord'; import DataRecords from './model/DataRecords'; @@ -24,17 +24,12 @@ export interface DataVariableListener { event: string; } -export interface DataSourceProps { +interface BaseDataSource { /** * DataSource id. */ id: string; - /** - * DataSource records. - */ - records?: DataRecords | DataRecord[] | DataRecordProps[]; - /** * DataSource validation and transformation factories. */ @@ -45,6 +40,14 @@ export interface DataSourceProps { */ skipFromStorage?: boolean; } +export interface DataSourceType extends BaseDataSource { + records: DataRecords; +} +export interface DataSourceProps extends BaseDataSource { + records?: DataRecords> | DataRecord>[] | ExtractRecordType[]; +} +export type ExtractRecordType = T extends { records: DataRecords } ? DR : never; +export type SingleRecordType = T extends Collection ? U : never; export interface DataSourceTransformers { onRecordSetValue?: (args: { id: string | number; key: string; value: any }) => any;