Operations: Server: How to Create a Database

Create a new database on a server.

Syntax

public CreateDatabaseOperation(DatabaseRecord databaseRecord)

public CreateDatabaseOperation(DatabaseRecord databaseRecord, int replicationFactor)
Parameters Type Description
databaseRecord DatabaseRecord Instance of DatabaseRecord containing database configuration
replicationFactor int Indicates how many nodes should contain the database

DatabaseRecord

DatabaseRecord is a collection of database configurations.

Constructor

Name Description
DatabaseRecord(string databaseName) Initialize a new database record

Properties

Name Type Description
disabled boolean (default: false) Disable the database
encrypted boolean (default: false) Enable database Encryption
deletionInProgress Map<String, DeletionInProgress> Mark the deletion of the database from specific nodes
topology DatabaseTopology Topology is 'null' by default. The server will decide on which nodes to place the database according to the Replication Factor
conflictSolverConfig ConflictSolver Specify the strategy to resolve Conflicts
indexes Map<String, IndexDefinition> Define Indexes
autoIndexes Map<String, AutoIndexDefinition> Define Auto Indexes
settings Map<String, String> Provide Configuration settings
revisions RevisionsConfiguration Set Revision configuration
expiration ExpirationConfiguration Set Expiration configuration
ravenConnectionStrings Map<String, RavenConnectionString> Add Raven Connection String
sqlConnectionStrings Map<String, SqlConnectionString> Add SQL Connection String
periodicBackups List<PeriodicBackupConfiguration> Add Backup tasks
externalReplications List<ExternalReplications> Add External Replication tasks
ravenEtls List<RavenEtlConfiguration> Add Raven ETL tasks
sqlEtls List<SqlEtlConfiguration> Add SQL ETL tasks
client ClientConfiguration Set Client Configuration

Remarks

If Topology is specified, the replicationFactor will be ignored.

Example

DatabaseRecord databaseRecord = new DatabaseRecord();
databaseRecord.setDatabaseName("MyNewDatabase");
store.maintenance().server().send(new CreateDatabaseOperation(databaseRecord));

Information

To ensure that a database exists before creating it we can use the following example:

Example - EnsureDatabaseExists

public void ensureDatabaseExists(IDocumentStore store, String database, boolean createDatabaseIfNotExists) {
    database = ObjectUtils.firstNonNull(database, store.getDatabase());

    if (StringUtils.isBlank(database)) {
        throw new IllegalArgumentException("Value cannot be null or whitespace");
    }

    try {
        store.maintenance().forDatabase(database).send(new GetStatisticsOperation());
    } catch (DatabaseDoesNotExistException e) {
        if (!createDatabaseIfNotExists) {
            throw e;
        }

        try {
            DatabaseRecord databaseRecord = new DatabaseRecord();
            databaseRecord.setDatabaseName(database);
            store.maintenance().server().send(new CreateDatabaseOperation(databaseRecord));
        } catch (ConcurrencyException ce) {
            // The database was already created before calling CreateDatabaseOperation
        }
    }
}

Note

The creation of a database requires an admin certificate