CollegeDB Configuration
The callback to perform after the initialization
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']);
});
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, usesconfigeven when anothercollegedbcall 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 withoutnodejs_compat), a callback runs against the module-level state likeinitializedoes. A different configuration started while another callback is running then throwsCONCURRENT_CONFIG, and the same configuration may overlap. createClient needs no async context and is the only isolation-safe API there.