Disable Entity Tracking
-
By default, each session tracks changes to all entities it has either stored, loaded, or queried for.
All changes are then persisted whensaveChanges
is called. -
Tracking can be disabled by any of the following:
Disable tracking a specific entity in session
- Tracking can be disabled for a specific entity in the session.
-
Once tracking is disabled for the entity then:
- Any changes made to this entity will be ignored for
saveChanges
. - Performing another
load
for this entity will Not generate another call to the server.
- Any changes made to this entity will be ignored for
Example
const session = documentStore.openSession();
// Load a product entity, the session will track its changes
const product = await session.load("products/1-A");
// Disable tracking for the loaded product entity
session.advanded.ignoreChangesFor(product);
// The following change will be ignored for saveChanges
product.unitsInStock += 1;
await session.saveChanges();
Syntax
session.advanced.ignoreChangesFor(entity);
Parameters | Type | Description |
---|---|---|
entity | object |
Instance of entity for which changes will be ignored |
Disable tracking all entities in session
- Tracking can be disabled for all entities in the session's options.
-
When tracking is disabled for the session:
- Method
store
will Not be available (an exception will be thrown if used). - Calling
load
orquery
will generate a call to the server and create new entities instances.
- Method
const session = documentStore.openSession({
// Disable tracking for all entities in the session's options
noTracking: true
});
// Load any entity, it will Not be tracked by the session
const employee1 = await session.load("employees/1-A");
// Loading again from same document will result in a new entity instance
const employee2 = await session.load("employees/1-A");
// Entities instances are not the same
assert.notStrictEqual(company1, company2);
// Calling saveChanges will throw an exception
await session.saveChanges();
Disable tracking query results
- Tracking can be disabled for all entities resulting from a query.
const session = documentStore.openSession();
// Define a query
const employeesResults = await session.query({ collection: "employees" })
.whereEquals("FirstName", "Robert")
// Set noTracking, all resulting entities will not be tracked
.noTracking()
.all();
// The following modification will not be tracked for saveChanges
const firstEmployee = employeesResults[0];
firstEmployee.lastName = "NewName";
// Change to 'firstEmployee' will not be persisted
session.saveChanges();
Customize tracking in conventions
- You can further customize and fine-tune which entities will not be tracked
by configuring theshouldIgnoreEntityChanges
convention method on the document store. - This customization rule will apply to all sessions opened for this document store.
Example
const customStore = new DocumentStore();
// Define the 'ignore' convention on your document store
customStore.conventions.shouldIgnoreEntityChanges =
(sessionOperations, entity, documentId) => {
// Define for which entities tracking should be disabled
// Tracking will be disabled ONLY for entities of type Employee whose firstName is Bob
return entity instanceof Employee && entity.firstName === "Bob";
};
customStore.initialize();
const session = customStore.openSession();
const employee1 = new Employee();
employee1.firstName = "Alice";
const employee2 = new Employee();
employee2.firstName = "Bob";
await session.store(employee1, "employees/1-A"); // This entity will be tracked
await session.store(employee2, "employees/2-A"); // Changes to this entity will be ignored
await session.saveChanges(); // Only employee1 will be persisted
employee1.firstName = "Bob"; // Changes to this entity will now be ignored
employee2.firstName = "Alice"; // This entity will now be tracked
session.saveChanges(); // Only employee2 is persisted
Syntax
store.conventions.shouldIgnoreEntityChanges = (sessionOperations, entity, documentId) => {
// Write your logic
// return value:
// true - entity will not be tracked
// false - entity will be tracked
}
Parameter | Type | Description |
---|---|---|
sessionOperations | InMemoryDocumentSessionOperations |
The session for which tracking is to be disabled |
entity | object |
The entity for which tracking is to be disabled |
documentId | string |
The entity's document ID |
Return Type | Description |
---|---|
boolean |
true - Entity will Not be trackedfalse - Entity will be tracked |