5-Minute Tutorial (Full Lifecycle Case)

This tutorial demonstrates the full lifecycle of using ULDA: creating a master file, adding a content file, updating it, changing the master file password, setting up real-time updates, and deleting both files.

import { Ulda } from '@zeroam/ulda';

async function quickStart() {
  // Initialize ULDA with your API key
  const ulda = new Ulda({
    apiKey: 'your-api-key', // Replace with your API key
    apiUrl: 'https://api.0am.ch',
    dev: true // Enable dev mode for debugging
  });

  const masterFileName = 'tutorialVault';
  const password = 'tutorialPass123';
  const newPassword = 'newPass456';

  try {
    // Step 1: Create and connect to a master file
    await ulda.createMasterFile(masterFileName, password);
    await ulda.connect(masterFileName, password);
    console.log('Master file created and connected');

    // Step 2: Create a content file
    const noteData = { text: 'My first note' };
    await ulda.createContentFile(noteData, 'quickNote');
    console.log('Created note:', ulda.data['quickNote'].content);

    // Step 3: Update the content file
    await ulda.data['quickNote'].update({ text: 'Updated note', priority: 'urgent' });
    console.log('Updated note:', ulda.data['quickNote'].content);

    // Step 4: Change the master file password
    await ulda.changeMasterFilePassword(newPassword);
    console.log('Master file password changed successfully');
    // Reconnect with the new password
    await ulda.connect(masterFileName, newPassword);

    // Step 5: Set up real-time updates
    await ulda.connectToUpdateContentFile((response) => {
      console.log('Real-time update received:', response);
    });
    console.log('Listening for real-time updates');

    // Step 6: Delete the content file
    const deleteResult = await ulda.data['quickNote'].delete();
    console.log('Content file deletion:', deleteResult.status ? 'Success' : 'Failed');

    // Step 7: Delete the master file
    const masterDeleteResult = await ulda.deleteMasterFile();
    console.log('Master file deletion:', masterDeleteResult.status ? 'Success' : 'Failed');
  } catch (error) {
    console.error('Error:', error.message);
  }
}

quickStart().catch(console.error);

Steps Explained:

  1. Initialize the Ulda class with your API key, optional API URL, and dev mode.
  2. Create a master file and connect to it using a unique name and password.
  3. Create a content file with initial data.
  4. Update the content file with new fields.
  5. Change the master file’s password using changeMasterFilePassword and reconnect.
  6. Set up a WebSocket listener for real-time updates.
  7. Delete the content file and then the master file.

Note: Master file names must be unique within the same API key, and content file names must be unique within the same master file.

Run this code in a Node.js environment with the ULDA library installed. Replace 'your-api-key' with your actual API key from 0 am Admin Portal. This tutorial showcases ULDA’s core functionality in a concise workflow.