15
15
* limitations under the License.
16
16
*/
17
17
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' ;
21
19
import { _FirebaseNamespace } from '@firebase/app-types/private' ;
22
20
import { Database } from './src/api/Database' ;
23
21
import { DataSnapshot } from './src/api/DataSnapshot' ;
@@ -29,30 +27,26 @@ import * as INTERNAL from './src/api/internal';
29
27
import * as TEST_ACCESS from './src/api/test_access' ;
30
28
import './src/nodePatches' ;
31
29
import * 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 ;
32
34
33
35
/**
34
36
* A one off register function which returns a database based on the app and
35
37
* passed database URL.
36
38
*
37
39
* @param app A valid FirebaseApp-like object
38
40
* @param url A valid Firebase databaseURL
41
+ * @param version custom version e.g. firebase-admin version
39
42
*/
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 ) {
48
44
/**
49
45
* This should allow the firebase-admin package to provide a custom version
50
46
* to the backend
51
47
*/
52
48
CONSTANTS . NODE_ADMIN = true ;
53
- if ( version ) {
54
- firebase . SDK_VERSION = version ;
55
- }
49
+ setSDKVersion ( version ) ;
56
50
57
51
return {
58
52
instance : RepoManager . getInstance ( ) . databaseFromApp ( app , url ) ,
@@ -70,6 +64,9 @@ export function initStandalone(
70
64
}
71
65
72
66
export function registerDatabase ( instance : FirebaseNamespace ) {
67
+ // set SDK_VERSION
68
+ setSDKVersion ( instance . SDK_VERSION ) ;
69
+
73
70
// Register the Database Service with the 'firebase' namespace.
74
71
const namespace = ( instance as _FirebaseNamespace ) . INTERNAL . registerService (
75
72
'database' ,
@@ -94,7 +91,21 @@ export function registerDatabase(instance: FirebaseNamespace) {
94
91
}
95
92
}
96
93
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
+ }
98
109
99
110
// Types to export for the admin SDK
100
111
export { Database , Query , Reference , enableLogging , ServerValue } ;
0 commit comments