v0.6.0-rc.1
HomeDocumentationGetting started

Connect once. Use it everywhere.

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 SDK 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.

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.

Documentation for BsonLite 0.6.0-rc.1. View SDK source · Download the validated SDK