powr-sdk-api
Version:
Shared API core library for PowrStack projects. Zero dependencies - works with Express, Next.js API routes, and other frameworks. All features are optional and install only what you need.
36 lines (34 loc) • 757 B
JavaScript
;
// Centralized database service for powr-sdk-api
// This provides a single connection point for all projects
const {
MongoClient
} = require('mongodb');
const {
config
} = require('../config');
let client = null;
let db = null;
const connectDB = async () => {
if (!config.mongoUri) {
throw new Error('POWR_DB_URI environment variable is required');
}
try {
client = new MongoClient(config.mongoUri);
await client.connect();
db = client.db();
console.log('Connected to PowrBase Database');
} catch (error) {
console.error('PowrBase Database connection error:', error);
throw error;
}
};
const getDb = async () => {
if (!db) {
await connectDB();
}
return db;
};
module.exports = {
getDb
};