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

    Function collegedb

    • Initializes the configuration and then performs a callback once the configuration has finished initializing.

      The callback runs against a state of its own, so every module-level call it makes, before or after an await, uses config even when another collegedb call with a different configuration overlaps it. The module-level configuration is left as it was. A later call with the same bindings and options reuses that state, so per-request callers keep their caches.

      That isolation needs AsyncLocalStorage. On a runtime without it (Workers without nodejs_compat), a callback runs against the module-level state like initialize does. A different configuration started while another callback is running then throws CONCURRENT_CONFIG, and the same configuration may overlap. createClient needs no async context and is the only isolation-safe API there.

      Type Parameters

      • T

      Parameters

      • config: CollegeDBConfig

        CollegeDB Configuration

      • callback: () => T

        The callback to perform after the initialization

      Returns Promise<Awaited<T>>

      The result of the callback

      CONCURRENT_CONFIG when there is no async context and a callback with a different configuration is running

      import { collegedb, first } from 'collegedb'

      const result = collegedb({
      kv: env.KV,
      shards: {
      'db-primary': env.DB_PRIMARY, // Existing DB with data
      'db-secondary': env.DB_SECONDARY // Another existing DB
      },
      strategy: 'hash'
      }, async () => {
      return await first('user-123', 'SELECT * FROM users WHERE id = ?', ['user-123']);
      });