Skip to content

client.createDatabase()

Oxford Harrison edited this page Nov 10, 2024 · 9 revisions

Programmatically perform a CREATE DATABASE operation.

Syntax

client.createDatabase(
    createSpec: string | DatabaseSchemaJson,
    options?: CreateOptions
): Promise<Savepoint | boolean>;
type CreateOptions {
    [key: string]: any;
} & QueryOptions
Param Description
createSpec A database name, or an object specifying the intended database structure to create.
options Extra parameters for the query—which extends standard QueryOptions.

CreateOptions

Param Applicable to Description
ifNotExists A flag to conditionally create the database.

Return Value

  • A Savepoint instance (See ➞ Savepoint) or the boolean true when savepoint creation has been disabled via options.noCreateSavepoint; (Compare ➞ Query Return Value)

Usage

Specify database by name:

const savepoint = await client.createDatabase(
    'database_1',
    { desc: 'Just testing database creation' }
);

or by a schema object, optionally specifying a list of tables to create along with the database. (With each listed table corresponding to TableSchemaJson (in schema.json).):

const savepoint = await client.createDatabase(
    {
        name: 'database_1',
        tables: [{
            name: 'table_1'
            columns: [
                { name: 'column_1', type: 'int' }, 
                { name: 'column_2', type: 'time' }
            ]
        }]
    },
    { desc: 'Just testing database creation' }
);

Conditionally create using the options.ifNotExists parameter:

const savepoint = await client.createDatabase(
    'database_1',
    { desc: 'Just testing database creation', ifNotExists: true }
);
Clone this wiki locally