Admin Node.js SDK เวอร์ชัน 10 นำเสนอการเปลี่ยนแปลงที่สำคัญสองประการ:
- การสนับสนุน Node.js 10 ถูกยกเลิก (นี่คือ การเปลี่ยนแปลงครั้งใหญ่ )
- SDK ได้นำรูปแบบ API แบบโมดูลาร์มาใช้
คู่มือนี้จะให้คำแนะนำและข้อมูลเพื่อช่วยนักพัฒนาอัปเกรดแอป Node.js ที่มีอยู่จาก Admin SDK เวอร์ชันก่อนหน้าเป็นเวอร์ชัน 10
อัปเดต Node.js เป็นเวอร์ชัน 12 หรือสูงกว่า
ในการเปิดตัว Admin Node.js SDK v10 นั้น Firebase ได้ยุติการสนับสนุน Node.js 10 แล้ว นักพัฒนาต้องใช้ Node.js 12 หรือสูงกว่าเมื่อใช้ Admin SDK หากคุณใช้ Admin Node.js SDK ร่วมกับ Cloud Functions สำหรับ Firebase ตรวจสอบให้แน่ใจว่าคุณได้ อัปเกรด Node.js เวอร์ชัน เป็น 12 หรือสูงกว่าแล้ว
ใช้โมดูลแทนเนมสเปซ
นับตั้งแต่ก่อตั้ง Admin Node.js SDK ได้นำเสนอ API ที่เสถียรซึ่งมีโครงสร้างเป็นลำดับชั้นเนมสเปซที่ซ้อนกัน ด้วยเหตุนี้ คุณอาจคุ้นเคยกับการเขียนโค้ดที่มีลักษณะดังนี้:
// 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');
ตั้งแต่เวอร์ชัน 10 เป็นต้นไป Admin Node.js SDK เสนอจุดเริ่มต้นโมดูลหลายจุดพร้อมการส่งออกที่มีชื่อ เราขอแนะนำให้นักพัฒนาใช้จุดเริ่มต้นใหม่เหล่านี้เพื่อเข้าถึง API ต่างๆ ของ SDK แทนที่จะใช้เนมสเปซ admin
ระบบส่วนกลาง
ตัวอย่างข้างต้นจะมีลักษณะเช่นนี้กับจุดเข้าโมดูลใหม่:
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');
โหนด 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');
การใช้จุดเข้าโมดูลาร์ v10
โปรดทราบว่าในตัวอย่างข้างต้น คุณจะไม่ได้นำเข้าเนมสเปซของ admin
ระบบส่วนกลางอีกต่อไป แต่คุณนำเข้าเฉพาะสัญลักษณ์ที่คุณต้องการจากจุดเข้าใช้งานโมดูลต่างๆ อย่างชัดเจนแทน นอกจากนี้ นักพัฒนา TypeScript ไม่ต้องใช้ตัวระบุประเภทที่ซ้อนกันสามชั้นอีกต่อไป เช่น admin.auth.UserRecord
และ admin.database.Reference
เนื่องจากแต่ละประเภทเป็นของโมดูลเดียว คุณจึงสามารถนำเข้าโดยใช้ชื่อย่อ เช่น UserRecord
และ Reference
นี่คือจุดเข้าใช้งานโมดูลทั้งหมดที่มีอยู่ใน SDK ตั้งแต่เวอร์ชัน 10:
- firebase-admin/app
- firebase-ผู้ดูแลระบบ/auth
- firebase-ผู้ดูแลระบบ/ฐานข้อมูล
- firebase-ผู้ดูแลระบบ/firestore
- firebase-admin/instance-id
- ผู้ดูแลระบบ firebase/แมชชีนเลิร์นนิง
- firebase-admin/ข้อความ
- firebase-admin/project-management
- firebase-admin/remote-config.php
- ผู้ดูแลระบบ firebase/กฎความปลอดภัย
- firebase-admin/storage
ตารางต่อไปนี้แสดงไวยากรณ์การนำเข้าการแทนที่สำหรับฟังก์ชันเนมสเปซแบบเดิมแต่ละรายการ:
v9 | v10 |
---|---|
admin.initializeApp() | import { initializeApp } from 'firebase-admin/app' |
admin.app() | import { getApp } from 'firebase-admin/ap' |
admin.credential.cert() | import { cert } from 'firebase-admin/app' |
admin.auth() | import { getAuth } from 'firebase-admin/auth' |
admin.database() | import { getDatabase } from 'firebase-admin/database' |
admin.firestore() | import { getFirestore } from 'firebase-admin/firestore' |
admin.instanceId() | import { getInstanceId } from 'firebase-admin/instance-id' |
admin.machineLearning() | import { getMachineLearning } from 'firebase-admin/machine-learning' |
admin.messaging() | import { getMessaging } from 'firebase-admin/messaging' |
admin.projectManagement() | import { getProjectManagement } from 'firebase-admin/project-management' |
admin.remoteConfig() | import { getRemoteConfig } from 'firebase-admin/remote-config' |
admin.securityRules() | import { getSecurityRules } from 'firebase-admin/security-rules' |
admin.storage() | import { getStorage } from 'firebase-admin/storage' |
ใช้ฟังก์ชันที่ส่งออกแทนวิธีการบนแอป
ใน API เดิม ออบเจ็กต์ App
เปิดเผยวิธีการต่างๆ เช่น app.auth()
และ app.database()
เราขอแนะนำให้นักพัฒนาหลีกเลี่ยงการใช้วิธีการเหล่านี้ และใช้จุดเข้าโมดูลเดียวกันที่อธิบายไว้ข้างต้นแทนเพื่อรับอินสแตนซ์บริการที่กำหนดขอบเขตไปยังออบเจ็กต์แอ App
ที่กำหนด และทำงานเฉพาะแอปอื่นๆ
v9 | v10 |
---|---|
app.auth() | import { getAuth } from 'firebase-admin/auth'; |
app.database() | import { getDatabase } from 'firebase-admin/database'; |
app.database(url) | import { getDatabaseWithUrl } from 'firebase-admin/database'; |
app.firestore() | import { getFirestore } from 'firebase-admin/firestore' |
app.instanceId() | import { getInstanceId } from 'firebase-admin/instance-id' |
app.machineLearning() | import { getMachineLearning } from 'firebase-admin/machine-learning' |
app.messaging() | import { getMessaging } from 'firebase-admin/messaging' |
app.projectManagement() | import { getProjectManagement } from 'firebase-admin/project-management' |
app.remoteConfig() | import { getRemoteConfig } from 'firebase-admin/remote-config' |
app.securityRules() | import { getSecurityRules } from 'firebase-admin/security-rules' |
app.storage() | import { getStorage } from 'firebase-admin/storage' |
app.delete() | import { deleteApp } from 'firebase-admin/app'; |
รองรับโมดูล ES
Node.js 12 ขึ้นไปมาพร้อมกับการสนับสนุนรุ่นทดลองสำหรับโมดูล ES ซึ่งช่วยให้นักพัฒนาที่ไม่ใช่ TypeScript สามารถใช้คีย์เวิร์ด export
และ import
ในโค้ดของตนได้ ตั้งแต่เวอร์ชัน v10 เป็นต้นไป Admin Node.js SDK ยังให้การสนับสนุนโมดูล ES เพื่อให้นักพัฒนาที่ใช้โมดูล ES บน Node.js ธรรมดาสามารถนำเข้า SDK โดยใช้ไวยากรณ์ import
ได้
หากต้องการใช้โมดูล ES กับ Admin SDK ขั้นแรกตรวจสอบให้แน่ใจว่าคุณได้เปิดใช้งานการรองรับ ESM สำหรับรันไทม์ Node.js ของคุณแล้ว โดยปกติจะทำได้โดยการเพิ่มฟิลด์ "type": "module"
ลงในไฟล์ package.json
ของคุณ จากนั้นคุณสามารถเขียนโค้ดแอปพลิเคชันที่มีลักษณะดังนี้:
// 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');