Node.js SDK Admin SDK v10(모듈형 SDK)으로 업그레이드

Admin Node.js SDK 버전 10에는 다음과 같은 두 가지 중요한 변경사항이 있습니다.

  • Node.js 10 지원 중단(브레이킹 체인지)
  • SDK에서 모듈형 API 패턴 채택

이 가이드에서는 개발자가 기존 Node.js 앱을 Admin SDK의 이전 버전에서 v10으로 업그레이드하는 데 도움이 되는 안내와 정보를 제공합니다.

Node.js를 v12 이상으로 업데이트

Admin Node.js SDK v10 출시와 함께 Firebase는 Node.js 10 지원을 중단했습니다. 개발자는 Admin SDK를 사용할 때 Node.js 12 이상을 사용해야 합니다. Admin Node.js SDK를 Firebase용 Cloud Functions와 함께 사용하는 경우 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');

Admin Node.js SDK는 v10부터 이름이 지정된 내보내기가 있는 여러 모듈 진입점을 제공합니다. 개발자는 전역 admin 네임스페이스를 사용하는 대신 새로운 진입점을 사용하여 SDK의 다양한 API에 액세스하는 것이 좋습니다.

새로운 모듈 진입점을 사용하면 위의 예시는 다음과 같이 작성됩니다.

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

v10 모듈형 진입점 사용

위의 예시에서는 더 이상 전역 admin 네임스페이스를 가져오지 않습니다. 대신 여러 모듈 진입점에서 필요한 기호만 명시적으로 가져옵니다. 또한 TypeScript 개발자는 더 이상 admin.auth.UserRecordadmin.database.Reference와 같은 삼중 중첩 유형 식별자를 사용할 필요가 없습니다. 각 유형은 정확히 하나의 모듈에 속하므로 UserRecordReference와 같은 짧은 이름으로 가져오기만 하면 됩니다.

다음은 v10부터 SDK에서 사용할 수 있는 모든 모듈 진입점입니다.

  • 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

다음 표는 각 기존 네임스페이스 함수에 대한 대체 가져오기 구문을 보여줍니다.

v9 v10
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();

앱에서 메서드 대신 내보낸 함수 사용

기존 API에서 App 객체는 app.auth()app.database()와 같은 여러 메서드를 노출했습니다. 개발자는 이러한 메서드를 사용하지 않고 대신 위에서 설명된 것과 동일한 모듈 진입점을 사용하여 지정된 App 객체로 범위가 지정된 서비스 인스턴스를 가져오고 다른 앱별 작업을 수행하는 것이 좋습니다.

v9 v10
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);

ES 모듈 지원

Node.js 12 이상은 ES 모듈을 실험적으로 지원하므로 TypeScript를 사용하지 않는 개발자도 자신의 코드에 exportimport 키워드를 사용할 수 있습니다. Admin Node.js SDK는 v10부터 ES 모듈도 지원하므로 일반 Node.js에서 ES 모듈을 구현하는 개발자는 import 구문을 사용하여 SDK를 가져올 수 있습니다.

ES 모듈을 Admin SDK와 함께 사용하려면 먼저 Node.js 런타임에 ESM 지원을 사용 설정해야 합니다. 보통 package.json 파일에 "type": "module" 필드를 추가하여 사용 설정할 수 있습니다. 그런 다음 애플리케이션 코드를 다음과 같이 작성할 수 있습니다.

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