File

src/lib/firestore-extended.class.ts

Description

FirestoreExt Class that uses the FirestoreWrapper Simply extend this class and give it an initialized FirebaseApp to use the FireStoreExtended methods.

Extends

FirestoreExtended

Index

Properties
Methods

Constructor

constructor(firebaseApp: FirebaseApp, defaultDocId: string)
Parameters :
Name Type Optional
firebaseApp FirebaseApp No
defaultDocId string No

Properties

Public defaultDocId
Type : string
Default value : 'data'
Inherited from FirestoreExtended
The default name given to a subCollection document when no name is given

Methods

Public add$
add$(data: T, collectionRef: CollectionReference, subCollectionWriters: SubCollectionWriter[], docId?: string)
Inherited from FirestoreExtended
Type parameters :
  • T

Add document to firestore and split it up into sub collection.

Parameters :
Name Type Optional Default value Description
data T No

the data to be saved

collectionRef CollectionReference<T> No

CollectionReference reference to where on firestore the item should be saved

subCollectionWriters SubCollectionWriter[] No []

see documentation for SubCollectionWriter for more details on how these are used

docId string Yes

If a docId is given it will use that specific id when saving the doc, if no docId is given a random id will be used.

Protected addSimple$
addSimple$(data: T, collectionRef: CollectionReference, id?: string)
Inherited from FirestoreExtended
Type parameters :
  • T

A replacement/extension to the AngularFirestoreCollection.add. Does the same as AngularFirestoreCollection.add but can also add createdDate and modifiedDate and returns the data with the added properties in FirebaseDbItem

Used internally

Parameters :
Name Type Optional Description
data T No

the data to be added to the document, cannot contain types firestore won't allow

collectionRef CollectionReference<T> No

the CollectionReference where the document should be added

id string Yes

if given the added document will be given this id, otherwise a random unique id will be used.

Protected batchCommit$
batchCommit$(batch: WriteBatch)
Inherited from FirestoreExtended

Turn a batch into an Observable instead of Promise.

For some reason angularfire returns a promise on batch.commit() instead of an observable like for everything else.

This method turns it into an observable

Parameters :
Name Type Optional
batch WriteBatch No
Returns : Observable<void>
Public changeDocId$
changeDocId$(docRef: DocumentReference, newId: string, subCollectionQueries: SubCollectionQuery[], subCollectionWriters?: SubCollectionWriter[])
Inherited from FirestoreExtended
Type parameters :
  • T

Firestore doesn't allow you do change the name or move a doc directly so you will have to create a new doc under the new name and then delete the old doc. returns the new doc once the delete is done.

Parameters :
Name Type Optional Default value Description
docRef DocumentReference<T> No

DocumentReference to have its id changed

newId string No

the new id

subCollectionQueries SubCollectionQuery[] No []

if the document has child documents the subCollectionQueries are needed to locate them

subCollectionWriters SubCollectionWriter[] Yes

if the document has child documents the SubCollectionWriters are needed to add them back

cleanExtrasFromData
cleanExtrasFromData(data: | FireItem, subCollectionWriters?: SubCollectionWriter[], additionalFieldsToRemove?: string[])
Inherited from FirestoreExtended
Type parameters :
  • T

clean FirestoreBaseItem properties from the data. Usually done if you wish to save the data to firestore, since some FirestoreBaseItem properties are of non allowed types.

Goes through each level and removes DbItemExtras In case you wish to save the data

Parameters :
Name Type Optional Description
data | FireItem No

data to be cleaned, either single item or an array of items

subCollectionWriters SubCollectionWriter[] Yes

if the document has child documents the SubCollectionWriters are needed to locate them

additionalFieldsToRemove string[] Yes
Returns : T
Public delete$
delete$(docRef: DocumentReference, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended

Delete Document and child documents. Takes a DocumentReference and an optional list of SubCollectionQuery

Parameters :
Name Type Optional Default value Description
docRef DocumentReference No

DocumentReference that is to be deleted

subCollectionQueries SubCollectionQuery[] No []

if the document has child documents the subCollectionQueries are needed to locate them

Returns : Observable<void>
Public deleteCollection$
deleteCollection$(collectionRef: CollectionReference, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

Delete all documents and sub collections as specified in subCollectionQueries. Not very efficient and causes a lot of db reads. If possible use the firebase CLI or the console instead when deleting large collections.

Parameters :
Name Type Optional Default value
collectionRef CollectionReference<T> No
subCollectionQueries SubCollectionQuery[] No []
Returns : Observable<any>
Public deleteDocByPath$
deleteDocByPath$(docPath: string, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended

Delete firestore document by path Convenience method in case we do not have direct access to the AngularFirestoreDocument reference

Parameters :
Name Type Optional Default value Description
docPath string No

A string representing the path of the referenced document (relative to the root of the database).

subCollectionQueries SubCollectionQuery[] No []

if the document has child documents the subCollectionQueries are needed to locate them

Returns : Observable<any>
Public deleteIndexedItemInArray$
deleteIndexedItemInArray$(items: Array<FireItem<T>>, indexToDelete: number, subCollectionQueries: SubCollectionQuery[], useCopy: boolean)
Inherited from FirestoreExtended
Type parameters :
  • T

Use when you wish to delete an indexed document and have the remaining documents update their indices to reflect the change.

Parameters :
Name Type Optional Default value Description
items Array<FireItem<T>> No

array of FireItem docs with index variables to be updated

indexToDelete number No
subCollectionQueries SubCollectionQuery[] No []
useCopy boolean No false
Returns : Observable<void>
Public deleteIndexedItemsInArray$
deleteIndexedItemsInArray$(items: Array<FireItem<T>>, indicesToDelete: number[], subCollectionQueries: SubCollectionQuery[], useCopy: boolean)
Inherited from FirestoreExtended
Type parameters :
  • T

Use when you wish to delete several indexed documents and have the remaining documents update their indices to reflect the change.

Parameters :
Name Type Optional Default value Description
items Array<FireItem<T>> No

array of FireItem docs with index variables to be updated

indicesToDelete number[] No
subCollectionQueries SubCollectionQuery[] No []
useCopy boolean No false
Returns : Observable<void>
Public deleteItem$
deleteItem$(item: FireItem<T>, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

Delete document by FirestoreItem

A very convenient method to remove a previously fetched document. Requires that the document/Item is previously fetched since the item needs to be a FireItem, i.e. includes firestoreMetadata.

Parameters :
Name Type Optional Default value Description
item FireItem<T> No

FirestoreItem to be deleted

subCollectionQueries SubCollectionQuery[] No []

if the document has child documents the subCollectionQueries are needed to locate them

Returns : Observable<any>
Public deleteMultiple$
deleteMultiple$(docRefs: DocumentReference[], subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

Delete Documents and child documents

Parameters :
Name Type Optional Default value Description
docRefs DocumentReference<T>[] No
  • A list of DocumentReference that are to be deleted
subCollectionQueries SubCollectionQuery[] No []

if the document has child documents the subCollectionQueries are needed to locate them

Returns : Observable<any>
Public deleteMultipleByPaths$
deleteMultipleByPaths$(docPaths: string[])
Inherited from FirestoreExtended
Parameters :
Name Type Optional
docPaths string[] No
Returns : Observable<any>
Protected deleteMultipleSimple$
deleteMultipleSimple$(docRefs: DocumentReference[])
Inherited from FirestoreExtended

Delete Documents

Parameters :
Name Type Optional Description
docRefs DocumentReference[] No
  • A list of DocumentReference that are to be deleted
Returns : Observable<void>
Protected getBatchFromMoveItemInIndexedDocs
getBatchFromMoveItemInIndexedDocs(items: Array<FireItem<T>>, fromIndex: number, toIndex: number, useCopy)
Inherited from FirestoreExtended
Type parameters :
  • T

Does the heavy lifting when it comes to updating multiple docs to change their index. Not called directly.

Parameters :
Name Type Optional Default value Description
items Array<FireItem<T>> No

array of FireItem docs with index variables to be updated

fromIndex number No
toIndex number No
useCopy No false

if true the given array will not be updated

Returns : WriteBatch
Protected getBatchFromTransferItemInIndexedDocs
getBatchFromTransferItemInIndexedDocs(previousArray: Array<FireItem<T>>, currentArray: Array<FireItem<T>>, previousIndex: number, currentIndex: number, currentArrayName: string, additionalDataUpdateOnMovedItem?: literal type, isUpdateModifiedDateOnMovedItem, useCopy)
Inherited from FirestoreExtended
Type parameters :
  • T

Used mainly for drag and drop scenarios where we drag an item from one list to another and the the docs have an index value and a groupName.

Parameters :
Name Type Optional Default value
previousArray Array<FireItem<T>> No
currentArray Array<FireItem<T>> No
previousIndex number No
currentIndex number No
currentArrayName string No
additionalDataUpdateOnMovedItem literal type Yes
isUpdateModifiedDateOnMovedItem No true
useCopy No false
Returns : WriteBatch
Protected getBatchFromUpdateIndexFromListOfDocs
getBatchFromUpdateIndexFromListOfDocs(items: Array<FireItem<T>>, batch: WriteBatch)
Inherited from FirestoreExtended
Type parameters :
  • T

Run this on collections with a fixed order using an index: number attribute; This will update that index with the index in the collectionData, so it should be sorted by index first. Basically needs to be run after a delete

Parameters :
Name Type Optional Default value
items Array<FireItem<T>> No
batch WriteBatch No writeBatch(this.fs.firestore)
Returns : WriteBatch
Public getDeleteBatch$
getDeleteBatch$(docRef: DocumentReference, subCollectionQueries: SubCollectionQuery[], batch: WriteBatch)
Inherited from FirestoreExtended

Returns WriteBatch that is set to delete Document and child documents of given docRef

Parameters :
Name Type Optional Default value Description
docRef DocumentReference No

DocumentReference that is to be deleted

subCollectionQueries SubCollectionQuery[] No []

if the document has child documents the subCollectionQueries are needed to locate them

batch WriteBatch No writeBatch(this.fs.firestore)
Returns : Observable<WriteBatch>
Protected getDeleteMultipleSimpleBatch
getDeleteMultipleSimpleBatch(docRefs: DocumentReference[], batch: WriteBatch)
Inherited from FirestoreExtended
Parameters :
Name Type Optional Default value
docRefs DocumentReference[] No
batch WriteBatch No writeBatch(this.fs.firestore)
Returns : WriteBatch
Protected getDocumentReferencesDeep$
getDocumentReferencesDeep$(ref: DocumentReference | CollectionReference, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended

Returns an Observable containing a list of DocumentReference found under the given docRef using the SubCollectionQuery[] Mainly used to delete a docFs and its sub docs

Parameters :
Name Type Optional Default value Description
ref DocumentReference | CollectionReference No

: DocumentReference | CollectionReference

subCollectionQueries SubCollectionQuery[] No []

: SubCollectionQuery[]

Returns : Observable<DocumentReference[]>
Protected getDocumentReferencesFromCollectionRef$
getDocumentReferencesFromCollectionRef$(collectionRef: CollectionReference, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T
Parameters :
Name Type Optional Default value
collectionRef CollectionReference<T> No
subCollectionQueries SubCollectionQuery[] No []
Returns : Observable<DocumentReference[]>
Protected getDocumentReferencesFromDocRef$
getDocumentReferencesFromDocRef$(docRef: DocumentReference, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T
Parameters :
Name Type Optional Default value
docRef DocumentReference<T> No
subCollectionQueries SubCollectionQuery[] No []
Returns : Observable<DocumentReference[]>
Protected getDocumentReferencesFromItem
getDocumentReferencesFromItem(item: FireItem<T>, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

Used by deleteDeepByItem$ to get all the AngularFirestoreDocuments to be deleted including child documents using SubCollectionQueries

Internal use

Parameters :
Name Type Optional Default value Description
item FireItem<T> No

FirestoreItem from where we get the AngularFirestoreDocuments

subCollectionQueries SubCollectionQuery[] No []

if the dbItem has child documents the subCollectionQueries are needed to locate them

Returns : DocumentReference[]
Protected getPathsFromItemDeepRecursiveHelper
getPathsFromItemDeepRecursiveHelper(item: T, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

DO NOT CALL THIS METHOD, its meant as a support method for getDocs$

Parameters :
Name Type Optional Default value
item T No
subCollectionQueries SubCollectionQuery[] No []
Returns : string[]
Public listenForCollection$
listenForCollection$(_query: Query, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

Same as AngularFirestoreCollection.snapshotChanges but it adds the properties in FirebaseDbItem.

Important to understand this is will trigger for every change/update on any of the documents we are listening to. That means that if any document we are listening to is changed the entire object will be triggered containing the updated data.

Example usage.

ngFirestoreDeep: RxFirestoreExtended; // RxFirestoreExtended variable restaurantCollectionFs = this.ngFireStore.collection('restaurants'); // AngularFirestoreCollectionRef to restaurants

constructor(private ngFireStore: AngularFirestore) { this.ngFirestoreDeep = new RxFirestoreExtended(ngFireStore); // initialize AngularFireStoreDeep with AngularFirestore }

listenForRestaurants$(): Observable<RestaurantItem[]> { return this.ngFirestoreDeep.listenForCollection$(this.restaurantCollectionFs); }

If you do not wish to listen for changes and only care about getting the values once

getRestaurants$(): Observable<RestaurantItem[]> { return this.ngFirestoreDeep.listenForCollection$(this.restaurantCollectionFs).pipe( take(1) ); }

Parameters :
Name Type Optional Default value Description
_query Query<T> No

the collectionRef which will be listened to

subCollectionQueries SubCollectionQuery[] No []
Public listenForCollectionRecursively$
listenForCollectionRecursively$(collectionPath: string, collectionKey: string, orderKey?: string)
Inherited from FirestoreExtended
Type parameters :
  • T

Listens for collections inside collections with the same name to an unlimited depth and returns all of it as an array.

Parameters :
Name Type Optional
collectionPath string No
collectionKey string No
orderKey string Yes
Returns : Observable<any>
Protected listenForCollectionsDeep
listenForCollectionsDeep(item: FireItem<T>, subCollectionQueries: SubCollectionQuery[])
Inherited from FirestoreExtended
Type parameters :
  • T

Used internally for both listenForDoc and listenForCollection in order to recursively get collections.

Please use listenForDoc or listenForCollection.

Parameters :
Name Type Optional Default value
item FireItem<T> No
subCollectionQueries SubCollectionQuery[] No []
Protected listenForCollectionSimple$
listenForCollectionSimple$(_query: Query)
Inherited from FirestoreExtended
Type parameters :
  • T

Listens for single collection and returns an array of documents as FireItem[] Used internally, please use listenForCollection$() instead.

Parameters :
Name Type Optional Description
_query Query<T> No

the Query which will be listened to

Public listenForDoc$
listenForDoc$(docRef: DocumentReference, subCollectionQueries: SubCollectionQuery[], actionIfNotExist: DocNotExistAction)
Inherited from FirestoreExtended
Type parameters :
  • T

Allows for listening to documents and collections n deep up to the firestore max of 100 levels.

Triggers for any change in any document that is listened to.

E.x: const subCollectionQueries: SubCollectionQuery[] = [ { name: 'data' }, { name: 'secure' }, { name: 'variants' }, { name: 'images', queryFn: ref => ref.orderBy('index'), collectionWithNames: [ { name: 'secure'} ] }, ];

this.listenForDocAndSubCollections<Product>(docFs, collections)

Wrapper for listenForDocDeepRecursiveHelper$ so that we can cast the return to the correct type All logic is in listenForDocDeepRecursiveHelper$.

Parameters :
Name Type Optional Default value Description
docRef DocumentReference<T> No
  • a docRef with potential queryFn
subCollectionQueries SubCollectionQuery[] No []
  • see example
actionIfNotExist DocNotExistAction No DocNotExistAction.RETURN_ALL_BUT_DATA

Action to take if document does not exist

Protected listenForDocDeepRecursiveHelper$
listenForDocDeepRecursiveHelper$(docRef: DocumentReference, subCollectionQueries: SubCollectionQuery[], actionIfNotExist: DocNotExistAction)
Inherited from FirestoreExtended
Type parameters :
  • T

DO NOT CALL THIS METHOD, meant to be used solely by listenForDocAndSubCollections$

Parameters :
Name Type Optional Default value
docRef DocumentReference<T> No
subCollectionQueries SubCollectionQuery[] No []
actionIfNotExist DocNotExistAction No DocNotExistAction.RETURN_NULL
Returns : Observable<any>
Protected listenForDocSimple$
listenForDocSimple$(docRef: DocumentReference, actionIfNotExist: DocNotExistAction)
Inherited from FirestoreExtended
Type parameters :
  • T

Same as AngularFirestoreDocument.snapshotChanges but it adds the properties in FirebaseDbItem and also allows for to choose action to take when document does not exist

Important to understand this is will trigger for every change/update on the document we are listening to.

Parameters :
Name Type Optional Default value Description
docRef DocumentReference<any> No

DocumentReference that will be listened to

actionIfNotExist DocNotExistAction No DocNotExistAction.RETURN_ALL_BUT_DATA

Action to take if document does not exist

Public moveItemInArray$
moveItemInArray$(items: Array<FireItem<T>>, fromIndex: number, toIndex: number, useCopy)
Inherited from FirestoreExtended
Type parameters :
  • T

Moved item within the same list so we need to update the index of all items in the list; Use a copy if you dont wish to update the given array, for example when you want to just listen for the change of the db.. The reason to not do this is because it takes some time for the db to update and it looks better if the list updates immediately.

Parameters :
Name Type Optional Default value Description
items Array<FireItem<T>> No

array of FireItem docs with index variables to be updated

fromIndex number No
toIndex number No
useCopy No false

if true the given array will not be updated

Returns : Observable<void>
Protected removeDataExtrasRecursiveHelper
removeDataExtrasRecursiveHelper(dbItem: T, subCollectionWriters: SubCollectionWriter[], additionalFieldsToRemove: string[])
Inherited from FirestoreExtended
Type parameters :
  • T

Recursive method to clean FirestoreBaseItem properties from the dbItem

Parameters :
Name Type Optional Default value Description
dbItem T No

the data to be cleaned

subCollectionWriters SubCollectionWriter[] No []

list of SubCollectionWriters to handle sub collections

additionalFieldsToRemove string[] No []
Returns : T
Protected splitDataIntoCurrentDocAndSubCollections
splitDataIntoCurrentDocAndSubCollections(data: T, subCollectionWriters: SubCollectionWriter[])
Inherited from FirestoreExtended
Type parameters :
  • T

DO NOT CALL THIS METHOD, used in addDeep and updateDeep to split the data into currentDoc and subCollections Only goes one sub level deep;

Parameters :
Name Type Optional Default value
data T No
subCollectionWriters SubCollectionWriter[] No []
Public transferItemInIndexedDocs
transferItemInIndexedDocs(previousArray: Array<FireItem<T>>, currentArray: Array<FireItem<T>>, previousIndex: number, currentIndex: number, currentArrayName: string, additionalDataUpdateOnMovedItem?: literal type, isUpdateModifiedDateOnMovedItem, useCopy)
Inherited from FirestoreExtended
Type parameters :
  • T
Parameters :
Name Type Optional Default value
previousArray Array<FireItem<T>> No
currentArray Array<FireItem<T>> No
previousIndex number No
currentIndex number No
currentArrayName string No
additionalDataUpdateOnMovedItem literal type Yes
isUpdateModifiedDateOnMovedItem No true
useCopy No false
Returns : Observable<void>
Public update$
update$(data: UpdateData>, docRef: DocumentReference, subCollectionWriters: SubCollectionWriter[], isAddModifiedDate: boolean)
Inherited from FirestoreExtended
Type parameters :
  • T

Update document and child documents

Be careful when updating a document of any kind since we allow partial data there cannot be any type checking prior to update so its possible to introduce spelling mistakes on attributes and so forth

Parameters :
Name Type Optional Default value Description
data UpdateData<Partial<T>> No

the data that is to be added or updated { [field: string]: any }

docRef DocumentReference<T> No

DocumentReference to be updated

subCollectionWriters SubCollectionWriter[] No []

if the data contains properties that should be placed in child collections and documents specify that here

isAddModifiedDate boolean No true

if true the modifiedDate property is added/updated on the affected documents

Returns : Observable<void>
Protected updateDeepToBatchHelper
updateDeepToBatchHelper(data: UpdateData, docRef: DocumentReference, subCollectionWriters: SubCollectionWriter[], isAddModifiedDate: boolean, batch?: WriteBatch)
Inherited from FirestoreExtended
Type parameters :
  • T

DO NOT CALL THIS METHOD, used by update deep

Parameters :
Name Type Optional Default value
data UpdateData<T> No
docRef DocumentReference<T> No
subCollectionWriters SubCollectionWriter[] No []
isAddModifiedDate boolean No true
batch WriteBatch Yes
Returns : WriteBatch
Public updateMultiple$
updateMultiple$(docRefs: DocumentReference[], data: A, isAddModifiedDate: boolean)
Inherited from FirestoreExtended
Type parameters :
  • A

Update/ add data to the firestore documents

Parameters :
Name Type Optional Default value Description
docRefs DocumentReference[] No

list of DocumentReference to be have their data updated

data A No

data to add/update

isAddModifiedDate boolean No true

if true the modifiedDate is added/updated

Returns : Observable<void>
Protected updateSimple$
updateSimple$(data: UpdateData>, docRef: DocumentReference, isAddModifiedDate: boolean)
Inherited from FirestoreExtended
Type parameters :
  • T

Used internally for updates that doesn't affect child documents

Parameters :
Name Type Optional Default value
data UpdateData<Partial<T>> No
docRef DocumentReference<T> No
isAddModifiedDate boolean No true
Returns : Observable<void>
import {FirebaseApp} from 'firebase/app';
import {FirestoreWrapper} from './firestore-wrapper';
import {FirestoreExtended} from './firestore-extended';

/**
 * FirestoreExt Class that uses the FirestoreWrapper
 * Simply extend this class and give it an initialized FirebaseApp to use the FireStoreExtended methods.
 */
export class FirestoreExt extends FirestoreExtended {
  constructor(firebaseApp: FirebaseApp, defaultDocId: string = 'data') {
    super(new FirestoreWrapper(firebaseApp), defaultDocId);
  }
}

result-matching ""

    No results matching ""