{"version":3,"file":"nx-ddd-firestore-adapters-firebase.mjs","sources":["../../../../../packages/@nx-ddd/firestore/src/lib/adapters/firebase/firebase.adapter.ts","../../../../../packages/@nx-ddd/firestore/src/lib/adapters/firebase/nx-ddd-firestore-adapters-firebase.ts"],"sourcesContent":["import { doc, collection, collectionGroup, FieldValue, Timestamp, setDoc, getDoc, getDocs, getFirestore, onSnapshot, deleteDoc, writeBatch, query, orderBy, limit, updateDoc, where, FieldPath, getCountFromServer } from 'firebase/firestore';\nimport { Subject } from 'rxjs';\nimport { Injectable } from '@angular/core';\nimport type firestore from 'firebase/firestore';\nimport {\n  FirestoreAdapter,\n  QueryFn as _QueryFn,\n  WhereFilterOp,\n  DocumentChangeAction,\n  DocumentSnapshot,\n  CollectionReference as _CollectionReference,\n  CollectionGroup as _CollectionGroup,\n  Query as _Query,\n  DocumentReference,\n  DocumentData\n} from '@nx-ddd/firestore';\n\ntype CollectionReference<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData,\n> = _CollectionReference<AppModelType, DbModelType, firestore.CollectionReference<AppModelType, DbModelType>>;\n\ntype CollectionGroup<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n> = _CollectionGroup<AppModelType, DbModelType, firestore.Query<AppModelType, DbModelType>>;\n\ntype Query<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n> = _Query<AppModelType, DbModelType, firestore.Query<AppModelType, DbModelType>>;\n\ntype QueryFn<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n> = _QueryFn<AppModelType, DbModelType, firestore.QueryConstraint>\n\ntype Origin<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData,\n> = firestore.CollectionReference<AppModelType, DbModelType>\n  | firestore.Query<AppModelType, DbModelType>\n  | firestore.Query<AppModelType, DbModelType>;\n\ntype CollectionLike<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData,\n> = CollectionReference<AppModelType, DbModelType> \n  | CollectionGroup<AppModelType, DbModelType>\n  | Query<AppModelType, DbModelType>;\n\n// Utility type to map Origin to CollectionLike\ntype InferCollectionLike<O extends Origin> = \n  O extends firestore.CollectionReference<infer A, infer B> ? CollectionReference<A, B> :\n  O extends firestore.Query<infer A, infer B> ? CollectionGroup<A, B> :\n  O extends firestore.Query<infer A, infer B> ? Query<A, B> :\n  never;\n\nexport function wrapCollectionLike<O extends Origin>(origin: O): InferCollectionLike<O> {\n  return {\n    __ref: origin,\n    stateChanges: () => {\n      const subject = new Subject<DocumentChangeAction<any>[]>();\n      onSnapshot(origin, (snapshot) => subject.next(snapshot.docChanges().map(change => ({\n        type: change.type,\n        payload: {\n          doc: change.doc\n        },\n      }))));\n      return subject.asObservable();\n    },\n    get: () => getDocs(origin),\n    count: () => getDocs(origin).then(snapshot => snapshot.size),\n  } as never as InferCollectionLike<O>;\n}\n\nexport function unwrapCollectionLike<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n>(\n  collection: CollectionLike<AppModelType, DbModelType>\n): Origin<AppModelType, DbModelType> {\n  return collection.__ref;\n}\n\n@Injectable()\nexport class FirebaseFirestoreAdapter<D> extends FirestoreAdapter<D> {\n\n  constructor(public firestore = getFirestore()) { super() }\n\n  get FieldValue(): typeof FieldValue {\n    return FieldValue;  \n  }\n\n  get Timestamp(): typeof Timestamp {\n    return Timestamp;  \n  }\n\n  get FieldPath(): FieldPath {\n    return new FieldPath();\n  }\n\n  protected isTimestamp(v: any): v is Timestamp {\n    return v instanceof Timestamp;\n  }\n\n  protected isFieldValue(v: any): v is FieldValue {\n    return v instanceof this.FieldValue;\n  }\n\n  doc(path: string): DocumentReference<any> {\n    const docRef = doc(this.firestore, path);\n    return {\n      __ref: docRef,\n      exists: () => getDoc(docRef).then(doc => doc.exists()),\n      set: (data) => setDoc(docRef, data),\n      get: () => getDoc(docRef),\n      update: (data) => updateDoc(docRef, data),\n      delete: () => deleteDoc(docRef),\n      stateChanges: () => {\n        const subject = new Subject<DocumentSnapshot<any>>();\n        onSnapshot(docRef, (doc) => subject.next({\n          id: doc.id,\n          ref: doc.ref,\n          data: () => doc.data(),\n          get: (fieldPath: string) => doc.get(fieldPath),\n        }));\n        return subject.asObservable();\n      },\n    }\n  }\n\n  collection<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(path: string): CollectionReference<AppModelType, DbModelType> {\n    const ref = collection(this.firestore, path);\n    return wrapCollectionLike(ref) as CollectionReference<AppModelType, DbModelType>;\n  }\n\n  collectionGroup<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(collectionId: string): CollectionGroup<AppModelType, DbModelType> {\n    const ref = collectionGroup(this.firestore, collectionId);\n    return wrapCollectionLike(ref) as CollectionReference<AppModelType, DbModelType>;\n  }\n\n  query<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(\n    collection: CollectionReference<AppModelType, DbModelType>,\n    ...queryFnArray: QueryFn[]\n  ): Query<AppModelType, DbModelType> {\n    const ref = unwrapCollectionLike(collection);\n    return wrapCollectionLike(query(ref, ...queryFnArray.map(queryFn => queryFn())));\n  }\n\n  where<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(fieldPath: string, opStr: WhereFilterOp, value: unknown): QueryFn {\n    return () => where(fieldPath, opStr, value);\n  }\n\n  orderBy<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(key: string, order: 'asc' | 'desc' = 'asc'): QueryFn {\n    return () => orderBy(key, order);\n  }\n\n  limit<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(n: number): QueryFn {\n    return () => limit(n);\n  }\n\n  runTransaction() {\n    throw new Error('FirebaseFirestoreAdapter#runTransaction is not implemented');\n  }\n\n  batch() {\n    return writeBatch(this.firestore);\n  }\n}","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":[],"mappings":";;;;;;AA0DM,SAA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