1
1
const debug = require ( 'debug' ) ( 'firestore-snippets-node' ) ;
2
2
3
3
// [START firestore_deps]
4
- const admin = require ( 'firebase-admin' ) ;
4
+ import { initializeApp , applicationDefault , cert } from 'firebase-admin/app' ;
5
+ import { getFirestore , Timestamp , FieldValue } from 'firebase-admin/firestore' ;
5
6
// [END firestore_deps]
6
7
7
8
// We supress these logs when not in NODE_ENV=debug for cleaner Mocha output
8
9
const console = { log : debug } ;
9
10
10
- async function initializeApp ( ) {
11
+ async function demoInitializeApp ( ) {
11
12
process . env . GCLOUD_PROJECT = 'firestorebeta1test2' ;
12
13
// [START initialize_app]
13
14
14
- admin . initializeApp ( {
15
- credential : admin . credential . applicationDefault ( )
15
+ initializeApp ( {
16
+ credential : applicationDefault ( )
16
17
} ) ;
17
18
18
- const db = admin . firestore ( ) ;
19
+ const db = getFirestore ( ) ;
19
20
// [END initialize_app]
20
21
return db ;
21
22
}
22
23
23
24
async function initializeAppFunctions ( ) {
24
25
process . env . GCLOUD_PROJECT = 'firestorebeta1test2' ;
25
26
// [START initialize_app_functions]
26
- admin . initializeApp ( ) ;
27
+ initializeApp ( ) ;
27
28
28
- const db = admin . firestore ( ) ;
29
+ const db = getFirestore ( ) ;
29
30
30
31
// [END initialize_app_functions]
31
32
return db ;
@@ -36,11 +37,11 @@ async function initializeAppSA() {
36
37
37
38
const serviceAccount = require ( './path/to/serviceAccountKey.json' ) ;
38
39
39
- admin . initializeApp ( {
40
- credential : admin . credential . cert ( serviceAccount )
40
+ initializeApp ( {
41
+ credential : cert ( serviceAccount )
41
42
} ) ;
42
43
43
- const db = admin . firestore ( ) ;
44
+ const db = getFirestore ( ) ;
44
45
45
46
// [END initialize_app_service_account]
46
47
return db ;
@@ -171,7 +172,7 @@ async function dataTypes(db) {
171
172
stringExample : 'Hello, World!' ,
172
173
booleanExample : true ,
173
174
numberExample : 3.14159265 ,
174
- dateExample : admin . firestore . Timestamp . fromDate ( new Date ( 'December 10, 1815' ) ) ,
175
+ dateExample : Timestamp . fromDate ( new Date ( 'December 10, 1815' ) ) ,
175
176
arrayExample : [ 5 , true , 'hello' ] ,
176
177
nullExample : null ,
177
178
objectExample : {
@@ -249,11 +250,11 @@ async function updateDocumentArray(db) {
249
250
250
251
// Atomically add a new region to the "regions" array field.
251
252
const unionRes = await washingtonRef . update ( {
252
- regions : admin . firestore . FieldValue . arrayUnion ( 'greater_virginia' )
253
+ regions : FieldValue . arrayUnion ( 'greater_virginia' )
253
254
} ) ;
254
255
// Atomically remove a region from the "regions" array field.
255
256
const removeRes = await washingtonRef . update ( {
256
- regions : admin . firestore . FieldValue . arrayRemove ( 'east_coast' )
257
+ regions : FieldValue . arrayRemove ( 'east_coast' )
257
258
} ) ;
258
259
// [END firestore_data_set_array_operations]
259
260
// [END update_document_array]
@@ -270,7 +271,7 @@ async function updateDocumentIncrement(db) {
270
271
271
272
// Atomically increment the population of the city by 50.
272
273
const res = await washingtonRef . update ( {
273
- population : admin . firestore . FieldValue . increment ( 50 )
274
+ population : FieldValue . increment ( 50 )
274
275
} ) ;
275
276
// [END firestore_data_set_numeric_increment]
276
277
// [END update_document_increment]
@@ -314,9 +315,6 @@ async function updateServerTimestamp(db) {
314
315
315
316
// [START update_with_server_timestamp]
316
317
// [START firestore_data_set_server_timestamp]
317
- // Get the `FieldValue` object
318
- const FieldValue = admin . firestore . FieldValue ;
319
-
320
318
// Create a document reference
321
319
const docRef = db . collection ( 'objects' ) . doc ( 'some-id' ) ;
322
320
@@ -334,9 +332,6 @@ async function updateDeleteField(db) {
334
332
const admin = require ( 'firebase-admin' ) ;
335
333
// [START update_delete_field]
336
334
// [START firestore_data_delete_field]
337
- // Get the `FieldValue` object
338
- const FieldValue = admin . firestore . FieldValue ;
339
-
340
335
// Create a document reference
341
336
const cityRef = db . collection ( 'cities' ) . doc ( 'BJ' ) ;
342
337
@@ -1058,8 +1053,8 @@ async function deleteQueryBatch(db, query, resolve) {
1058
1053
1059
1054
describe ( 'Firestore Smoketests' , ( ) => {
1060
1055
1061
- admin . initializeApp ( ) ;
1062
- const db = admin . firestore ( ) ;
1056
+ initializeApp ( ) ;
1057
+ const db = getFirestore ( ) ;
1063
1058
1064
1059
it ( 'should get an empty document' , ( ) => {
1065
1060
return getDocumentEmpty ( db ) ;
0 commit comments