@edgedev/firebase 1.4.9 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +15 -3
  2. package/edgeFirebase.ts +108 -41
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -249,12 +249,24 @@ edgeFirebase.removeUser("user@edgemarketingdesign.com");
249
249
 
250
250
 
251
251
 
252
- ### List Users
252
+ ### Users Snapshot Data
253
253
 
254
- This will list all users that are members of the collection and subcollections passed to the function that the user running the function has assign access for, it will be a listed index by email/user id.
254
+ This will create a reactive object (users) that contains the members of the collection and subcollections passed to the snapshot that the user running the function has assign access for, it will be a listed index by email/user id. Passing no collection will get all users that the user running has assign access for.
255
255
 
256
256
  ```javascript
257
- const users = await edgeFirebase.listUsers("myItems");
257
+ edgeFirebase.startUsersSnapshot("myItems");
258
+ // Stop users snapshot:
259
+ edgeFirebase.stopUsersSnapshot();
260
+ ```
261
+
262
+ ```vue
263
+ <template>
264
+ <div>
265
+ <div v-for="user in edgeFirebase.users" :key="item">
266
+ {{ user.email }}
267
+ </div>
268
+ </div>
269
+ </template>
258
270
  ```
259
271
 
260
272
  ```typescript
package/edgeFirebase.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { initializeApp } from "firebase/app";
2
- import { reactive } from "vue";
2
+ import { reactive, computed } from "vue";
3
3
  import {
4
4
  getFirestore,
5
5
  collection,
@@ -213,6 +213,7 @@ export const EdgeFirebase = class {
213
213
  this.user.specialPermissions = specialPermissions;
214
214
  await this.listCollectionsCanAssign()
215
215
  }
216
+ this.stopSnapshot('userMeta')
216
217
  const metaUnsubscribe = onSnapshot(
217
218
  doc(this.db, "users", this.user.email),
218
219
  (doc) => {
@@ -733,6 +734,63 @@ export const EdgeFirebase = class {
733
734
 
734
735
  // Simple Store Items (add matching key per firebase collection)
735
736
  public data: CollectionDataObject = reactive({});
737
+ private usersByCollections: CollectionDataObject = reactive({});
738
+
739
+ public users = computed(() => {
740
+ const userList = {};
741
+ const keys = Object.keys(JSON.parse(JSON.stringify(this.usersByCollections)));
742
+ keys.forEach(key => {
743
+ const users = this.usersByCollections[key];
744
+ if (key.startsWith("ROLES|")) {
745
+ const collectionPathCheck = key.replace("ROLES|", "")
746
+ const userKeys = Object.keys(users);
747
+ if (Object.keys(users).length > 0) {
748
+ userKeys.forEach(userKey => {
749
+ const user = users[userKey];
750
+ if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
751
+ userList[user.email] = {
752
+ docId: user.docId,
753
+ email: user.email,
754
+ roles: [{collectionPath: collectionPathCheck, role: user.roles[collectionPathCheck].role }],
755
+ specialPermissions: [],
756
+ meta: user.meta,
757
+ last_updated: user.last_updated,
758
+ userId: user.userId,
759
+ uid: user.uid
760
+ }
761
+ } else {
762
+ userList[user.email].roles.push({ collectionPath: collectionPathCheck, role: user.roles[collectionPathCheck].role })
763
+ }
764
+ });
765
+ }
766
+ }
767
+ if (key.startsWith("SPECIALPERMISSIONS|")) {
768
+ const collectionPathCheck = key.replace("SPECIALPERMISSIONS|", "")
769
+ const userKeys = Object.keys(users);
770
+ if (Object.keys(users).length > 0) {
771
+ userKeys.forEach(userKey => {
772
+ const user = users[userKey];
773
+ if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
774
+ userList[user.email] = {
775
+ docId: user.docId,
776
+ email: user.email,
777
+ roles: [],
778
+ specialPermissions: [{ collectionPath: collectionPathCheck, permissions: user.specialPermissions[collectionPathCheck].permissions }],
779
+ meta: user.meta,
780
+ last_updated: user.last_updated,
781
+ userId: user.userId,
782
+ uid: user.uid
783
+ }
784
+ } else {
785
+ userList[user.email].specialPermissions.push({ collectionPath: collectionPathCheck, permissions: user.specialPermissions[collectionPathCheck].permissions })
786
+ }
787
+ });
788
+ }
789
+ }
790
+ });
791
+ return userList;
792
+ });
793
+
736
794
  public unsubscibe: CollectionUnsubscribeObject = reactive({});
737
795
  public user: UserDataObject = reactive({
738
796
  uid: null,
@@ -993,6 +1051,7 @@ export const EdgeFirebase = class {
993
1051
  ): Promise<actionResponse> => {
994
1052
  const canRead = await this.permissionCheck("read", collectionPath);
995
1053
  this.data[collectionPath] = {};
1054
+ this.stopSnapshot(collectionPath);
996
1055
  this.unsubscibe[collectionPath] = null;
997
1056
  if (canRead) {
998
1057
  const q = this.getQuery(collectionPath, queryList, orderList, max);
@@ -1067,70 +1126,78 @@ export const EdgeFirebase = class {
1067
1126
  this.user.canAssignCollectionPaths = collectionPathList;
1068
1127
  };
1069
1128
 
1070
- public listUsers = async (collectionPath = ''): Promise<usersByEmail> => {
1071
- const userList = {};
1129
+ public stopUsersSnapshot = (): void => {
1130
+ const keys = Object.keys(this.usersByCollections).filter((key) => key.startsWith('ROLES|') || key.startsWith('SPECIALPERMISSIONS|'));
1131
+ keys.forEach((key) => {
1132
+ this.stopSnapshot(key);
1133
+ });
1134
+ }
1072
1135
 
1136
+ public startUsersSnapshot = async(collectionPath = ''): Promise<void> => {
1137
+ this.stopUsersSnapshot();
1073
1138
  for (const collectionPathCheck of this.user.canAssignCollectionPaths) {
1074
-
1139
+
1075
1140
  if (collectionPathCheck.startsWith(collectionPath.replaceAll('/', '-'))) {
1076
- const roleUsers = await getDocs(
1077
- query(
1078
- collection(this.db, "users"),
1079
- where(
1080
- "roles." + collectionPathCheck + ".collectionPath",
1081
- "==",
1082
- collectionPathCheck
1083
- )
1141
+ this.usersByCollections['ROLES|' + collectionPathCheck] = {};
1142
+ let q = query(
1143
+ collection(this.db, "users"),
1144
+ where(
1145
+ "roles." + collectionPathCheck + ".collectionPath",
1146
+ "==",
1147
+ collectionPathCheck
1084
1148
  )
1085
- );
1086
-
1087
- roleUsers.forEach((doc) => {
1088
- const user = doc.data();
1089
- if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
1090
- userList[user.email] = {
1149
+ )
1150
+ const rolesUnsubscribe = await onSnapshot(q, (querySnapshot) => {
1151
+ const items = {};
1152
+ querySnapshot.forEach((doc) => {
1153
+ const user = doc.data();
1154
+ const item = {
1091
1155
  docId: user.docId,
1092
1156
  email: user.email,
1093
- roles: [{collectionPath: collectionPathCheck, role: user.roles[collectionPathCheck].role }],
1157
+ roles: user.roles,
1094
1158
  specialPermissions: [],
1095
1159
  meta: user.meta,
1096
1160
  last_updated: user.last_updated,
1097
1161
  userId: user.userId,
1098
1162
  uid: user.uid
1099
1163
  }
1100
- } else {
1101
- userList[user.email].roles.push({ collectionPath: collectionPathCheck, role: user.roles[collectionPathCheck].role })
1102
- }
1164
+ items[doc.id] = item;
1165
+ });
1166
+ this.usersByCollections['ROLES|' + collectionPathCheck] = items;
1103
1167
  });
1104
- const specialPermissionsUsers = await getDocs(
1105
- query(
1106
- collection(this.db, "users"),
1107
- where(
1108
- "specialPermissions." + collectionPathCheck + ".collectionPath",
1109
- "==",
1110
- collectionPathCheck
1111
- )
1168
+ this.unsubscibe['ROLES|' + collectionPathCheck] = rolesUnsubscribe
1169
+
1170
+ this.usersByCollections['SPECIALPERMISSIONS|' + collectionPathCheck] = {};
1171
+ q = query(
1172
+ collection(this.db, "users"),
1173
+ where(
1174
+ "specialPermissions." + collectionPathCheck + ".collectionPath",
1175
+ "==",
1176
+ collectionPathCheck
1112
1177
  )
1113
- );
1114
- specialPermissionsUsers.forEach((doc) => {
1115
- const user = doc.data();
1116
- if (!Object.prototype.hasOwnProperty.call(userList, user.docId)) {
1117
- userList[user.email] = {
1178
+ )
1179
+
1180
+ const specialPermissionsunsubscribe = await onSnapshot(q, (querySnapshot) => {
1181
+ const items = {};
1182
+ querySnapshot.forEach((doc) => {
1183
+ const user = doc.data();
1184
+ const item = {
1118
1185
  docId: user.docId,
1119
1186
  email: user.email,
1120
- role: [],
1121
- specialPermissions: [{ collectionPath: collectionPathCheck, permissions: user.specialPermissions[collectionPathCheck].permissions }],
1187
+ roles: [],
1188
+ specialPermissions: user.specialPermissions,
1122
1189
  meta: user.meta,
1123
1190
  last_updated: user.last_updated,
1124
1191
  userId: user.userId,
1125
1192
  uid: user.uid
1126
1193
  }
1127
- } else {
1128
- userList[user.email].specialPermissions.push({ collectionPath: collectionPathCheck, permissions: user.specialPermissions[collectionPathCheck].permissions })
1129
- }
1194
+ items[doc.id] = item;
1195
+ });
1196
+ this.usersByCollections['SPECIALPERMISSIONS|' + collectionPathCheck] = items;
1130
1197
  });
1198
+ this.unsubscibe['SPECIALPERMISSIONS|' + collectionPathCheck] = specialPermissionsunsubscribe;
1131
1199
  }
1132
1200
  }
1133
- return userList;
1134
1201
  };
1135
1202
 
1136
1203
  public removeUserRoles = async (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.4.9",
3
+ "version": "1.5.2",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 global composable for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {