jspsych-datamanager
Version:
A package to manage data for jsPsych experiments. Currently Firebase and Supabase are supported.
96 lines (93 loc) • 3.32 kB
text/typescript
import { BaseManagerOptions, DataManager, ExperimentData, TrialData } from '@jspsych-datamanager/core';
/**
* Configuration interface for Supabase initialization
*/
interface SupabaseConfig {
/** Supabase project URL */
url: string;
/** Supabase anon/public key */
anonKey: string;
}
/**
* Options specific to SupabaseManager initialization
*/
interface SupabaseManagerOptions extends BaseManagerOptions {
/** Name of the table to use (default: "experiments") */
tableName?: string;
/** Specific row ID to use (optional) */
rowId?: string;
}
/**
* A class to manage Supabase operations for jsPsych experiments
*
* This class extends DataManager to provide Supabase-specific implementation
* for storing and managing experiment data.
*
* @example
* ```typescript
* const supabaseManager = new SupabaseManager(supabaseConfig, {
* tableName: "my-experiments",
* metadata: { version: "1.0.0" }
* });
* ```
*
* IMPORTANT: Before using this manager, make sure to:
* 1. Create the table in your Supabase dashboard
* 2. Set up Row Level Security (RLS) policies to allow operations:
* - Go to Authentication > Policies
* - Add a policy for INSERT: "Enable inserts for authenticated users" with USING(true)
* - Add a policy for SELECT: "Enable select for authenticated users" with USING(true)
*/
declare class SupabaseManager extends DataManager {
private readonly supabase;
private readonly tableName;
private rowId?;
private numberOfOperations;
private initialized;
private pendingTrials;
/**
* Creates a new SupabaseManager instance
* @param supabaseConfig Supabase configuration object
* @param options Additional options for initialization
*/
constructor(supabaseConfig: SupabaseConfig, options?: SupabaseManagerOptions);
/**
* Initializes the experiment data in Supabase
* @param additionalData Additional data to include in the experiment document
* @throws {Error} If initialization fails
*/
initializeExperiment(additionalData?: Partial<ExperimentData>): Promise<void>;
/**
* Adds a new trial to the experiment data
* @param trialData The trial data to add
* @throws {Error} If storing the trial fails
*/
addTrialData(trialData: TrialData): Promise<void>;
/**
* Gets the total number of operations performed
* @returns The number of operations performed
*/
getNumberOfOperations(): number;
/**
* Creates a callback function for jsPsych's on_data_update event
* @returns A function that handles trial data updates
*/
createDataUpdateCallback(): (data: TrialData) => TrialData;
/**
* Creates a callback function for jsPsych's on_finish event
* @returns A function that handles experiment completion
*/
createFinishCallback(): () => void;
/**
* Checks if the manager has been properly initialized
* @returns True if initialized, false otherwise
*/
isInitialized(): boolean;
/**
* Handles RLS policy error by providing helpful information on how to fix it
* @param error The error object from Supabase
* @private
*/
private handleRlsError;
}
export { type SupabaseConfig, SupabaseManager, type SupabaseManagerOptions };