1
1
import { badRequest } from "../../common/error" ;
2
2
import { initializeApp , deleteApp , cert } from "firebase-admin/app" ;
3
3
import { getDatabase , Reference } from "firebase-admin/database" ;
4
+ import {
5
+ CollectionReference ,
6
+ DocumentReference ,
7
+ getFirestore ,
8
+ OrderByDirection ,
9
+ } from "firebase-admin/firestore" ;
4
10
import { DataSourceDataType } from "./dataSourceConfig" ;
5
11
import { ActionDataType } from "./queryConfig" ;
6
12
@@ -25,6 +31,24 @@ export async function runFirebasePlugin(
25
31
return fn ( ref ) ;
26
32
} ;
27
33
34
+ const withFirestoreCollection = < T > ( fn : ( ref : CollectionReference ) => T ) : T => {
35
+ if ( ! ( "collection" in actionData ) ) {
36
+ throw badRequest ( "not a firestore action with collection:" + actionName ) ;
37
+ }
38
+ const ref = getFirestore ( ) . collection ( actionData . collection ) ;
39
+ return fn ( ref ) ;
40
+ } ;
41
+
42
+ const withFirestoreDoc = < T > ( fn : ( ref : DocumentReference ) => T ) : T => {
43
+ if ( ! ( "collection" in actionData ) || ! ( "documentId" in actionData ) ) {
44
+ throw badRequest ( "not a firestore action with collection and documentId:" + actionName ) ;
45
+ }
46
+ const ref = getFirestore ( ) . collection ( actionData . collection ) . doc ( actionData . documentId ) ;
47
+ return fn ( ref ) ;
48
+ } ;
49
+
50
+ const successResult = { success : true } ;
51
+
28
52
try {
29
53
// firebase
30
54
if ( actionName === "RTDB.QueryDatabase" ) {
@@ -33,15 +57,79 @@ export async function runFirebasePlugin(
33
57
}
34
58
35
59
if ( actionName === "RTDB.SetData" ) {
36
- return await witDbRef ( ( ref ) => ref . set ( actionData . data ) ) ;
60
+ await witDbRef ( ( ref ) => ref . set ( actionData . data ) ) ;
61
+ return successResult ;
37
62
}
38
63
39
64
if ( actionName === "RTDB.UpdateData" ) {
40
- return await witDbRef ( ( ref ) => ref . update ( actionData . data ) ) ;
65
+ await witDbRef ( ( ref ) => ref . update ( actionData . data ) ) ;
66
+ return successResult ;
41
67
}
42
68
43
69
if ( actionName === "RTDB.AppendDataToList" ) {
44
- return await witDbRef ( ( ref ) => ref . push ( actionData . data ) ) ;
70
+ await witDbRef ( ( ref ) => ref . push ( actionData . data ) ) ;
71
+ return successResult ;
72
+ }
73
+
74
+ // firebase
75
+ if ( actionName === "FS.GetCollections" ) {
76
+ let collections ;
77
+ if ( actionData . parentDocumentId ) {
78
+ collections = await getFirestore ( ) . doc ( actionData . parentDocumentId ) . listCollections ( ) ;
79
+ } else {
80
+ collections = await getFirestore ( ) . listCollections ( ) ;
81
+ }
82
+ return collections . map ( ( i ) => i . id ) ;
83
+ }
84
+
85
+ if ( actionName === "FS.QueryFireStore" ) {
86
+ const data = await withFirestoreCollection ( async ( ref ) => {
87
+ let query ;
88
+ if ( actionData . orderBy ) {
89
+ query = ref . orderBy (
90
+ actionData . orderBy ,
91
+ ( actionData . orderDirection || "asc" ) as OrderByDirection
92
+ ) ;
93
+ }
94
+ if ( actionData . limit > 0 ) {
95
+ query = ( query || ref ) . limit ( actionData . limit ) ;
96
+ }
97
+ const snapshot = await ( query || ref ) . get ( ) ;
98
+ if ( snapshot . empty ) {
99
+ return [ ] ;
100
+ }
101
+ return snapshot . docs . map ( ( i ) => i . data ( ) ) ;
102
+ } ) ;
103
+ return data ;
104
+ }
105
+
106
+ if ( actionName === "FS.GetDocument" ) {
107
+ return await withFirestoreDoc ( async ( ref ) => ( await ref . get ( ) ) . data ( ) ) ;
108
+ }
109
+
110
+ if ( actionName === "FS.InsertDocument" ) {
111
+ return await withFirestoreCollection ( async ( ref ) => {
112
+ if ( actionData . documentId ) {
113
+ await ref . doc ( actionData . documentId ) . set ( actionData . data ) ;
114
+ } else {
115
+ await ref . add ( actionData . data ) ;
116
+ }
117
+ return successResult ;
118
+ } ) ;
119
+ }
120
+
121
+ if ( actionName === "FS.UpdateDocument" ) {
122
+ return await withFirestoreDoc ( async ( ref ) => {
123
+ await ref . update ( actionData . data ) ;
124
+ return successResult ;
125
+ } ) ;
126
+ }
127
+
128
+ if ( actionName === "FS.DeleteDocument" ) {
129
+ return await withFirestoreDoc ( async ( ref ) => {
130
+ await ref . delete ( ) ;
131
+ return successResult ;
132
+ } ) ;
45
133
}
46
134
} finally {
47
135
deleteApp ( app ) ;
0 commit comments