v0.7.0-alpha.0
Home / Documentation / Connections

Connections

Manage startup, shared access, independent stores and clean shutdown.

The shared connection

javascript
const BsonLite = require('./BsonLite.cjs');

async function main() {
  const database = await BsonLite.connect('myapp');
  await BsonLite.ping();
  console.log(BsonLite.status().port);
  // Now start your application.
}
main().catch(console.error);

connect and start are aliases. They return the shared Database handle. Concurrent calls with matching settings reuse the same startup operation. Conflicting settings are refused. A default name is inferred from existing supported metadata, or bsonlite for a new unnamed store.

From another file

javascript
// users.js — called after connect has completed
const BsonLite = require('./BsonLite.cjs');

module.exports.findUsers = () => {
  return BsonLite.collection('users').find({ active: true }).toArray();
};

Use the same BsonLite file path in your application. Calling collection or ping before connect completes throws BSONLITE_NOT_STARTED. Queries are asynchronous; find returns a cursor that you can materialize with toArray.

Independent database handles

javascript
const inventory = await BsonLite.open({
  name: 'inventory',
  directory: './InventoryDatabase'
});

try {
  await inventory.collection('products').insertOne({ sku: 'NOTE-01' });
  const names = await inventory.listCollections().toArray();
} finally {
  await inventory.close();
}

open accepts an options object and creates a separately owned handle. It does not configure the top-level shared connection. Separate stores need separate directories; opening the same directory twice is refused. Port selection is automatic for each engine.

Connect from Console

The updated download announces running BsonLite connections to BsonLite Console automatically. Start the app, select its database in Console and click Connect. No extra project folder or helper setup is needed. The app must stay running under the same OS account. Read the Console guide for BsonLite update requirements, manual URLs and write access.

To disable automatic tool access, pass console: false in the connect options or set BSONLITE_CONSOLE=0 before starting the app. Stop the existing BsonLite connection before changing this setting.

Shut down cleanly

javascript
process.once('SIGINT', async () => {
  // First stop accepting application requests.
  await BsonLite.disconnect();
});
process.once('SIGTERM', async () => {
  await BsonLite.disconnect();
});

disconnect and stop are aliases for the shared instance. A Database handle uses close. Await shutdown before taking a physical backup or attempting migration. The supplied starter coordinates HTTP server closure with database shutdown.

BsonLite 0.7.0-alpha.0 · BsonLite Console 0.3.0 · BsonLite source · Downloads