Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { combineLatest, defer, of } from 'rxjs';
  3. import { map, switchMap, tap } from 'rxjs/operators';
  4. import { AngularFirestore } from 'angularfire2/firestore';
  5.  
  6. @Injectable()
  7. export class RxService {
  8. constructor(private readonly fireStore: AngularFirestore) {
  9.  
  10. }
  11.  
  12. public docJoin(paths: { [key: string]: string }) {
  13. return source =>
  14. defer(() => {
  15. let parent;
  16. const keys = Object.keys(paths);
  17.  
  18. return source.pipe(
  19. switchMap(data => {
  20. parent = data;
  21. const docs$ = keys.map(k => {
  22. const fullPath = `${paths[k]}/${parent[k]}`;
  23. return this.fireStore.doc(fullPath).valueChanges();
  24. });
  25. return combineLatest(docs$);
  26. }),
  27. map(arr => {
  28. const joins = keys.reduce((acc, cur, idx) => {
  29. return { ...acc, [cur]: arr[idx] };
  30. }, {});
  31. return { ...parent, ...joins };
  32. })
  33. );
  34. });
  35. }
  36.  
  37. public leftJoinDocument(field, fieldName, collection) {
  38. return source =>
  39. defer(() => {
  40. let collectionData;
  41. const cache = new Map();
  42.  
  43. return source.pipe(
  44. switchMap(data => {
  45. cache.clear();
  46. collectionData = data as any[];
  47.  
  48. const reads$ = [];
  49. let i = 0;
  50. for (const doc of collectionData) {
  51. if (!doc[field] || cache.get(doc[field])) {
  52. continue;
  53. }
  54.  
  55. reads$.push(
  56. this.fireStore
  57. .collection(collection)
  58. .doc(doc[field])
  59. .valueChanges()
  60. );
  61. cache.set(doc[field], i);
  62. i++;
  63. }
  64.  
  65. return reads$.length ? combineLatest(reads$) : of([]);
  66. }),
  67. map(joins => {
  68. return collectionData.map((v, i) => {
  69. const joinIdx = cache.get(v[field]);
  70. return { ...v, [fieldName]: joins[joinIdx] || null };
  71. });
  72. })
  73. );
  74. });
  75. }
  76.  
  77. public leftJoin(field, collection, type = 'all', field2 = field) {
  78. return source =>
  79. defer(() => {
  80. let collectionData;
  81. let totalJoins = 0;
  82.  
  83. return source.pipe(
  84. switchMap(data => {
  85. collectionData = data as any[];
  86.  
  87. const reads$ = [];
  88. for (const doc of collectionData) {
  89. if (doc[field] || doc[field2]) {
  90. let q;
  91. if (type === 'all') {
  92. q = ref => ref.where(field, '==', doc[field2]);
  93. } else if (type === 'event') {
  94. q = ref => ref.where(field, '==', doc[field2]).where('status', '==', 'approved');
  95. }
  96.  
  97. reads$.push(
  98. this.fireStore.collection(collection, q).snapshotChanges()
  99. .pipe(map((snapshot) => {
  100. return snapshot.map((data) => {
  101. return {
  102. id: data.payload.doc.id,
  103. ...data.payload.doc.data()
  104. }
  105. });
  106. }))
  107. );
  108. } else {
  109. reads$.push(of([]));
  110. }
  111. }
  112.  
  113. return combineLatest(reads$);
  114. }),
  115. map(joins => {
  116. return collectionData.map((v, i) => {
  117. totalJoins += joins[i].length;
  118. return { ...v, ['mazlum']: joins[i] || null };
  119. });
  120. }),
  121. tap(final => {
  122. // console.log(
  123. // `Queried ${(final as any).length}, Joined ${totalJoins} docs`
  124. // );
  125. totalJoins = 0;
  126. })
  127. );
  128. });
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement