Skip to content

Commit

Permalink
for performance reasons, just do not allow directly assigning data in…
Browse files Browse the repository at this point in the history
… updater functions
  • Loading branch information
zetavg committed Nov 21, 2023
1 parent 5a6db8a commit 767e3f5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
6 changes: 4 additions & 2 deletions Data/lib/functions/getSaveDatum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ export default function getSaveDatum({
| [
T,
string,
(d: DataMeta<T> & { [key: string]: unknown }) => Partial<DataType<T>>,
(
d: Readonly<DataMeta<T> & { [key: string]: unknown }>,
) => Partial<DataType<T>>,
],
options: {
noTouch?: boolean;
Expand Down Expand Up @@ -171,7 +173,7 @@ export default function getSaveDatum({
const existingData = await getDatum(type, id);
if (!existingData) throw new Error(`Data not found: ${type} ${id}`);

const updatedData = updater(JSON.parse(JSON.stringify(existingData)));
const updatedData = updater(existingData);
const dataToSave: DataMeta<T> & { [key: string]: unknown } = {
...(existingData
? (Object.fromEntries(
Expand Down
4 changes: 3 additions & 1 deletion Data/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ export type SaveDatum = <T extends DataTypeName>(
| [
T,
string,
(d: DataMeta<T> & { [key: string]: unknown }) => Partial<DataType<T>>,
(
d: Readonly<DataMeta<T> & { [key: string]: unknown }>,
) => Partial<DataType<T>>,
],
options?: {
/** Set to true to not update the `__updated_at` field. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1654,40 +1654,40 @@ describe('saveDatum', () => {
});
});

it('updates the datum while the updater function directly assigns new value to the datum', async () => {
await withContext(async context => {
const d = new CouchDBData(context);

const collection = await d.saveDatum({
__type: 'collection',
name: 'Collection',
icon_name: 'box',
icon_color: 'gray',
collection_reference_number: '1',
});

const item = await d.saveDatum({
__type: 'item',
collection_id: collection.__id,
name: 'Item',
icon_name: 'cube-outline',
icon_color: 'gray',
});

await d.saveDatum([
'item',
item.__id || '',
datum => {
datum.name = 'Updated Item';
return datum;
},
]);

expect((await d.getDatum('item', item.__id || ''))?.name).toBe(
'Updated Item',
);
});
});
// it('updates the datum while the updater function directly assigns new value to the datum', async () => {
// await withContext(async context => {
// const d = new CouchDBData(context);

// const collection = await d.saveDatum({
// __type: 'collection',
// name: 'Collection',
// icon_name: 'box',
// icon_color: 'gray',
// collection_reference_number: '1',
// });

// const item = await d.saveDatum({
// __type: 'item',
// collection_id: collection.__id,
// name: 'Item',
// icon_name: 'cube-outline',
// icon_color: 'gray',
// });

// await d.saveDatum([
// 'item',
// item.__id || '',
// datum => {
// datum.name = 'Updated Item';
// return datum;
// },
// ]);

// expect((await d.getDatum('item', item.__id || ''))?.name).toBe(
// 'Updated Item',
// );
// });
// });

it('returns the saved datum', async () => {
await withContext(async context => {
Expand Down

0 comments on commit 767e3f5

Please sign in to comment.