Nâng cấp lên SDK quản trị của Node.js SDK phiên bản 10 (SDK mô-đun)

Phiên bản 10 của SDK Node.js dành cho quản trị viên có 2 thay đổi quan trọng:

  • Ngừng hỗ trợ Node.js 10 (đây là một thay đổi mang tính đột phá)
  • SDK đã áp dụng mẫu API mô-đun

Hướng dẫn này cung cấp thông tin và hướng dẫn để giúp nhà phát triển nâng cấp các ứng dụng Node.js hiện có từ các phiên bản trước của Admin SDK lên phiên bản 10.

Cập nhật Node.js lên phiên bản 12 trở lên

Với bản phát hành SDK Node.js dành cho quản trị viên phiên bản 10, Firebase đã ngừng hỗ trợ Node.js 10. Nhà phát triển phải sử dụng Node.js 12 trở lên khi dùng Admin SDK. Nếu bạn đang sử dụng SDK Node.js dành cho quản trị viên cùng với Cloud Functions for Firebase, hãy đảm bảo rằng bạn đã nâng cấp phiên bản Node.js lên 12 trở lên.

Sử dụng mô-đun thay vì không gian tên

Kể từ khi ra đời, SDK Node.js dành cho quản trị viên đã cung cấp một API ổn định được cấu trúc dưới dạng hệ phân cấp không gian tên lồng nhau. Do đó, bạn có thể đã quen với việc viết mã như sau:

// 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');

Kể từ phiên bản 10, SDK Node.js dành cho quản trị viên cung cấp nhiều điểm truy cập mô-đun có các thành phần xuất được đặt tên. Nhà phát triển nên sử dụng các điểm truy cập mới này để truy cập vào nhiều API của SDK, thay vì sử dụng không gian tên admin toàn cục.

Đây là giao diện của ví dụ trên với các điểm truy cập mô-đun mới:

TypeScript

// 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');

Sử dụng các điểm truy cập mô-đun phiên bản 10

Xin lưu ý rằng trong các ví dụ ở trên, bạn không còn nhập không gian tên admin toàn cục nữa. Thay vào đó, bạn chỉ nhập rõ ràng các ký hiệu cần thiết từ một số điểm truy cập mô-đun. Ngoài ra, nhà phát triển TypeScript không còn phải sử dụng các mã nhận dạng loại lồng nhau ba lần như admin.auth.UserRecordadmin.database.Reference. Vì mỗi loại thuộc về đúng một mô-đun, nên bạn chỉ cần nhập các loại đó theo tên ngắn như UserRecordReference.

Dưới đây là tất cả các điểm truy cập mô-đun có trong SDK kể từ phiên bản 10:

  • firebase-admin/app
  • firebase-admin/auth
  • firebase-admin/database
  • firebase-admin/firestore
  • firebase-admin/instance-id
  • firebase-admin/machine-learning
  • firebase-admin/messaging
  • firebase-admin/project-management
  • firebase-admin/remote-config
  • firebase-admin/security-rules
  • firebase-admin/storage

Bảng sau đây cho thấy cú pháp nhập thay thế cho từng hàm không gian tên cũ:

Phiên bản 9 Phiên bản 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();

Sử dụng các hàm đã xuất thay vì các phương thức trên Ứng dụng

Trong API cũ, đối tượng App đã hiển thị một số phương thức như app.auth()app.database(). Nhà phát triển nên tránh sử dụng các phương thức này và thay vào đó, hãy sử dụng các điểm truy cập mô-đun tương tự như mô tả ở trên để lấy các thực thể dịch vụ được phạm vi đến một đối tượng App nhất định và thực hiện các tác vụ khác dành riêng cho ứng dụng.

Phiên bản 9 Phiên bản 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);

Hỗ trợ mô-đun ES

Node.js 12 trở lên đi kèm với tính năng hỗ trợ thử nghiệm cho các mô-đun ES, cho phép ngay cả những nhà phát triển không phải TypeScript cũng có thể sử dụng các từ khoá exportimport trong mã của họ. Kể từ bản phát hành phiên bản 10, SDK Node.js dành cho quản trị viên cũng hỗ trợ các mô-đun ES, để nhà phát triển triển khai các mô-đun ES trên Node.js thuần tuý có thể nhập SDK bằng cú pháp import.

Để sử dụng các mô-đun ES với Admin SDK, trước tiên, hãy đảm bảo rằng bạn đã bật tính năng hỗ trợ ESM cho thời gian chạy Node.js. Bạn thường thực hiện việc này bằng cách thêm trường "type": "module" vào tệp package.json. Sau đó, bạn có thể viết mã xử lý ứng dụng như sau:

// 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');