Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/ragaeeb/shamela/llms.txt

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

Overview

The master database contains comprehensive information about all books, authors, and categories available in the Shamela library. This function downloads the database files, creates the necessary tables, and exports the data in the specified format (JSON or SQLite).

Function Signature

downloadMasterDatabase(options: DownloadMasterOptions): Promise<string>

Parameters

options
DownloadMasterOptions
required
Configuration options for downloading the master database

Returns

string
string
The path of the created output file

Examples

Download as SQLite Database

import { downloadMasterDatabase } from 'shamela';

const dbPath = await downloadMasterDatabase({
  outputFile: { path: './shamela_master.db' }
});

console.log(`Downloaded to: ${dbPath}`);

Download as JSON

const jsonPath = await downloadMasterDatabase({
  outputFile: { path: './shamela_master.json' }
});

console.log(`Master data saved to: ${jsonPath}`);

Using Pre-fetched Metadata

import { getMasterMetadata, downloadMasterDatabase } from 'shamela';

// Fetch metadata first
const metadata = await getMasterMetadata();
console.log(`Latest version: ${metadata.version}`);

// Download using the metadata
const dbPath = await downloadMasterDatabase({
  masterMetadata: metadata,
  outputFile: { path: './master.db' }
});

Custom Writer (Browser Environment)

const downloadMaster = async () => {
  const path = await downloadMasterDatabase({
    outputFile: {
      path: 'master.json',
      writer: async (payload) => {
        // Save to IndexedDB, localStorage, or trigger download
        const blob = new Blob([payload], { type: 'application/json' });
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'master.json';
        a.click();
      }
    }
  });
};

Output Format

SQLite Format

The SQLite database contains the following tables:
  • authors - Author information
  • books - Book metadata
  • categories - Category hierarchy

JSON Format

{
  "version": 123,
  "authors": [
    {
      "id": 1,
      "name": "محمد بن إسماعيل البخاري",
      "death_year": 256
      // ... other fields
    }
  ],
  "books": [
    {
      "id": 26592,
      "name": "صحيح البخاري",
      "author_id": 1,
      "category_id": 5
      // ... other fields
    }
  ],
  "categories": [
    {
      "id": 1,
      "name": "الحديث الشريف",
      "parent_id": null
      // ... other fields
    }
  ]
}

Error Handling

This function throws an error when:
  • Download fails
  • Expected tables are missing from the archive
  • Database operations fail
  • File write operations fail
  • Unsupported file extension is provided
try {
  const path = await downloadMasterDatabase({
    outputFile: { path: './master.db' }
  });
  console.log('Success:', path);
} catch (error) {
  console.error('Download failed:', error.message);
}

Performance Notes

  • The master database is typically 50-100MB compressed
  • Download time depends on network speed
  • SQLite format is more compact than JSON
  • Pre-fetching metadata can save one HTTP round-trip

Build docs developers (and LLMs) love