|
|
@ -31,18 +31,15 @@ |
|
|
|
|
|
|
|
|
import { AddOptions, collectionEvents, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common'; |
|
|
import { AddOptions, collectionEvents, CombinedModelConstructorOptions, Model, RemoveOptions } from '../../common'; |
|
|
import EditorModel from '../../editor/model/Editor'; |
|
|
import EditorModel from '../../editor/model/Editor'; |
|
|
import { DataSourceProps } from '../types'; |
|
|
import { DataSourceTransformers, DataSourceType, DataSourceProps, RecordPropsType, DataRecordProps } from '../types'; |
|
|
import { DataSourceTransformers, DataSourceType, SingleRecordType } from '../types'; |
|
|
|
|
|
import DataRecord from './DataRecord'; |
|
|
import DataRecord from './DataRecord'; |
|
|
import DataRecords from './DataRecords'; |
|
|
import DataRecords from './DataRecords'; |
|
|
import DataSources from './DataSources'; |
|
|
import DataSources from './DataSources'; |
|
|
|
|
|
|
|
|
interface DataSourceOptions extends CombinedModelConstructorOptions<{ em: EditorModel }, DataSource> {} |
|
|
interface DataSourceOptions extends CombinedModelConstructorOptions<{ em: EditorModel }, DataSource> {} |
|
|
|
|
|
export default class DataSource<DRProps extends DataRecordProps = DataRecordProps> extends Model< |
|
|
export default class DataSource< |
|
|
DataSourceType<DRProps> |
|
|
DS extends DataSourceType = DataSourceType, |
|
|
> { |
|
|
DR extends SingleRecordType<DS['records']> = SingleRecordType<DS['records']>, |
|
|
|
|
|
> extends Model<DS> { |
|
|
|
|
|
transformers: DataSourceTransformers; |
|
|
transformers: DataSourceTransformers; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -56,7 +53,7 @@ export default class DataSource< |
|
|
return { |
|
|
return { |
|
|
records: [], |
|
|
records: [], |
|
|
transformers: {}, |
|
|
transformers: {}, |
|
|
} as unknown as Partial<DS>; |
|
|
} as unknown as DataSourceType<DRProps>; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -64,23 +61,23 @@ export default class DataSource< |
|
|
* It sets up the transformers and initializes the collection of records. |
|
|
* It sets up the transformers and initializes the collection of records. |
|
|
* If the `records` property is not an instance of `DataRecords`, it will be converted into one. |
|
|
* If the `records` property is not an instance of `DataRecords`, it will be converted into one. |
|
|
* |
|
|
* |
|
|
* @param {DataSourceProps} props - Properties to initialize the data source. |
|
|
* @param {DataSourceProps<DRProps>} props - Properties to initialize the data source. |
|
|
* @param {DataSourceOptions} opts - Options to initialize the data source. |
|
|
* @param {DataSourceOptions} opts - Options to initialize the data source. |
|
|
* @name constructor |
|
|
* @name constructor |
|
|
*/ |
|
|
*/ |
|
|
constructor(props: DataSourceProps<DS>, opts: DataSourceOptions) { |
|
|
constructor(props: DataSourceProps<DRProps>, opts: DataSourceOptions) { |
|
|
super( |
|
|
super( |
|
|
{ |
|
|
{ |
|
|
...props, |
|
|
...props, |
|
|
records: [], |
|
|
records: [], |
|
|
} as unknown as DS, |
|
|
} as unknown as DataSourceType<DRProps>, |
|
|
opts, |
|
|
opts, |
|
|
); |
|
|
); |
|
|
const { records, transformers } = props; |
|
|
const { records, transformers } = props; |
|
|
this.transformers = transformers || {}; |
|
|
this.transformers = transformers || ({} as DataSourceTransformers); |
|
|
|
|
|
|
|
|
if (!(records instanceof DataRecords)) { |
|
|
if (!(records instanceof DataRecords)) { |
|
|
this.set({ records: new DataRecords(records!, { dataSource: this }) } as Partial<DS>); |
|
|
this.set({ records: new DataRecords(records!, { dataSource: this }) } as Partial<DataSourceType<DRProps>>); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.listenTo(this.records, 'add', this.onAdd); |
|
|
this.listenTo(this.records, 'add', this.onAdd); |
|
|
@ -90,11 +87,11 @@ export default class DataSource< |
|
|
/** |
|
|
/** |
|
|
* Retrieves the collection of records associated with this data source. |
|
|
* Retrieves the collection of records associated with this data source. |
|
|
* |
|
|
* |
|
|
* @returns {DataRecords} The collection of data records. |
|
|
* @returns {DataRecords<DRProps>} The collection of data records. |
|
|
* @name records |
|
|
* @name records |
|
|
*/ |
|
|
*/ |
|
|
get records() { |
|
|
get records() { |
|
|
return this.attributes.records as NonNullable<DS['records']>; |
|
|
return this.attributes.records as NonNullable<DataRecords<DRProps>>; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
@ -111,23 +108,23 @@ export default class DataSource< |
|
|
* Handles the `add` event for records in the data source. |
|
|
* Handles the `add` event for records in the data source. |
|
|
* This method triggers a change event on the newly added record. |
|
|
* This method triggers a change event on the newly added record. |
|
|
* |
|
|
* |
|
|
* @param {DataRecord} dr - The data record that was added. |
|
|
* @param {DataRecord<DRProps>} dr - The data record that was added. |
|
|
* @private |
|
|
* @private |
|
|
* @name onAdd |
|
|
* @name onAdd |
|
|
*/ |
|
|
*/ |
|
|
onAdd(dr: DataRecord) { |
|
|
onAdd(dr: DataRecord<DRProps>) { |
|
|
dr.triggerChange(); |
|
|
dr.triggerChange(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Adds a new record to the data source. |
|
|
* Adds a new record to the data source. |
|
|
* |
|
|
* |
|
|
* @param {DataRecordProps} record - The properties of the record to add. |
|
|
* @param {DRProps} record - The properties of the record to add. |
|
|
* @param {AddOptions} [opts] - Options to apply when adding the record. |
|
|
* @param {AddOptions} [opts] - Options to apply when adding the record. |
|
|
* @returns {DataRecord} The added data record. |
|
|
* @returns {DataRecord} The added data record. |
|
|
* @name addRecord |
|
|
* @name addRecord |
|
|
*/ |
|
|
*/ |
|
|
addRecord(record: DR, opts?: AddOptions) { |
|
|
addRecord(record: DRProps, opts?: AddOptions) { |
|
|
return this.records.add(record, opts); |
|
|
return this.records.add(record, opts); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -135,18 +132,18 @@ export default class DataSource< |
|
|
* Retrieves a record from the data source by its ID. |
|
|
* Retrieves a record from the data source by its ID. |
|
|
* |
|
|
* |
|
|
* @param {string | number} id - The ID of the record to retrieve. |
|
|
* @param {string | number} id - The ID of the record to retrieve. |
|
|
* @returns {DataRecord | undefined} The data record, or `undefined` if no record is found with the given ID. |
|
|
* @returns {DataRecord<DRProps> | undefined} The data record, or `undefined` if no record is found with the given ID. |
|
|
* @name getRecord |
|
|
* @name getRecord |
|
|
*/ |
|
|
*/ |
|
|
getRecord(id: string | number) { |
|
|
getRecord(id: string | number) { |
|
|
return this.records.get(id) as DR | undefined; |
|
|
return this.records.get(id) as DataRecord | undefined; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Retrieves all records from the data source. |
|
|
* Retrieves all records from the data source. |
|
|
* Each record is processed with the `getRecord` method to apply any read transformers. |
|
|
* Each record is processed with the `getRecord` method to apply any read transformers. |
|
|
* |
|
|
* |
|
|
* @returns {Array<DataRecord | undefined>} An array of data records. |
|
|
* @returns {Array<DataRecord<DRProps> | undefined>} An array of data records. |
|
|
* @name getRecords |
|
|
* @name getRecords |
|
|
*/ |
|
|
*/ |
|
|
getRecords() { |
|
|
getRecords() { |
|
|
@ -158,10 +155,10 @@ export default class DataSource< |
|
|
* |
|
|
* |
|
|
* @param {string | number} id - The ID of the record to remove. |
|
|
* @param {string | number} id - The ID of the record to remove. |
|
|
* @param {RemoveOptions} [opts] - Options to apply when removing the record. |
|
|
* @param {RemoveOptions} [opts] - Options to apply when removing the record. |
|
|
* @returns {DataRecord | undefined} The removed data record, or `undefined` if no record is found with the given ID. |
|
|
* @returns {DataRecord<DRProps> | undefined} The removed data record, or `undefined` if no record is found with the given ID. |
|
|
* @name removeRecord |
|
|
* @name removeRecord |
|
|
*/ |
|
|
*/ |
|
|
removeRecord(id: string | number, opts?: RemoveOptions): DataRecord | undefined { |
|
|
removeRecord(id: string | number, opts?: RemoveOptions) { |
|
|
const record = this.getRecord(id); |
|
|
const record = this.getRecord(id); |
|
|
if (record?.mutable === false && !opts?.dangerously) { |
|
|
if (record?.mutable === false && !opts?.dangerously) { |
|
|
throw new Error('Cannot remove immutable record'); |
|
|
throw new Error('Cannot remove immutable record'); |
|
|
@ -173,11 +170,11 @@ export default class DataSource< |
|
|
/** |
|
|
/** |
|
|
* Replaces the existing records in the data source with a new set of records. |
|
|
* Replaces the existing records in the data source with a new set of records. |
|
|
* |
|
|
* |
|
|
* @param {Array<DataRecordProps>} records - An array of data record properties to set. |
|
|
* @param {Array<DRProps>} records - An array of data record properties to set. |
|
|
* @returns {Array<DataRecord>} An array of the added data records. |
|
|
* @returns {Array<DataRecord>} An array of the added data records. |
|
|
* @name setRecords |
|
|
* @name setRecords |
|
|
*/ |
|
|
*/ |
|
|
setRecords(records: DR[]) { |
|
|
setRecords(records: DRProps[]) { |
|
|
this.records.reset([], { silent: true }); |
|
|
this.records.reset([], { silent: true }); |
|
|
|
|
|
|
|
|
records.forEach((record) => { |
|
|
records.forEach((record) => { |
|
|
|