Driver API

Work with driver data from simulations

Interactive Examples

Fetch Drivers

No drivers loaded yet. Click the button to fetch drivers.

Collection Access

import { initializeSGERP } from 'sgerp-frontend-lib';

const api = initializeSGERP({
  activeConnection: 'admin@localhost',
  connections: {
    'admin@localhost': {
      id: 'admin@localhost',
      environment: 'localhost',
      user: 'admin',
      password: 'your-password',
      baseUrl: 'https://sgerp-stage.d.gcdev.swatrider.com',
    },
  },
});

const drivers = api.collections.driver;

Fetching Drivers

// Fetch first 20 drivers
await drivers.fetch({ limit: 20 });

// Fetch with offset
await drivers.fetch({ limit: 20, offset: 20 });

// Filter by project
await drivers.fetch({ project_id: 1 });

// Fetch all drivers (be careful with large datasets)
await drivers.fetchAll();

Accessing Driver Data

// Get all drivers
const allDrivers = drivers.models;

// Get a specific driver by ID
const driver = drivers.get(7);

// Check if a driver exists
const exists = drivers.has(7);

// Get first/last driver
const first = drivers.first();
const last = drivers.last();

// Check collection state
const isEmpty = drivers.isEmpty();
const count = drivers.length;
const hasMore = drivers.hasMore;

Driver Model

interface Driver {
  id: number;
  created_at: string;
  modified_at: string;
  first_name: string;
  last_name: string | null;
  external_id: string | null;
  user_id: number | null;
  project_id: number;
}

Collection Utility Methods

Grouping

// Group drivers by project
const byProject = drivers.groupBy('project_id');

// Group by external ID
const byExternalId = drivers.groupBy('external_id');

Sorting

// Sort by first name
const byFirstName = drivers.sortBy('first_name');

// Sort by last name
const byLastName = drivers.sortBy('last_name');

// Sort by ID
const byId = drivers.sortBy('id');

// Sort by custom function
const sorted = drivers.sortBy(d => `${d.first_name} ${d.last_name}`);

Filtering

// Filter with custom logic
const activeDrivers = drivers.filter(d => d.user_id !== null);

// Find specific driver
const found = drivers.find(d => d.external_id === 'DRV001');

// Where clause
const projectDrivers = drivers.where({
  project_id: 1
});

Plucking Values

// Get all driver names
const names = drivers.pluck('first_name');

// Get all project IDs
const projectIds = drivers.pluck('project_id');

// Get full names
const fullNames = drivers.models.map(d => 
  `${d.first_name} ${d.last_name || ''}`
);

Pagination

// Fetch first page
await drivers.fetch({ limit: 10 });

console.log(`Loaded ${drivers.length} drivers`);
console.log(`Has more: ${drivers.hasMore}`);

// Load next page
if (drivers.hasMore) {
  await drivers.fetchNext();
  console.log(`Now have ${drivers.length} drivers`);
}

Error Handling

try {
  await drivers.fetch({ limit: 20 });
  console.log('Drivers loaded:', drivers.length);
} catch (error) {
  if (error instanceof APIError) {
    console.error('API Error:', error.message);
    console.error('Status:', error.status);
  } else {
    console.error('Error:', error);
  }
}

Common Patterns

Get All Drivers for a Project

// Fetch all drivers filtered by project on the server
await drivers.fetchAll({ project_id: 1 });
console.log(`Project has ${drivers.length} drivers`);

Find Driver by External ID

await drivers.fetch({ external_id: 'DRV001' });
const driver = drivers.first();
if (driver) {
  console.log(`Found driver: ${driver.first_name} ${driver.last_name}`);
}

Get Drivers with User Accounts

await drivers.fetch({ limit: 100 });
const withAccounts = drivers.filter(d => d.user_id !== null);
console.log(`${withAccounts.length} drivers have user accounts`);

Search by Name

// Use server-side filtering for efficient search
await drivers.fetch({ first_name__icontains: 'john' });
const results = drivers.models;
console.log(`Found ${results.length} drivers matching "john"`);

Type Definitions

The full Driver type is exported from the library:

import type { Driver } from 'sgerp-frontend-lib';

const driver: Driver = {
  id: 7,
  created_at: '2020-06-04T12:15:46.940042+00:00',
  modified_at: '2020-06-04T12:19:59.693296+00:00',
  first_name: 'John',
  last_name: 'Doe',
  external_id: 'DRV001',
  user_id: 42,
  project_id: 1,
};