Uaktualnij do Node.js SDK Admin SDK v10 (modułowy SDK)

Wersja 10 pakietu Admin Node.js SDK wprowadza dwie ważne zmiany:

  • Zakończono wsparcie dla Node.js 10 (jest to przełomowa zmiana )
  • Zestaw SDK przyjął modułowy wzorzec API

Ten przewodnik zawiera instrukcje i informacje, które pomogą programistom uaktualnić istniejące aplikacje Node.js z wcześniejszych wersji pakietu Admin SDK do wersji 10.

Zaktualizuj Node.js do wersji 12 lub nowszej

Wraz z wersją Admin Node.js SDK v10, Firebase zaprzestała obsługi Node.js 10. Programiści muszą używać Node.js 12 lub nowszego, aby korzystać z Admin SDK. Jeśli używasz pakietu Admin Node.js SDK razem z Cloud Functions dla Firebase, upewnij się, że zaktualizowałeś wersję Node.js do wersji 12 lub nowszej.

Używaj modułów zamiast przestrzeni nazw

Od samego początku pakiet Admin Node.js SDK oferuje stabilny interfejs API o strukturze zagnieżdżonej hierarchii przestrzeni nazw. W rezultacie mogłeś zaznajomić się z pisaniem kodu wyglądającego tak:

// Import the global admin namespace
import * as admin from 'firebase-admin';

const app: admin.app.App = admin.initializeApp();

const token: string = await admin.auth().createCustomToken('alice');

const user: admin.auth.UserRecord = await admin.auth().getUser('bob');

Począwszy od wersji 10, pakiet Admin Node.js SDK oferuje wiele punktów wejścia modułów z nazwanymi eksportami. Zalecamy programistom korzystanie z tych nowych punktów wejścia w celu uzyskania dostępu do różnych interfejsów API zestawu SDK zamiast korzystania z przestrzeni nazw admin globalnego.

Oto jak wyglądałby powyższy przykład z nowymi punktami wejścia modułu:

Maszynopis

// Import only what you need
import { initializeApp, App } from 'firebase-admin/app';
import { getAuth, UserRecord } from 'firebase-admin/auth';

const app: App = initializeApp();

const token: string = await getAuth().createCustomToken('alice');

const user: UserRecord = getAuth().getUser('bob');

Node.js

// Import only what you need
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');

const app = initializeApp();

const token = await getAuth().createCustomToken('alice');

const user = getAuth().getUser('bob');

Korzystanie z modułowych punktów wejścia v10

Pamiętaj, że w powyższych przykładach nie importujesz już przestrzeni nazw admin globalnego. Zamiast tego jawnie importujesz tylko potrzebne symbole z kilku punktów wejścia modułu. Ponadto programiści TypeScriptu nie muszą już używać potrójnie zagnieżdżonych identyfikatorów typów, takich jak admin.auth.UserRecord i admin.database.Reference . Ponieważ każdy typ należy do dokładnie jednego modułu, możesz po prostu zaimportować je według krótkich nazw, takich jak UserRecord i Reference .

Oto wszystkie punkty wejścia modułów dostępne w zestawie SDK od wersji 10:

  • administrator bazy ogniowej/aplikacja
  • administrator bazy ogniowej/auth
  • administrator bazy ogniowej/baza danych
  • administrator bazy ogniowej/firestore
  • administrator bazy ogniowej/id-instancji
  • administrator bazy ogniowej/uczenie maszynowe
  • administrator bazy ogniowej/wiadomości
  • administrator bazy ogniowej/zarządzanie projektami
  • Firebase-admin/remote-config
  • administrator-firebase/reguły-bezpieczeństwa
  • administrator bazy ogniowej/storage

Poniższa tabela przedstawia zastępczą składnię importu dla każdej ze starszych funkcji przestrzeni nazw:

v9 wersja 10
admin.initializeApp() import { initializeApp } from 'firebase-admin/app'

initializeApp();

admin.app() import { getApp } from 'firebase-admin/ap'

getApp();

admin.credential.cert() import { cert } from 'firebase-admin/app'

cert();

admin.auth() import { getAuth } from 'firebase-admin/auth'

getAuth();

admin.database() import { getDatabase } from 'firebase-admin/database'

getDatabase();

admin.firestore() import { getFirestore } from 'firebase-admin/firestore'

getFirestore();

admin.instanceId() import { getInstanceId } from 'firebase-admin/instance-id'

getInstanceId();

admin.machineLearning() import { getMachineLearning } from 'firebase-admin/machine-learning'

getMachineLearning();

admin.messaging() import { getMessaging } from 'firebase-admin/messaging'

getMessaging()

admin.projectManagement() import { getProjectManagement } from 'firebase-admin/project-management'

getProjectManagement();

admin.remoteConfig() import { getRemoteConfig } from 'firebase-admin/remote-config'

getRemoteConfig();

admin.securityRules() import { getSecurityRules } from 'firebase-admin/security-rules'

getSecurityRules()

admin.storage() import { getStorage } from 'firebase-admin/storage'

getStorage();

Użyj wyeksportowanych funkcji zamiast metod w aplikacji

W starszym interfejsie API obiekt App udostępniał wiele metod, takich jak app.auth() i app.database() . Zalecamy programistom unikanie korzystania z tych metod i zamiast tego używanie tych samych punktów wejścia modułu opisanych powyżej w celu uzyskania wystąpień usług w zakresie danego obiektu App i wykonywania innych zadań specyficznych dla aplikacji.

v9 wersja 10
app.auth() import { getAuth } from 'firebase-admin/auth';

getAuth(app);

app.database() import { getDatabase } from 'firebase-admin/database';

getDatabase(app);

app.database(url) import { getDatabaseWithUrl } from 'firebase-admin/database';

getDatabaseWithUrl(url, app);

app.firestore() import { getFirestore } from 'firebase-admin/firestore'

getFirestore(app);

app.instanceId() import { getInstanceId } from 'firebase-admin/instance-id'

getInstanceId(app);

app.machineLearning() import { getMachineLearning } from 'firebase-admin/machine-learning'

getMachineLearning(app);

app.messaging() import { getMessaging } from 'firebase-admin/messaging'

getMessaging(app);

app.projectManagement() import { getProjectManagement } from 'firebase-admin/project-management'

getProjectManagement(app);

app.remoteConfig() import { getRemoteConfig } from 'firebase-admin/remote-config'

getRemoteConfig(app);

app.securityRules() import { getSecurityRules } from 'firebase-admin/security-rules'

getSecurityRules(app);

app.storage() import { getStorage } from 'firebase-admin/storage'

getStorage(app);

app.delete() import { deleteApp } from 'firebase-admin/app';

deleteApp(app);

Obsługa modułów ES

Node.js 12 i nowsze wersje oferują eksperymentalną obsługę modułów ES, umożliwiając nawet programistom nieznającym TypeScriptu używanie w swoim kodzie słów kluczowych export i import . Począwszy od wersji 10, pakiet Admin Node.js SDK zapewnia także obsługę modułów ES, dzięki czemu programiści wdrażający moduły ES w zwykłym Node.js mogą importować zestaw SDK przy użyciu składni import .

Aby używać modułów ES z pakietem Admin SDK, najpierw upewnij się, że w środowisku wykonawczym Node.js włączono obsługę ESM. Zwykle robi się to poprzez dodanie pola "type": "module" do pliku package.json . Następnie możesz napisać kod aplikacji, który wygląda następująco:

// With {type: module} in the package.json...

// Import only what you need
import { initializeApp }  from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';

const app = initializeApp();

const token = await getAuth().createCustomToken('alice');

const user = getAuth().getUser('bob');