@earth-app/collegedb
    Preparing search index...

    Function integrateExistingDatabase

    • Performs a complete drop-in integration of an existing database. This is the main function for integrating CollegeDB with an existing database that already contains data. It discovers tables, validates them, creates shard mappings, and optionally adds the shard_mappings table if needed.

      When migrateOtherColumns is enabled, the function will also create additional lookup keys for username, email, and name columns if they exist in the table. This allows these fields to be used as lookup keys in addition to the primary key.

      Parameters

      • d1: D1Database

        The existing D1 database to integrate

      • shardName: string

        The shard binding name for this database

      • mapper: KVShardMapper

        KVShardMapper instance for storing mappings

      • options: IntegrationOptions = {}

        Configuration options for the integration

        Configuration options for integrating an existing database with CollegeDB. Allows customization of which tables to process, primary key column, sharding strategy, and whether to add the shard_mappings table.

        • OptionaladdShardMappingsTable?: boolean
        • OptionaldryRun?: boolean
        • OptionalmigrateOtherColumns?: boolean
        • OptionalprimaryKeyColumn?: string
        • Optionalstrategy?: ShardingStrategy
        • Optionaltables?: string[]

      Returns Promise<IntegrationResult>

      Promise resolving to integration summary

      If integration fails

      import { KVShardMapper } from './kvmap.js';

      const mapper = new KVShardMapper(env.KV);
      const result = await integrateExistingDatabase(env.DB_EXISTING, 'db-existing', mapper, {
      tables: ['users', 'posts'],
      strategy: 'hash',
      addShardMappingsTable: true,
      migrateOtherColumns: true // Creates additional lookup keys
      });

      console.log(`Integrated ${result.totalRecords} records from ${result.tablesProcessed} tables`);
      // Now users can be looked up by username:john, email:john@example.com, or name:John Doe