11import { badRequest } from "../../common/error" ;
22import { initializeApp , deleteApp , cert } from "firebase-admin/app" ;
33import { getDatabase , Reference } from "firebase-admin/database" ;
4+ import {
5+ CollectionReference ,
6+ DocumentReference ,
7+ getFirestore ,
8+ OrderByDirection ,
9+ } from "firebase-admin/firestore" ;
410import { DataSourceDataType } from "./dataSourceConfig" ;
511import { ActionDataType } from "./queryConfig" ;
612
@@ -25,6 +31,24 @@ export async function runFirebasePlugin(
2531 return fn ( ref ) ;
2632 } ;
2733
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+
2852 try {
2953 // firebase
3054 if ( actionName === "RTDB.QueryDatabase" ) {
@@ -33,15 +57,79 @@ export async function runFirebasePlugin(
3357 }
3458
3559 if ( actionName === "RTDB.SetData" ) {
36- return await witDbRef ( ( ref ) => ref . set ( actionData . data ) ) ;
60+ await witDbRef ( ( ref ) => ref . set ( actionData . data ) ) ;
61+ return successResult ;
3762 }
3863
3964 if ( actionName === "RTDB.UpdateData" ) {
40- return await witDbRef ( ( ref ) => ref . update ( actionData . data ) ) ;
65+ await witDbRef ( ( ref ) => ref . update ( actionData . data ) ) ;
66+ return successResult ;
4167 }
4268
4369 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+ } ) ;
45133 }
46134 } finally {
47135 deleteApp ( app ) ;
0 commit comments