Get Started
Install Polybase
From Package Manager:
npm install @polybase/client
Initialize the library
import { Polybase } from "@polybase/client";
const db = new Polybase({
defaultNamespace: "your-namespace",
});
If you specify a defaultNamespace, it will be automatically added for you when you create a collection instance.
Create a collection
You must create a collection before writing and reading data to Polybase. Create collections using Polybase Explorer (recommended) or programatically using the JavaScript SDK.
Watch Shaki create a simple collection using the Polybase Explorer.
Here’s an example Schema:
@public
collection City {
id: string;
name: string;
country?: string;
constructor (id: string, name: string) {
this.id = id;
this.name = name;
}
setCountry (country: string) {
this.country = country;
}
}
A Polybase collection
is just like an Ethereum contract
, but instead of being for a single record, collections describes the rules for a set of records.
Create a collection record
When you create a new record, the constructor
function in your collection is called with the parameters you provide.
import { Polybase } from "@polybase/client";
const db = new Polybase({ defaultNamespace: "your-namespace" });
// Based on the collection code above, "new-york" is the `id` of the new record.
// The `id` "new-york" must be unique (not already exist in the collection)
await db.collection("City").create(["new-york", "New York"]);
// new-york is the `id`, New York is the `name`
Now go view the collection in the Polybase Explorer.
Update a collection record
You can update records by calling methods defined on your collection.
import { Polybase } from "@polybase/client";
const db = new Polybase({ defaultNamespace: "your-namespace" });
await db.collection("City").call("setCountry", ["USA"]);
Read a record
import { Polybase } from "@polybase/client";
const db = new Polybase({ defaultNamespace: "your-namespace" });
const data = await db.collection("City").record("new-york").get();
Live Example
Open in CodeSandbox
View Schema for example in explorer