@debito/hippo-lib
Version:
Double-entry accounting library for CouchDB
138 lines (121 loc) • 3.48 kB
JavaScript
import CouchDBDriver from './drivers/CouchDBDriver.js';
import ErrorLog from './ErrorLog.js';
// Configuration storage
let metaDB = null; // hippo-meta database connection
let bookDB = null; // current book database connection
let currentBookId = null;
let serverProfile = null;
let userProfile = null; // { userId, organizationId }
let _cachedCOA = null; // COA document cache (shared with coa.js)
/**
* Initialize metadata database connection
* @param {Object} server - Server connection details
* @param {string} server.username - CouchDB username
* @param {string} server.password - CouchDB password
* @param {string} server.url - CouchDB server URL
*/
function initMetadataConnection(server) {
serverProfile = server;
metaDB = new CouchDBDriver({
url: server.url,
username: server.username,
password: server.password,
activeDatabase: 'hippo-meta'
});
ErrorLog._attachToDriver(metaDB);
}
/**
* Switch to a specific book database
* @param {string} bookId - Book identifier
*/
function switchToBook(bookId) {
if (!serverProfile) {
throw new Error('Server not initialized. Call initMetadataConnection() first.');
}
bookDB = new CouchDBDriver({
url: serverProfile.url,
username: serverProfile.username,
password: serverProfile.password,
activeDatabase: bookId
});
currentBookId = bookId;
ErrorLog._attachToDriver(bookDB);
// New book = different COA doc, clear cache
_cachedCOA = null;
}
/**
* Get metadata database driver instance
* @returns {CouchDBDriver} Metadata database driver
*/
function getMetaDB() {
if (!metaDB) {
throw new Error('Metadata database not initialized. Call hippo-lib.init() first.');
}
return metaDB;
}
/**
* Get current book ID
* @returns {string} Current book identifier
*/
function getCurrentBookId() {
if (!currentBookId) {
throw new Error('No book selected. Call hippo-lib.init() with a bookId or hippo.switchBook().');
}
return currentBookId;
}
/**
* Get database driver instance (book database)
* @returns {CouchDBDriver} Book database driver
*/
function getDB() {
if (!bookDB) {
throw new Error('No book selected. Call hippo-lib.init() with a bookId or hippo.switchBook().');
}
return bookDB;
}
/**
* Get current database name (book ID)
* @returns {string} Current book identifier
*/
function getDatabaseName() {
if (!currentBookId) {
throw new Error('No book selected. Call hippo-lib.init() with a bookId or hippo.switchBook().');
}
return currentBookId;
}
/**
* Store user profile globally
* @param {Object} profile - { userId, organizationId }
*/
function setUserProfile(profile) {
userProfile = profile;
}
/**
* Get stored user profile
* @returns {Object|null} User profile
*/
function getUserProfile() {
return userProfile;
}
export {
initMetadataConnection,
switchToBook,
getMetaDB,
getCurrentBookId,
getDB,
getDatabaseName,
setUserProfile,
getUserProfile
};
export default {
initMetadataConnection,
switchToBook,
setUserProfile,
getUserProfile,
get metaDB() { return getMetaDB(); },
get currentBookId() { return getCurrentBookId(); },
get db() { return getDB(); },
get DATABASE_NAME() { return getDatabaseName(); },
get cachedCOA() { return _cachedCOA; },
set cachedCOA(val) { _cachedCOA = val; }
};