Features
- Compatible with the official client
- Improved TypeScript function signatures - learn more
- Inferred return type for document and sub-document operation
- Path autocomplete for sub-document operation
- Chainable sub-document operations
- Async Stack trace
- HTTP Client
- Vitest fixtures
Compatible with the official client
Cbjs is compatible with the official client.
The main goal of Cbjs is to bring TypeScript support. Cbjs also bring more sound and consistent behaviour, sometimes leading to small runtime changes. See runtime changes.
KeyValue Supercharged Types
Once you have defined your Cluster Types, you will unlock many powers.
Inferred return type
const { content: book } = await collection.get(bookId);
const { content: [firstAuthor], } = await collection.lookupIn(bookId).get('authors[0]');
IDE Autocompletion
const {
content: [title],
} = await collection
.lookupIn(bookId)
.get('ti');
const {
content: [quaterSales],
} = await collection
.lookupIn(bookId)
.get('qua');
You can learn more about this topic on the page dedicated to Cluster Types.
Discriminated Unions
Because Cbjs uses discriminated unions, type guards emerge naturally.
Sub-document lookup
// The official library would give you : { value?: any; error: Error | null }
// With Cbjs you get :
const { content: [title] } = await collection.lookupIn(bookId).get('title');
if (title.error) {
throw new Error('Failed to retrieve the title.');
}
// Because of the discriminated union, the previous condition acts as a type guard.
console.log(title);
Callbacks
// Check the type of `res` before the condition and after ✨
const result = await collection.get(bookId, (err, res) => {
if (err) return;
console.log(res);
});
Better overall result types
Another example of what Cbjs does for you.
const { expiryTime } = await collection.get(bookId);
const { expiryTime } = await collection.get(bookId, { withExpiry: true });
Async stack trace
Because the official client is a C++ binding, the stack trace is not available by default when a method throws an error.
Asynchronous calls internal to the library have been rewritten in order to provide an actual, useful stack trace at runtime.
Without cbjs
:
node:internal/process/esm_loader:97
internalBinding('errors').triggerUncaughtException(
With cbjs
:
DocumentNotFoundError: document not found
at errorFromCpp (/project/node_modules/@cbjsdev/cbjs/src/bindingutilities.ts:787:14)
at Collection.remove (/project/node_modules/@cbjsdev/cbjs/src/collection.ts:1417:19)
at remove (/project/index.ts:31:3)