How to Check for Entity Changes



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().


const session = documentStore.openSession();

// 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

Get entity changes

  • Use the session's advanced method whatChangedFor() to get all changes made to the specified entity
    within the session.

  • Details will include:

    • The name and path of the changed field
    • Its old and new values
    • The type of change
  • Note: whatChangedFor() reports changes made prior to calling saveChanges().
    Calling it immediately after saveChanges will return no results, since all changes are cleared at that point.


Example I

const session = documentStore.openSession();

// Store (add) a new entity, it will be tracked by the session
let employee = new Employee();
employee.firstName = "John";
employee.lastName = "Doe";
await session.store(employee, "employees/1-A");

// Get the changes for the entity in the session
// Call 'whatChangedFor', pass the entity object in the param
const changesForEmployee = session.advanced.whatChangedFor(employee);

// Assert there was a single change for this entity
assert.equal(changesForEmployee.length, 1);

// Get the change type
const changeType = changesForEmployee[0].change;
assert.equal(changeType, "DocumentAdded");

await session.saveChanges();
Example II

const session = documentStore.openSession();

// Load the entity, it will be tracked by the session
const employee = await session.load("employees/1-A");

// Modify the entity
employee.firstName = "Jim";
employee.lastName = "Brown";

// Get the changes for the entity in the session
// Call 'whatChangedFor', pass the entity object in the param
const changesForEmployee = session.advanced.whatChangedFor(employee);

assert.equal(changesForEmployee[0].fieldName, "firstName");
assert.equal(changesForEmployee[0].fieldNewValue, "Jim");
assert.equal(changesForEmployee[0].change, "FieldChanged");

assert.equal(changesForEmployee[1].fieldName, "lastName");
assert.equal(changesForEmployee[1].fieldNewValue, "Brown");
assert.equal(changesForEmployee[1].change, "FieldChanged");

Syntax

session.advanced.hasChanged(entity);
Return value
boolean true - modifications were made to the entity in this session.
false - no modifications were made.

session.advanced.whatChangedFor(entity);
Return value
DocumentsChanges[] List of changes made to the entity

class DocumentsChanges {
    // Previous field value
    fieldOldValue; // object

    // Current field value
    fieldNewValue; // object        
    
    // Name of field on which the change occurred
    fieldName; // string

    // Path of field on which the change occurred
    fieldPath; // string

    // Path + Name of field on which the change occurred
    fieldFullName; // string

    // Type of change that occurred, can be: 
    //     "DocumentDeleted"
    //     "DocumentAdded"
    //     "FieldChanged"
    //     "NewField"
    //     "RemovedField"
    //     "ArrayValueChanged"
    //     "ArrayValueAdded"
    //     "ArrayValueRemoved"
    change; // string
}