# All files / src/lib db.ts

88.78% Statements 198/223
77.77% Branches 42/54
100% Functions 25/25
88.78% Lines 198/223

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
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    2241x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    50x
    50x
    50x
    50x
    3x
    3x
    47x
    47x
    47x
    1x
    1x
    1x
    4x
    4x
    1x
    1x
    3x
    3x
    1x
    1x
    8x
    8x

    8x
    8x
    1x
    1x
    3x
    3x
    1x
    1x
    2x
    2x
    1x
    1x
    5x
    5x

    5x
    5x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    6x
    6x
    4x
    4x
    4x
    4x
    4x
    4x
    1x
    1x
    2x
    3x
    2x
    1x
    1x
    1x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    2x
    2x
    2x
    1x
    1x
    2x
    1x
    1x
    1x
    16x
    16x

    16x
    16x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    4x
    4x

    4x
    4x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    5x
    5x
    5x
    5x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    3x
    5x
    5x
    5x
    1x
    1x
    1x
    2x
    2x
    2x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
     /**
     * Unified storage interface - routes to IndexedDB or Flask based on configuration
     */

    import type { Snippet, Namespace } from './types';
    import { getStorageConfig, FlaskStorageAdapter } from './storage';
    import * as IndexedDBStorage from './indexeddb-storage';

    // Helper to get the active storage backend
    function getActiveStorage() {
      const config = getStorageConfig();

      if (config.backend === 'flask' && config.flaskUrl) {
        return new FlaskStorageAdapter(config.flaskUrl);
      }

      return null; // Use IndexedDB
    }

    // Snippet operations
    export async function getAllSnippets(): Promise<Snippet[]> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.getAllSnippets();
      }
      return await IndexedDBStorage.getAllSnippets();
    }

    export async function getSnippet(id: string): Promise<Snippet | null> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.getSnippet(id);
      }
      return await IndexedDBStorage.getSnippet(id);
    }

    export async function createSnippet(snippet: Snippet): Promise<void> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.createSnippet(snippet);
      }
      return await IndexedDBStorage.createSnippet(snippet);
    }

    export async function updateSnippet(snippet: Snippet): Promise<void> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.updateSnippet(snippet);
      }
      return await IndexedDBStorage.updateSnippet(snippet);
    }

    export async function deleteSnippet(id: string): Promise<void> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.deleteSnippet(id);
      }
      return await IndexedDBStorage.deleteSnippet(id);
    }

    export async function getSnippetsByNamespace(namespaceId: string): Promise<Snippet[]> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.getSnippetsByNamespace(namespaceId);
      }
      return await IndexedDBStorage.getSnippetsByNamespace(namespaceId);
    }

    export async function moveSnippetToNamespace(snippetId: string, namespaceId: string): Promise<void> {
      const snippet = await getSnippet(snippetId);
      if (!snippet) throw new Error('Snippet not found');

      snippet.namespaceId = namespaceId;
      snippet.updatedAt = Date.now();

      await updateSnippet(snippet);
    }

    export async function bulkMoveSnippets(snippetIds: string[], namespaceId: string): Promise<void> {
      for (const id of snippetIds) {
        await moveSnippetToNamespace(id, namespaceId);
      }
    }

    export async function getAllTemplates(): Promise<Snippet[]> {
      const snippets = await getAllSnippets();
      return snippets.filter(s => s.isTemplate);
    }

    export async function createTemplate(snippet: Omit<Snippet, 'id' | 'createdAt' | 'updatedAt'>): Promise<void> {
      const template: Snippet = {
        ...snippet,
        id: Date.now().toString(),
        createdAt: Date.now(),
        updatedAt: Date.now(),
        isTemplate: true,
      };
      await createSnippet(template);
    }

    export async function syncTemplatesFromJSON(templates: unknown[]): Promise<void> {
      // This would sync predefined templates - implement as needed
      console.log('Syncing templates', templates.length);
    }

    export async function seedDatabase(): Promise<void> {
      // Seed with default namespace if needed
      const namespaces = await getAllNamespaces();
      if (namespaces.length === 0) {
        await ensureDefaultNamespace();
      }
    }

    // Namespace operations
    export async function getAllNamespaces(): Promise<Namespace[]> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.getAllNamespaces();
      }
      return await IndexedDBStorage.getAllNamespaces();
    }

    export async function getNamespaceById(id: string): Promise<Namespace | null> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.getNamespace(id);
      }
      return await IndexedDBStorage.getNamespace(id);
    }

    export async function createNamespace(namespace: Namespace): Promise<void> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.createNamespace(namespace);
      }
      return await IndexedDBStorage.createNamespace(namespace);
    }

    export async function deleteNamespace(id: string): Promise<void> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.deleteNamespace(id);
      }
      return await IndexedDBStorage.deleteNamespace(id);
    }

    export async function ensureDefaultNamespace(): Promise<Namespace> {
      const namespaces = await getAllNamespaces();
      let defaultNs = namespaces.find(ns => ns.isDefault);

      if (!defaultNs) {
        defaultNs = {
          id: 'default',
          name: 'Default',
          createdAt: Date.now(),
          isDefault: true,
        };
        await createNamespace(defaultNs);
      }

      return defaultNs;
    }

    // Database operations
    export async function initDB(): Promise<void> {
      // Initialize IndexedDB or verify Flask connection
      const flask = getActiveStorage();
      if (flask) {
        const connected = await flask.testConnection();
        if (!connected) {
          throw new Error('Failed to connect to Flask backend');
        }
      } else {
        // Initialize IndexedDB
        await IndexedDBStorage.openDB();
      }

      // Ensure default namespace exists
      await ensureDefaultNamespace();
    }

    export async function clearDatabase(): Promise<void> {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.clearDatabase();
      }
      return await IndexedDBStorage.clearDatabase();
    }

    export async function getDatabaseStats() {
      const flask = getActiveStorage();
      if (flask) {
        return await flask.getStats();
      }
      return await IndexedDBStorage.getDatabaseStats();
    }

    export async function exportDatabase(): Promise<string> {
      const flask = getActiveStorage();
      if (flask) {
        const data = await flask.exportDatabase();
        return JSON.stringify(data, null, 2);
      }
      const data = await IndexedDBStorage.exportDatabase();
      return JSON.stringify(data, null, 2);
    }

    export async function importDatabase(jsonData: string): Promise<void> {
      const data = JSON.parse(jsonData);
      const flask = getActiveStorage();
      if (flask) {
        return await flask.importDatabase(data);
      }
      await IndexedDBStorage.importDatabase(data);
    }

    export function validateDatabaseSchema(): Promise<boolean> {
      // With IndexedDB, schema is always valid
      return Promise.resolve(true);
    }

    // For backward compatibility
    export const saveDB = async () => { /* No-op with IndexedDB */ };