| 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.
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities");
const { data, block } = await collectionReference.record("id").get();The .onSnapshot() handler is called on every update for the record after the
write is confirmed.
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db
.collection("cities")
.record("id")
.onSnapshot(
(newDoc) => {
// Handle the change
},
(err) => {
// Optional error handler
}
);const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities");
const records = await collectionReference.get();To use the where() filter, you must have a corresponding index specified on
the collection.
const db = new Polybase({ defaultNamespace: "your-namespace" });
const collectionReference = db.collection("cities");
const records = await collectionReference.where("country", "==", "UK").get();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
}
);