Skip to content

Commit ec05596

Browse files
committed
Firestore
1 parent e95db7c commit ec05596

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

firestore/main/index.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
const debug = require('debug')('firestore-snippets-node');
22

33
// [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';
56
// [END firestore_deps]
67

78
// We supress these logs when not in NODE_ENV=debug for cleaner Mocha output
89
const console = {log: debug};
910

10-
async function initializeApp() {
11+
async function demoInitializeApp() {
1112
process.env.GCLOUD_PROJECT = 'firestorebeta1test2';
1213
// [START initialize_app]
1314

14-
admin.initializeApp({
15-
credential: admin.credential.applicationDefault()
15+
initializeApp({
16+
credential: applicationDefault()
1617
});
1718

18-
const db = admin.firestore();
19+
const db = getFirestore();
1920
// [END initialize_app]
2021
return db;
2122
}
2223

2324
async function initializeAppFunctions() {
2425
process.env.GCLOUD_PROJECT = 'firestorebeta1test2';
2526
// [START initialize_app_functions]
26-
admin.initializeApp();
27+
initializeApp();
2728

28-
const db = admin.firestore();
29+
const db = getFirestore();
2930

3031
// [END initialize_app_functions]
3132
return db;
@@ -36,11 +37,11 @@ async function initializeAppSA() {
3637

3738
const serviceAccount = require('./path/to/serviceAccountKey.json');
3839

39-
admin.initializeApp({
40-
credential: admin.credential.cert(serviceAccount)
40+
initializeApp({
41+
credential: cert(serviceAccount)
4142
});
4243

43-
const db = admin.firestore();
44+
const db = getFirestore();
4445

4546
// [END initialize_app_service_account]
4647
return db;
@@ -171,7 +172,7 @@ async function dataTypes(db) {
171172
stringExample: 'Hello, World!',
172173
booleanExample: true,
173174
numberExample: 3.14159265,
174-
dateExample: admin.firestore.Timestamp.fromDate(new Date('December 10, 1815')),
175+
dateExample: Timestamp.fromDate(new Date('December 10, 1815')),
175176
arrayExample: [5, true, 'hello'],
176177
nullExample: null,
177178
objectExample: {
@@ -249,11 +250,11 @@ async function updateDocumentArray(db) {
249250

250251
// Atomically add a new region to the "regions" array field.
251252
const unionRes = await washingtonRef.update({
252-
regions: admin.firestore.FieldValue.arrayUnion('greater_virginia')
253+
regions: FieldValue.arrayUnion('greater_virginia')
253254
});
254255
// Atomically remove a region from the "regions" array field.
255256
const removeRes = await washingtonRef.update({
256-
regions: admin.firestore.FieldValue.arrayRemove('east_coast')
257+
regions: FieldValue.arrayRemove('east_coast')
257258
});
258259
// [END firestore_data_set_array_operations]
259260
// [END update_document_array]
@@ -270,7 +271,7 @@ async function updateDocumentIncrement(db) {
270271

271272
// Atomically increment the population of the city by 50.
272273
const res = await washingtonRef.update({
273-
population: admin.firestore.FieldValue.increment(50)
274+
population: FieldValue.increment(50)
274275
});
275276
// [END firestore_data_set_numeric_increment]
276277
// [END update_document_increment]
@@ -314,9 +315,6 @@ async function updateServerTimestamp(db) {
314315

315316
// [START update_with_server_timestamp]
316317
// [START firestore_data_set_server_timestamp]
317-
// Get the `FieldValue` object
318-
const FieldValue = admin.firestore.FieldValue;
319-
320318
// Create a document reference
321319
const docRef = db.collection('objects').doc('some-id');
322320

@@ -334,9 +332,6 @@ async function updateDeleteField(db) {
334332
const admin = require('firebase-admin');
335333
// [START update_delete_field]
336334
// [START firestore_data_delete_field]
337-
// Get the `FieldValue` object
338-
const FieldValue = admin.firestore.FieldValue;
339-
340335
// Create a document reference
341336
const cityRef = db.collection('cities').doc('BJ');
342337

@@ -1058,8 +1053,8 @@ async function deleteQueryBatch(db, query, resolve) {
10581053

10591054
describe('Firestore Smoketests', () => {
10601055

1061-
admin.initializeApp();
1062-
const db = admin.firestore();
1056+
initializeApp();
1057+
const db = getFirestore();
10631058

10641059
it('should get an empty document', () => {
10651060
return getDocumentEmpty(db);

firestore/solution-aggregation/functions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const functions = require('firebase-functions');
2-
const admin = require('firebase-admin');
2+
import { getFirestore } from 'firebase-admin/firestore';
33

4-
const db = admin.firestore();
4+
const db = getFirestore();
55
// [START_EXCLUDE]
66
const settings = {timestampsInSnapshots: true};
77
db.settings(settings);

firestore/solution-deletes/functions/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
const admin = require('firebase-admin');
1+
import { initializeApp } from 'firebase-admin/app';
2+
import { getAuth } from 'firebase-admin/auth';
3+
24
const firebase_tools = require('firebase-tools');
35
const functions = require('firebase-functions');
46

5-
admin.initializeApp();
7+
initializeApp();
68

79

810
/**
@@ -17,8 +19,7 @@ admin.initializeApp();
1719
exports.mintAdminToken = functions.https.onCall(async (data, context) => {
1820
const uid = data.uid;
1921

20-
const token = await admin
21-
.auth()
22+
const token = await getAuth()
2223
.createCustomToken(uid, { admin: true });
2324

2425
return { token };

firestore/solution-sharded-timestamp/nonShardedTimestamps.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import { initializeApp } from 'firebase-admin/app';
2+
import { getFirestore, Timestamp } from 'firebase-admin/firestore';
13
const util = require('util');
2-
const admin = require('firebase-admin');
3-
admin.initializeApp();
4+
5+
initializeApp();
46

57
// Create a new client
6-
const fs = admin.firestore();
8+
const fs = getFirestore();
79

810
// [START fs_sharded_timestamps_pre_insert]
911
async function insertData() {
@@ -16,7 +18,7 @@ async function insertData() {
1618
},
1719
exchange: 'EXCHG1',
1820
instrumentType: 'commonstock',
19-
timestamp: admin.firestore.Timestamp.fromMillis(
21+
timestamp: Timestamp.fromMillis(
2022
Date.parse('2019-01-01T13:45:23.010Z'))
2123
},
2224
{
@@ -27,7 +29,7 @@ async function insertData() {
2729
},
2830
exchange: 'EXCHG2',
2931
instrumentType: 'commonstock',
30-
timestamp: admin.firestore.Timestamp.fromMillis(
32+
timestamp: Timestamp.fromMillis(
3133
Date.parse('2019-01-01T13:45:23.101Z'))
3234
},
3335
{
@@ -38,7 +40,7 @@ async function insertData() {
3840
},
3941
exchange: 'EXCHG1',
4042
instrumentType: 'etf',
41-
timestamp: admin.firestore.Timestamp.fromMillis(
43+
timestamp: Timestamp.fromMillis(
4244
Date.parse('2019-01-01T13:45:23.001Z'))
4345
}
4446
];

firestore/solution-sharded-timestamp/shardedTimestamps.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const util = require('util');
2-
const admin = require('firebase-admin');
3-
admin.initializeApp();
2+
import { initializeApp } from 'firebase-admin/app';
3+
import { getFirestore, Timestamp } from 'firebase-admin/firestore';
4+
5+
initializeApp();
46

57
// Create a new client
6-
const fs = admin.firestore();
8+
const fs = getFirestore();
79
const MAX_IN_VALUES = 10;
810

911
// [START fs_sharded_timestamps_define_shards]
@@ -44,7 +46,7 @@ async function insertData() {
4446
},
4547
exchange: 'EXCHG1',
4648
instrumentType: 'commonstock',
47-
timestamp: admin.firestore.Timestamp.fromMillis(
49+
timestamp: Timestamp.fromMillis(
4850
Date.parse('2019-01-01T13:45:23.010Z'))
4951
},
5052
{
@@ -56,7 +58,7 @@ async function insertData() {
5658
},
5759
exchange: 'EXCHG2',
5860
instrumentType: 'commonstock',
59-
timestamp: admin.firestore.Timestamp.fromMillis(
61+
timestamp: Timestamp.fromMillis(
6062
Date.parse('2019-01-01T13:45:23.101Z'))
6163
},
6264
{
@@ -68,7 +70,7 @@ async function insertData() {
6870
},
6971
exchange: 'EXCHG1',
7072
instrumentType: 'etf',
71-
timestamp: admin.firestore.Timestamp.fromMillis(
73+
timestamp: Timestamp.fromMillis(
7274
Date.parse('2019-01-01T13:45:23.001Z'))
7375
}
7476
];

0 commit comments

Comments
 (0)