From b81a172c10a0f1f22953a941ceb3d3008d4fa7e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20W=C3=BCsten?= <42694155+janwuesten@users.noreply.github.com> Date: Fri, 30 Aug 2024 17:20:52 +0200 Subject: [PATCH] Added information for Firestore --- .../defineable-firestore/getting-started.mdx | 9 -- .../_category_.json | 2 +- docs/docs/integration/firestore.mdx | 85 +++++++++++++++++++ 3 files changed, 86 insertions(+), 10 deletions(-) delete mode 100644 docs/docs/defineable-firestore/getting-started.mdx rename docs/docs/{defineable-firestore => integration}/_category_.json (66%) create mode 100644 docs/docs/integration/firestore.mdx diff --git a/docs/docs/defineable-firestore/getting-started.mdx b/docs/docs/defineable-firestore/getting-started.mdx deleted file mode 100644 index 3a4605f..0000000 --- a/docs/docs/defineable-firestore/getting-started.mdx +++ /dev/null @@ -1,9 +0,0 @@ ---- -sidebar_position: 1 ---- - -# Getting started - -:::info -The Google Firebase Firestore extension is beeing developed at the moment. -::: \ No newline at end of file diff --git a/docs/docs/defineable-firestore/_category_.json b/docs/docs/integration/_category_.json similarity index 66% rename from docs/docs/defineable-firestore/_category_.json rename to docs/docs/integration/_category_.json index f0c77b5..b0306d5 100644 --- a/docs/docs/defineable-firestore/_category_.json +++ b/docs/docs/integration/_category_.json @@ -1,5 +1,5 @@ { - "label": "Firestore extension", + "label": "Integration", "position": 4, "link": { "type": "generated-index" diff --git a/docs/docs/integration/firestore.mdx b/docs/docs/integration/firestore.mdx new file mode 100644 index 0000000..ec78be6 --- /dev/null +++ b/docs/docs/integration/firestore.mdx @@ -0,0 +1,85 @@ +--- +sidebar_position: 1 +--- + +# Firestore + +:::tip +Using Definable with Firebase Firestore is slightly different depending if you use Firestore for Web, Admin SDK or React Native. +Always make sure to follow the official Firebase Firestore documentation to better understand how to use Firestore in general. +This documentation only focuses on the usage of Firestore for Web in combination with Definable, as using Definable in any other scenario is not much different. +::: + +## Example Definable + +The following `Definable` class is the class used for all following examples. +This example class also shows how to use `Timestamp`. + +```ts +import { Definable, DefinableDefinition } from "definable" +import { Timestamp } from "firebase/firestore" + +export class Event extends Definable { + eventName: string = "" + from: Date = new Date() + to: Date = new Date() + + definition({ prop }: DefinableDefinition): void { + prop("eventName") + .useDeserializer((data) => this.eventName = data ?? "") + .useSerializer(() => this.eventName) + prop("from") + .useDeserializer((data) => this.from = data?.toDate() ?? new Date()) + .useSerializer(() => Timestamp.fromDate(this.from)) + prop("to") + .useDeserializer((data) => this.to = data?.toDate() ?? new Date()) + .useSerializer(() => Timestamp.fromDate(this.to)) + } +} +``` + +## Reading a single document + +```ts +import { collection, doc, getDoc } from "firebase/firestore" + +const collectionRef = collection(firestore, "event") +const documentRef = doc(collectionRef, "my-event-id") +const responseDocument = await getDoc(documentRef) + +// TODO: Check if document exists +const event = new Event().deserialize(responseDocument.data(), { reference: responseDocument.id }) + +// To read (or set) the document ID: +const documentID = event.reference +``` + +## Reading multiple documents (query, getDocs...) + +```ts +import { collection, doc, getDocs } from "firebase/firestore" + +const events: Event[] = [] +const collectionRef = collection(firestore, "event") +const response = await getDocs(collectionRef) +for (const doc of response.docs) { + events.push(new Event().deserialize(doc.data(), { reference: doc.id })) +} +console.log(events) +``` + +## Writing a document +```ts +import { collection, doc, addDoc, updateDoc } from "firebase/firestore" + +const collectionRef = collection(firestore, "event") + +const event = new Event() + +// Adding document +const response = await addDoc(collectionRef, event.serialize()) +event.reference = test.id + +// Setting or updating document +await updateDoc(doc(collectionRef, event.reference), event.serialize()) +``` \ No newline at end of file