Inserts multiple documents into the database in a single request.

This function performs an asynchronous request to the server to insert multiple documents of the specified type into the database. The documents are provided as an array and must adhere to the schema defined for the document type in the system. This is useful for batch processing when multiple documents need to be inserted at once.

// Insert multiple 'Employee' documents in a single request.
const employees: BareBoneDoc[] = [
{ doctype: 'Employee', name: 'EMP-001', employee_name: 'John Doe', designation: 'Software Engineer', department: 'Engineering' },
{ doctype: 'Employee', name: 'EMP-002', employee_name: 'Jane Roe', designation: 'Product Manager', department: 'Product' },
];
const employeeIds = await insertMany(employees);
console.log(employeeIds); // Logs an array of identifiers for the newly inserted 'Employee' documents
// Add multiple 'Product' documents at once.
const products: BareBoneDoc[] = [
{ doctype: 'Product', name: 'PRD-123', product_name: 'Laptop', price: 999.99, stock: 50 },
{ doctype: 'Product', name: 'PRD-124', product_name: 'Mouse', price: 25.99, stock: 150 },
];
const productIds = await insertMany(products);
console.log(productIds); // Logs an array of identifiers for the newly inserted 'Product' documents
// Create multiple 'Order' documents in one request.
const orders: BareBoneDoc[] = [
{ doctype: 'Order', name: 'ORD-456', customer_name: 'Alice Smith', order_date: '2024-09-08', total_amount: 299.99 },
{ doctype: 'Order', name: 'ORD-457', customer_name: 'Bob Johnson', order_date: '2024-09-09', total_amount: 199.99 },
];
const orderIds = await insertMany(orders);
console.log(orderIds); // Logs an array of identifiers for the newly inserted 'Order' documents

Throws an error if the request fails or if there is an issue with any of the documents being inserted.

  • Type Parameters

    Parameters

    • docs: T[]

      An array of documents to be inserted. Each document should conform to the schema of the document type being created.

    Returns Promise<string[]>

    A promise that resolves to an array of identifiers (usually names or IDs) of the newly inserted documents.