Files
rxminder/services/couchdb.factory.ts

45 lines
1.3 KiB
TypeScript

// Production CouchDB Service Configuration
// This file determines whether to use mock localStorage or real CouchDB
import { CouchDBService as MockCouchDBService } from './couchdb';
import { getEnvVar, isTest } from '../utils/env';
// Environment detection
const isProduction = () => {
// Always use mock service in test environment
if (isTest()) {
return false;
}
// Check if we're in a Docker environment or if CouchDB URL is configured
const couchdbUrl = getEnvVar('VITE_COUCHDB_URL') || getEnvVar('COUCHDB_URL');
return !!couchdbUrl && couchdbUrl !== 'mock';
};
// Create the database service based on environment
const createDbService = () => {
if (isProduction()) {
try {
// Use dynamic require to avoid TypeScript resolution issues
const {
CouchDBService: RealCouchDBService,
} = require('./couchdb.production');
return new RealCouchDBService();
} catch (error) {
console.warn(
'Production CouchDB service not available, falling back to mock:',
error
);
return new MockCouchDBService();
}
} else {
return new MockCouchDBService();
}
};
// Export the database service instance
export const dbService = createDbService();
// Re-export the error class
export { CouchDBError } from './couchdb';