@spica-devkit/bucket

Manage bucket schemas and read, write and stream bucket data with a typed client.

Terminal
npm install @spica-devkit/bucket
TypeScript
import * as Bucket from "@spica-devkit/bucket";

Bucket.initialize({apikey: process.env.SPICA_API_KEY, publicUrl: "https://api.example.com/api"});

Data

FunctionDescription
data.get<T>(bucketId, documentId, {queryParams?, headers?})Get one document.
data.getAll<T>(bucketId, {queryParams?, headers?})List documents. With paginate: true, returns {meta, data}.
data.insert<T>(bucketId, document, headers?)Insert a document.
data.insertMany<T>(bucketId, documents, headers?)Insert several documents with one batch request.
data.update<T>(bucketId, documentId, document, headers?)Replace a document.
data.patch(bucketId, documentId, changes, headers?)Patch a document.
data.remove(bucketId, documentId, headers?)Delete a document.
data.removeMany(bucketId, documentIds, headers?)Delete several documents with one batch request.

queryParams accepts the query parameters of the data API: filter, sort, limit, skip, paginate, relation and localize.

TypeScript
interface Post {
  _id?: string;
  title: string;
  status: "draft" | "published";
  views: number;
}

const {meta, data} = await Bucket.data.getAll<Post>(POSTS, {
  queryParams: {
    filter: 'document.status == "published"',
    sort: {views: -1},
    limit: 10,
    paginate: true,
    relation: ["author"]
  },
  headers: {"Accept-Language": "en_US"}
});

const created = await Bucket.data.insert<Post>(POSTS, {title: "Hello", status: "draft", views: 0});
await Bucket.data.patch(POSTS, created._id, {status: "published"});

Realtime

data.realtime.getAll and data.realtime.get open a realtime connection and return an RxJS observable of the current result, plus methods to write through the connection.

TypeScript
const posts = Bucket.data.realtime.getAll<Post>(POSTS, {
  filter: 'document.status == "published"',
  sort: {created_at: -1},
  limit: 20
});

const subscription = posts.subscribe(list => render(list));

posts.insert({title: "Live", status: "published", views: 0});
posts.patch({_id: "6b0b...", views: 10});
posts.replace({_id: "6b0b...", title: "Replaced", status: "draft", views: 0});
posts.remove({_id: "6b0b..."});

subscription.unsubscribe();
TypeScript
const post = Bucket.data.realtime.get<Post>(POSTS, postId);
post.subscribe(current => render(current));
post.patch({views: 11});
FunctionReturns
data.realtime.getAll(bucketId, queryParams?, messageCallback?)Observable of the document list, with insert, replace, patch, remove.
data.realtime.get(bucketId, documentId, messageCallback?, queryParams?)Observable of one document, with replace, patch, remove.

messageCallback receives the server's response to each write, for example {status: 204, message: "No Content"}. Connections reconnect automatically when they drop.

Schemas

FunctionDescription
get(bucketId)Get a bucket definition.
getAll()List buckets.
insert(bucket)Create a bucket.
insertMany(buckets)Create several buckets.
update(bucketId, bucket)Replace a bucket definition.
remove(bucketId)Delete a bucket and its data.
removeMany(bucketIds)Delete several buckets.

Typed clients

Generate interfaces and query cursors for all your buckets with spica bucket orm.