@hyper-fetch/firebase 5.0.0
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.
- package/.eslintrc.json +6 -0
- package/.firebaserc +5 -0
- package/README.md +73 -0
- package/database.rules.json +11 -0
- package/dist/index.cjs.js +1204 -0
- package/dist/index.cjs.js.map +7 -0
- package/dist/index.d.ts +293 -0
- package/dist/index.esm.js +1270 -0
- package/dist/index.esm.js.map +7 -0
- package/firebase.json +28 -0
- package/firestore.indexes.json +4 -0
- package/firestore.rules +8 -0
- package/package.json +75 -0
- package/src/adapter/admin/adapter.admin.sockets.ts +14 -0
- package/src/adapter/admin/adapter.admin.ts +72 -0
- package/src/adapter/admin/adapter.admin.types.ts +15 -0
- package/src/adapter/admin/index.ts +3 -0
- package/src/adapter/browser/adapter.browser.sockets.ts +18 -0
- package/src/adapter/browser/adapter.browser.ts +81 -0
- package/src/adapter/browser/adapter.browser.types.ts +15 -0
- package/src/adapter/browser/index.ts +3 -0
- package/src/adapter/index.ts +3 -0
- package/src/adapter/types/adapter.firestore.types.ts +78 -0
- package/src/adapter/types/adapter.realtime.types.ts +82 -0
- package/src/adapter/types/firestore.admin.types.ts +17 -0
- package/src/adapter/types/index.ts +4 -0
- package/src/adapter/types/realtime.admin.types.ts +17 -0
- package/src/constraints/constraints.types.ts +71 -0
- package/src/constraints/firebase.constraints.ts +117 -0
- package/src/constraints/index.ts +2 -0
- package/src/firestore/admin/firestore.admin.methods.ts +86 -0
- package/src/firestore/admin/firestore.admin.sockets.ts +86 -0
- package/src/firestore/admin/firestore.admin.utils.ts +69 -0
- package/src/firestore/admin/index.ts +2 -0
- package/src/firestore/browser/firestore.browser.methods.ts +105 -0
- package/src/firestore/browser/firestore.browser.sockets.ts +102 -0
- package/src/firestore/browser/firestore.browser.utils.ts +39 -0
- package/src/firestore/browser/index.ts +3 -0
- package/src/firestore/index.ts +3 -0
- package/src/firestore/utils/firestore.utils.ts +17 -0
- package/src/firestore/utils/index.ts +1 -0
- package/src/index.ts +5 -0
- package/src/realtime/admin/index.ts +2 -0
- package/src/realtime/admin/realtime.admin.methods.ts +74 -0
- package/src/realtime/admin/realtime.admin.sockets.ts +93 -0
- package/src/realtime/admin/realtime.admin.utils.ts +54 -0
- package/src/realtime/browser/index.ts +3 -0
- package/src/realtime/browser/realtime.browser.methods.ts +78 -0
- package/src/realtime/browser/realtime.browser.sockets.ts +96 -0
- package/src/realtime/browser/realtime.browser.utils.ts +59 -0
- package/src/realtime/index.ts +3 -0
- package/src/realtime/utils/index.ts +1 -0
- package/src/realtime/utils/realtime.utils.ts +7 -0
- package/src/utils/cache.utils.ts +33 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/misc.ts +10 -0
- package/test.sh +3 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { AdapterType } from "@hyper-fetch/core";
|
|
2
|
+
import { DatabaseReference, DataSnapshot } from "firebase/database";
|
|
3
|
+
import { SocketAdapterType } from "@hyper-fetch/sockets";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
PermittedConstraints,
|
|
7
|
+
RealtimeConstraintsUnion,
|
|
8
|
+
RealtimePermittedMethods,
|
|
9
|
+
SharedQueryConstraints,
|
|
10
|
+
} from "constraints";
|
|
11
|
+
|
|
12
|
+
export type RealtimeSocketAdapterType = SocketAdapterType<
|
|
13
|
+
never,
|
|
14
|
+
RealtimeDbOnValueMethodExtra,
|
|
15
|
+
{ onlyOnce?: boolean } & RealtimeDBQueryParams
|
|
16
|
+
>;
|
|
17
|
+
|
|
18
|
+
export type RealtimeDbAdapterType =
|
|
19
|
+
| AdapterType<
|
|
20
|
+
DefaultRealtimeDBAdapterOptions,
|
|
21
|
+
"get",
|
|
22
|
+
RealtimeDBStatuses,
|
|
23
|
+
RealtimeDbGetMethodExtra,
|
|
24
|
+
RealtimeDBQueryParams
|
|
25
|
+
>
|
|
26
|
+
| AdapterType<
|
|
27
|
+
DefaultRealtimeDBAdapterOptions,
|
|
28
|
+
"push",
|
|
29
|
+
RealtimeDBStatuses,
|
|
30
|
+
RealtimeDbPushMethodExtra,
|
|
31
|
+
Record<string, never>
|
|
32
|
+
>
|
|
33
|
+
| AdapterType<
|
|
34
|
+
DefaultRealtimeDBAdapterOptions,
|
|
35
|
+
"set" | "update" | "remove",
|
|
36
|
+
RealtimeDBStatuses,
|
|
37
|
+
RealtimeDbDefaultExtra,
|
|
38
|
+
Record<string, never>
|
|
39
|
+
>;
|
|
40
|
+
|
|
41
|
+
export type DefaultRealtimeDBAdapterOptions = {
|
|
42
|
+
priority?: number;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export enum RealtimeDBMethods {
|
|
46
|
+
set = "set",
|
|
47
|
+
push = "push",
|
|
48
|
+
update = "update",
|
|
49
|
+
get = "get",
|
|
50
|
+
remove = "remove",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type RealtimeDBMethodsUnion =
|
|
54
|
+
| RealtimeDBMethods.set
|
|
55
|
+
| RealtimeDBMethods.push
|
|
56
|
+
| RealtimeDBMethods.update
|
|
57
|
+
| RealtimeDBMethods.get
|
|
58
|
+
| RealtimeDBMethods.remove;
|
|
59
|
+
|
|
60
|
+
export type RealtimeDBStatuses = "success" | "error" | "emptyResource";
|
|
61
|
+
export type RealtimeDbOnValueMethodExtra = {
|
|
62
|
+
ref: DatabaseReference;
|
|
63
|
+
snapshot: DataSnapshot;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export type RealtimeDbGetMethodExtra = {
|
|
67
|
+
ref: DatabaseReference;
|
|
68
|
+
snapshot: DataSnapshot;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type RealtimeDbDefaultExtra = {
|
|
72
|
+
ref: DatabaseReference;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type RealtimeDbPushMethodExtra = {
|
|
76
|
+
ref: DatabaseReference;
|
|
77
|
+
key: string;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export type RealtimeDBQueryParams = {
|
|
81
|
+
constraints?: PermittedConstraints<RealtimePermittedMethods, RealtimeConstraintsUnion | SharedQueryConstraints>[];
|
|
82
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DocumentReference, DocumentSnapshot, Query } from "firebase-admin/firestore";
|
|
2
|
+
import { SocketAdapterType } from "@hyper-fetch/sockets";
|
|
3
|
+
|
|
4
|
+
import { FirestoreQueryParams } from "adapter";
|
|
5
|
+
|
|
6
|
+
export type FirestoreAdminSocketAdapterType = SocketAdapterType<
|
|
7
|
+
never,
|
|
8
|
+
FirestoreAdminOnSnapshotExtra,
|
|
9
|
+
{ groupByChangeType?: boolean } & FirestoreQueryParams
|
|
10
|
+
>;
|
|
11
|
+
|
|
12
|
+
export type FirestoreAdminOnSnapshotExtra = {
|
|
13
|
+
ref?: DocumentReference | Query;
|
|
14
|
+
snapshot?: DocumentSnapshot;
|
|
15
|
+
unsubscribe?: () => void;
|
|
16
|
+
groupedResult?: { added: DocumentSnapshot[]; modified: DocumentSnapshot[]; removed: DocumentSnapshot[] };
|
|
17
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SocketAdapterType } from "@hyper-fetch/sockets";
|
|
2
|
+
import { Reference, DataSnapshot } from "firebase-admin/database";
|
|
3
|
+
|
|
4
|
+
import { RealtimeDBQueryParams, RealtimeDBStatuses } from "adapter/index";
|
|
5
|
+
|
|
6
|
+
export type RealtimeAdminSocketAdapterType = SocketAdapterType<
|
|
7
|
+
never,
|
|
8
|
+
RealtimeAdminOnValueMethodExtra,
|
|
9
|
+
{ onlyOnce?: boolean } & RealtimeDBQueryParams,
|
|
10
|
+
never
|
|
11
|
+
>;
|
|
12
|
+
|
|
13
|
+
export type RealtimeAdminOnValueMethodExtra = {
|
|
14
|
+
ref: Reference;
|
|
15
|
+
snapshot: DataSnapshot;
|
|
16
|
+
status: RealtimeDBStatuses;
|
|
17
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import {
|
|
2
|
+
$endAt,
|
|
3
|
+
$endBefore,
|
|
4
|
+
$equalTo,
|
|
5
|
+
$limit,
|
|
6
|
+
$limitToFirst,
|
|
7
|
+
$limitToLast,
|
|
8
|
+
$orderBy,
|
|
9
|
+
$orderByChild,
|
|
10
|
+
$orderByKey,
|
|
11
|
+
$orderByValue,
|
|
12
|
+
$startAfter,
|
|
13
|
+
$startAt,
|
|
14
|
+
$where,
|
|
15
|
+
FirestoreQueryConstraints,
|
|
16
|
+
RealtimeQueryConstraints,
|
|
17
|
+
} from "./firebase.constraints";
|
|
18
|
+
|
|
19
|
+
export type RealtimeConstraintsUnion = `${RealtimeQueryConstraints}`;
|
|
20
|
+
export type FirestoreConstraintsUnion = `${FirestoreQueryConstraints}`;
|
|
21
|
+
|
|
22
|
+
export type RealtimePermittedMethods =
|
|
23
|
+
| ReturnType<typeof $startAt>
|
|
24
|
+
| ReturnType<typeof $startAfter>
|
|
25
|
+
| ReturnType<typeof $endAt>
|
|
26
|
+
| ReturnType<typeof $endBefore>
|
|
27
|
+
| ReturnType<typeof $orderByChild>
|
|
28
|
+
| ReturnType<typeof $orderByValue>
|
|
29
|
+
| ReturnType<typeof $orderByKey>
|
|
30
|
+
| ReturnType<typeof $limitToFirst>
|
|
31
|
+
| ReturnType<typeof $limitToLast>
|
|
32
|
+
| ReturnType<typeof $equalTo>;
|
|
33
|
+
|
|
34
|
+
export type FirestorePermittedMethods =
|
|
35
|
+
| ReturnType<typeof $where>
|
|
36
|
+
| ReturnType<typeof $orderBy>
|
|
37
|
+
| ReturnType<typeof $limit>
|
|
38
|
+
| ReturnType<typeof $startAt>
|
|
39
|
+
| ReturnType<typeof $startAfter>
|
|
40
|
+
| ReturnType<typeof $endAt>
|
|
41
|
+
| ReturnType<typeof $endBefore>;
|
|
42
|
+
|
|
43
|
+
type ExtractConstraintType<T> = T extends { toString: () => string; type: infer ThisType; values: any }
|
|
44
|
+
? ThisType
|
|
45
|
+
: never;
|
|
46
|
+
|
|
47
|
+
export type PermittedConstraints<T, U> = ExtractConstraintType<T> extends U ? T : never;
|
|
48
|
+
//
|
|
49
|
+
// // TODO - why second and third elements compare to LIMIT and all others to FirestorePermittedMethods (that is correct)
|
|
50
|
+
// type firestoreConstraints = PermittedConstraints<
|
|
51
|
+
// FirestorePermittedMethods,
|
|
52
|
+
// FirestoreConstraintsUnion | SharedQueryConstraints
|
|
53
|
+
// >;
|
|
54
|
+
// type realtimeConstraints = PermittedConstraints<
|
|
55
|
+
// RealtimePermittedMethods,
|
|
56
|
+
// RealtimeConstraintsUnion | SharedQueryConstraints
|
|
57
|
+
// >;
|
|
58
|
+
// // const firestoreCheck: firestoreConstraints[] = [
|
|
59
|
+
// // $where("type", "==", "Green"),
|
|
60
|
+
// // $orderByChild("sdfs"),
|
|
61
|
+
// // $equalTo("sdfs"),
|
|
62
|
+
// // $equalTo("ds"),
|
|
63
|
+
// // ];
|
|
64
|
+
// // TODO - same here - why compares incorrect one to 'EQUAL_TO'
|
|
65
|
+
// const realtimeCheck: realtimeConstraints[] = [
|
|
66
|
+
// $orderByChild("sdfs"),
|
|
67
|
+
// $startAt(100),
|
|
68
|
+
// $where("type", "==", "Green"),
|
|
69
|
+
// $where("type", "==", "Green"),
|
|
70
|
+
// $where("type", "==", "Green"),
|
|
71
|
+
// ];
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { OrderByDirection, WhereFilterOp } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
export enum SharedQueryConstraints {
|
|
4
|
+
START_AT = "startAt",
|
|
5
|
+
START_AFTER = "startAfter",
|
|
6
|
+
END_AT = "endAt",
|
|
7
|
+
END_BEFORE = "endBefore",
|
|
8
|
+
}
|
|
9
|
+
export enum RealtimeQueryConstraints {
|
|
10
|
+
ORDER_BY_CHILD = "orderByChild",
|
|
11
|
+
ORDER_BY_KEY = "orderByKey",
|
|
12
|
+
ORDER_BY_VALUE = "orderByValue",
|
|
13
|
+
LIMIT_TO_FIRST = "limitToFirst",
|
|
14
|
+
LIMIT_TO_LAST = "limitToLast",
|
|
15
|
+
EQUAL_TO = "equalTo",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export enum FirestoreQueryConstraints {
|
|
19
|
+
WHERE = "where",
|
|
20
|
+
ORDER_BY = "orderBy",
|
|
21
|
+
LIMIT = "limit",
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const $where = (fieldPath: string, opStr: WhereFilterOp, value: any) => {
|
|
25
|
+
return {
|
|
26
|
+
toString: () => `${FirestoreQueryConstraints.WHERE}_${fieldPath}${opStr}${value}`,
|
|
27
|
+
type: FirestoreQueryConstraints.WHERE as const,
|
|
28
|
+
values: [fieldPath, opStr, value],
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const $orderBy = (fieldPath: string, directionStr?: OrderByDirection) => {
|
|
33
|
+
return {
|
|
34
|
+
toString: () => `${FirestoreQueryConstraints.ORDER_BY}_${fieldPath}_${directionStr || ""}`,
|
|
35
|
+
type: FirestoreQueryConstraints.ORDER_BY as const,
|
|
36
|
+
values: [fieldPath, directionStr],
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const $limit = (num: number) => {
|
|
41
|
+
return {
|
|
42
|
+
toString: () => `${FirestoreQueryConstraints.LIMIT}_${num}`,
|
|
43
|
+
type: FirestoreQueryConstraints.LIMIT as const,
|
|
44
|
+
values: [num],
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const startEndConstraintsImpl = (constraintType: SharedQueryConstraints) => {
|
|
49
|
+
return (...docOrFields: any[]) => {
|
|
50
|
+
if (docOrFields.length === 1 && docOrFields[0].query) {
|
|
51
|
+
return {
|
|
52
|
+
toString: () => `${constraintType}_${docOrFields[0].query?._query?.memoizedTarget?.memoizedCanonicalId}`,
|
|
53
|
+
type: constraintType,
|
|
54
|
+
values: [docOrFields],
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
return {
|
|
58
|
+
toString: () => `${constraintType}_${docOrFields}`,
|
|
59
|
+
type: constraintType,
|
|
60
|
+
values: [docOrFields],
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export const $startAt = startEndConstraintsImpl(SharedQueryConstraints.START_AT as const);
|
|
66
|
+
export const $startAfter = startEndConstraintsImpl(SharedQueryConstraints.START_AFTER as const);
|
|
67
|
+
|
|
68
|
+
export const $endAt = startEndConstraintsImpl(SharedQueryConstraints.END_AT as const);
|
|
69
|
+
export const $endBefore = startEndConstraintsImpl(SharedQueryConstraints.END_BEFORE as const);
|
|
70
|
+
|
|
71
|
+
export const $orderByChild = (path: string) => {
|
|
72
|
+
return {
|
|
73
|
+
toString: () => `${RealtimeQueryConstraints.ORDER_BY_CHILD}_${path}`,
|
|
74
|
+
type: RealtimeQueryConstraints.ORDER_BY_CHILD as const,
|
|
75
|
+
values: [path],
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export const $orderByKey = () => {
|
|
80
|
+
return {
|
|
81
|
+
toString: () => `${RealtimeQueryConstraints.ORDER_BY_KEY}`,
|
|
82
|
+
type: RealtimeQueryConstraints.ORDER_BY_KEY as const,
|
|
83
|
+
values: [],
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export const $orderByValue = () => {
|
|
88
|
+
return {
|
|
89
|
+
toString: () => `${RealtimeQueryConstraints.ORDER_BY_VALUE}`,
|
|
90
|
+
type: RealtimeQueryConstraints.ORDER_BY_VALUE as const,
|
|
91
|
+
values: [],
|
|
92
|
+
};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const $limitToFirst = (num: number) => {
|
|
96
|
+
return {
|
|
97
|
+
toString: () => `${RealtimeQueryConstraints.LIMIT_TO_FIRST}_${num}`,
|
|
98
|
+
type: RealtimeQueryConstraints.LIMIT_TO_FIRST as const,
|
|
99
|
+
values: [num],
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export const $limitToLast = (num: number) => {
|
|
104
|
+
return {
|
|
105
|
+
toString: () => `${RealtimeQueryConstraints.LIMIT_TO_LAST}_${num}`,
|
|
106
|
+
type: RealtimeQueryConstraints.LIMIT_TO_LAST as const,
|
|
107
|
+
values: [num],
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const $equalTo = (value: any) => {
|
|
112
|
+
return {
|
|
113
|
+
toString: () => `${RealtimeQueryConstraints.LIMIT_TO_LAST}_${value}`,
|
|
114
|
+
type: RealtimeQueryConstraints.EQUAL_TO as const,
|
|
115
|
+
values: [value],
|
|
116
|
+
};
|
|
117
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { CollectionReference, DocumentReference, DocumentSnapshot, Firestore } from "firebase-admin/firestore";
|
|
2
|
+
import { RequestInstance } from "@hyper-fetch/core";
|
|
3
|
+
|
|
4
|
+
import { FirestoreMethodsUnion } from "adapter";
|
|
5
|
+
import { getStatus } from "utils";
|
|
6
|
+
import { applyFireStoreAdminConstraints, getRef, getOrderedResultFirestore } from "firestore";
|
|
7
|
+
import {
|
|
8
|
+
FirestoreConstraintsUnion,
|
|
9
|
+
FirestorePermittedMethods,
|
|
10
|
+
PermittedConstraints,
|
|
11
|
+
SharedQueryConstraints,
|
|
12
|
+
} from "constraints";
|
|
13
|
+
|
|
14
|
+
export const getFirestoreAdminMethods = <R extends RequestInstance>(
|
|
15
|
+
request: R,
|
|
16
|
+
database: Firestore,
|
|
17
|
+
url: string,
|
|
18
|
+
onSuccess,
|
|
19
|
+
onError,
|
|
20
|
+
resolve,
|
|
21
|
+
events: { onRequestStart; onResponseEnd; onResponseStart; onRequestEnd },
|
|
22
|
+
): ((
|
|
23
|
+
methodName: FirestoreMethodsUnion,
|
|
24
|
+
data: {
|
|
25
|
+
constraints?: PermittedConstraints<FirestorePermittedMethods, FirestoreConstraintsUnion | SharedQueryConstraints>[];
|
|
26
|
+
data?: any;
|
|
27
|
+
options?: Record<string, any>;
|
|
28
|
+
},
|
|
29
|
+
) => Promise<void>) => {
|
|
30
|
+
const [cleanUrl] = url.split("?");
|
|
31
|
+
const methods = {
|
|
32
|
+
getDoc: async () => {
|
|
33
|
+
const path = getRef(database, cleanUrl);
|
|
34
|
+
const snapshot = (await path.get()) as DocumentSnapshot;
|
|
35
|
+
const result = snapshot.data() || null;
|
|
36
|
+
const status = result ? "success" : "emptyResource";
|
|
37
|
+
return { result, status, extra: { ref: path, snapshot } };
|
|
38
|
+
},
|
|
39
|
+
getDocs: async ({ constraints = [] }) => {
|
|
40
|
+
const path = getRef(database, cleanUrl) as CollectionReference;
|
|
41
|
+
const query = applyFireStoreAdminConstraints(path, constraints);
|
|
42
|
+
const querySnapshot = await query.get();
|
|
43
|
+
const result = getOrderedResultFirestore(querySnapshot);
|
|
44
|
+
const status = getStatus(result);
|
|
45
|
+
|
|
46
|
+
return { result, status, extra: { ref: path, snapshot: querySnapshot } };
|
|
47
|
+
},
|
|
48
|
+
setDoc: async ({ data, options }: { data?: any; options?: Record<string, any> }) => {
|
|
49
|
+
const path = getRef(database, cleanUrl) as DocumentReference;
|
|
50
|
+
const merge = options?.merge === true;
|
|
51
|
+
const res = await path.set(data, { merge });
|
|
52
|
+
return { result: res, status: "success", extra: { ref: path } };
|
|
53
|
+
},
|
|
54
|
+
addDoc: async ({ data }: { data?: any }) => {
|
|
55
|
+
const ref = getRef(database, cleanUrl) as CollectionReference;
|
|
56
|
+
const docRef = await ref.add(data);
|
|
57
|
+
return { result: data, status: "success", extra: { ref: docRef } };
|
|
58
|
+
},
|
|
59
|
+
updateDoc: async ({ data }: { data?: any }) => {
|
|
60
|
+
const path = getRef(database, cleanUrl) as DocumentReference;
|
|
61
|
+
await path.update(data);
|
|
62
|
+
return { result: data, status: "success", extra: { ref: path } };
|
|
63
|
+
},
|
|
64
|
+
deleteDoc: async () => {
|
|
65
|
+
const path = getRef(database, cleanUrl) as DocumentReference;
|
|
66
|
+
await path.delete();
|
|
67
|
+
return { result: null, status: "success", extra: { ref: path } };
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return async (methodName: FirestoreMethodsUnion, data) => {
|
|
72
|
+
try {
|
|
73
|
+
events.onRequestStart();
|
|
74
|
+
const { result, status, extra } = await methods[methodName](data);
|
|
75
|
+
events.onRequestEnd();
|
|
76
|
+
events.onResponseStart();
|
|
77
|
+
onSuccess(result, status, extra, resolve);
|
|
78
|
+
events.onResponseEnd();
|
|
79
|
+
} catch (e) {
|
|
80
|
+
events.onRequestEnd();
|
|
81
|
+
events.onResponseStart();
|
|
82
|
+
onError(e, "error", {}, resolve);
|
|
83
|
+
events.onResponseEnd();
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { CollectionReference, DocumentReference, DocumentSnapshot, Firestore, Query } from "firebase-admin/firestore";
|
|
2
|
+
import { getSocketAdapterBindings } from "@hyper-fetch/sockets";
|
|
3
|
+
|
|
4
|
+
import { applyFireStoreAdminConstraints, getRef } from "./firestore.admin.utils";
|
|
5
|
+
import { getGroupedResultFirestore, getOrderedResultFirestore } from "../utils";
|
|
6
|
+
import { getStatus } from "utils";
|
|
7
|
+
import { FirestoreAdminSocketAdapterType } from "adapter";
|
|
8
|
+
|
|
9
|
+
export const firestoreAdminSockets = (database: Firestore): FirestoreAdminSocketAdapterType => {
|
|
10
|
+
return (socket) => {
|
|
11
|
+
const {
|
|
12
|
+
open,
|
|
13
|
+
connecting,
|
|
14
|
+
reconnectionAttempts,
|
|
15
|
+
listeners,
|
|
16
|
+
removeListener,
|
|
17
|
+
onReconnect,
|
|
18
|
+
onListen,
|
|
19
|
+
onEvent,
|
|
20
|
+
onError,
|
|
21
|
+
} = getSocketAdapterBindings(socket, { open: true });
|
|
22
|
+
|
|
23
|
+
const connect = () => {
|
|
24
|
+
throw new Error("Connect function is not implemented for Firestore Admin socket.");
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const disconnect = () => {
|
|
28
|
+
throw new Error("Cannot disconnect from Firestore database socket. Use 'app.delete()' instead.");
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const reconnect = () => {
|
|
32
|
+
onReconnect(disconnect, connect);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const listen: ReturnType<FirestoreAdminSocketAdapterType>["listen"] = (listener, callback) => {
|
|
36
|
+
const fullUrl = socket.url + listener.endpoint;
|
|
37
|
+
const { options } = listener;
|
|
38
|
+
let pathRef: DocumentReference | Query = getRef(database, fullUrl);
|
|
39
|
+
if (pathRef instanceof CollectionReference) {
|
|
40
|
+
pathRef = applyFireStoreAdminConstraints(pathRef, options?.constraints || []);
|
|
41
|
+
}
|
|
42
|
+
let unsubscribe = () => {};
|
|
43
|
+
let unmount = () => {};
|
|
44
|
+
let clearListeners = () => {};
|
|
45
|
+
|
|
46
|
+
unsubscribe = pathRef.onSnapshot(
|
|
47
|
+
(snapshot) => {
|
|
48
|
+
const response =
|
|
49
|
+
snapshot instanceof DocumentSnapshot ? snapshot.data() || null : getOrderedResultFirestore(snapshot);
|
|
50
|
+
const status = getStatus(response);
|
|
51
|
+
const groupedResult = options?.groupByChangeType === true ? getGroupedResultFirestore(snapshot) : null;
|
|
52
|
+
const extra = { ref: pathRef, snapshot, unsubscribe, groupedResult, status };
|
|
53
|
+
callback({ data: response, extra });
|
|
54
|
+
onEvent(listener.endpoint, response, extra);
|
|
55
|
+
},
|
|
56
|
+
(error) => {
|
|
57
|
+
onError(error);
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
unmount = onListen(listener, callback, unsubscribe);
|
|
61
|
+
clearListeners = () => {
|
|
62
|
+
unsubscribe();
|
|
63
|
+
unmount();
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return clearListeners;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const emit = async () => {
|
|
70
|
+
throw new Error("Cannot emit from Firestore database socket.");
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
open,
|
|
75
|
+
reconnectionAttempts,
|
|
76
|
+
listeners,
|
|
77
|
+
connecting,
|
|
78
|
+
listen,
|
|
79
|
+
removeListener,
|
|
80
|
+
emit,
|
|
81
|
+
connect,
|
|
82
|
+
reconnect,
|
|
83
|
+
disconnect,
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { CollectionReference, DocumentReference, Firestore } from "firebase-admin/lib/firestore";
|
|
2
|
+
import { OrderByDirection } from "firebase/firestore";
|
|
3
|
+
|
|
4
|
+
import { FirestorePermittedMethods, FirestoreQueryConstraints, SharedQueryConstraints } from "../../constraints";
|
|
5
|
+
|
|
6
|
+
export const getRef = (db: Firestore, fullUrl: string) => {
|
|
7
|
+
const withoutSurroundingSlashes = fullUrl.replace(/^\/|\/$/g, "");
|
|
8
|
+
const urlParts = withoutSurroundingSlashes.split("/").map((element, index) => {
|
|
9
|
+
return index % 2 === 0 ? ["collection", element] : ["doc", element];
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
return urlParts.reduce((_db, value) => {
|
|
13
|
+
const [method, pathPart] = value;
|
|
14
|
+
if (method === "doc" && "doc" in _db) {
|
|
15
|
+
return _db.doc(pathPart);
|
|
16
|
+
}
|
|
17
|
+
if (method === "collection" && "collection" in _db) {
|
|
18
|
+
return _db.collection(pathPart);
|
|
19
|
+
}
|
|
20
|
+
return _db;
|
|
21
|
+
}, db as unknown as Firestore | CollectionReference | DocumentReference) as CollectionReference | DocumentReference;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const applyFireStoreAdminConstraint = (
|
|
25
|
+
collectionRef: CollectionReference,
|
|
26
|
+
{ type, values }: FirestorePermittedMethods,
|
|
27
|
+
) => {
|
|
28
|
+
switch (type) {
|
|
29
|
+
case FirestoreQueryConstraints.WHERE: {
|
|
30
|
+
const [fieldPath, strOp, value] = values;
|
|
31
|
+
return collectionRef.where(fieldPath, strOp, value);
|
|
32
|
+
}
|
|
33
|
+
case FirestoreQueryConstraints.ORDER_BY: {
|
|
34
|
+
const [field, ord] = values;
|
|
35
|
+
return collectionRef.orderBy(field, ord as OrderByDirection);
|
|
36
|
+
}
|
|
37
|
+
case FirestoreQueryConstraints.LIMIT: {
|
|
38
|
+
const [limitValue] = values;
|
|
39
|
+
return collectionRef.limit(limitValue);
|
|
40
|
+
}
|
|
41
|
+
case SharedQueryConstraints.START_AT: {
|
|
42
|
+
const [docOrFields] = values;
|
|
43
|
+
return collectionRef.startAt(docOrFields);
|
|
44
|
+
}
|
|
45
|
+
case SharedQueryConstraints.START_AFTER: {
|
|
46
|
+
const [docOrFields] = values;
|
|
47
|
+
return collectionRef.startAfter(docOrFields);
|
|
48
|
+
}
|
|
49
|
+
case SharedQueryConstraints.END_AT: {
|
|
50
|
+
const [docOrFields] = values;
|
|
51
|
+
return collectionRef.endAt(docOrFields);
|
|
52
|
+
}
|
|
53
|
+
case SharedQueryConstraints.END_BEFORE: {
|
|
54
|
+
const [docOrFields] = values;
|
|
55
|
+
return collectionRef.endBefore(docOrFields);
|
|
56
|
+
}
|
|
57
|
+
default:
|
|
58
|
+
throw new Error(`Unknown method ${type}`);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const applyFireStoreAdminConstraints = (
|
|
63
|
+
collectionRef: CollectionReference,
|
|
64
|
+
constraints: FirestorePermittedMethods[],
|
|
65
|
+
) => {
|
|
66
|
+
return constraints.reduce((collection, constraint) => {
|
|
67
|
+
return applyFireStoreAdminConstraint(collection, constraint);
|
|
68
|
+
}, collectionRef);
|
|
69
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import {
|
|
2
|
+
addDoc,
|
|
3
|
+
collection,
|
|
4
|
+
deleteDoc,
|
|
5
|
+
doc,
|
|
6
|
+
Firestore,
|
|
7
|
+
getDoc,
|
|
8
|
+
getDocs,
|
|
9
|
+
query,
|
|
10
|
+
setDoc,
|
|
11
|
+
updateDoc,
|
|
12
|
+
} from "firebase/firestore";
|
|
13
|
+
import { RequestInstance } from "@hyper-fetch/core";
|
|
14
|
+
|
|
15
|
+
import { FirestoreMethodsUnion } from "adapter/types";
|
|
16
|
+
import {
|
|
17
|
+
FirestoreConstraintsUnion,
|
|
18
|
+
FirestorePermittedMethods,
|
|
19
|
+
PermittedConstraints,
|
|
20
|
+
SharedQueryConstraints,
|
|
21
|
+
} from "constraints";
|
|
22
|
+
import { getStatus } from "utils";
|
|
23
|
+
import { getOrderedResultFirestore } from "../utils/firestore.utils";
|
|
24
|
+
import { mapConstraint } from "./firestore.browser.utils";
|
|
25
|
+
|
|
26
|
+
export const getFirestoreBrowserMethods = <R extends RequestInstance>(
|
|
27
|
+
request: R,
|
|
28
|
+
database: Firestore,
|
|
29
|
+
url: string,
|
|
30
|
+
onSuccess,
|
|
31
|
+
onError,
|
|
32
|
+
resolve,
|
|
33
|
+
events: { onResponseStart; onRequestStart; onRequestEnd; onResponseEnd },
|
|
34
|
+
): ((
|
|
35
|
+
methodName: FirestoreMethodsUnion,
|
|
36
|
+
data: {
|
|
37
|
+
constraints?: PermittedConstraints<FirestorePermittedMethods, FirestoreConstraintsUnion | SharedQueryConstraints>[];
|
|
38
|
+
data?: any;
|
|
39
|
+
options?: Record<string, any>;
|
|
40
|
+
},
|
|
41
|
+
) => Promise<void>) => {
|
|
42
|
+
const [cleanUrl] = url.split("?");
|
|
43
|
+
const methods = {
|
|
44
|
+
getDoc: async () => {
|
|
45
|
+
const path = doc(database, cleanUrl);
|
|
46
|
+
const snapshot = await getDoc(path);
|
|
47
|
+
const result = snapshot.data() || null;
|
|
48
|
+
const status = result ? "success" : "emptyResource";
|
|
49
|
+
return { result, status, extra: { ref: path, snapshot } };
|
|
50
|
+
},
|
|
51
|
+
getDocs: async ({
|
|
52
|
+
constraints = [],
|
|
53
|
+
}: {
|
|
54
|
+
constraints?: PermittedConstraints<
|
|
55
|
+
FirestorePermittedMethods,
|
|
56
|
+
FirestoreConstraintsUnion | SharedQueryConstraints
|
|
57
|
+
>[];
|
|
58
|
+
}) => {
|
|
59
|
+
const queryConstraints = constraints.map((constr) => mapConstraint(constr));
|
|
60
|
+
const path = collection(database, cleanUrl);
|
|
61
|
+
const q = query(path, ...queryConstraints);
|
|
62
|
+
const querySnapshot = await getDocs(q);
|
|
63
|
+
const result = getOrderedResultFirestore(querySnapshot);
|
|
64
|
+
const status = getStatus(result);
|
|
65
|
+
return { result, status, extra: { ref: path, snapshot: querySnapshot } };
|
|
66
|
+
},
|
|
67
|
+
setDoc: async ({ data, options }: { data?: any; options?: any }) => {
|
|
68
|
+
const path = doc(database, cleanUrl);
|
|
69
|
+
const merge = options?.merge === true;
|
|
70
|
+
await setDoc(path, data, { merge });
|
|
71
|
+
return { result: data, status: "success", extra: { ref: path } };
|
|
72
|
+
},
|
|
73
|
+
addDoc: async ({ data }: { data?: any }) => {
|
|
74
|
+
const path = collection(database, cleanUrl);
|
|
75
|
+
const docRef = await addDoc(path, data);
|
|
76
|
+
return { result: data, status: "success", extra: { ref: docRef } };
|
|
77
|
+
},
|
|
78
|
+
updateDoc: async ({ data }: { data?: any }) => {
|
|
79
|
+
const path = doc(database, cleanUrl);
|
|
80
|
+
await updateDoc(path, data);
|
|
81
|
+
return { result: data, status: "success", extra: { ref: path } };
|
|
82
|
+
},
|
|
83
|
+
deleteDoc: async () => {
|
|
84
|
+
const path = doc(database, cleanUrl);
|
|
85
|
+
await deleteDoc(path);
|
|
86
|
+
return { result: null, status: "success", extra: { ref: path } };
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
return async (methodName: FirestoreMethodsUnion, data) => {
|
|
91
|
+
try {
|
|
92
|
+
events.onRequestStart();
|
|
93
|
+
const { result, status, extra } = await methods[methodName](data);
|
|
94
|
+
events.onRequestEnd();
|
|
95
|
+
events.onResponseStart();
|
|
96
|
+
onSuccess(result, status, extra, resolve);
|
|
97
|
+
events.onResponseEnd();
|
|
98
|
+
} catch (e) {
|
|
99
|
+
events.onRequestEnd();
|
|
100
|
+
events.onResponseStart();
|
|
101
|
+
onError(e, "error", {}, resolve);
|
|
102
|
+
events.onResponseEnd();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
};
|