Drive class for managing file storage drives

This class represents a drive in the Mosaia platform, which is a container for organizing and managing files and documents. Drives can be scoped to users or organizations.

Features:

  • Drive configuration management
  • File and document organization
  • Access control
  • Metadata management

Drives provide:

  • File storage organization
  • User/org-scoped storage
  • File metadata management
  • Access control

Basic drive setup:

import { Drive } from 'mosaia-node-sdk';

// Create a new drive
const drive = new Drive({
name: 'My Documents',
description: 'Personal document storage'
});

await drive.save();

Managing drive items:

// Get all items in the drive
const items = await drive.items.get();

// Create a new item (metadata-only)
const item = await drive.items.create({
name: 'document.pdf',
path: '/documents'
});

Hierarchy (View Summary)

Constructors

  • Creates a new Drive instance

    Initializes a drive with the provided configuration data and optional URI. The drive represents a container for organizing files and documents.

    Parameters

    • data: Partial<DriveInterface>

      Drive configuration data

    • Optionaluri: string

      Optional URI path for the drive endpoint. Defaults to '/drive'

    Returns Drive

    const drive = new Drive({
    name: 'Project Files',
    description: 'Storage for project documents'
    });

Accessors

  • get items(): DriveItems

    Get the drive's items collection

    This getter provides access to the drive's items through the DriveItems collection. It enables management of all files and documents within the drive.

    Returns DriveItems

    DriveItems collection for managing drive items

    List items:

    const items = await drive.items.get();
    items.forEach(item => {
    console.log(`Item: ${item.name}`);
    });

    Create item:

    const item = await drive.items.create({
    name: 'document.pdf',
    path: '/documents',
    size: 1024
    });
  • get uploads(): UploadJobs

    Get the drive's upload jobs collection

    This getter provides access to the drive's upload jobs through the UploadJobs collection. It enables management of all upload jobs for the drive.

    Returns UploadJobs

Methods

  • Check if the entity is active

    This method checks the active status of the entity. Most entities in the system can be active or inactive, which affects their availability and usability in the platform.

    Returns boolean

    True if the entity is active, false otherwise

    const user = new User(userData);
    if (user.isActive()) {
    // Perform operations with active user
    } else {
    console.log('User is inactive');
    }
  • Convert model instance to interface data

    This method serializes the model instance to a plain object that matches the interface type. This is useful for:

    • Sending data to the API
    • Storing data in a database
    • Passing data between components
    • Debugging model state

    Returns DriveInterface

    The model data as a plain object matching the interface type

    const user = new User({
    email: 'user@example.com',
    firstName: 'John'
    });

    const data = user.toJSON();
    console.log(data); // { email: '...', firstName: '...' }

    // Use with JSON.stringify
    const json = JSON.stringify(user);
  • Convert model instance to API payload

    This method creates a payload suitable for API requests by:

    • Converting the model to a plain object
    • Removing read-only fields (like 'id')
    • Ensuring proper data format for the API

    Returns Partial<DriveInterface>

    A clean object suitable for API requests

    const user = new User({
    id: '123', // Will be removed from payload
    email: 'new@example.com',
    firstName: 'John'
    });

    const payload = user.toAPIPayload();
    // payload = { email: '...', firstName: '...' }
    // Note: 'id' is removed as it's read-only

    await apiClient.POST('/users', payload);
  • Update model data with new values

    This method updates the model's data and instance properties with new values. It performs a shallow merge of the updates with existing data, allowing for partial updates of the model's properties.

    Parameters

    • updates: Partial<DriveInterface>

      Object containing properties to update

    Returns void

    const user = new User({
    email: 'old@example.com',
    firstName: 'John'
    });

    // Update multiple properties
    user.update({
    email: 'new@example.com',
    lastName: 'Doe'
    });

    // Save changes to API
    await user.save();

    This method only updates the local model instance. To persist changes to the API, call save after updating.

  • Save the model instance to the API

    This method persists the current state of the model to the API using a PUT request. It requires the model to have an ID (existing instance). For new instances, use the collection's create method instead.

    The method:

    1. Validates the model has an ID
    2. Sends current data to the API
    3. Updates local instance with API response

    Returns Promise<DriveInterface>

    Promise resolving to the updated model data

    When model has no ID

    When API request fails

    const user = new User({
    id: '123',
    email: 'user@example.com'
    });

    // Update and save
    user.update({ firstName: 'John' });
    await user.save();

    Error handling:

    try {
    await user.save();
    } catch (error) {
    if (error.message.includes('ID is required')) {
    // Handle missing ID error
    } else {
    // Handle API errors
    }
    }
  • Delete the model instance from the API

    This method permanently deletes the model instance from the API and clears the local data. This operation cannot be undone.

    The method:

    1. Validates the model has an ID
    2. Sends DELETE request to the API
    3. Clears local instance data on success

    Returns Promise<void>

    Promise that resolves when deletion is successful

    When model has no ID

    When API request fails

    Basic deletion:

    const user = await users.get({}, 'user-id');
    if (user) {
    await user.delete();
    // User is now deleted and instance is cleared
    }

    Error handling:

    try {
    await user.delete();
    console.log('User deleted successfully');
    } catch (error) {
    if (error.message.includes('ID is required')) {
    console.error('Cannot delete - no ID');
    } else {
    console.error('Deletion failed:', error.message);
    }
    }