# All files / framework/core DatabaseManager.ts

0% Statements 0/168
0% Branches 0/40
0% Functions 0/20
0% Lines 0/165

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
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369

     import { ModelRegistry } from './ModelRegistry';
    import { FrameworkOrbitDBService } from '../services/OrbitDBService';
    import { StoreType } from '../types/framework';
    import { UserMappings } from '../types/models';

    export class UserMappingsData implements UserMappings {
      constructor(
        public userId: string,
        public databases: Record<string, string>,
      ) {}
    }

    export class DatabaseManager {
      private orbitDBService: FrameworkOrbitDBService;
      private databases: Map<string, any> = new Map();
      private userMappings: Map<string, any> = new Map();
      private globalDatabases: Map<string, any> = new Map();
      private globalDirectoryShards: any[] = [];
      private initialized: boolean = false;

      constructor(orbitDBService: FrameworkOrbitDBService) {
        this.orbitDBService = orbitDBService;
      }

      async initializeAllDatabases(): Promise<void> {
        if (this.initialized) {
          return;
        }

        console.log('🚀 Initializing DebrosFramework databases...');

        // Initialize global databases first
        await this.initializeGlobalDatabases();

        // Initialize system databases (user directory, etc.)
        await this.initializeSystemDatabases();

        this.initialized = true;
        console.log('✅ Database initialization complete');
      }

      private async initializeGlobalDatabases(): Promise<void> {
        const globalModels = ModelRegistry.getGlobalModels();

        console.log(`📊 Creating ${globalModels.length} global databases...`);

        for (const model of globalModels) {
          const dbName = `global-${model.modelName.toLowerCase()}`;

          try {
            const db = await this.createDatabase(dbName, model.dbType, 'global');
            this.globalDatabases.set(model.modelName, db);

            console.log(`✓ Created global database: ${dbName} (${model.dbType})`);
          } catch (error) {
            console.error(`❌ Failed to create global database ${dbName}:`, error);
            throw error;
          }
        }
      }

      private async initializeSystemDatabases(): Promise<void> {
        console.log('🔧 Creating system databases...');

        // Create global user directory shards
        const DIRECTORY_SHARD_COUNT = 4; // Configurable

        for (let i = 0; i < DIRECTORY_SHARD_COUNT; i++) {
          const shardName = `global-user-directory-shard-${i}`;
          try {
            const shard = await this.createDatabase(shardName, 'keyvalue', 'system');
            this.globalDirectoryShards.push(shard);

            console.log(`✓ Created directory shard: ${shardName}`);
          } catch (error) {
            console.error(`❌ Failed to create directory shard ${shardName}:`, error);
            throw error;
          }
        }

        console.log(`✅ Created ${this.globalDirectoryShards.length} directory shards`);
      }

      async createUserDatabases(userId: string): Promise<UserMappingsData> {
        console.log(`👤 Creating databases for user: ${userId}`);

        const userScopedModels = ModelRegistry.getUserScopedModels();
        const databases: Record<string, string> = {};

        // Create mappings database first
        const mappingsDBName = `${userId}-mappings`;
        const mappingsDB = await this.createDatabase(mappingsDBName, 'keyvalue', 'user');

        // Create database for each user-scoped model
        for (const model of userScopedModels) {
          const dbName = `${userId}-${model.modelName.toLowerCase()}`;

          try {
            const db = await this.createDatabase(dbName, model.dbType, 'user');
            databases[`${model.modelName.toLowerCase()}DB`] = db.address.toString();

            console.log(`✓ Created user database: ${dbName} (${model.dbType})`);
          } catch (error) {
            console.error(`❌ Failed to create user database ${dbName}:`, error);
            throw error;
          }
        }

        // Store mappings in the mappings database
        await mappingsDB.set('mappings', databases);
        console.log(`✓ Stored database mappings for user ${userId}`);

        // Register in global directory
        await this.registerUserInDirectory(userId, mappingsDB.address.toString());

        const userMappings = new UserMappingsData(userId, databases);

        // Cache for future use
        this.userMappings.set(userId, userMappings);

        console.log(`✅ User databases created successfully for ${userId}`);
        return userMappings;
      }

      async getUserDatabase(userId: string, modelName: string): Promise<any> {
        const mappings = await this.getUserMappings(userId);
        const dbKey = `${modelName.toLowerCase()}DB`;
        const dbAddress = mappings.databases[dbKey];

        if (!dbAddress) {
          throw new Error(`Database not found for user ${userId} and model ${modelName}`);
        }

        // Check if we have this database cached
        const cacheKey = `${userId}-${modelName}`;
        if (this.databases.has(cacheKey)) {
          return this.databases.get(cacheKey);
        }

        // Open the database
        const db = await this.openDatabase(dbAddress);
        this.databases.set(cacheKey, db);

        return db;
      }

      async getUserMappings(userId: string): Promise<UserMappingsData> {
        // Check cache first
        if (this.userMappings.has(userId)) {
          return this.userMappings.get(userId);
        }

        // Get from global directory
        const shardIndex = this.getShardIndex(userId, this.globalDirectoryShards.length);
        const shard = this.globalDirectoryShards[shardIndex];

        if (!shard) {
          throw new Error('Global directory not initialized');
        }

        const mappingsAddress = await shard.get(userId);
        if (!mappingsAddress) {
          throw new Error(`User ${userId} not found in directory`);
        }

        const mappingsDB = await this.openDatabase(mappingsAddress);
        const mappings = await mappingsDB.get('mappings');

        if (!mappings) {
          throw new Error(`No database mappings found for user ${userId}`);
        }

        const userMappings = new UserMappingsData(userId, mappings);

        // Cache for future use
        this.userMappings.set(userId, userMappings);

        return userMappings;
      }

      async getGlobalDatabase(modelName: string): Promise<any> {
        const db = this.globalDatabases.get(modelName);
        if (!db) {
          throw new Error(`Global database not found for model: ${modelName}`);
        }
        return db;
      }

      async getGlobalDirectoryShards(): Promise<any[]> {
        return this.globalDirectoryShards;
      }

      private async createDatabase(name: string, type: StoreType, _scope: string): Promise<any> {
        try {
          const db = await this.orbitDBService.openDatabase(name, type);

          // Store database reference
          this.databases.set(name, db);

          return db;
        } catch (error) {
          console.error(`Failed to create database ${name}:`, error);
          throw new Error(`Database creation failed for ${name}: ${error}`);
        }
      }

      private async openDatabase(address: string): Promise<any> {
        try {
          // Check if we already have this database cached by address
          if (this.databases.has(address)) {
            return this.databases.get(address);
          }

          // Open database by address (implementation may vary based on OrbitDB version)
          const orbitdb = this.orbitDBService.getOrbitDB();
          const db = await orbitdb.open(address);

          // Cache the database
          this.databases.set(address, db);

          return db;
        } catch (error) {
          console.error(`Failed to open database at address ${address}:`, error);
          throw new Error(`Database opening failed: ${error}`);
        }
      }

      private async registerUserInDirectory(userId: string, mappingsAddress: string): Promise<void> {
        const shardIndex = this.getShardIndex(userId, this.globalDirectoryShards.length);
        const shard = this.globalDirectoryShards[shardIndex];

        if (!shard) {
          throw new Error('Global directory shards not initialized');
        }

        try {
          await shard.set(userId, mappingsAddress);
          console.log(`✓ Registered user ${userId} in directory shard ${shardIndex}`);
        } catch (error) {
          console.error(`Failed to register user ${userId} in directory:`, error);
          throw error;
        }
      }

      private getShardIndex(key: string, shardCount: number): number {
        // Simple hash-based sharding
        let hash = 0;
        for (let i = 0; i < key.length; i++) {
          hash = ((hash << 5) - hash + key.charCodeAt(i)) & 0xffffffff;
        }
        return Math.abs(hash) % shardCount;
      }

      // Database operation helpers
      async getAllDocuments(database: any, dbType: StoreType): Promise<any[]> {
        try {
          switch (dbType) {
            case 'eventlog':
              const iterator = database.iterator();
              return iterator.collect();

            case 'keyvalue':
              return Object.values(database.all());

            case 'docstore':
              return database.query(() => true);

            case 'feed':
              const feedIterator = database.iterator();
              return feedIterator.collect();

            case 'counter':
              return [{ value: database.value, id: database.id }];

            default:
              throw new Error(`Unsupported database type: ${dbType}`);
          }
        } catch (error) {
          console.error(`Error fetching documents from ${dbType} database:`, error);
          throw error;
        }
      }

      async addDocument(database: any, dbType: StoreType, data: any): Promise<string> {
        try {
          switch (dbType) {
            case 'eventlog':
              return await database.add(data);

            case 'keyvalue':
              await database.set(data.id, data);
              return data.id;

            case 'docstore':
              return await database.put(data);

            case 'feed':
              return await database.add(data);

            case 'counter':
              await database.inc(data.amount || 1);
              return database.id;

            default:
              throw new Error(`Unsupported database type: ${dbType}`);
          }
        } catch (error) {
          console.error(`Error adding document to ${dbType} database:`, error);
          throw error;
        }
      }

      async updateDocument(database: any, dbType: StoreType, id: string, data: any): Promise<void> {
        try {
          switch (dbType) {
            case 'keyvalue':
              await database.set(id, data);
              break;

            case 'docstore':
              await database.put(data);
              break;

            default:
              // For append-only stores, we add a new entry
              await this.addDocument(database, dbType, data);
          }
        } catch (error) {
          console.error(`Error updating document in ${dbType} database:`, error);
          throw error;
        }
      }

      async deleteDocument(database: any, dbType: StoreType, id: string): Promise<void> {
        try {
          switch (dbType) {
            case 'keyvalue':
              await database.del(id);
              break;

            case 'docstore':
              await database.del(id);
              break;

            default:
              // For append-only stores, we might add a deletion marker
              await this.addDocument(database, dbType, { _deleted: true, id, deletedAt: Date.now() });
          }
        } catch (error) {
          console.error(`Error deleting document from ${dbType} database:`, error);
          throw error;
        }
      }

      // Cleanup methods
      async stop(): Promise<void> {
        console.log('🛑 Stopping DatabaseManager...');

        // Clear caches
        this.databases.clear();
        this.userMappings.clear();
        this.globalDatabases.clear();
        this.globalDirectoryShards = [];

        this.initialized = false;
        console.log('✅ DatabaseManager stopped');
      }
    }