Table Operations
This script shows how to do these table operations in NocoDB:
- Check if a table exists.
- Create a new table if it does not exist.
- List all tables in the current base.
For details on each table operation and its 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);Last updated on
Latest product updates?See Changelog