Skip to content

Latest commit

 

History

History
127 lines (101 loc) · 2.87 KB

docs.md

File metadata and controls

127 lines (101 loc) · 2.87 KB

Certainly! Here's a list demonstrating how to use each feature of the NitroDB class:

  1. 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');
  2. 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');
  3. Custom Serialization/Deserialization:

    // Serialize data
    const serializedData = db.serialize({ key: 'value' });
    
    // Deserialize data
    const deserializedData = db.deserialize(serializedData);
  4. Setting Values:

    db.set('key', 'value');
  5. Pushing to Arrays:

    db.push('arrayKey', 'newValue');
  6. Querying the Database:

    const results = db.query((data) => data.key === 'value');
  7. Transactions:

    db.transaction(() => {
        db.set('key1', 'value1');
        db.set('key2', 'value2');
    });
  8. Backup and Restore:

    // Backup the database
    db.backup('/path/to/backup.json');
    
    // Restore the database
    db.restore('/path/to/backup.json');
  9. Checking Key Existence:

    const exists = db.has('key');
  10. Resetting Values:

    db.resetval('key');
  11. Versioning:

    const version = db.getVersion();
    db.updateVersion(version + 1);
  12. 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}`);
    });
  13. Optimizing Queries:

    const optimizedResults = db.optimizeQuery((data) => data.key === 'value');
  14. Batch Operations:

    // Batch set
    db.batchSet({ key1: 'value1', key2: 'value2' });
    
    // Batch push
    db.batchPush({ arrayKey: ['newValue1', 'newValue2'] });
  15. Cache Integration:

    // Add to cache
    db.addToCache('key', 'value');
    
    // Retrieve from cache
    const cachedValue = db.getFromCache('key');
    
    // Clear cache
    db.clearCache();
  16. 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.