Skip to content

database.createTable()

Oxford Harrison edited this page Nov 15, 2024 · 7 revisions

DOCSAPIDatabase API


Programmatically perform a CREATE TABLE operation.

See related ➞ CREATE TABLE

Syntax

database.createTable(
    createSpec: string | TableSchemaJson,
    options?: CreateOptions
): Promise<CreateTableResult>;
Param Interfaces Description
createSpec TableSchemaJson A table name, or an object specifying the intended table structure to create.
options? CreateOptions Optional extra parameters for the query.

CreateOptions

type CreateOptions = {
    ifNotExists?: boolean;
} & QueryOptions;
Param Interfaces Description
ifNotExists? - An optional flag that adds an EXISTS check to the operation. Defaults to false.
Interface Description
QueryOptions Inherited options.

CreateTableResult

type CreateTableResult = Table | TableSchema | Savepoint | null;
Type Interfaces Description
Table Table For a CREATE operation without a RETURNING clause—an instance of Table on the just created object.
TableSchema TableSchema For an operation with a RETURNING clause set to SCHEMA-the resulting database schema instance.
Savepoint | null Savepoint For an operation with a RETURNING clause set to SAVEPOINT-the Savepoint instance associated with the DDL operation; null when savepoint creation has been disabled at the server level.

Usage

See examples ➞ CREATE TABLE

Call patterns

Specify table by name:

const table = await database.createTable(
    'table_1',
    { desc: 'Create description' }
);

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

const table = await database.createTable(
    {
        name: 'table_1'
        columns: [
            { name: 'column_1', type: 'int' }, 
            { name: 'column_2', type: 'time' }
        ]
    },
    { desc: 'Create description' }
);
Clone this wiki locally