@functional-systems/lambdadb
v0.3.2
Published
Developer-friendly & type-safe Typescript SDK specifically catered to leverage *LambdaDB* API.
Readme
lambdadb
Developer-friendly & type-safe Typescript SDK specifically catered to leverage LambdaDB API.
Summary
LambdaDB API: LambdaDB Open API Spec
Table of Contents
SDK Installation
The SDK can be installed with either npm, pnpm, bun or yarn package managers.
NPM
npm add @functional-systems/lambdadbPNPM
pnpm add @functional-systems/lambdadbBun
bun add @functional-systems/lambdadbYarn
yarn add @functional-systems/lambdadb[!NOTE] This package is published with CommonJS and ES Modules (ESM) support.
Requirements
For supported JavaScript runtimes, please consult RUNTIMES.md.
SDK Example Usage
We recommend the collection-scoped client (LambdaDBClient): you get a handle for a collection once and then call methods without passing collectionName on every request.
Recommended: LambdaDBClient (collection-scoped)
The client connects to {baseUrl}/projects/{projectName}. Defaults: baseUrl https://api.lambdadb.ai, projectName playground. Override with baseUrl and projectName when creating the client.
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
projectApiKey: "<YOUR_PROJECT_API_KEY>",
// Optional: baseUrl (default "https://api.lambdadb.ai"), projectName (default "playground")
});
async function run() {
// List all collections in the project (optional: pass { size, pageToken } for pagination)
const list = await client.listCollections();
console.log(list); // list.collections[].createdAt etc. are Date
// Work with a specific collection — no collectionName in every call
const collection = client.collection("my-collection");
await collection.get();
await collection.docs.list({ size: 20 });
await collection.docs.upsert({ docs: [{ id: "1", text: "hello" }] });
// For large document sets (up to 200MB), use bulkUpsertDocs for a single-call flow
// await collection.docs.bulkUpsertDocs({ docs: largeDocArray });
}
run();TypeScript types
Import request and response types from the package for type-safe usage. Use the input types for method arguments and the response types for return values.
import {
LambdaDBClient,
type CreateCollectionInput,
type QueryCollectionInput,
type QueryCollectionResponse,
type ListDocsInput,
type ListDocsResponse,
} from "@functional-systems/lambdadb";
const client = new LambdaDBClient({ projectApiKey: "..." });
const collection = client.collection("my-collection");
// Typed list params
const params: ListDocsInput = { size: 20, pageToken: undefined };
const listResult: ListDocsResponse = await collection.docs.list(params);
// Typed query body and response (or use createQueryInput helper)
const queryBody: QueryCollectionInput = {
query: { text: "hello" },
size: 10,
};
const queryResult: QueryCollectionResponse = await collection.query(queryBody);Common types: CreateCollectionInput, UpdateCollectionInput, QueryCollectionInput, ListDocsInput, ListCollectionsInput, UpsertDocsInput, DeleteDocsInput, FetchDocsInput, BulkUpsertInput; response types such as QueryCollectionResponse, ListDocsResponse, ListCollectionsResponseWithDates, GetCollectionResponseWithDates, FetchDocsResponse, MessageResponse; and model types like CollectionResponseWithDates, IndexConfigsUnion, PartitionConfig, FieldsSelectorUnion. Collection list/get responses expose timestamp fields (createdAt, updatedAt, dataUpdatedAt) as Date. All are exported from the main package.
Pagination
Documents: Use listPages() to iterate over all pages without loading everything into memory, or listAll() to fetch all docs into a single list. Each page is one API response; the API limits response size by payload, not by document count, so the number of docs per page may be less than the requested size and can vary from page to page. The methods list(), query(), and fetch() automatically resolve documents from the presigned URL when the API returns them via docsUrl (isDocsInline: false), so you always receive docs in the response.
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({ projectApiKey: "..." });
const collection = client.collection("my-collection");
// Page-by-page (memory efficient)
for await (const page of collection.docs.listPages({ size: 50 })) {
console.log(page.docs.length, page.nextPageToken ?? "last page");
}
// Or load all docs (for small/medium collections)
const { docs, total } = await collection.docs.listAll({ size: 100 });
console.log(docs.length, total);Collections: Use listCollections(params?) with optional size and pageToken, or listCollectionsPages(params?) / listAllCollections(params?) to iterate or fetch all collections across pages.
// One page
const page = await client.listCollections({ size: 20 });
// Page-by-page
for await (const page of client.listCollectionsPages({ size: 20 })) {
console.log(page.collections.length, page.nextPageToken ?? "last");
}
// All collections
const { collections } = await client.listAllCollections({ size: 50 });Query helper
Use createQueryInput() to build query parameters for collection.query() or collection.querySafe():
import { LambdaDBClient, createQueryInput } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({ projectApiKey: "..." });
const collection = client.collection("my-collection");
const input = createQueryInput({ text: "hello" }, { size: 10 });
const result = await collection.query(input);Authentication
Per-Client Security Schemes
This SDK supports the following security scheme globally:
| Name | Type | Scheme | Environment Variable |
| --------------- | ------ | ------- | -------------------------- |
| projectApiKey | apiKey | API key | LAMBDADB_PROJECT_API_KEY |
To authenticate with the API the projectApiKey parameter must be set when initializing the SDK client instance. For example:
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
projectApiKey: "<YOUR_PROJECT_API_KEY>",
});
async function run() {
const result = await client.listCollections();
console.log(result);
}
run();Available Resources and Operations
Collections
- list - List all collections in an existing project (supports pagination:
size,pageToken). On LambdaDBClient uselistCollections(params?),listCollectionsPages(params?), orlistAllCollections(params?)for iteration; collection responses includecreatedAt/updatedAt/dataUpdatedAtasDate. - create - Create a collection.
- delete - Delete an existing collection.
- get - Get metadata of an existing collection.
- update - Configure a collection.
- query - Search a collection with a query and return the most similar documents.
Collections.Docs
- listDocs - List documents in a collection.
- upsert - Upsert documents into a collection. Note that the maximum supported payload size is 6MB.
- bulkUpsertDocs - Bulk upsert documents in one call (up to 200MB); use this for best DX when you have a document list.
- getBulkUpsert - Request required info to upload documents.
- bulkUpsert - Bulk upsert documents into a collection. Note that the maximum supported object size is 200MB.
- update - Update documents in a collection. Note that the maximum supported payload size is 6MB.
- delete - Delete documents by document IDs or query filter from a collection.
- fetch - Lookup and return documents by document IDs from a collection.
Legacy API (LambdaDB)
The classic client LambdaDB is still supported for compatibility. New code should prefer LambdaDBClient and client.collection(name).
import { LambdaDB } from "@functional-systems/lambdadb";
const lambdaDB = new LambdaDB({ projectApiKey: "<YOUR_PROJECT_API_KEY>" });
const result = await lambdaDB.collections.list();
await lambdaDB.collections.docs.listDocs({ collectionName: "my-collection", size: 20 });See docs/sdks/collections/README.md and docs/sdks/docs/README.md for the full legacy API reference.
Standalone functions
All the methods listed above are available as standalone functions. These functions are ideal for use in applications running in the browser, serverless runtimes or other environments where application bundle size is a primary concern. When using a bundler to build your application, all unused functionality will be either excluded from the final bundle or tree-shaken away.
To read more about standalone functions, check FUNCTIONS.md.
collectionsCreate- Create a collection.collectionsDelete- Delete an existing collection.collectionsDocsBulkUpsert- Bulk upsert documents into a collection. Note that the maximum supported object size is 200MB.collectionsDocsDelete- Delete documents by document IDs or query filter from a collection.collectionsDocsFetch- Lookup and return documents by document IDs from a collection.collectionsDocsGetBulkUpsert- Request required info to upload documents.collectionsDocsListDocs- List documents in a collection.collectionsDocsUpdate- Update documents in a collection. Note that the maximum supported payload size is 6MB.collectionsDocsUpsert- Upsert documents into a collection. Note that the maximum supported payload size is 6MB.collectionsGet- Get metadata of an existing collection.collectionsList- List all collections in an existing project.collectionsQuery- Search a collection with a query and return the most similar documents.collectionsUpdate- Configure a collection.
Retries
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a retryConfig object to the call:
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
projectApiKey: "<YOUR_PROJECT_API_KEY>",
});
async function run() {
const result = await client.listCollections(undefined, {
retries: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
});
console.log(result);
}
run();If you'd like to override the default retry strategy for all operations that support retries, you can provide a retryConfig at SDK initialization:
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
retryConfig: {
strategy: "backoff",
backoff: {
initialInterval: 1,
maxInterval: 50,
exponent: 1.1,
maxElapsedTime: 100,
},
retryConnectionErrors: false,
},
projectApiKey: "<YOUR_PROJECT_API_KEY>",
});
async function run() {
const result = await client.listCollections();
console.log(result);
}
run();
Timeout and request-level options: You can set a request timeout (ms) and retry behavior when creating the client or per call. If not set, there is no request timeout. The RetryConfig type is exported from the package for typing your options.
import { LambdaDBClient, type RetryConfig } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
projectApiKey: "...",
timeoutMs: 30_000, // 30s timeout for all requests
retryConfig: { strategy: "backoff", retryConnectionErrors: true },
});
// Override per request
await client.listCollections(undefined, { timeoutMs: 10_000, retries: { strategy: "none" } });Error Handling
LambdaDBError is the base class for all HTTP error responses. It has the following properties:
| Property | Type | Description |
| ------------------- | ---------- | --------------------------------------------------------------------------------------- |
| error.message | string | Error message |
| error.statusCode | number | HTTP response status code eg 404 |
| error.headers | Headers | HTTP response headers |
| error.body | string | HTTP body. Can be empty string if no body is returned. |
| error.rawResponse | Response | Raw HTTP response |
| error.data$ | | Optional. Some errors may contain structured data. See Error Classes. |
Default methods (throw on error)
Regular methods throw on failure. Catch errors and use instanceof to narrow types. Error classes are exported from the main package:
import {
LambdaDBClient,
LambdaDBError,
UnauthenticatedError,
ResourceNotFoundError,
} from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
projectApiKey: "<YOUR_PROJECT_API_KEY>",
});
async function run() {
try {
const result = await client.listCollections();
console.log(result);
} catch (error) {
if (error instanceof LambdaDBError) {
console.log(error.message, error.statusCode, error.body);
if (error instanceof UnauthenticatedError) {
console.log(error.data$.message);
}
if (error instanceof ResourceNotFoundError) {
console.log("Not found:", error.data$);
}
}
}
}
run();Safe methods (return Result)
Use *Safe methods to get a Result<T, E> instead of throwing. You can handle errors without try/catch and narrow with type guards:
import {
LambdaDBClient,
Result,
ResourceNotFoundError,
} from "@functional-systems/lambdadb";
const client = new LambdaDBClient({ projectApiKey: "..." });
const result = await client.listCollectionsSafe();
if (result.ok) {
console.log(result.value.collections);
} else {
const err = result.error;
if (err instanceof ResourceNotFoundError) {
console.log("Not found:", err.data$);
} else {
console.error(err);
}
}Available Safe methods: listCollectionsSafe, createCollectionSafe, collection.getSafe, collection.updateSafe, collection.deleteSafe, collection.querySafe, collection.docs.listSafe, collection.docs.upsertSafe, collection.docs.updateSafe, collection.docs.deleteSafe, collection.docs.fetchSafe, collection.docs.getBulkUpsertSafe, collection.docs.bulkUpsertSafe, collection.docs.bulkUpsertDocsSafe. The Result type and OK / ERR helpers are exported from the package.
Error Classes
Primary errors:
LambdaDBError: The base class for HTTP error responses.UnauthenticatedError: Unauthenticated. Status code401.TooManyRequestsError: Too many requests. Status code429.InternalServerError: Internal server error. Status code500.ResourceNotFoundError: Resource not found. Status code404. *
Network errors:
ConnectionError: HTTP client was unable to make a request to a server.RequestTimeoutError: HTTP request timed out due to an AbortSignal signal.RequestAbortedError: HTTP request was aborted by the client.InvalidRequestError: Any input used to create a request is invalid.UnexpectedClientError: Unrecognised or unexpected error.
Inherit from LambdaDBError:
BadRequestError: Bad request. Status code400. Applicable to 9 of 13 methods.*ResourceAlreadyExistsError: Resource already exists. Status code409. Applicable to 1 of 13 methods.*ResponseValidationError: Type mismatch between the data returned from the server and the structure expected by the SDK. Seeerror.rawValuefor the raw value anderror.pretty()for a nicely formatted multi-line string.
* Check the method documentation to see if the error is applicable.
Server Selection (API base URL)
LambdaDBClient builds the API base as {baseUrl}/projects/{projectName}. You can override the defaults when creating the client.
| Option | Type | Default | Description |
| -------------- | -------- | --------------------------- | ------------------------------------ |
| baseUrl | string | "https://api.lambdadb.ai" | API base URL (no trailing slash). |
| projectName | string | "playground" | Project name (path segment). |
| serverURL | string | — | Full base URL (overrides baseUrl + projectName). |
| projectHost | string | — | Legacy: host path for URL (e.g. api.lambdadb.ai/projects/my-project). |
Using baseUrl and projectName (recommended)
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
projectApiKey: "<YOUR_PROJECT_API_KEY>",
baseUrl: "https://api.lambdadb.ai",
projectName: "my-project",
});
const result = await client.listCollections();Override with full server URL
To set the base URL in one go, use serverURL:
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({
serverURL: "https://api.lambdadb.ai/projects/my-project",
projectApiKey: "<YOUR_PROJECT_API_KEY>",
});
const result = await client.listCollections();Custom HTTP Client
The TypeScript SDK makes API calls using an HTTPClient that wraps the native
Fetch API. This
client is a thin wrapper around fetch and provides the ability to attach hooks
around the request lifecycle that can be used to modify the request or handle
errors and response.
The HTTPClient constructor takes an optional fetcher argument that can be
used to integrate a third-party HTTP client or when writing tests to mock out
the HTTP client and feed in fixtures.
The following example shows how to use the "beforeRequest" hook to to add a
custom header and a timeout to requests and how to use the "requestError" hook
to log errors:
import { LambdaDBClient } from "@functional-systems/lambdadb";
import { HTTPClient } from "@functional-systems/lambdadb/lib/http";
const httpClient = new HTTPClient({
// fetcher takes a function that has the same signature as native `fetch`.
fetcher: (request) => {
return fetch(request);
}
});
httpClient.addHook("beforeRequest", (request) => {
const nextRequest = new Request(request, {
signal: request.signal || AbortSignal.timeout(5000)
});
nextRequest.headers.set("x-custom-header", "custom value");
return nextRequest;
});
httpClient.addHook("requestError", (error, request) => {
console.group("Request Error");
console.log("Reason:", `${error}`);
console.log("Endpoint:", `${request.method} ${request.url}`);
console.groupEnd();
});
const client = new LambdaDBClient({ httpClient, projectApiKey: "<YOUR_PROJECT_API_KEY>" });Debugging
You can setup your SDK to emit debug logs for SDK requests and responses.
You can pass a logger that matches console's interface as an SDK option.
[!WARNING] Beware that debug logging will reveal secrets, like API tokens in headers, in log messages printed to a console or files. It's recommended to use this feature only during local development and not in production.
import { LambdaDBClient } from "@functional-systems/lambdadb";
const client = new LambdaDBClient({ debugLogger: console, projectApiKey: "<YOUR_PROJECT_API_KEY>" });You can also enable a default debug logger by setting an environment variable LAMBDADB_DEBUG to true.
Development
Maturity
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version unless you are intentionally looking for the latest version.
Contributions
We welcome contributions. The recommended API is implemented in src/client.ts (LambdaDBClient, collection-scoped). The rest of src/ (funcs, models, lib) is maintained manually; see docs/OPENAPI_UPDATE.md for how API changes are applied. Feel free to open a PR or an issue.
