40 lines
938 B
TypeScript
40 lines
938 B
TypeScript
|
import * as plugins from './plugins.js';
|
||
|
import type { IDatabaseConfig } from './skr.types.js';
|
||
|
|
||
|
let dbInstance: plugins.smartdata.SmartdataDb | null = null;
|
||
|
|
||
|
export const getDb = async (
|
||
|
config?: IDatabaseConfig,
|
||
|
): Promise<plugins.smartdata.SmartdataDb> => {
|
||
|
if (!dbInstance) {
|
||
|
if (!config) {
|
||
|
throw new Error(
|
||
|
'Database configuration required for first initialization',
|
||
|
);
|
||
|
}
|
||
|
|
||
|
dbInstance = new plugins.smartdata.SmartdataDb({
|
||
|
mongoDbUrl: config.mongoDbUrl,
|
||
|
mongoDbName: config.dbName || 'skr_accounting',
|
||
|
});
|
||
|
|
||
|
await dbInstance.init();
|
||
|
}
|
||
|
|
||
|
return dbInstance;
|
||
|
};
|
||
|
|
||
|
export const getDbSync = (): plugins.smartdata.SmartdataDb => {
|
||
|
if (!dbInstance) {
|
||
|
throw new Error('Database not initialized. Call getDb() first.');
|
||
|
}
|
||
|
return dbInstance;
|
||
|
};
|
||
|
|
||
|
export const closeDb = async (): Promise<void> => {
|
||
|
if (dbInstance) {
|
||
|
await dbInstance.close();
|
||
|
dbInstance = null;
|
||
|
}
|
||
|
};
|