All files / src/lib/powersync db.ts

100% Statements 6/6
100% Branches 4/4
100% Functions 1/1
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34        3x                     4x   3x 2x                         3x    
import { browser } from '$app/environment';
import { PowerSyncDatabase } from '@powersync/web';
import { AppSchema } from './schema';
 
let db: PowerSyncDatabase | null = null;
 
/**
 * Returns the PowerSync database singleton.
 * Returns null when running server-side (SSR) to prevent WASM loading errors.
 *
 * Usage:
 *   const db = getDB();
 *   if (!db) return; // server-side
 */
export function getDB(): PowerSyncDatabase | null {
  if (!browser) return null;
 
  if (!db) {
    db = new PowerSyncDatabase({
      schema: AppSchema,
      database: {
        dbFilename: 'strengthsys.db',
      },
      // Single-tab mode: no SharedWorker coordination needed for this app.
      // Multi-tab support can be enabled later if cross-tab sync is required.
      flags: {
        enableMultiTabs: false,
      },
    });
  }
 
  return db;
}