[GH-ISSUE #10] Copying objects within DataProxy #12

Closed
opened 2026-05-23 08:36:25 -06:00 by gitea-mirror · 2 comments
Owner

Originally created by @appy-one on GitHub (Jun 11, 2021).
Original GitHub issue: https://github.com/appy-one/acebase-core/issues/10

Originally assigned to: @appy-one on GitHub.

When assigning a proxied object to another location within the DataProxy, making changes to it will also change the original in memory. It updates the db at the right location though, so there is a discrepancy between in-memory and db states.

Example:

const proxy = await db.ref('cars').proxy();
const cars = proxy.value;
// Copy sportscar to create a new racecar:
cars.racecar = products.sportscar;
// Make change to racecar:
cars.racecar.horsepower = 980;
if (cars.sportscar.horsepower === cars.racecar.horsepower) {
   // Not good: change was also made in original object
}

Note that this is normal javascript object behavior but should be prevented in this proxied database context.
The solution would be to clone the object at assignment.

Originally created by @appy-one on GitHub (Jun 11, 2021). Original GitHub issue: https://github.com/appy-one/acebase-core/issues/10 Originally assigned to: @appy-one on GitHub. When assigning a proxied object to another location within the DataProxy, making changes to it will also change the original in memory. It updates the db at the right location though, so there is a discrepancy between in-memory and db states. Example: ```js const proxy = await db.ref('cars').proxy(); const cars = proxy.value; // Copy sportscar to create a new racecar: cars.racecar = products.sportscar; // Make change to racecar: cars.racecar.horsepower = 980; if (cars.sportscar.horsepower === cars.racecar.horsepower) { // Not good: change was also made in original object } ``` Note that this is normal javascript object behavior but should be prevented in this proxied database context. The solution would be to clone the object at assignment.
gitea-mirror 2026-05-23 08:36:25 -06:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@appy-one commented on GitHub (Jun 11, 2021):

When cloning assigned objects, this will also fix unexpected situations where the database is not updated, but the in-memory value is:

const sportscar = { make: 'Ferrari', model: 'Testarossa', horsepower: 390 };
const proxy = await db.ref('cars').proxy();
const cars = proxy.value;
// Create sportscar:
cars.sportscar = sportscar;

// .. Later (other tick): ..
// Won't update database:
sportscar.top_speed = 290; // (km/h)
cars.sportscar.top_speed === 290; // true, but not in db

// Updates database and original object:
cars.sportscar.top_speed = 290;
sportscar.top_speed === 290; // true

When cloning the value, this will become the behavior:

// Won't update database:
sportscar.top_speed = 290;
cars.sportscar.top_speed === 290; // false

// Updates database but not the original object:
cars.sportscar.top_speed = 290;
sportscar.top_speed === 290; // false
<!-- gh-comment-id:859426515 --> @appy-one commented on GitHub (Jun 11, 2021): When cloning assigned objects, this will also fix unexpected situations where the database is not updated, but the in-memory value is: ```js const sportscar = { make: 'Ferrari', model: 'Testarossa', horsepower: 390 }; const proxy = await db.ref('cars').proxy(); const cars = proxy.value; // Create sportscar: cars.sportscar = sportscar; // .. Later (other tick): .. // Won't update database: sportscar.top_speed = 290; // (km/h) cars.sportscar.top_speed === 290; // true, but not in db // Updates database and original object: cars.sportscar.top_speed = 290; sportscar.top_speed === 290; // true ``` When cloning the value, this will become the behavior: ```js // Won't update database: sportscar.top_speed = 290; cars.sportscar.top_speed === 290; // false // Updates database but not the original object: cars.sportscar.top_speed = 290; sportscar.top_speed === 290; // false ```
Author
Owner

@appy-one commented on GitHub (Jul 14, 2021):

Fix published in acebase-core v1.5.0

<!-- gh-comment-id:879855415 --> @appy-one commented on GitHub (Jul 14, 2021): Fix published in `acebase-core` v1.5.0
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/acebase-core#12
No description provided.