ROOM64 / KNOWLEDGE / NOCODB

Records: CRUD Operations

This script demonstrates how to create, read, update, and delete records in a NocoDB table.

อ่านต้นฉบับที่ NocoDB Official Docs ↗
เอกสารอ้างอิงภาษาอังกฤษเนื้อหาจากเอกสาร NocoDB คงรายละเอียดและโค้ดตามต้นฉบับ · ต้นฉบับอัปเดต 2026-08-17 ชื่อเมนูและฟีเจอร์ที่ใช้ได้ขึ้นอยู่กับเวอร์ชันและรุ่นที่ทีมใช้งาน

Workflows · ระบบอัตโนมัติและ Scripts · Records: CRUD Operations

← Generic Examples

สารบัญในหน้านี้

This script demonstrates how to create, read, update, and delete records in a NocoDB table.

This script demonstrates how to perform basic CRUD (Create, Read, Update, Delete) operations on records in a NocoDB table. It includes creating a table if it doesn't exist, adding fields, creating demo records, updating those records, and finally deleting them.

More information about the NocoDB API can be found in the NocoDB data API documentation .

const tableName = 'Contacts';
const fields = [
  { name: 'Name', type: 'SingleLineText' },
  { name: 'Email', type: 'Email' },
  {
    name: 'Status',
    type: 'SingleSelect',
    options: {
      choices: [
        { title: 'Active' },
        { title: 'Pending' },
        { title: 'Inactive' }
      ]
    }
  }
];

// Step 1: Create table if it doesn’t exist
let table = base.getTable(tableName);
if (!table) {
  table = await base.createTableAsync(tableName, []);
}

// Step 2: Create any missing fields
const existingFieldNames = table.fields.map(f => f.name);
for (let field of fields) {
  if (!existingFieldNames.includes(field.name)) {
    await table.createFieldAsync({ name: field.name, type: field.type, options: field.options });
  }
}

// Step 3: Create demo records
const recordsToCreate = [
  { fields: {
    'Name': 'John Doe',
    'Email': 'john.doe@example.com',
    'Status': 'Active'
  }},
  { fields: {
    'Name': 'Jane Smith',
    'Email': 'jane.smith@example.com',
    'Status': 'Pending'
  }},
  { fields: {
    'Name': 'Bob Johnson',
    'Email': 'bob.johnson@example.com',
    'Status': 'Active'
  }}
];

const createdRecords = await table.createRecordsAsync(recordsToCreate);
output.text(`✅ Created ${createdRecords.length} new records.`);

// Step 4: Read the created records
const records = await table.selectRecordsAsync();
const created = records.records.filter(r => createdRecords.includes(r.id));

const displayBeforeUpdate = created.map(r => ({
  Name: r.getCellValue('Name'),
  Email: r.getCellValue('Email'),
  Status: r.getCellValue('Status')
}));

output.text('📖 Records after creation:');
output.table(displayBeforeUpdate);

// Step 5: Update status of each record to "Inactive"
const updates = created.map(r => ({
  id: r.id,
  fields: {
    Status: 'Inactive'
  }
}));

await table.updateRecordsAsync(updates);

const updated = await table.selectRecordsAsync();
const updatedDisplay = updated.records.filter(r => createdRecords.includes(r.id)).map(r => ({
  Name: r.getCellValue('Name'),
  Email: r.getCellValue('Email'),
  Status: r.getCellValue('Status')
}));

output.text('🔁 Records after status update:');
output.table(updatedDisplay);

// Step 6: Delete the created records
await table.deleteRecordsAsync(createdRecords);
output.text(`🗑️ Deleted ${createdRecords.length} records.`);

เนื้อหาเอกสารอ้างอิงและภาพประกอบมาจาก NocoDB Official Docs · บทความแนะนำภาษาไทยเรียบเรียงโดย Room64 · เว็บไซต์นี้เป็นคู่มือประกอบการใช้งานโดย Room64 · เอกสารทางการ ↗

มีงานที่อยากให้ Software, AI หรือ Automation ช่วยอยู่ไหม?

เล่า workflow หรือปัญหาที่ทีมกำลังเจอ เราช่วยดูได้ว่าควรใช้ระบบสำเร็จรูป เชื่อมเครื่องมือเดิม หรือพัฒนาเพิ่มเฉพาะส่วนไหน

คุยกับเราทาง LINEhello@room64.net