@razaman2/collection-proxy v0.0.33
@razaman2/collection-proxy
A custom firestore client that provides a simple and consistent interface for interacting with the database.
Install the dependencies
npm install --save @razaman2/collection-proxy
pnpm add @razaman2/collection-proxy
THINGS YOU SHOULD KNOW
All documents are created with the auto generated properties outlined by the following json object.
{
"belongsTo": [
"document-path"
],
"createdAt": "serverTimestamp",
"updatedAt": "serverTimestamp",
"id": "document-id"
}
- The
belongsTo
is an array that stores all the document paths that have a relationship with the current document. ThebelongsTo
will always include the current document path.A settings document with id 111, that belongs to a user with id 222, and a company with id 333, would look like this
{belongsTo: ["settings/111", "users/222", "companies/333"]}
. This relationship is intended to be queried using thearray-contains
or thearray-contains-any
operators. This approach provides the ability to run queries likeget all settings documents that belong to user with id 222
orget all settings documents that belong to company with id 333
. - The
createdAt
key stores a firestore serverTimestamp that represents exactly when the document was created. - The
updatedAt
key stores a firestore serverTimestamp that represents the last time the document was updated. It will be automatically updated when the document is updated using the update method from the firestore-proxy. - The
id
key stores the assigned document id on the document data.
Initializing the Collection
The firestore-proxy is compatible with both the namespaced
and the modular
firestore api's inclluding the firestore admin-sdk
. The collection has a static configuration which allows you to configure the api once in the entry point of your app.
be sure to initialize your firestore app!
Namespaced API, including the Admin SDK
All you need to do to configure the namespaced api is to provide it with your firestore instance.
import firebase from "firebase/compat/app";
import "firebase/compat/firestore";
import {Collection} from "@razaman2/collection-proxy";
firebase.initializeApp({projectId: "demo-app"});
Collection.setConfig({
getFirestore: firestore,
// you can enable operation logging to console
logging: true,
});
// And thats it!
Modular API
Import the functions from the firestore library and provide them to the collection config. You can provide only the functions that are needed for the operation you are trying to perform.
import {initializeApp} from "firebase/app";
import {getFirestore, collection, doc, getDoc, getDocs, collectionGroup, onSnapshot, setDoc, updateDoc, deleteDoc, deleteField, arrayUnion, serverTimestamp, writeBatch} from "firebase/firestore";
import {Collection} from "@razaman2/collection-proxy";
initializeApp({projectId: "demo-app"});
Collection.setConfig({
// you can enable operation logging to console
logging: true,
// base setup to start working with the collection
modular: true,
getFirestore: getFirestore(),
collection,
doc,
// when reading single document
getDoc,
// when reading multiple documents
getDocs,
// when reading from a collection group
collectionGroup,
// when reading documents using a snapshot
onSnapshot,
// when creating, updating or deleting documents
setDoc,
// when updating documents
updateDoc,
// when deleting documents
deleteDoc,
// when you want to delete undefined document fields
deleteField,
// when creating documents
arrayUnion,
// when creating or updating documents
serverTimestamp,
// when updating documents
writeBatch,
});
Reading Documents
// init with document id
const collection = await Collection.setCollectionName("users").init(1);
const data = collection.getData();
// get document for provided id
const collection = await Collection.setCollectionName("users").getDocument(1, {realtime: false});
const data = collection.getData();
// set
const collection = await Collection.setCollectionName("users").setDoc(1).init();
Creating Documents
// create document with auto-generated id
await Collection.setCollectionName("tests").create({
data: {name: "john doe"},
});
// create document with id from setDoc
await Collection.setCollectionName("tests").setDoc("123").create({
data: {name: "john doe"},
});
// create document with id from payload data
await Collection.setCollectionName("tests", {
payload: {
data: {id: "123", name: "john doe"},
},
}).create();
// create document with id from document path
await Collection.setPath("tests/123").create({
data: {name: "john doe"},
});
// create document with setPath and setDoc
await Collection.setPath("tests").setDoc("123").create({
data: {name: "john doe"},
});
Updating Documents
// update document with auto-generated id
await Collection.setCollectionName("tests").update({
data: {name: "jane doe"},
});
// update document with id from setDoc
await Collection.setCollectionName("tests").setDoc("123").update({
data: {name: "jane doe"},
});
// update document with id from payload data
await Collection.setCollectionName("tests", {
payload: {
data: {id: "123", name: "jane doe"},
},
}).update();
// update document with id from document path
await Collection.setPath("tests/123").update({
data: {name: "jane doe"},
});
// update document with setPath and setDoc
await Collection.setPath("tests").setDoc("123").update({
data: {name: "jane doe"},
});
Deleting Documents
// delete document with id from setDoc
await Collection.setCollectionName("tests").setDoc("123").delete();
// delete document with id from payload data
await Collection.setCollectionName("tests").setData({id: "123"}).delete();
// delete document with id from document path
await Collection.setPath("tests/123").delete();
// delete document with setPath and setDoc
await Collection.setPath("tests").setDoc("123").delete();
Audit trail
you can attach built in audit trail to create
, update
and delete
operations
import {Update} from "@razaman2/collection-proxy";
// audit trail any create, update or delete operations on the proxy
Collection.setCollectionName("tests").onWrite({
handler: (collection) => new Update(collection),
triggers: ["create", "update", "delete"], // when you perform an operation listed in the triggers array, an associated update will be created to reflect the before and after state of the document.
});
// attach audit trail when creating a document
Collection.setCollectionName("tests").create({
data: {id: "123", name: "john doe"},
update: (collection) => new Update(collection),
});
// attach audit trail when updating a document
Collection.setCollectionName("tests").update({
data: {id: "123", name: "jane doe"},
update: (collection) => new Update(collection),
});
// attach audit trail when deleting a document
Collection.setCollectionName("tests").delete({
data: {id: "123", name: "jane doe"},
update: (collection) => new Update(collection),
});
8 months ago
9 months ago
9 months ago
10 months ago
1 year ago
1 year ago
12 months ago
12 months ago
11 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
11 months ago
11 months ago
10 months ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago