Skip to content

Commit 5f10306

Browse files
authored
Lazy loading the Firestore implementation (#110)
* Lazy loading the Firestore implementation * Integration tests for module lazy loading * Extracted variable in test
1 parent c28ac52 commit 5f10306

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/firestore/firestore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,8 @@ function initFirestore(app: FirebaseApp): Firestore {
111111
'to use Cloud Firestore API.',
112112
});
113113
}
114-
return new Firestore(options);
114+
115+
// Lazy-load the Firestore implementation here, which in turns loads gRPC.
116+
let firestoreDatabase = require('@google-cloud/firestore');
117+
return new firestoreDatabase(options);
115118
}

test/integration/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,34 @@ utils.assert(
9797
'admin.messaging(app).app',
9898
'App instances do not match.'
9999
);
100+
utils.assert(
101+
_.isEqual(admin.storage(nonNullApp).app, nonNullApp),
102+
'admin.storage(app).app',
103+
'App instances do not match.'
104+
);
105+
106+
// Firestore should not be loaded yet.
107+
var gcloud = require.cache[require.resolve('@google-cloud/firestore')];
108+
utils.assert(
109+
typeof gcloud === 'undefined',
110+
'require(firebase-admin)',
111+
'Firestore module already loaded'
112+
);
113+
114+
// Calling admin.firestore should load Firestore
115+
const firestoreNamespace = admin.firestore;
116+
utils.assert(
117+
typeof firestoreNamespace !== 'undefined',
118+
'admin.firestore',
119+
'Firestore namespace could not be loaded.'
120+
);
121+
122+
gcloud = require.cache[require.resolve('@google-cloud/firestore')];
123+
utils.assert(
124+
typeof gcloud !== 'undefined',
125+
'admin.firestore',
126+
'Firestore module not loaded'
127+
);
100128

101129
/**
102130
* Prompts the developer whether the Database rules should be

0 commit comments

Comments
 (0)