lambdadb

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 1, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

README

lambdadb

Developer-friendly & type-safe Go SDK specifically catered to leverage lambdadb API.

Summary

LambdaDB API: LambdaDB Open API Spec

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/lambdadb/go-lambdadb

SDK Example Usage

Example
package main

import (
	"context"
	lambdadb "github.com/lambdadb/go-lambdadb"
	"log"
)

func main() {
	ctx := context.Background()

	client := lambdadb.New(
		lambdadb.WithBaseURL("https://api.lambdadb.ai"),
		lambdadb.WithProjectName("playground"),
		lambdadb.WithAPIKey("<YOUR_PROJECT_API_KEY>"),
	)

	res, err := client.Collections.List(ctx, nil)
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response (e.g. res.Collections)
	}
}

Collection-scoped usage

Use client.Collection(name) to work with a single collection without passing the collection name on every call:

	coll := client.Collection("my-collection")
	meta, err := coll.Get(ctx)
	docs, err := coll.Docs().List(ctx, nil)
	err = coll.Docs().Upsert(ctx, lambdadb.UpsertDocsInput{Docs: myDocs})

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

You can configure it using the WithAPIKey option when initializing the SDK client instance. For example:

package main

import (
	"context"
	lambdadb "github.com/lambdadb/go-lambdadb"
	"log"
)

func main() {
	ctx := context.Background()

	client := lambdadb.New(
		lambdadb.WithBaseURL("https://api.lambdadb.ai"),
		lambdadb.WithProjectName("playground"),
		lambdadb.WithAPIKey("<YOUR_PROJECT_API_KEY>"),
	)

	res, err := client.Collections.List(ctx, nil)
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response (e.g. res.Collections)
	}
}

Available Resources and Operations

Available methods
Project-level: client.Collections
  • List - List all collections in the project.
  • Create - Create a collection.
Collection-scoped: client.Collection(name)

Use client.Collection("name") for operations on a single collection (no need to pass the collection name on every call):

  • Collection: Get, Update, Delete, Query (metadata and search). When the API returns isDocsInline=false and docsUrl, Query automatically fetches docs from the presigned URL so result.Docs is always populated.
  • Collection.Docs(): List, Upsert, Fetch, Update, Delete, GetBulkUpsertInfo, BulkUpsert, BulkUpsertDocuments (document operations). List and Fetch do the same presigned-URL resolution for docs when the API returns isDocsInline=false and docsUrl. Use BulkUpsertDocuments for a one-step bulk upload (presigned URL + upload + complete). Bulk upsert payload is limited to 200MB (lambdadb.MaxBulkUpsertPayloadBytes); see docs API for details.

Pagination

List endpoints return one page at a time. Use iterators to walk all pages without managing tokens, or ListAll to load every page into a single slice.

Iterator (page-by-page)
	// Documents
	iter := client.Collection("my-collection").Docs().ListIterator(ctx, &lambdadb.ListDocsOpts{Size: lambdadb.Int64(100)})
	for {
		page, err := iter.Next(ctx)
		if err != nil {
			return err
		}
		if page == nil {
			break
		}
		for _, d := range page.Docs {
			// d is ListDocsDoc: d.Collection, d.Doc
		}
	}

	// Collections
	iter := client.Collections.ListIterator(ctx, &lambdadb.ListCollectionsOpts{Size: lambdadb.Int64(20)})
	for {
		page, err := iter.Next(ctx)
		if err != nil {
			return err
		}
		if page == nil {
			break
		}
		for _, c := range page.Collections {
			// process collection
		}
	}
ListAll (fetch all pages)

Use when you need the full list in memory; avoid on very large datasets.

	docs, err := client.Collection("my-collection").Docs().ListAll(ctx, &lambdadb.ListDocsOpts{Size: lambdadb.Int64(100)})
	// ...
	colls, err := client.Collections.ListAll(ctx, &lambdadb.ListCollectionsOpts{Size: lambdadb.Int64(20)})

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 retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	lambdadb "github.com/lambdadb/go-lambdadb"
	"github.com/lambdadb/go-lambdadb/retry"
	"log"
	"models/operations"
)

func main() {
	ctx := context.Background()

	client := lambdadb.New(
		lambdadb.WithAPIKey("<YOUR_PROJECT_API_KEY>"),
	)

	res, err := client.Collections.List(ctx, nil, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response (e.g. res.Collections)
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	lambdadb "github.com/lambdadb/go-lambdadb"
	"github.com/lambdadb/go-lambdadb/retry"
	"log"
)

func main() {
	ctx := context.Background()

	client := lambdadb.New(
		lambdadb.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		lambdadb.WithAPIKey("<YOUR_PROJECT_API_KEY>"),
	)

	res, err := client.Collections.List(ctx, nil)
	if err != nil {
		log.Fatal(err)
	}
	if res != nil {
		// handle response (e.g. res.Collections)
	}
}

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both.

By Default, an API error will return apierrors.APIError. When custom error responses are specified for an operation, the SDK may also return their associated error. You can refer to respective Errors tables in SDK docs for more details on possible error types for each operation.

For example, the List function may return the following errors:

Error Type Status Code Content Type
apierrors.UnauthenticatedError 401 application/json
apierrors.ResourceNotFoundError 404 application/json
apierrors.TooManyRequestsError 429 application/json
apierrors.InternalServerError 500 application/json
apierrors.APIError 4XX, 5XX */*
Example
package main

import (
	"context"
	"errors"
	lambdadb "github.com/lambdadb/go-lambdadb"
	"github.com/lambdadb/go-lambdadb/models/apierrors"
	"log"
)

func main() {
	ctx := context.Background()

	client := lambdadb.New(
		lambdadb.WithBaseURL("https://api.lambdadb.ai"),
		lambdadb.WithProjectName("playground"),
		lambdadb.WithAPIKey("<YOUR_PROJECT_API_KEY>"),
	)

	res, err := client.Collections.List(ctx, nil)
	if err != nil {
		var authErr *apierrors.UnauthenticatedError
		if errors.As(err, &authErr) {
			log.Fatal(authErr.Error())
		}
		var notFoundErr *apierrors.ResourceNotFoundError
		if errors.As(err, &notFoundErr) {
			log.Fatal(notFoundErr.Error())
		}
		var rateLimitErr *apierrors.TooManyRequestsError
		if errors.As(err, &rateLimitErr) {
			log.Fatal(rateLimitErr.Error())
		}
		var serverErr *apierrors.InternalServerError
		if errors.As(err, &serverErr) {
			log.Fatal(serverErr.Error())
		}
		var apiErr *apierrors.APIError
		if errors.As(err, &apiErr) {
			log.Fatal(apiErr.Error())
		}
	}
}

Configuration

Configuration follows the REST API path structure. Defaults match the OpenAPI spec.

Option Default Description
WithBaseURL(baseURL string) https://api.lambdadb.ai API base URL (scheme + host).
WithProjectName(projectName string) playground Project name (path segment).
WithAPIKey(apiKey string) (none) Project API key. Can also use LAMBDADB_PROJECT_API_KEY env.

The effective request base is BaseURL + "/projects/" + ProjectName. Example:

	client := lambdadb.New(
		lambdadb.WithBaseURL("https://api.lambdadb.ai"),
		lambdadb.WithProjectName("my-project"),
		lambdadb.WithAPIKey("your-api-key"),
	)

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"

	"github.com/lambdadb/go-lambdadb"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = lambdadb.New(lambdadb.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

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. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

Documentation

Index

Constants

View Source
const MaxBulkUpsertPayloadBytes = 200 * 1024 * 1024

MaxBulkUpsertPayloadBytes is the typical LambdaDB bulk upsert payload limit (200MB). The actual limit is returned by GetBulkUpsertInfo (sizeLimitBytes); use this constant only for reference or when validating before calling the API.

View Source
const Version = "0.3.0"

Version is the SDK version. Use it for SDKVersion and User-Agent.

Variables

This section is empty.

Functions

func Bool

func Bool(b bool) *bool

Bool returns a pointer to b.

func Float32

func Float32(f float32) *float32

Float32 returns a pointer to f.

func Float64

func Float64(f float64) *float64

Float64 returns a pointer to f.

func Int

func Int(i int) *int

Int returns a pointer to i.

func Int64

func Int64(i int64) *int64

Int64 returns a pointer to i.

func Pointer

func Pointer[T any](v T) *T

Pointer returns a pointer to v.

func String

func String(s string) *string

String returns a pointer to s.

Types

type BulkUpsertInput added in v0.2.0

type BulkUpsertInput = operations.BulkUpsertDocsRequestBody

BulkUpsertInput is the body for bulk upsert (alias of operations.BulkUpsertDocsRequestBody).

type Client added in v0.2.0

type Client struct {
	SDKVersion string

	// Collections provides project-level collection operations (list and create).
	Collections *ProjectCollections
	// contains filtered or unexported fields
}

Client is the LambdaDB API client.

func New

func New(opts ...SDKOption) *Client

New creates a new LambdaDB client with the given options.

func (*Client) Collection added in v0.2.0

func (c *Client) Collection(name string) *Collection

Collection returns a handle for the named collection. Use it for collection-level operations and document operations without passing the collection name each time.

type Collection added in v0.2.0

type Collection struct {
	// contains filtered or unexported fields
}

Collection is a handle for a single collection (by name). Use it for collection-level operations and document operations without passing the collection name each time.

func (*Collection) Delete added in v0.2.0

Delete deletes the collection.

func (*Collection) Docs added in v0.2.0

func (c *Collection) Docs() *CollectionDocs

Docs returns a handle for document operations on this collection.

func (*Collection) Get added in v0.2.0

Get returns metadata for the collection.

func (*Collection) Query added in v0.2.0

func (c *Collection) Query(ctx context.Context, input QueryInput, opts ...operations.Option) (*QueryResult, error)

Query runs a search query on the collection. When the API returns isDocsInline=false and docsUrl, the SDK fetches docs from the presigned URL automatically so result.Docs is always populated.

func (*Collection) Update added in v0.2.0

Update updates the collection configuration and returns the updated collection metadata.

type CollectionDocs added in v0.2.0

type CollectionDocs struct {
	// contains filtered or unexported fields
}

CollectionDocs provides document operations for a single collection.

func (*CollectionDocs) BulkUpsert added in v0.2.0

BulkUpsert bulk upserts documents into the collection. The uploaded object (via presigned URL) must not exceed the size limit returned by GetBulkUpsertInfo (e.g. 200MB).

func (*CollectionDocs) BulkUpsertDocuments added in v0.2.0

func (d *CollectionDocs) BulkUpsertDocuments(ctx context.Context, body UpsertDocsInput, opts ...operations.Option) (*operations.BulkUpsertDocsResponse, error)

BulkUpsertDocuments runs the full bulk upsert flow: obtains a presigned URL, uploads the documents as JSON, and completes the bulk upsert. It is a convenience over calling GetBulkUpsertInfo, uploading to the presigned URL, and BulkUpsert separately. The body format is the same as Upsert (docs array). When the API returns sizeLimitBytes in GetBulkUpsertInfo, payload size is validated against it before uploading.

func (*CollectionDocs) Delete added in v0.2.0

Delete deletes documents from the collection.

func (*CollectionDocs) Fetch added in v0.2.0

Fetch fetches documents by IDs from the collection. When the API returns isDocsInline=false and docsUrl, the SDK fetches docs from the presigned URL automatically so result.Docs is always populated.

func (*CollectionDocs) GetBulkUpsertInfo added in v0.2.0

func (d *CollectionDocs) GetBulkUpsertInfo(ctx context.Context, opts ...operations.Option) (*GetBulkUpsertInfoResult, error)

GetBulkUpsertInfo returns info required for bulk upload (presigned URL, object key, and optionally sizeLimitBytes). When sizeLimitBytes is present, the upload payload must not exceed it (e.g. LambdaDB uses 200MB).

func (*CollectionDocs) List added in v0.2.0

func (d *CollectionDocs) List(ctx context.Context, listOpts *ListDocsOpts, opts ...operations.Option) (*ListDocsResult, error)

List lists documents in the collection (one page). Pass nil for listOpts to use defaults (no size limit, no page token).

func (*CollectionDocs) ListAll added in v0.2.0

func (d *CollectionDocs) ListAll(ctx context.Context, listOpts *ListDocsOpts, opts ...operations.Option) ([]map[string]any, error)

ListAll fetches all document pages and returns a single slice of docs. Use with care on large collections. listOpts.Size is recommended (e.g. 100) to control page size; nil listOpts uses API defaults. The API may return fewer docs per page than requested (e.g. due to payload limits); iteration continues until the API reports no more pages via nextPageToken.

func (*CollectionDocs) ListIterator added in v0.2.0

func (d *CollectionDocs) ListIterator(ctx context.Context, listOpts *ListDocsOpts, opts ...operations.Option) *DocListIterator

ListIterator returns an iterator that fetches document list pages. Pass nil for listOpts to use defaults. Call Next(ctx) in a loop; when it returns (nil, nil), there are no more pages.

func (*CollectionDocs) Update added in v0.2.0

Update updates documents in the collection.

func (*CollectionDocs) Upsert added in v0.2.0

Upsert upserts documents into the collection.

type CollectionListIterator added in v0.2.0

type CollectionListIterator struct {
	// contains filtered or unexported fields
}

CollectionListIterator iterates over collection list pages without managing page tokens manually. Create it with ListIterator; then call Next until it returns (nil, nil).

func (*CollectionListIterator) Next added in v0.2.0

Next fetches the next page. It returns (result, nil), (nil, error), or (nil, nil) when there are no more pages.

type Collections

type Collections struct {
	// contains filtered or unexported fields
}

func (*Collections) Create

Create a collection.

func (*Collections) Delete

func (s *Collections) Delete(ctx context.Context, collectionName string, opts ...operations.Option) (*operations.DeleteCollectionResponse, error)

Delete an existing collection.

func (*Collections) Get

func (s *Collections) Get(ctx context.Context, collectionName string, opts ...operations.Option) (*operations.GetCollectionResponse, error)

Get metadata of an existing collection.

func (*Collections) List

List all collections in an existing project. Pass nil for listOpts to use defaults (no size/page token).

func (*Collections) Query

Query - Search a collection with a query and return the most similar documents.

func (*Collections) Update

Update - Configure a collection.

type CreateCollectionOptions added in v0.2.0

type CreateCollectionOptions = operations.CreateCollectionRequest

CreateCollectionOptions configures a new collection (alias of operations.CreateCollectionRequest).

type DeleteDocsInput added in v0.2.0

type DeleteDocsInput = operations.DeleteDocsRequestBody

DeleteDocsInput is the body for deleting documents (alias of operations.DeleteDocsRequestBody).

type DocListIterator added in v0.2.0

type DocListIterator struct {
	// contains filtered or unexported fields
}

DocListIterator iterates over document list pages without managing page tokens manually. Create it with ListIterator; then call Next until it returns (nil, nil). Whether there are more pages is determined only by the API's nextPageToken—the number of documents per page may be less than the requested size (e.g. due to payload size limits).

func (*DocListIterator) Next added in v0.2.0

Next fetches the next page. It returns (result, nil), (nil, error), or (nil, nil) when there are no more pages.

type Docs

type Docs struct {
	// contains filtered or unexported fields
}

func (*Docs) BulkUpsert

BulkUpsert - Bulk upsert documents into a collection. Note that the maximum supported object size is 200MB.

func (*Docs) Delete

Delete documents by document IDs or query filter from a collection.

func (*Docs) Fetch

func (s *Docs) Fetch(ctx context.Context, collectionName string, body operations.FetchDocsRequestBody, opts ...operations.Option) (*operations.FetchDocsResponse, error)

Fetch - Lookup and return documents by document IDs from a collection.

func (*Docs) GetBulkUpsertInfo

func (s *Docs) GetBulkUpsertInfo(ctx context.Context, collectionName string, opts ...operations.Option) (*operations.GetBulkUpsertDocsResponse, error)

GetBulkUpsertInfo - Request required info to upload documents.

func (*Docs) List

func (s *Docs) List(ctx context.Context, collectionName string, size *int64, pageToken *string, opts ...operations.Option) (*operations.ListDocsResponse, error)

List documents in a collection.

func (*Docs) Update

Update documents in a collection. Note that the maximum supported payload size is 6MB.

func (*Docs) Upsert

Upsert documents into a collection. Note that the maximum supported payload size is 6MB.

type FetchDocsInput added in v0.2.0

type FetchDocsInput = operations.FetchDocsRequestBody

FetchDocsInput is the body for fetching documents by ID (alias of operations.FetchDocsRequestBody).

type FetchResult added in v0.2.0

type FetchResult struct {
	Docs  []operations.FetchDocsDoc
	Total int64
	Took  int64
}

FetchResult is the flattened result of fetching documents by ID.

type GetBulkUpsertInfoResult added in v0.2.0

type GetBulkUpsertInfoResult struct {
	URL            string
	ObjectKey      string
	Type           *operations.Type
	HTTPMethod     *operations.HTTPMethod
	SizeLimitBytes *int64
}

GetBulkUpsertInfoResult is the flattened result of getting bulk upsert upload info.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient provides an interface for supplying the SDK with a custom HTTP client.

type ListCollectionsOpts added in v0.2.0

type ListCollectionsOpts struct {
	// Max number of collections to return at once.
	Size *int64
	// Next page token for pagination.
	PageToken *string
}

ListCollectionsOpts holds optional parameters for listing collections. Pass nil to use defaults.

type ListCollectionsResult added in v0.2.0

type ListCollectionsResult struct {
	Collections   []components.CollectionResponse
	NextPageToken *string
}

ListCollectionsResult is the flattened result of listing collections (one page).

type ListDocsOpts added in v0.2.0

type ListDocsOpts struct {
	// Max number of documents to return at once.
	Size *int64
	// Next page token for pagination.
	PageToken *string
}

ListDocsOpts holds optional parameters for listing documents (request only). Pass nil to use defaults. Note: isDocsInline and docsUrl are response-only fields from the API; they are not request options. When the API returns isDocsInline=false and docsUrl, the SDK automatically fetches the document list from that URL.

type ListDocsResult added in v0.2.0

type ListDocsResult struct {
	Docs          []operations.ListDocsDoc
	Total         int64
	NextPageToken *string
}

ListDocsResult is the flattened result of listing documents (one page).

type ProjectCollections added in v0.2.0

type ProjectCollections struct {
	// contains filtered or unexported fields
}

ProjectCollections provides project-level collection operations.

func (*ProjectCollections) Create added in v0.2.0

Create creates a new collection and returns the created collection metadata.

func (*ProjectCollections) List added in v0.2.0

List returns a page of collections in the project. Pass nil for listOpts to use defaults (no size/page token).

func (*ProjectCollections) ListAll added in v0.2.0

ListAll fetches all collection pages and returns a single slice. Use with care on projects with many collections.

func (*ProjectCollections) ListIterator added in v0.2.0

ListIterator returns an iterator that fetches collection list pages. Pass nil for listOpts to use defaults. Call Next(ctx) in a loop; when it returns (nil, nil), there are no more pages.

type QueryInput added in v0.2.0

QueryInput is the query body for collection search (alias of operations.QueryCollectionRequestBody).

type QueryResult added in v0.2.0

type QueryResult struct {
	Docs     []operations.QueryCollectionDoc
	Took     int64
	Total    int64
	MaxScore *float64
}

QueryResult is the flattened result of a collection query.

type SDKOption

type SDKOption func(*Client)

SDKOption configures the client.

func WithAPIKey added in v0.2.0

func WithAPIKey(apiKey string) SDKOption

WithAPIKey sets the project API key for authentication.

func WithBaseURL added in v0.2.0

func WithBaseURL(baseURL string) SDKOption

WithBaseURL sets the API base URL (scheme + host, e.g. https://api.lambdadb.ai). Default is https://api.lambdadb.ai per OpenAPI spec.

func WithClient

func WithClient(client HTTPClient) SDKOption

WithClient sets the HTTP client used by the SDK.

func WithProjectName added in v0.2.0

func WithProjectName(projectName string) SDKOption

WithProjectName sets the project name (path segment, e.g. playground). Default is "playground" per OpenAPI spec.

func WithRetryConfig

func WithRetryConfig(retryConfig retry.Config) SDKOption

WithRetryConfig sets the default retry configuration.

func WithSecuritySource

func WithSecuritySource(security func(context.Context) (components.Security, error)) SDKOption

WithSecuritySource sets a function that provides security on each request.

func WithTimeout

func WithTimeout(timeout time.Duration) SDKOption

WithTimeout sets the default request timeout.

type UpdateCollectionOptions added in v0.2.0

type UpdateCollectionOptions = operations.UpdateCollectionRequestBody

UpdateCollectionOptions configures a collection update (alias of operations.UpdateCollectionRequestBody).

type UpdateDocsInput added in v0.2.0

type UpdateDocsInput = operations.UpdateDocsRequestBody

UpdateDocsInput is the body for updating documents (alias of operations.UpdateDocsRequestBody).

type UpsertDocsInput added in v0.2.0

type UpsertDocsInput = operations.UpsertDocsRequestBody

UpsertDocsInput is the body for upserting documents (alias of operations.UpsertDocsRequestBody).

Directories

Path Synopsis
internal
models

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL