Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/CodecraftBySyed/Mini-POS-System/llms.txt

Use this file to discover all available pages before exploring further.

Overview

The Products API provides functions for creating, reading, and deleting product records. All functions are asynchronous and return Promises.

addProduct()

Adds a new product to the database.
name
string
required
The name of the product
price
string | number
required
The price of the product. Will be converted to a number.
id
Promise<number>
Resolves with the auto-generated ID of the new product

Implementation

js/db.js
export async function addProduct(name, price) {
  return tx('products', 'readwrite', (s) => s.add({ name, price: Number(price) }));
}

Example Usage

import { addProduct } from './js/db.js';

// Add a product
const productId = await addProduct('Coffee', 3.99);
console.log(`Product added with ID: ${productId}`);

// Add multiple products
await addProduct('Tea', 2.99);
await addProduct('Sandwich', 5.50);

Error Handling

try {
  await addProduct('Latte', 4.50);
} catch (error) {
  console.error('Failed to add product:', error);
  // Handle: database unavailable, transaction failed, etc.
}
Common Errors:
  • Transaction fails if database is unavailable
  • Invalid price values are converted to NaN
  • Missing parameters result in incomplete records

getProducts()

Retrieves all products from the database.
products
Promise<Array<Object>>
Resolves with an array of product objects

Product Object Structure

{
  id: number,        // Auto-generated unique identifier
  name: string,      // Product name
  price: number      // Product price
}

Implementation

js/db.js
export async function getProducts() {
  return tx('products', 'readonly', (s) => {
    return new Promise((resolve) => {
      const items = [];
      s.openCursor().onsuccess = (e) => {
        const cur = e.target.result;
        if (cur) { items.push(cur.value); cur.continue(); }
        else resolve(items);
      };
    });
  });
}

Example Usage

import { getProducts } from './js/db.js';

// Get all products
const products = await getProducts();
console.log(products);
// [
//   { id: 1, name: 'Coffee', price: 3.99 },
//   { id: 2, name: 'Tea', price: 2.99 },
//   { id: 3, name: 'Sandwich', price: 5.50 }
// ]

// Display products
products.forEach(product => {
  console.log(`${product.name}: ₹${product.price}`);
});

// Check if products exist
if (products.length === 0) {
  console.log('No products found');
}

Error Handling

try {
  const products = await getProducts();
  if (products.length === 0) {
    console.log('Database is empty');
  }
} catch (error) {
  console.error('Failed to retrieve products:', error);
}

deleteProduct()

Deletes a product from the database by ID.
id
string | number
required
The ID of the product to delete. Will be converted to a number.
void
Promise<void>
Resolves when the product is successfully deleted

Implementation

js/db.js
export async function deleteProduct(id) {
  return tx('products', 'readwrite', (s) => s.delete(Number(id)));
}

Example Usage

import { deleteProduct, getProducts } from './js/db.js';

// Delete a product by ID
await deleteProduct(1);
console.log('Product deleted');

// Delete and verify
await deleteProduct(2);
const remaining = await getProducts();
console.log(`${remaining.length} products remaining`);

// Delete multiple products
const productIds = [3, 4, 5];
for (const id of productIds) {
  await deleteProduct(id);
}

Error Handling

try {
  await deleteProduct(999);
  // Note: Succeeds even if product doesn't exist
  console.log('Delete operation completed');
} catch (error) {
  console.error('Failed to delete product:', error);
}
Behavior Notes:
  • Deleting a non-existent ID succeeds silently
  • Invalid IDs are converted to numbers (may result in unexpected behavior)
  • The operation is atomic within a transaction

Build docs developers (and LLMs) love