Demonstrates how to check for table existence, create a table, and list all tables in the base.
This script demonstrates how to check for the existence of a table, create a new table if it doesn't exist, and list all tables in the current base in NocoDB.
For detailed descriptions of each table operation and associated configuration options, refer to the NocoDB meta API documentation .
// NocoDB Script: Table Operations
// Demonstrates how to check for table existence, create a table, and list all tables in the base.
const tableName = 'Inventory';
// ------------------------------
// Check if table exists
// ------------------------------
let table = base.getTable(tableName);
if (!table) {
output.text(`🔍 Table "${tableName}" not found. Creating it now...`);
try {
// ------------------------------
// CREATE TABLE
// ------------------------------
table = await base.createTableAsync(tableName, [
{ name: 'Item ID', type: "SingleLineText" },
{ name: 'Item Name', type: "SingleLineText" },
{ name: 'Quantity', type: "Number" },
{
name: 'Category',
type: "SingleSelect",
options: {
choices: [
{ title: 'Electronics', color: '#FFC107' },
{ title: 'Clothing', color: '#007BFF' },
{ title: 'Home & Garden', color: '#28A745' },
{ title: 'Books', color: '#6F42C1' },
{ title: 'Sports', color: '#DC3545' }
]
}
}
]);
output.text(`✅ Table "${tableName}" created successfully!`);
} catch (error) {
output.text(`❌ Error creating table: ${error.message}`);
}
} else {
output.text(`ℹ️ Table "${tableName}" already exists.`);
}
// ------------------------------
// READ: List all tables in base
// ------------------------------
output.markdown('### 📋 Tables in this base');
const tableSummary = base.tables.map(t => ({
'Table Name': t.name,
'Table ID': t.id,
'Fields': t.fields.length,
'Views': t.views.length
}));
output.table(tableSummary);
Related pages
- Fields: Text-Based : Learn how to create and manage text-based fields in NocoDB.
- Fields: Numerical : Learn how to create numerical fields for storing quantities, percentages, currency, and more.
- Fields: Date & Time : Create fields to capture dates, times, and durations.
- Fields: Select : Create single select and multi select fields for categorizing data.
- Fields: System : Capture automatic audit information with system fields in NocoDB.
- Fields: User & Collaborator : Assign or reference users directly within records.
- Fields: Virtual & Formula : Add calculated or derivative data with virtual fields in NocoDB.
- Fields: Miscellaneous : Additional field types for attachments, checkboxes, ratings, and structured data.

