DDLManager
DDL (Data Definition Language) Manager
Provides methods for managing database schemas and tables programmatically. This includes creating schemas, creating tables with custom columns, listing schemas and tables, and deleting tables.
Example
Section titled “Example”const ddl = client.admin.ddl
// Create a new schemaawait ddl.createSchema('analytics')
// Create a table with columnsawait ddl.createTable('analytics', 'events', [ { name: 'id', type: 'UUID', primaryKey: true, defaultValue: 'gen_random_uuid()' }, { name: 'user_id', type: 'UUID', nullable: false }, { name: 'event_name', type: 'TEXT', nullable: false }, { name: 'event_data', type: 'JSONB' }, { name: 'created_at', type: 'TIMESTAMPTZ', defaultValue: 'NOW()' }])
// List all schemasconst { schemas } = await ddl.listSchemas()
// List all tables in a schemaconst { tables } = await ddl.listTables('analytics')
// Delete a tableawait ddl.deleteTable('analytics', 'events')Constructors
Section titled “Constructors”new DDLManager()
Section titled “new DDLManager()”new DDLManager(
fetch):DDLManager
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
fetch | FluxbaseFetch |
Returns
Section titled “Returns”Methods
Section titled “Methods”createSchema()
Section titled “createSchema()”createSchema(
name):Promise<CreateSchemaResponse>
Create a new database schema
Creates a new schema in the database. Schemas are used to organize tables into logical groups and provide namespace isolation.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
name | string | Schema name (must be valid PostgreSQL identifier) |
Returns
Section titled “Returns”Promise<CreateSchemaResponse>
Promise resolving to CreateSchemaResponse
Example
Section titled “Example”// Create a schema for analytics dataconst result = await client.admin.ddl.createSchema('analytics')console.log(result.message) // "Schema created successfully"console.log(result.schema) // "analytics"createTable()
Section titled “createTable()”createTable(
schema,name,columns):Promise<CreateTableResponse>
Create a new table in a schema
Creates a new table with the specified columns. Supports various column options including primary keys, nullability, data types, and default values.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
schema | string | Schema name where the table will be created |
name | string | Table name (must be valid PostgreSQL identifier) |
columns | CreateColumnRequest[] | Array of column definitions |
Returns
Section titled “Returns”Promise<CreateTableResponse>
Promise resolving to CreateTableResponse
Examples
Section titled “Examples”// Create a users tableawait client.admin.ddl.createTable('public', 'users', [ { name: 'id', type: 'UUID', primaryKey: true, defaultValue: 'gen_random_uuid()' }, { name: 'email', type: 'TEXT', nullable: false }, { name: 'name', type: 'TEXT' }, { name: 'created_at', type: 'TIMESTAMPTZ', nullable: false, defaultValue: 'NOW()' }])// Create a products table with JSONB metadataawait client.admin.ddl.createTable('public', 'products', [ { name: 'id', type: 'SERIAL', primaryKey: true }, { name: 'name', type: 'TEXT', nullable: false }, { name: 'price', type: 'DECIMAL(10,2)', nullable: false }, { name: 'metadata', type: 'JSONB' }, { name: 'in_stock', type: 'BOOLEAN', defaultValue: 'true' }])deleteTable()
Section titled “deleteTable()”deleteTable(
schema,name):Promise<DeleteTableResponse>
Delete a table from a schema
Permanently deletes a table and all its data. This operation cannot be undone.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
schema | string | Schema name containing the table |
name | string | Table name to delete |
Returns
Section titled “Returns”Promise<DeleteTableResponse>
Promise resolving to DeleteTableResponse
Examples
Section titled “Examples”// Delete a tableconst result = await client.admin.ddl.deleteTable('public', 'old_data')console.log(result.message) // "Table deleted successfully"// Safe deletion with confirmationconst confirm = await askUser('Are you sure you want to delete this table?')if (confirm) { await client.admin.ddl.deleteTable('analytics', 'events') console.log('Table deleted')}listSchemas()
Section titled “listSchemas()”listSchemas():
Promise<ListSchemasResponse>
List all database schemas
Retrieves a list of all schemas in the database. This includes both system schemas (like ‘public’, ‘pg_catalog’) and user-created schemas.
Returns
Section titled “Returns”Promise<ListSchemasResponse>
Promise resolving to ListSchemasResponse
Example
Section titled “Example”const { schemas } = await client.admin.ddl.listSchemas()
schemas.forEach(schema => { console.log(`Schema: ${schema.name}, Owner: ${schema.owner}`)})listTables()
Section titled “listTables()”listTables(
schema?):Promise<ListTablesResponse>
List all tables in the database or a specific schema
Retrieves a list of all tables. If a schema is specified, only tables from that schema are returned. Otherwise, all tables from all schemas are returned.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
schema? | string | Optional schema name to filter tables |
Returns
Section titled “Returns”Promise<ListTablesResponse>
Promise resolving to ListTablesResponse
Examples
Section titled “Examples”// List all tables in the public schemaconst { tables } = await client.admin.ddl.listTables('public')
tables.forEach(table => { console.log(`Table: ${table.schema}.${table.name}`) table.columns?.forEach(col => { console.log(` - ${col.name}: ${col.type}`) })})// List all tables across all schemasconst { tables } = await client.admin.ddl.listTables()
const tablesBySchema = tables.reduce((acc, table) => { if (!acc[table.schema]) acc[table.schema] = [] acc[table.schema].push(table.name) return acc}, {} as Record<string, string[]>)
console.log(tablesBySchema)