1515 * limitations under the License.
1616 */
1717
18- import firebase from '@firebase/app' ;
19- import { CONSTANTS , isNodeSdk } from '@firebase/util' ;
20- import { FirebaseApp , FirebaseNamespace } from '@firebase/app-types' ;
18+ import { FirebaseNamespace , FirebaseApp } from '@firebase/app-types' ;
2119import { _FirebaseNamespace } from '@firebase/app-types/private' ;
2220import { Database } from './src/api/Database' ;
2321import { DataSnapshot } from './src/api/DataSnapshot' ;
@@ -29,30 +27,26 @@ import * as INTERNAL from './src/api/internal';
2927import * as TEST_ACCESS from './src/api/test_access' ;
3028import './src/nodePatches' ;
3129import * as types from '@firebase/database-types' ;
30+ import { setSDKVersion } from './src/core/version' ;
31+ import { CONSTANTS , isNodeSdk } from '@firebase/util' ;
32+
33+ const ServerValue = Database . ServerValue ;
3234
3335/**
3436 * A one off register function which returns a database based on the app and
3537 * passed database URL.
3638 *
3739 * @param app A valid FirebaseApp-like object
3840 * @param url A valid Firebase databaseURL
41+ * @param version custom version e.g. firebase-admin version
3942 */
40-
41- const ServerValue = Database . ServerValue ;
42-
43- export function initStandalone (
44- app : FirebaseApp ,
45- url : string ,
46- version ?: string
47- ) {
43+ export function initStandalone ( app : FirebaseApp , url : string , version : string ) {
4844 /**
4945 * This should allow the firebase-admin package to provide a custom version
5046 * to the backend
5147 */
5248 CONSTANTS . NODE_ADMIN = true ;
53- if ( version ) {
54- firebase . SDK_VERSION = version ;
55- }
49+ setSDKVersion ( version ) ;
5650
5751 return {
5852 instance : RepoManager . getInstance ( ) . databaseFromApp ( app , url ) ,
@@ -70,6 +64,9 @@ export function initStandalone(
7064}
7165
7266export function registerDatabase ( instance : FirebaseNamespace ) {
67+ // set SDK_VERSION
68+ setSDKVersion ( instance . SDK_VERSION ) ;
69+
7370 // Register the Database Service with the 'firebase' namespace.
7471 const namespace = ( instance as _FirebaseNamespace ) . INTERNAL . registerService (
7572 'database' ,
@@ -94,7 +91,21 @@ export function registerDatabase(instance: FirebaseNamespace) {
9491 }
9592}
9693
97- registerDatabase ( firebase ) ;
94+ try {
95+ // If @firebase /app is not present, skip registering database.
96+ // It could happen when this package is used in firebase-admin which doesn't depend on @firebase/app.
97+ // Previously firebase-admin depends on @firebase/app, which causes version conflict on
98+ // @firebase /app when used together with the js sdk. More detail:
99+ // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596
100+ const firebase = require ( '@firebase/app' ) . default ;
101+ registerDatabase ( firebase ) ;
102+ } catch ( err ) {
103+ // catch and ignore 'MODULE_NOT_FOUND' error in firebase-admin context
104+ // we can safely ignore this error because RTDB in firebase-admin works without @firebase/app
105+ if ( err . code !== 'MODULE_NOT_FOUND' ) {
106+ throw err ;
107+ }
108+ }
98109
99110// Types to export for the admin SDK
100111export { Database , Query , Reference , enableLogging , ServerValue } ;
0 commit comments