Skip to content

Commit 17620db

Browse files
authored
Multiproject snippets (#228)
1 parent 3b929e7 commit 17620db

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

firebaseapp-next/firebaseapp.js

+58
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,61 @@ function multpleFirebaseApps() {
2525
// getDatabase(secondaryApp)
2626
// [END firebase_secondary]
2727
}
28+
29+
function defaultInitOptions() {
30+
const firebaseConfig = {
31+
// ...
32+
};
33+
34+
// [START app_default_init_options]
35+
const { initializeApp } = require("firebase/app");
36+
const { getStorage } = require("firebase/storage");
37+
const { getFirestore } = require("firebase/firestore");
38+
39+
// Initialize Firebase with a "default" Firebase project
40+
const defaultProject = initializeApp(firebaseConfig);
41+
42+
console.log(defaultProject.name); // "[DEFAULT]"
43+
44+
// Option 1: Access Firebase services via the defaultProject variable
45+
let defaultStorage = getStorage(defaultProject);
46+
let defaultFirestore = getFirestore(defaultProject);
47+
48+
// Option 2: Access Firebase services using shorthand notation
49+
defaultStorage = getStorage();
50+
defaultFirestore = getFirestore();
51+
// [END app_default_init_options]
52+
}
53+
54+
function multiProjectInitOptions() {
55+
const firebaseConfig = {
56+
// ...
57+
};
58+
59+
const otherProjectFirebaseConfig = {
60+
// ...
61+
};
62+
63+
// [START app_multi_project_init_options]
64+
const { initializeApp, getApp } = require("firebase/app");
65+
const { getStorage } = require("firebase/storage");
66+
const { getFirestore } = require("firebase/firestore");
67+
68+
// Initialize Firebase with a default Firebase project
69+
initializeApp(firebaseConfig);
70+
71+
// Initialize Firebase with a second Firebase project
72+
const otherProject = initializeApp(otherProjectFirebaseConfig, "other");
73+
74+
console.log(getApp().name); // "[DEFAULT]"
75+
console.log(otherProject.name); // "otherProject"
76+
77+
// Use the shorthand notation to access the default project's Firebase services
78+
const defaultStorage = getStorage();
79+
const defaultFirestore = getFirestore();
80+
81+
// Use the otherProject variable to access the second project's Firebase services
82+
const otherStorage = getStorage(otherProject);
83+
const otherFirestore = getFirestore(otherProject);
84+
// [END app_multi_project_init_options]
85+
}

firebaseapp/firebaseapp.js

+52-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function multpleFirebaseApps() {
66
// - Project ID
77
// - App ID
88
// - API Key
9-
var secondaryAppConfig = {
9+
const secondaryAppConfig = {
1010
projectId: "<PROJECT_ID>",
1111
appId: "<APP_ID>",
1212
apiKey: "<API_KEY>",
@@ -17,8 +17,58 @@ function multpleFirebaseApps() {
1717

1818
// [START firebase_secondary]
1919
// Initialize another app with a different config
20-
var secondaryApp = firebase.initializeApp(secondaryAppConfig, "secondary");
20+
const secondaryApp = firebase.initializeApp(secondaryAppConfig, "secondary");
2121
// Access services, such as the Realtime Database
2222
// secondaryApp.database();
2323
// [END firebase_secondary]
2424
}
25+
26+
function defaultInitOptions() {
27+
const firebaseConfig = {
28+
// ...
29+
};
30+
31+
// [START app_default_init_options]
32+
// Initialize Firebase with a "default" Firebase project
33+
const defaultProject = firebase.initializeApp(firebaseConfig);
34+
35+
console.log(defaultProject.name); // "[DEFAULT]"
36+
37+
// Option 1: Access Firebase services via the defaultProject variable
38+
let defaultStorage = defaultProject.storage();
39+
let defaultFirestore = defaultProject.firestore();
40+
41+
// Option 2: Access Firebase services using shorthand notation
42+
defaultStorage = firebase.storage();
43+
defaultFirestore = firebase.firestore();
44+
// [END app_default_init_options]
45+
}
46+
47+
function multiProjectInitOptions() {
48+
const firebaseConfig = {
49+
// ...
50+
};
51+
52+
const otherProjectFirebaseConfig = {
53+
// ...
54+
};
55+
56+
// [START app_multi_project_init_options]
57+
// Initialize Firebase with a default Firebase project
58+
firebase.initializeApp(firebaseConfig);
59+
60+
// Initialize Firebase with a second Firebase project
61+
const otherProject = firebase.initializeApp(otherProjectFirebaseConfig, "other");
62+
63+
console.log(firebase.app().name); // "[DEFAULT]"
64+
console.log(otherProject.name); // "otherProject"
65+
66+
// Use the shorthand notation to access the default project's Firebase services
67+
const defaultStorage = firebase.storage();
68+
const defaultFirestore = firebase.firestore();
69+
70+
// Use the otherProject variable to access the second project's Firebase services
71+
const otherStorage = otherProject.storage();
72+
const otherFirestore = otherProject.firestore();
73+
// [END app_multi_project_init_options]
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firebaseapp-next/firebaseapp.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START app_default_init_options_modular]
8+
import { initializeApp } from "firebase/app";
9+
import { getStorage } from "firebase/storage";
10+
import { getFirestore } from "firebase/firestore";
11+
12+
// Initialize Firebase with a "default" Firebase project
13+
const defaultProject = initializeApp(firebaseConfig);
14+
15+
console.log(defaultProject.name); // "[DEFAULT]"
16+
17+
// Option 1: Access Firebase services via the defaultProject variable
18+
let defaultStorage = getStorage(defaultProject);
19+
let defaultFirestore = getFirestore(defaultProject);
20+
21+
// Option 2: Access Firebase services using shorthand notation
22+
defaultStorage = getStorage();
23+
defaultFirestore = getFirestore();
24+
// [END app_default_init_options_modular]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firebaseapp-next/firebaseapp.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START app_multi_project_init_options_modular]
8+
import { initializeApp, getApp } from "firebase/app";
9+
import { getStorage } from "firebase/storage";
10+
import { getFirestore } from "firebase/firestore";
11+
12+
// Initialize Firebase with a default Firebase project
13+
initializeApp(firebaseConfig);
14+
15+
// Initialize Firebase with a second Firebase project
16+
const otherProject = initializeApp(otherProjectFirebaseConfig, "other");
17+
18+
console.log(getApp().name); // "[DEFAULT]"
19+
console.log(otherProject.name); // "otherProject"
20+
21+
// Use the shorthand notation to access the default project's Firebase services
22+
const defaultStorage = getStorage();
23+
const defaultFirestore = getFirestore();
24+
25+
// Use the otherProject variable to access the second project's Firebase services
26+
const otherStorage = getStorage(otherProject);
27+
const otherFirestore = getFirestore(otherProject);
28+
// [END app_multi_project_init_options_modular]

0 commit comments

Comments
 (0)