15
15
* limitations under the License.
16
16
*/
17
17
18
- import firebase from '@firebase/app' ;
19
- import { FirebaseNamespace } from '@firebase/app-types' ;
18
+ import { FirebaseNamespace , FirebaseApp } from '@firebase/app-types' ;
20
19
import { _FirebaseNamespace } from '@firebase/app-types/private' ;
21
20
import { Database } from './src/api/Database' ;
22
21
import { DataSnapshot } from './src/api/DataSnapshot' ;
@@ -29,21 +28,50 @@ import * as TEST_ACCESS from './src/api/test_access';
29
28
import './src/nodePatches' ;
30
29
import * as types from '@firebase/database-types' ;
31
30
import { setSDKVersion } from './src/core/version' ;
31
+ import { CONSTANTS } from '@firebase/util' ;
32
+
33
+
34
+ const ServerValue = Database . ServerValue ;
32
35
33
36
/**
34
37
* A one off register function which returns a database based on the app and
35
38
* passed database URL.
36
39
*
37
40
* @param app A valid FirebaseApp-like object
38
41
* @param url A valid Firebase databaseURL
42
+ * @param version custom version e.g. firebase-admin version
39
43
*/
44
+ export function initStandalone (
45
+ app : FirebaseApp ,
46
+ url : string ,
47
+ version : string
48
+ ) {
49
+ /**
50
+ * This should allow the firebase-admin package to provide a custom version
51
+ * to the backend
52
+ */
53
+ CONSTANTS . NODE_ADMIN = true ;
54
+ setSDKVersion ( version ) ;
40
55
41
- const ServerValue = Database . ServerValue ;
56
+ return {
57
+ instance : RepoManager . getInstance ( ) . databaseFromApp ( app , url ) ,
58
+ namespace : {
59
+ Reference,
60
+ Query,
61
+ Database,
62
+ DataSnapshot,
63
+ enableLogging,
64
+ INTERNAL ,
65
+ ServerValue,
66
+ TEST_ACCESS
67
+ }
68
+ } ;
69
+ }
42
70
43
71
export function registerDatabase ( instance : FirebaseNamespace ) {
44
72
45
73
// set SDK_VERSION
46
- setSDKVersion ( firebase . SDK_VERSION ) ;
74
+ setSDKVersion ( instance . SDK_VERSION ) ;
47
75
48
76
// Register the Database Service with the 'firebase' namespace.
49
77
( instance as _FirebaseNamespace ) . INTERNAL . registerService (
@@ -65,7 +93,18 @@ export function registerDatabase(instance: FirebaseNamespace) {
65
93
) ;
66
94
}
67
95
68
- registerDatabase ( firebase ) ;
96
+ try {
97
+ // If @firebase /app is not present, skip registering database.
98
+ // It could happen when this package is used in firebase-admin which doesn't depend on @firebase/app.
99
+ // Previously firebase-admin depends on @firebase/app, which causes version conflict on
100
+ // @firebase /app when used together with the js sdk. More detail:
101
+ // https://github.com/firebase/firebase-js-sdk/issues/1696#issuecomment-501546596
102
+ const firebase = require ( '@firebase/app' ) ;
103
+ registerDatabase ( firebase ) ;
104
+ } catch ( err ) {
105
+
106
+ }
107
+
69
108
70
109
// Types to export for the admin SDK
71
110
export { Database , Query , Reference , enableLogging , ServerValue } ;
0 commit comments