Open a Session


  • A session object is obtained from the document store.

  • Various session options can be configured using the SessionOptions object.
    If no database is specified in the options then the default database is assumed.

  • Most methods on the session object are asynchronous and return a Promise.
    Either use async & await or .then() & callback functions.
    Refer to the specific documentation for each method usage.

  • In this page:


Syntax

  • Use openSession() to open a session from the document store.
  • The following overloads are available:

openSession();

openSession(database);

openSession(sessionOptions);
Parameter Type Description
database string The session will operate on this database,
overriding the default database set in the document store.
options SessionOptions An object with session configuration options. See details below.
Return Value Description
IDocumentSession Instance of a session object

Session options

  • The SessionOptions object contains various options to configure the Session's behavior.
Option Type Description Default Value
database string The session will operate on this database,
overriding the Default Database.
null - the session operates on the Default Database
noTracking boolean true - The session tracks changes made to all entities it loaded, stored, or queried for.
false - Tracking will be turned off.
Learn more in Disable tracking
false
noCaching boolean true - Server responses will Not be cached.
false - The session caches the server responses.
Learn more in Disable caching
false
requestExecutor RequestExecutor ( Advanced option )
The request executor the session should use.
null - the default request executor is used
transactionMode TransactionMode Specify the session's transaction mode
SingleNode / ClusterWide
Learn more in Cluster-wide vs. Single-node
SingleNode
  • Experts Only:
Option Type Description Default Value
disableAtomicDocumentWrites-
InClusterWideTransaction
boolean ( Experts only )
true - Disable Atomic-Guards in cluster-wide sessions.
false - Automatic atomic writes in cluster-wide sessions are enabled.
Learn more in Atomic-Guards
false

Open session example

  • The following example opens a cluster-wide Session:

// Open the session, pass the options object to the session
const session = documentStore.openSession({
    database: "your_database_name",
    transactionMode: "ClusterWide"
});

//   Run your business logic:
//
//   Store entities
//   Load and Modify entities
//   Query indexes & collections
//   Delete documents
//   ... etc.

// For example: load a document and modify it
// Note: 'load' returns a Promise and must be awaited
const entity = await session.load("companies/1-A");
entity.name = "NewCompanyName";

// Save you changes
// Note: 'saveChanges' also returns a Promise and must be awaited
await session.saveChanges();
// When 'SaveChanges' returns successfully,
// all changes made to the entities in the session are persisted to the documents in the database
// Open the session, pass the options object to the session
const session = documentStore.openSession({
    database: "your_database_name",
    transactionMode: "ClusterWide"
});

//   Run your business logic:
//
//   Store entities
//   Load and Modify entities
//   Query indexes & collections
//   Delete documents
//   ... etc.

// For example: load a document, modify it, and save
// Note: 'load' & 'saveChanges' each return a Promise that is then handled by callback functions
session.load("companies/1-A")
    .then((company) => {
        company.name = "NewCompanyName";
    })
    .then(() => session.saveChanges())
    .then(() => {
        // When 'SaveChanges' returns successfully,
        // all changes made to the entities in the session are persisted to the documents in the database
    });