Inserts a new document into the database.

This function performs an asynchronous request to the server to insert a new document of the specified type into the database. The document is provided as a parameter and must adhere to the schema defined for the document type in the system.

// Insert a new 'Employee' document with specified fields.
const newEmployee: BareBoneDoc = {
doctype: 'Employee',
name: 'EMP-001',
employee_name: 'John Doe',
designation: 'Software Engineer',
department: 'Engineering',
};
const insertedEmployee = await insertDoc(newEmployee);
console.log(insertedEmployee); // Logs the newly inserted 'Employee' document
// Add a new 'Product' document with details.
const newProduct: BareBoneDoc = {
doctype: 'Product',
name: 'PRD-123',
product_name: 'Laptop',
price: 999.99,
stock: 50,
};
const insertedProduct = await insertDoc(newProduct);
console.log(insertedProduct); // Logs the newly inserted 'Product' document
// Create a new 'Order' document with order details.
const newOrder: BareBoneDoc = {
doctype: 'Order',
name: 'ORD-456',
customer_name: 'Alice Smith',
order_date: '2024-09-08',
total_amount: 299.99,
};
const insertedOrder = await insertDoc(newOrder);
console.log(insertedOrder); // Logs the newly inserted 'Order' document

Throws an error if the request fails or if there is an issue with the document being inserted.

  • Type Parameters

    Parameters

    • doc: T

      The document to be inserted. The type T should match the schema of the document type being created.

    Returns Promise<T>

    A promise that resolves to the newly inserted document of type T. The returned document includes the data as saved in the database.