Table Operations

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);

Last updated on

Latest product updates?See Changelog
Stay in the loop? Follow us onLinkedInLinkedInYouTubeYouTubeXX