UNPKG

hadith-collections

Version:

A comprehensive npm package for searching and browsing hadith collections with Arabic and English support

80 lines (67 loc) • 2.83 kB
const HadithDB = require('./index.js'); async function example() { console.log('šŸŒ™ Hadith Collections Example\n'); // Initialize the database const hadithDb = new HadithDB(); await hadithDb.connect(); try { // Get database information console.log('šŸ“Š Database Information:'); const info = await hadithDb.getInfo(); console.log(`Version: ${info.version}`); console.log(`Collections: ${info.statistics.collection_count}`); console.log(`Total Books: ${info.statistics.total_books}\n`); // List all collections console.log('šŸ“š Available Collections:'); const collections = await hadithDb.getCollections(); collections.forEach(collection => { console.log(`${collection.id}. ${collection.title_en} (${collection.title})`); }); console.log(); // Get books from Sahih al-Bukhari (collection 1) console.log('šŸ“– Books in Sahih al-Bukhari:'); const books = await hadithDb.getBooks(1); books.slice(0, 5).forEach(book => { console.log(`${book.display_number}. ${book.title_en} (${book.title})`); }); if (books.length > 5) { console.log(`... and ${books.length - 5} more books`); } console.log(); // Search for hadiths about prayer console.log('šŸ” Searching for "prayer":'); const searchResults = await hadithDb.search('prayer', { limit: 3 }); console.log(`Found ${searchResults.total} results (${searchResults.arabic.length} Arabic, ${searchResults.english.length} English)`); if (searchResults.english.length > 0) { console.log('\nšŸ“„ Sample English result:'); const firstResult = searchResults.english[0]; console.log(`Content: ${firstResult.content}`); console.log(`Reference: ${firstResult.reference}\n`); } // Get a random hadith console.log('šŸŽ² Random Hadith:'); const randomHadith = await hadithDb.getRandomHadith(); if (randomHadith) { console.log(`URN: ${randomHadith.urn}`); console.log(`Content: ${randomHadith.content}`); // Try to get English translation const englishVersion = await hadithDb.getEnglishHadithByUrn(randomHadith.urn); if (englishVersion) { console.log(`English: ${englishVersion.content}`); } } console.log(); // Get collection statistics console.log('šŸ“ˆ Collection Statistics:'); const bukhariStats = await hadithDb.getCollectionStats(1); console.log(`Sahih al-Bukhari: ${bukhariStats.book_count} books, ${bukhariStats.chapter_count} chapters`); } catch (error) { console.error('Error:', error.message); } finally { // Always close the database connection await hadithDb.close(); console.log('\nāœ… Database connection closed'); } } // Run the example example().catch(console.error);