1717 */
1818import { FirestoreBigQueryEventHistoryTracker } from "@firebaseextensions/firestore-bigquery-change-tracker" ;
1919import * as firebase from "firebase-admin" ;
20+ import { getFirestore } from "firebase-admin/firestore" ;
2021import * as fs from "fs" ;
2122import * as util from "util" ;
2223
@@ -50,6 +51,7 @@ const run = async (): Promise<number> => {
5051 useEmulator,
5152 cursorPositionFile,
5253 transformFunctionUrl,
54+ firestoreInstanceId,
5355 } = config ;
5456 if ( useEmulator ) {
5557 console . log ( "Using emulator" ) ;
@@ -63,14 +65,23 @@ const run = async (): Promise<number> => {
6365 // Initialize Firebase
6466 // This uses applicationDefault to authenticate
6567 // Please see https://cloud.google.com/docs/authentication/production
68+ let app : firebase . app . App ;
6669 if ( ! firebase . apps . length ) {
67- firebase . initializeApp ( {
70+ app = firebase . initializeApp ( {
6871 projectId : projectId ,
6972 credential : firebase . credential . applicationDefault ( ) ,
7073 databaseURL : `https://${ projectId } .firebaseio.com` ,
7174 } ) ;
75+ } else {
76+ app = firebase . app ( ) ;
7277 }
7378
79+ // Get the Firestore instance for the specified database
80+ const db =
81+ firestoreInstanceId && firestoreInstanceId !== "(default)"
82+ ? getFirestore ( app , firestoreInstanceId )
83+ : getFirestore ( app ) ;
84+
7485 // We pass in the application-level "tableId" here. The tracker determines
7586 // the name of the raw changelog from this field.
7687 // TODO: fix this type, it should include clustering apparently
@@ -83,6 +94,7 @@ const run = async (): Promise<number> => {
8394 useNewSnapshotQuerySyntax,
8495 bqProjectId : bigQueryProjectId ,
8596 transformFunction : transformFunctionUrl ,
97+ firestoreInstanceId : firestoreInstanceId ,
8698 } ) ;
8799
88100 await initializeDataSink ( dataSink , config ) ;
@@ -103,10 +115,10 @@ const run = async (): Promise<number> => {
103115
104116 if ( await exists ( cursorPositionFile ) ) {
105117 let cursorDocumentId = ( await read ( cursorPositionFile ) ) . toString ( ) ;
106- cursor = await firebase . firestore ( ) . doc ( cursorDocumentId ) . get ( ) ;
118+ cursor = await db . doc ( cursorDocumentId ) . get ( ) ;
107119 logs . resumingImport ( config , cursorDocumentId ) ;
108120 }
109- const totalRowsImported = await runSingleThread ( dataSink , config , cursor ) ;
121+ const totalRowsImported = await runSingleThread ( dataSink , config , cursor , db ) ;
110122 try {
111123 await unlink ( cursorPositionFile ) ;
112124 } catch ( e ) {
0 commit comments