title |
---|
Read Data |
There are two ways to retrieve data in Polybase.
- Fetch data once with
.get()
- Listen for real time updates with
.onSnapshot()
You can view our example app Polybase Social to see it working in action.
You can read data once, by calling .record(id: string).get()
on a collection.
import { Polybase } from "@polybase/client"
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities");
async function getRecord () {
const { data, block } = await collectionReference.record("id").get();
}
The .onSnapshot()
handler is called on every update for the record after the
write is confirmed.
import { Polybase } from "@polybase/client"
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db
.collection("cities")
.record("id")
.onSnapshot(
(newDoc) => {
// Handle the change
},
(err) => {
// Optional error handler
}
);
import { Polybase } from "@polybase/client"
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities");
export async function listRecords () {
const records = await collectionReference.get();
}
To use the where()
filter, you must have a corresponding index specified on
the collection.
import { Polybase } from "@polybase/client"
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities");
export async function listRecordsWithFilter () {
const records = await collectionReference.where("country", "==", "UK").get();
}
import { Polybase } from "@polybase/client"
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities").onSnapshot(
(newDoc) => {
// Handle the change
},
(err) => {
// Optional error handler
}
);
You can also watch for changes on a filtered query.
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db
.collection("cities")
.where("country", "==", "UK")
.onSnapshot(
(newDoc) => {
// Handle the change
},
(err) => {
// Optional error handler
}
);