- Guides
- Get Started
Guides
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
Create collections using Polybase Explorer or the JavaScript SDK.
We suggest creating collections using Polybase Explorer. You will need to first login and create an account.
A Polybase collection describes the rules for a collection of data, not just a single record (as is the case with other smart collection languages).
In this example we use the JavaScript SDK to create a collection called City
that has name
and country
fields, and a setCountry()
function. You can define multiple
collections in a single applySchema
call.
await db.applySchema(`
@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;
}
}
@public
collection Country {
id: string;
name: string;
constructor (id: string, name: string) {
this.id = id;
this.name = name;
}
}
`,
"your-namespace"
); // your-namespace is optional if you have defined a default namespace
For more details on creating collections, see the collection overview.
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();