---
title: "How to Check for Session Changes"
url: "https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session"
version: "6.2"
language: "nodejs"
---

# How to Check for Session Changes

> Client language: Node.js. Also available for [C#](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session.md), [Java](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session.java.md), [Python](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session.python.md), [PHP](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-there-are-any-changes-on-a-session.php.md).
>
> RavenDB publishes an official agent skill for coding assistants at https://docs.ravendb.net/skills/ravendb/SKILL.md (also installable with `npx skills add ravendb/skills`). The skill describes itself as: "Read this before writing any RavenDB query, client call or /databases/ request. Covers server setup, document modelling, the Node.js, .NET and Python clients, RQL, indexes, vector search and AI agents. Code written from recall tends to run and return the wrong rows, because RQL resembles SQL and the clients take field names as plain strings."

> **Note**
>
> - The Session [tracks all changes](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.nodejs.md#tracking-changes)
>   made to all the entities it has either loaded, stored, deleted, or queried for,  
>   and persists to the server only what is needed when `saveChanges()` is called.
>
> - This article describes how to check for changes made to **all** tracked entities within the session.  
>   To check for changes to a specific **entity**, see [Check for entity changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.nodejs.md).
>
> - To get the list of all entities tracked by the session, see [Get tracked entities](https://docs.ravendb.net/6.2/client-api/session/how-to/get-tracked-entities.nodejs.md).
>
> - In this page:
>   - [Check for session changes](#check-for-session-changes)
>   - [Get session changes](#get-session-changes)
>   - [Syntax](#syntax)

## Check for session changes

- The session's advanced property `hasChanges` indicates whether any entities were added, modified, or deleted within the session.

- Note: The *hasChanges* property is cleared after calling `saveChanges()`.

```javascript
const session = store.openSession();

// No changes made yet - 'hasChanges' will be FALSE
assert.ok(!session.advanced.hasChanges());

// Store a new entity within the session
const employee = new Employee();
employee.firstName = "John";
employee.lastName = "Doe";

await session.store(employee);

// 'hasChanges' will now be TRUE
assert.ok(session.advanced.hasChanges());

// 'hasChanges' will reset to FALSE after saving changes 
await session.saveChanges();
assert.ok(!session.advanced.hasChanges());
```

## Get session changes

- Use the session's advanced method `whatChanged()` to get all changes made to all the entities tracked by the session.

- For each entity that was modified, the details will include:
  - The name and path of the changed field
  - Its old and new values
  - The type of change

- Note: `whatChanged()` reports changes made prior to calling `saveChanges()`.  
  Calling it immediately after *saveChanges* will return no results, as the changes are cleared.

##### Example I

```javascript
const session = store.openSession();

// Store (add) new entities, they will be tracked by the session
const employee1 = new Employee();
employee1.firstName = "John";
employee1.lastName = "Doe";

const employee2 = new Employee();
employee2.firstName = "Jane";
employee2.lastName = "Doe";

await session.store(employee1, "employees/1-A");
await session.store(employee2, "employees/2-A");

// Call 'WhatChanged' to get all changes in the session
const changes = session.advanced.whatChanged();
assert.equal(Object.keys(changes).length, 2);  // 2 entities were added

// Get the change details for an entity, specify the entity ID
const changesForEmployee = changes["employees/1-A"];
assert.equal(Object.keys(changesForEmployee).length, 1); // a single change for this entity (adding)

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

await session.saveChanges();
```

##### Example II

```javascript
const session = store.openSession();

// Load the entities, they will be tracked by the session
const employee1 = await session.load("employees/1-A");
const employee2 = await session.load("employees/2-A");

// Modify entities
employee1.firstName = "Jim";
employee1.lastName = "Brown";
employee2.lastName = "Smith";

// Delete an entity
session.delete(employee2);

// Call 'WhatChanged' to get all changes in the session
const changes = session.advanced.whatChanged();

// Get the change details for an entity, specify the entity ID
let changesForEmployee = changes["employees/1-A"];

assert.equal(changesForEmployee[0].fieldName, "firstName");  // Field name
assert.equal(changesForEmployee[0].fieldNewValue, "Jim");    // New value
assert.equal(changesForEmployee[0].change, "FieldChanged");  // Change type

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

// Note: for employee2 - even though the LastName was changed to 'Smith',
// the only reported change is the latest modification, which is the delete action. 
changesForEmployee = changes["employees/2-A"];
assert.equal(changesForEmployee[0].change, "DocumentDeleted");

await session.saveChanges();
```

## Syntax

```javascript
// HasChanges
session.advanced.hasChanges();
```

```javascript
// WhatChanged
session.advanced.whatChanged();
```

| Return value | |
| - | - |
| `Record<string, DocumentsChanges>` | Dictionary containing list of changes per document ID |

| `DocumentsChanges` | Type | Description |
| - | - | - |
| **fieldOldValue** | object | Previous field value |
| **fieldNewValue** | object | Current field value |
| **change** | string | Type of change that occurred. Can be: <br />`"DocumentDeleted"`, `"DocumentAdded"`,`"FieldChanged"`, `"NewField"`, `"RemovedField"`, `"ArrayValueChanged"`, `"ArrayValueAdded"`, `"ArrayValueRemoved"` |
| **fieldName** | string | Name of field on which the change occurred |
| **fieldPath** | string | Path of field on which the change occurred |
| **fieldFullName** | string | Path + Name of field on which the change occurred |

## See also

- [What is a session and how does it work](https://docs.ravendb.net/6.2/client-api/session/what-is-a-session-and-how-does-it-work.nodejs.md)
- [How to check for entity changes](https://docs.ravendb.net/6.2/client-api/session/how-to/check-if-entity-has-changed.nodejs.md)
- [Evict entity from session](https://docs.ravendb.net/6.2/client-api/session/how-to/evict-entity-from-a-session.nodejs.md)
- [Refresh entity](https://docs.ravendb.net/6.2/client-api/session/how-to/refresh-entity.nodejs.md)
