The existing D1 database to integrate
The shard binding name for this database
KVShardMapper instance for storing mappings
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.
Optional
addShardMappingsTable?: booleanOptional
dryRun?: booleanOptional
migrateOtherColumns?: booleanOptional
primaryKeyColumn?: stringOptional
strategy?: ShardingStrategyOptional
tables?: string[]Promise resolving to integration summary
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
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.