Repository navigation
Expand file tree
/
Copy pathdb.js
More file actions
129 lines (116 loc) · 3.34 KB
/
Copy pathdb.js
File metadata and controls
129 lines (116 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Database Connection Module
* Migrated: ZeroDB Migration - Issue #175
*
* This module provides database connection utilities for ZeroDB.
* MongoDB support removed in favor of ZeroDB-only architecture.
*/
const zerodbService = require('./services/zerodbService');
let isConnected = false;
/**
* Connect to ZeroDB
* @returns {Object} ZeroDB service instance
*/
async function connectDB() {
try {
if (isConnected && zerodbService.projectId) {
console.log('ZeroDB already connected...');
return zerodbService;
}
const token = process.env.AINATIVE_API_TOKEN;
if (!token) {
throw new Error('AINATIVE_API_TOKEN is required for ZeroDB connection');
}
const result = await zerodbService.initialize(token);
isConnected = true;
console.log('ZeroDB Connected...', { projectId: result.projectId });
return zerodbService;
} catch (err) {
console.error('ZeroDB connection error:', err);
throw err;
}
}
/**
* Disconnect from ZeroDB
* Note: ZeroDB uses HTTP API so no persistent connection to close
*/
async function disconnectDB() {
try {
isConnected = false;
console.log('ZeroDB Disconnected...');
} catch (err) {
console.error('ZeroDB disconnection error:', err);
}
}
/**
* Clear database tables (for testing)
* Warning: This will delete all data in the specified tables
*/
async function clearDB() {
try {
if (process.env.NODE_ENV === 'test') {
console.log('Clearing ZeroDB tables for testing...');
// Note: ZeroDB table clearing would need to be implemented
// based on the specific API capabilities
const tables = [
'users', 'companies', 'stakeholders', 'securities',
'transactions', 'documents', 'valuations',
'compliance_events', 'audit_logs'
];
for (const table of tables) {
try {
await zerodbService.deleteRows(table, {});
console.log(`Cleared table: ${table}`);
} catch (err) {
// Table might not exist or be empty
console.log(`Could not clear table ${table}: ${err.message}`);
}
}
}
} catch (err) {
console.error('Error clearing database:', err);
throw err;
}
}
/**
* Check if database is connected
* @returns {boolean} Connection status
*/
function isDBConnected() {
return isConnected && !!zerodbService.projectId;
}
/**
* Get database status
* @returns {Object} Database status information
*/
async function getDBStatus() {
if (!isConnected || !zerodbService.projectId) {
return { status: 'disconnected' };
}
try {
const status = await zerodbService.getDatabaseStatus();
return {
status: 'connected',
projectId: zerodbService.projectId,
...status
};
} catch (err) {
return {
status: 'error',
error: err.message
};
}
}
// Add cleanup handler for process termination
process.on('SIGTERM', async () => {
await disconnectDB();
process.exit(0);
});
module.exports = {
connectDB,
disconnectDB,
clearDB,
isDBConnected,
getDBStatus,
zerodbService
};