47 lines
1.4 KiB
TypeScript
47 lines
1.4 KiB
TypeScript
/**
|
|
* database.ts
|
|
* Dexie (IndexedDB) singleton for the P2P Survey App.
|
|
*
|
|
* All survey data — surveys, participant tokens, and responses — lives
|
|
* exclusively in the creator's browser. Dexie wraps the raw IndexedDB API
|
|
* with a clean, Promise-based interface and live-query support.
|
|
*
|
|
* Usage (inject via InjectionToken, see main.ts):
|
|
* constructor(@Inject(DATABASE_TOKEN) private db: AppDatabase) {}
|
|
*/
|
|
|
|
import Dexie, { type Table } from 'dexie';
|
|
import type { Survey, Participant, Response } from '../shared/models/survey.models';
|
|
|
|
/** Typed Dexie database class */
|
|
export class AppDatabase extends Dexie {
|
|
/** All surveys created by this user */
|
|
surveys!: Table<Survey, string>;
|
|
|
|
/**
|
|
* Pre-generated participant tokens.
|
|
* Primary key: token (UUID string).
|
|
*/
|
|
participants!: Table<Participant, string>;
|
|
|
|
/**
|
|
* Submitted responses.
|
|
* Primary key: id (UUID string).
|
|
*/
|
|
responses!: Table<Response, string>;
|
|
|
|
constructor() {
|
|
super('P2PSurveyDB');
|
|
|
|
this.version(1).stores({
|
|
// Indexed fields: primary key first, then fields used in queries
|
|
surveys: 'id, status, createdAt',
|
|
participants: 'token, surveyId, locked',
|
|
responses: 'id, surveyId, participantToken, submittedAt',
|
|
});
|
|
}
|
|
}
|
|
|
|
/** Module-level singleton — import this wherever you need direct DB access */
|
|
export const db = new AppDatabase();
|