## DataSource The `DataSource` class represents a data source within the editor. It manages a collection of data records and provides methods to interact with them. The `DataSource` can be extended with transformers to modify records during add, read, and delete operations. ### DataSource API * [addRecord][1] * [getRecord][2] * [getRecords][3] * [removeRecord][4] ### Example of Usage ```js const dataSource = new DataSource({ records: [ { id: 'id1', name: 'value1' }, { id: 'id2', name: 'value2' } ], }, { em: editor }); dataSource.addRecord({ id: 'id3', name: 'value3' }); ``` ### Parameters * `props` **DataSourceProps** Properties to initialize the data source. * `opts` **DataSourceOptions** Options to initialize the data source. ### hasProvider Indicates if the data source has a provider for records. ### getResolvedRecords Retrieves all records from the data source with resolved relations based on the schema. ### upSchema Update the schema. #### Parameters * `schema` **Partial\** * `opts` **SetOptions?** #### Examples ```javascript dataSource.upSchema({ name: { type: 'string' } }); ``` ### getSchemaField Get schema field definition. #### Parameters * `fieldKey` **any** #### Examples ```javascript const fieldSchema = dataSource.getSchemaField('name'); fieldSchema.type; // 'string' ``` ## defaults Returns the default properties for the data source. These include an empty array of records and an empty object of transformers. Returns **[Object][5]** The default attributes for the data source. ## constructor Initializes a new instance of the `DataSource` class. 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. ### Parameters * `props` **DataSourceProps\** Properties to initialize the data source. * `opts` **DataSourceOptions** Options to initialize the data source. ## records Retrieves the collection of records associated with this data source. Returns **DataRecords\** The collection of data records. ## records Retrieves the collection of records associated with this data source. Returns **DataRecords\** The collection of data records. ## em Retrieves the editor model associated with this data source. Returns **EditorModel** The editor model. ## addRecord Adds a new record to the data source. ### Parameters * `record` **DRProps** The properties of the record to add. * `opts` **AddOptions?** Options to apply when adding the record. Returns **DataRecord** The added data record. ## getRecord Retrieves a record from the data source by its ID. ### Parameters * `id` **([string][6] | [number][7])** The ID of the record to retrieve. Returns **(DataRecord\ | [undefined][8])** The data record, or `undefined` if no record is found with the given ID. ## getRecords Retrieves all records from the data source. Each record is processed with the `getRecord` method to apply any read transformers. Returns **[Array][9]<(DataRecord\ | [undefined][8])>** An array of data records. ## removeRecord Removes a record from the data source by its ID. ### Parameters * `id` **([string][6] | [number][7])** The ID of the record to remove. * `opts` **RemoveOptions?** Options to apply when removing the record. Returns **(DataRecord\ | [undefined][8])** The removed data record, or `undefined` if no record is found with the given ID. ## setRecords Replaces the existing records in the data source with a new set of records. ### Parameters * `records` **[Array][9]\** An array of data record properties to set. Returns **[Array][9]\** An array of the added data records. [1]: #addrecord [2]: #getrecord [3]: #getrecords [4]: #removerecord [5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object [6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String [7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number [8]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/undefined [9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array