Certainly! Here's a list demonstrating how to use each feature of the NitroDB class:
-
Initialization:
import GhostDB from 'ghost.database'; // Import the NitroDB class // Initialize NitroDB with file path, schema, and encryption key const db = new NitroDB('/path/to/database.json', { /* schema */ }, 'encryptionKey');
-
Event Handling:
// Listen for 'beforeSet' event db.on('beforeSet', (key, value) => { console.log(`Setting value ${value} for key ${key}`); }); // Emit 'beforeSet' event db.emit('beforeSet', 'key', 'value');
-
Custom Serialization/Deserialization:
// Serialize data const serializedData = db.serialize({ key: 'value' }); // Deserialize data const deserializedData = db.deserialize(serializedData);
-
Setting Values:
db.set('key', 'value');
-
Pushing to Arrays:
db.push('arrayKey', 'newValue');
-
Querying the Database:
const results = db.query((data) => data.key === 'value');
-
Transactions:
db.transaction(() => { db.set('key1', 'value1'); db.set('key2', 'value2'); });
-
Backup and Restore:
// Backup the database db.backup('/path/to/backup.json'); // Restore the database db.restore('/path/to/backup.json');
-
Checking Key Existence:
const exists = db.has('key');
-
Resetting Values:
db.resetval('key');
-
Versioning:
const version = db.getVersion(); db.updateVersion(version + 1);
-
Custom Hooks:
// Set up 'beforeSet' hook db.beforeSet((key, value) => { console.log(`Setting value ${value} for key ${key}`); }); // Set up 'afterSet' hook db.afterSet((key, value) => { console.log(`Value ${value} set for key ${key}`); });
-
Optimizing Queries:
const optimizedResults = db.optimizeQuery((data) => data.key === 'value');
-
Batch Operations:
// Batch set db.batchSet({ key1: 'value1', key2: 'value2' }); // Batch push db.batchPush({ arrayKey: ['newValue1', 'newValue2'] });
-
Cache Integration:
// Add to cache db.addToCache('key', 'value'); // Retrieve from cache const cachedValue = db.getFromCache('key'); // Clear cache db.clearCache();
-
Data Compression:
// Compress data const compressedData = db.compressData({ key: 'value' }); // Decompress data const decompressedData = db.decompressData(compressedData);
These examples demonstrate how to utilize each feature of the NitroDB class in TypeScript.