shelving
Version:
Toolkit for using data in JavaScript.
119 lines (118 loc) • 4.32 kB
JavaScript
import { addDoc, arrayRemove, arrayUnion, collection, deleteDoc, doc, documentId, getCount, getDoc, getDocs, increment, limit, orderBy, query, setDoc, updateDoc, where, } from "firebase/firestore/lite";
import { AsyncProvider } from "../../db/Provider.js";
import { UnimplementedError } from "../../error/UnimplementedError.js";
import { getItem } from "../../util/item.js";
import { getObject } from "../../util/object.js";
import { getFilters, getLimit, getOrders } from "../../util/query.js";
import { mapItems } from "../../util/transform.js";
import { getUpdates } from "../../util/update.js";
// Constants.
const ID = documentId();
// Map `Filter.types` to `WhereFilterOp`
const OPERATORS = {
is: "==",
not: "!=",
in: "in",
out: "not-in",
contains: "array-contains",
gt: ">",
gte: ">=",
lt: "<",
lte: "<=",
};
/** Create a corresponding `QueryReference` from a Query. */
function _getQuery(firestore, c, q) {
return q
? query(collection(firestore, c), ..._getConstraints(q))
: collection(firestore, c);
}
function* _getConstraints(q) {
for (const { key, direction } of getOrders(q))
yield orderBy(key === "id" ? ID : key, direction);
for (const { key, operator, value } of getFilters(q))
yield where(key === "id" ? ID : key, OPERATORS[operator], value);
const l = getLimit(q);
if (typeof l === "number")
yield limit(l);
}
function _getItem(snapshot) {
const data = snapshot.data();
return getItem(snapshot.id, data);
}
function _getOptionalItem(snapshot) {
const data = snapshot.data();
if (data)
return getItem(snapshot.id, data);
}
/** Convert `Updates` object into corresponding Firestore `FieldValue` instances. */
function _getFieldValues(updates) {
return getObject(mapItems(getUpdates(updates), _getFieldValue));
}
function _getFieldValue({ key, action, value }) {
if (action === "set")
return [key, value];
if (action === "sum")
return [key, increment(value)];
if (action === "with")
return [key, arrayUnion(...value)];
if (action === "omit")
return [key, arrayRemove(...value)];
return action; // Never happens.
}
/**
* Firestore Lite client database provider.
* - Works with the Firebase JS SDK.
* - Does not support offline mode.
* - Does not support realtime subscriptions.
*/
export class FirestoreLiteProvider extends AsyncProvider {
_firestore;
constructor(firestore) {
super();
this._firestore = firestore;
}
async getItem(c, id) {
const snapshot = await getDoc(doc(this._firestore, c, id));
return _getOptionalItem(snapshot);
}
getItemSequence() {
throw new UnimplementedError("FirestoreLiteProvider does not support realtime subscriptions");
}
async addItem(c, data) {
const reference = await addDoc(collection(this._firestore, c), data);
return reference.id;
}
async setItem(c, id, data) {
await setDoc(doc(this._firestore, c, id), data);
}
async updateItem(c, id, updates) {
await updateDoc(doc(this._firestore, c, id), _getFieldValues(updates));
}
async deleteItem(c, id) {
await deleteDoc(doc(this._firestore, c, id));
}
async countQuery(c, q) {
const snapshot = await getCount(_getQuery(this._firestore, c, q));
return snapshot.data().count;
}
async getQuery(c, q) {
const snapshot = await getDocs(_getQuery(this._firestore, c, q));
return snapshot.docs.map(_getItem);
}
getQuerySequence() {
throw new UnimplementedError("FirestoreLiteProvider does not support realtime subscriptions");
}
async setQuery(c, q, data) {
const snapshot = await getDocs(_getQuery(this._firestore, c, q));
await Promise.all(snapshot.docs.map(s => setDoc(s.ref, data)));
}
async updateQuery(c, q, updates) {
const snapshot = await getDocs(_getQuery(this._firestore, c, q));
const fieldValues = _getFieldValues(updates);
await Promise.all(snapshot.docs.map(s => updateDoc(s.ref, fieldValues)));
}
async deleteQuery(c, q) {
const snapshot = await getDocs(_getQuery(this._firestore, c, q));
await Promise.all(snapshot.docs.map(s => deleteDoc(s.ref)));
}
}