Skip to Content
Patient GraphRepositories

Patient Graph Repositories

The @loop/patient-graph package provides a repository layer for type-safe data access to the Patient Graph schema. Repositories abstract Drizzle ORM queries behind a clean interface.

Setup

import { createConnection, createRepositories } from '@loop/patient-graph'; const connection = createConnection(process.env.DATABASE_URL!); const repos = createRepositories(connection);

Available Repositories

Profiles Repository

// List profiles with filtering const profiles = await repos.profiles.list({ email: 'user@example.com', subscriptionTier: 'pro', limit: 20, offset: 0, }); // Get by ID const profile = await repos.profiles.findById('prof_abc123'); // Create const newProfile = await repos.profiles.create({ externalId: 'user_clerk_456', email: 'new@example.com', firstName: 'Jane', lastName: 'Doe', biologicalSex: 'female', }); // Update await repos.profiles.update('prof_abc123', { firstName: 'Janet', tags: ['vip'], }); // Delete await repos.profiles.delete('prof_abc123');

Labs Repository

// List labs for a customer const labs = await repos.labs.list({ customerId: 'prof_abc123', status: 'reviewed', fromDate: '2024-01-01', toDate: '2024-12-31', limit: 10, }); // Create lab result const lab = await repos.labs.create({ customerId: 'prof_abc123', labDate: '2024-06-01', provider: 'Quest Diagnostics', biomarkers: [ { code: 'testosterone-total', name: 'Total Testosterone', value: 650, unit: 'ng/dL' }, ], status: 'pending', });

Protocols Repository

// List active protocols const protocols = await repos.protocols.list({ customerId: 'prof_abc123', status: 'active', }); // Create protocol const protocol = await repos.protocols.create({ customerId: 'prof_abc123', title: 'BPC-157 Recovery', items: [ { compound: 'BPC-157', dosage: '250mcg', frequency: '2x daily', route: 'subcutaneous' }, ], status: 'active', startDate: '2024-06-01', endDate: '2024-07-27', }); // Update status await repos.protocols.update('proto_def456', { status: 'completed' });

Events Repository

// List events const events = await repos.patientEvents.list({ customerId: 'prof_abc123', type: 'lab_parsed', fromDate: '2024-06-01', }); // Create event await repos.patientEvents.create({ customerId: 'prof_abc123', type: 'note_added', description: 'Patient reported improved sleep quality', data: { category: 'wellness' }, source: 'luna-ai', });

Treatments Repository

const treatments = await repos.treatments.list({ customerId: 'prof_abc123', status: 'approved', }); await repos.treatments.create({ customerId: 'prof_abc123', rimoTreatmentId: 'rimo_treat_123', offeringName: 'TRT Protocol', status: 'pending', });

Prescriptions Repository

const prescriptions = await repos.prescriptions.list({ customerId: 'prof_abc123', treatmentId: 'treat_789', });

Conversation History Repository

const history = await repos.conversationHistory.list({ customerId: 'prof_abc123', channel: 'luna', limit: 50, }); await repos.conversationHistory.create({ customerId: 'prof_abc123', channel: 'luna', role: 'user', content: 'What are my latest lab results?', sessionId: 'sess_abc123', });

Wearable Data Repository

const wearableData = await repos.wearableData.list({ customerId: 'prof_abc123', source: 'oura', metricType: 'sleep', fromDate: '2024-06-01', });

RBAC Logs Repository

const logs = await repos.rbacLogs.list({ actorId: 'user_staff_123', outcome: 'denied', fromDate: '2024-06-01', });

Patient Context

The getPatientContext() function assembles a complete patient context from all repositories:

import { getPatientContext } from '@loop/patient-graph'; const context = await getPatientContext(userId); // Returns: // { // customerId: string, // profile: CustomerProfile, // conditions: string[], // medications: string[], // labResults: LabResult[], // activeProtocols: Protocol[], // emergencyFlags: string[], // consultationNotes: string[], // }

This is used by Luna AI to understand a patient’s complete health picture before responding.

Patient Timeline

import { getPatientTimeline } from '@loop/patient-graph'; const timeline = await getPatientTimeline(userId, { fromDate: '2024-01-01', limit: 100, });

Returns a chronological list of all patient events.