@debito/hippo-lib
Version:
Double-entry accounting library for CouchDB
139 lines (123 loc) âĸ 3.83 kB
JavaScript
import Account from './Account.js';
import LedgerEntry from './LedgerEntry.js';
import JournalEntry from './JournalEntry.js';
import COA from './coa.js';
import TrialBalance from './reports/TrialBalance.js';
import LedgerTemplate from './helpers/LedgerTemplate.js';
import JournalTemplate from './helpers/JournalTemplate.js';
import config, { getDB } from './config.js';
/**
* Initialize database indexes for hippo-lib
* Should be called once during application startup
*/
async function initializeDatabase() {
console.log('đ§ Initializing hippo-lib database...');
try {
const db = config.db;
// Create index for ledger entries sorted by date
await db.createIndex({
index: {
fields: ['docType', 'date']
},
name: 'ledger-by-date',
type: 'json'
});
console.log('â
Created index: ledger-by-date');
// Create index for ledger entries by account
await db.createIndex({
index: {
fields: ['docType', 'accountId']
},
name: 'ledger-by-account',
type: 'json'
});
console.log('â
Created index: ledger-by-account');
// Create index for ledger entries by journal entry
await db.createIndex({
index: {
fields: ['docType', 'journalEntryId']
},
name: 'ledger-by-journal',
type: 'json'
});
console.log('â
Created index: ledger-by-journal');
// Create index for accounts by name (for findByName)
await db.createIndex({
index: {
fields: ['docType', 'name']
},
name: 'account-by-name',
type: 'json'
});
console.log('â
Created index: account-by-name');
console.log('đ Database initialization completed successfully!');
} catch (error) {
// Indexes might already exist, which is fine
if (error.error === 'file_exists') {
console.log('âšī¸ Database indexes already exist');
} else {
console.error('â Database initialization failed:', error.message);
throw error;
}
}
}
/**
* Initialize hippo-lib with database configuration
* @param {string} username - CouchDB username
* @param {string} password - CouchDB password
* @param {string} database - Database name
* @returns {Promise<void>}
*/
async function init(username, password, database) {
if (!username || !password || !database) {
throw new Error('hippo-lib.init() requires username, password, and database');
}
// Initialize configuration
config.initConfig(username, password, database);
// Check if database exists, create if it doesn't
const db = config.db;
try {
await db.databaseInfo();
} catch (error) {
if (error.status === 404) {
console.log(`đ Creating database: ${database}`);
await db.createDatabase(database);
} else {
throw error;
}
}
// Initialize database indexes
await initializeDatabase();
console.log('â
hippo-lib initialized');
}
/**
* Get database information
* @returns {Promise<Object>} Database info from CouchDB
*/
async function getDatabaseInfo() {
const db = getDB();
return await db.databaseInfo();
}
export {
init,
getDatabaseInfo,
Account,
LedgerEntry,
JournalEntry,
COA,
TrialBalance,
LedgerTemplate,
JournalTemplate
};
// Default export for convenience
export default {
init,
getDatabaseInfo,
Account,
LedgerEntry,
JournalEntry,
COA,
TrialBalance,
LedgerTemplate,
JournalTemplate
};