{"version":3,"file":"nx-ddd-firestore-adapters-admin.mjs","sources":["../../../../../packages/@nx-ddd/firestore/src/lib/adapters/admin/admin.adapter.ts","../../../../../packages/@nx-ddd/firestore/src/lib/adapters/admin/nx-ddd-firestore-adapters-admin.ts"],"sourcesContent":["import { Injectable } from '@angular/core';\nimport admin from 'firebase-admin';\nimport { Subject } from 'rxjs';\nimport { DocumentData, Transaction } from 'firebase-admin/firestore';\nimport {\n  DocumentChangeAction,\n  DocumentReference,\n  CollectionReference as _CollectionReference,\n  CollectionGroup as _CollectionGroup,\n  Query as _Query } from '@nx-ddd/firestore';\nimport { FirestoreAdapter, QueryFn as _QueryFn, WhereFilterOp, provideFirestoreAdapter as _provideFirestoreAdapter } from '@nx-ddd/firestore';\n\ntype CollectionReference<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData,\n> = _CollectionReference<AppModelType, DbModelType, admin.firestore.CollectionReference<AppModelType, DbModelType>>;\n\ntype CollectionGroup<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n> = _CollectionGroup<AppModelType, DbModelType, admin.firestore.CollectionGroup<AppModelType, DbModelType>>;\n\ntype Query<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n> = _Query<AppModelType, DbModelType, admin.firestore.Query<AppModelType, DbModelType>>;\n\ntype QueryFn<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData\n> = _QueryFn<AppModelType, DbModelType, admin.firestore.Query>\n\ntype Origin<\n  AppModelType = DocumentData,\n  DbModelType extends DocumentData = DocumentData,\n> = admin.firestore.CollectionReference<AppModelType, DbModelType>\n  | admin.firestore.CollectionGroup<AppModelType, DbModelType>\n  | admin.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 admin.firestore.CollectionReference<infer A, infer B> ? CollectionReference<A, B> :\n  O extends admin.firestore.CollectionGroup<infer A, infer B> ? CollectionGroup<A, B> :\n  O extends admin.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      origin.onSnapshot(snapshot => {\n        const actions = snapshot.docChanges().map(change => ({\n          type: change.type, payload: {doc: change.doc},\n        }));\n        subject.next(actions);\n      });\n      return subject.asObservable();\n    },\n    get: () => origin.get(),\n    count: () => origin.get().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 AdminFirestoreAdapter<Date> extends FirestoreAdapter<Date> {\n  protected firestore = admin.firestore();\n\n  get FieldValue(): typeof admin.firestore.FieldValue {\n    return admin.firestore.FieldValue;  \n  }\n\n  get Timestamp(): typeof admin.firestore.Timestamp {\n    return admin.firestore.Timestamp;  \n  }\n\n  get FieldPath(): typeof admin.firestore.FieldPath {\n    return admin.firestore.FieldPath;\n  }\n\n  protected isTimestamp(v: any): v is admin.firestore.Timestamp {\n    return v instanceof this.Timestamp;\n  }\n\n  protected isFieldValue(v: any): v is admin.firestore.FieldValue {\n    return v instanceof this.FieldValue;\n  }\n\n  convertDateToTimestamp(date: Date): admin.firestore.Timestamp {\n    if (this.dateAdapter.isDate(date) && this.dateAdapter.isValid(date)) {\n      return admin.firestore.Timestamp.fromDate(this.dateAdapter.getJsDate(date));\n    }\n    throw new Error(`Invalid date: ${date}`);\n  }\n\n  doc(path: string): DocumentReference<any, admin.firestore.DocumentReference> {\n    const docRef = this.firestore.doc(path);\n    return {\n      __ref: docRef,\n      exists: () => docRef.get().then(snapshot => snapshot.exists),\n      set: (data, options?) => docRef.set(data, options),\n      get: () => docRef.get(),\n      update: (data) => docRef.update(data),\n      delete: () => docRef.delete(),\n    };\n  }\n\n  collection<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(path: string): CollectionReference<AppModelType, DbModelType> {\n    const collectionRef = this.firestore.collection(path);\n    return wrapCollectionLike(collectionRef) as CollectionReference<AppModelType, DbModelType>;\n  }\n\n  collectionGroup<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(collectionId: string): CollectionGroup<AppModelType, DbModelType> {\n    const collectionRef = this.firestore.collectionGroup(collectionId);\n    return wrapCollectionLike(collectionRef) as CollectionGroup<AppModelType, DbModelType>;\n  }\n  \n  runTransaction<T>(updateFunction: (transaction: Transaction) => Promise<T>) {\n    return this.firestore.runTransaction(updateFunction);\n  }\n\n  query<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(\n    collection: CollectionLike<AppModelType, DbModelType>,\n    ...queryFnArray: QueryFn<AppModelType, DbModelType>[]\n  ): Query<AppModelType, DbModelType> {\n    const ref = unwrapCollectionLike<AppModelType, DbModelType>(collection);\n    const query = queryFnArray.reduce((_ref, queryFn) => queryFn(_ref), ref);\n    return wrapCollectionLike(query) as Query<AppModelType, DbModelType>;\n  }\n\n  where<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(key: string, evaluation: WhereFilterOp, value: unknown): QueryFn<AppModelType, DbModelType> {\n    return (collection: Origin<AppModelType, DbModelType>) => collection.where(key, evaluation, value);  \n  }\n\n  orderBy<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(key: string, order: 'asc' | 'desc' = 'asc'): QueryFn<AppModelType, DbModelType> {\n    return (collection: Origin<AppModelType, DbModelType>) => collection.orderBy(key, order);\n  }\n\n  limit<\n    AppModelType = DocumentData,\n    DbModelType extends DocumentData = DocumentData,\n  >(n: number): QueryFn<AppModelType, DbModelType> {\n    return (collection: Origin<AppModelType, DbModelType>) => collection.limit(n);\n  }\n\n  batch() {\n    return this.firestore.batch();\n  }\n}\n\nexport function provideFirestoreAdapter() {\n  return _provideFirestoreAdapter(AdminFirestoreAdapter);\n}\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './index';\n"],"names":["_provideFirestoreAdapter"],"mappings":";;;;;;AAqDM,SAAU,kBAAkB,CAAmB,MAAS,EAAA;IAC5D,OAAO;AACL,QAAA,KAAK,EAAE,MAAM;QACb,YAAY,EAAE,MAAK;AACjB,YAAA,MAAM,OAAO,GAAG,IAAI,OAAO,EAA+B;AAC1D,YAAA,MAAM,CAAC,UAAU,CAAC,QAAQ,IAAG;AAC3B,gBAAA,MAAM,OAAO,GAAG,QAAQ,CAAC,UAAU,EAAE,CAAC,GAAG,CAAC,MAAM,KAAK;AACnD,oBAAA,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,OAAO,EAAE,EAAC,GAAG,EAAE,MAAM,CAAC,GAAG,EAAC;AAC9C,iBAAA,CAAC,CAAC;AACH,gBAAA,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC;AACvB,YAAA,CAAC,CAAC;AACF,YAAA,OAAO,OAAO,CAAC,YAAY,EAAE;QAC/B,CAAC;AACD,QAAA,GAAG,EAAE,MAAM,MAAM,CAAC,GAAG,EAAE;AACvB,QAAA,KAAK,EAAE,MAAM,MAAM,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,QAAQ,IAAI,QAAQ,CAAC,IAAI,CAAC;KACvB;AACtC;AAEM,SAAU,oBAAoB,CAIlC,UAAqD,EAAA;IAErD,OAAO,UAAU,CAAC,KAAK;AACzB;AAGM,MAAO,qBAA4B,SAAQ,gBAAsB,CAAA;AAC3D,IAAA,SAAS,GAAG,KAAK,CAAC,SAAS,EAAE;AAEvC,IAAA,IAAI,UAAU,G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