How to Check for Entity Changes
-
The Session tracks all changes made to all entities that it has either loaded, stored, or queried for,
and persists to the server only what is needed whensaveChanges()
is called. -
This article describes how to check for changes made in a specific entity within a session.
To check for changes on all tracked entities, see Check for session changes. -
In this page:
Check for entity changes
-
The session's advanced property
hasChanged
indicates whether the specified entity was added, modified, or deleted within the session. -
Note: The hasChanged property is cleared after calling
saveChanges()
.
// Store a new entity within the session
// =====================================
let employee = new Employee();
employee.firstName = "John";
employee.lastName = "Doe";
await session.store(employee, "employees/1-A");
// 'hasChanged' will be TRUE
assert.ok(session.advanced.hasChanged(employee));
// 'hasChanged' will reset to FALSE after saving changes
await session.saveChanges();
assert.ok(!session.advanced.hasChanged(employee));
// Load & modify entity within the session
// =======================================
employee = await session.load("employees/1-A");
assert.ok(!session.advanced.hasChanged(employee)); // FALSE
employee.lastName = "Brown";
assert.ok(session.advanced.hasChanged(employee)); // TRUE
await session.saveChanges();
assert.ok(!session.advanced.hasChanged(employee)); // FALSE
Syntax
session.advanced.hasChanged(entity);