Client API: How to Setup a Default Database

database property allows you to setup a default database for a DocumentStore. Implication of setting up a default database is that each time you access operations or create a session without explicitly passing database on which they should operate on then default database is assumed.

Example I

// without specifying `database`
// we will need to specify the database in each action
// if no database is passed explicitly we will get an error

const store = new DocumentStore([ "http://localhost:8080" ]);
store.initialize();

{
    const session = store.openSession("Northwind");
    // ...
}

const compactSettings = { databaseName: "Northwind" };
await store.maintenance.server.send(
    new CompactDatabaseOperation(compactSettings));

Example II

// when `database` is set to `Northwind`
// created `operations` or opened `sessions`
// will work on `Northwind` database by default
// if no database is passed explicitly
const store =  new DocumentStore("http://localhost:8080", "Northwind");
store.initialize();

{
    const northwindSession = store.openSession();
    // ...
}

await store.maintenance.send(
    new DeleteIndexOperation("NorthwindIndex"));

{
    const adventureWorksSession = store.openSession("AdventureWorks");
    // ...
}

await store.maintenance.forDatabase("AdventureWorks")
    .send(new DeleteIndexOperation("AdventureWorksIndex"));

Remarks

Note

By default value of database property in DocumentStore is null which means that in any action requiring a database name, we will have to specify the database.