Saves or updates a document in the database.

This function performs an asynchronous request to the server to save or update a document of the specified type. If the document already exists, it will be updated with the provided data; if it does not exist, a new document will be created. The document is provided as a parameter and must adhere to the schema defined for the document type in the system.

// Save or update an 'Employee' document with the given details.
const employee: BareBoneDoc = {
doctype: 'Employee',
name: 'EMP-001',
employee_name: 'John Doe',
designation: 'Senior Software Engineer', // Updated field
department: 'Engineering',
};
const updatedEmployee = await saveDoc(employee);
console.log(updatedEmployee); // Logs the saved or updated 'Employee' document
// Save or update a 'Product' document with updated price.
const product: BareBoneDoc = {
doctype: 'Product',
name: 'PRD-123',
product_name: 'Laptop',
price: 899.99, // Updated field
stock: 50,
};
const updatedProduct = await saveDoc(product);
console.log(updatedProduct); // Logs the saved or updated 'Product' document
// Save or update an 'Order' document with a new total amount.
const order: BareBoneDoc = {
doctype: 'Order',
name: 'ORD-456',
customer_name: 'Alice Smith',
order_date: '2024-09-08',
total_amount: 349.99, // Updated field
};
const updatedOrder = await saveDoc(order);
console.log(updatedOrder); // Logs the saved or updated 'Order' document

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

  • Type Parameters

    • T

    Parameters

    • doc: T

      The document to be saved or updated. The type T should match the schema of the document type being saved.

    Returns Promise<T>

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