From 733656f80605cbabc517e80fea28cf5e34bcdda4 Mon Sep 17 00:00:00 2001 From: danstarns Date: Wed, 28 Aug 2024 19:24:03 -0700 Subject: [PATCH] feat: add setRecords back --- docs/api/datasource.md | 11 ++++++++ src/data_sources/model/DataSource.ts | 16 +++++++++++ .../model/ComponentDataVariable.ts | 27 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/docs/api/datasource.md b/docs/api/datasource.md index 55597f473..a244fc1b6 100644 --- a/docs/api/datasource.md +++ b/docs/api/datasource.md @@ -106,6 +106,17 @@ If a transformer is provided for the `onRecordDelete` event, it will be applied 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. +If a transformer is provided for the `onRecordAdd` event, it will be applied to each record before adding it. + +### 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 diff --git a/src/data_sources/model/DataSource.ts b/src/data_sources/model/DataSource.ts index 9dae2b91c..4be2de3cf 100644 --- a/src/data_sources/model/DataSource.ts +++ b/src/data_sources/model/DataSource.ts @@ -177,4 +177,20 @@ export default class DataSource extends Model { return this.records.remove(id, opts); } + + /** + * Replaces the existing records in the data source with a new set of records. + * If a transformer is provided for the `onRecordAdd` event, it will be applied to each record before adding it. + * + * @param {Array} records - An array of data record properties to set. + * @returns {Array} An array of the added data records. + * @name setRecords + */ + setRecords(records: Array) { + this.records.reset([], { silent: true }); + + records.forEach((record) => { + this.records.add(record, { avoidTransformers: true }); + }); + } } diff --git a/test/specs/data_sources/model/ComponentDataVariable.ts b/test/specs/data_sources/model/ComponentDataVariable.ts index fea380ed6..2ac4766f5 100644 --- a/test/specs/data_sources/model/ComponentDataVariable.ts +++ b/test/specs/data_sources/model/ComponentDataVariable.ts @@ -124,6 +124,33 @@ describe('ComponentDataVariable', () => { expect(cmp.getEl()?.innerHTML).toContain('default'); }); + test('component updates on data source setRecords', () => { + const dataSource: DataSourceProps = { + id: 'component-setRecords', + records: [{ id: 'id1', name: 'init name' }], + }; + dsm.add(dataSource); + + const cmp = cmpRoot.append({ + tagName: 'div', + type: 'default', + components: [ + { + type: DataVariableType, + value: 'default', + path: `${dataSource.id}.id1.name`, + }, + ], + })[0]; + + expect(cmp.getEl()?.innerHTML).toContain('init name'); + + const ds = dsm.get(dataSource.id); + ds.setRecords([{ id: 'id1', name: 'updated name' }]); + + expect(cmp.getEl()?.innerHTML).toContain('updated name'); + }); + test('component updates on record removal', () => { const dataSource: DataSourceProps = { id: 'ds4',