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
- 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
ts
const { content: book } = await collection.get(bookId);
const { content: [firstAuthor], } = await collection.lookupIn(bookId).get('authors[0]');
IDE Autocompletion
ts
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
ts
// 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
ts
// 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.
ts
const { expiryTime } = await collection.get(bookId);
const { expiryTime } = await collection.get(bookId, { withExpiry: true });