1+ import { FirebaseApp } from 'firebase/app'
12import { DatabaseReference , DataSnapshot , Query } from 'firebase/database'
23import { App , ComponentPublicInstance , toRef } from 'vue'
34import { isVue3 } from 'vue-demi'
@@ -27,49 +28,6 @@ const databasePluginDefaults: Readonly<
2728 unbindName : '$rtdbUnbind' ,
2829}
2930
30- declare module '@vue/runtime-core' {
31- export interface ComponentCustomProperties {
32- /**
33- * Binds a reference
34- *
35- * @param name
36- * @param reference
37- * @param options
38- */
39- $rtdbBind (
40- name : string ,
41- reference : DatabaseReference | Query ,
42- options ?: _DatabaseRefOptions
43- ) : Promise < DataSnapshot >
44-
45- /**
46- * Unbinds a bound reference
47- */
48- $rtdbUnbind : ( name : string , reset ?: ResetOption ) => void
49-
50- /**
51- * Bound database references
52- */
53- $firebaseRefs : Readonly < Record < string , DatabaseReference > >
54- // _firebaseSources: Readonly<
55- // Record<string, Reference | Query>
56- // >
57- /**
58- * Existing unbind functions that get automatically called when the component is unmounted
59- * @internal
60- */
61- // _firebaseUnbinds: Readonly<
62- // Record<string, UnbindWithReset>
63- // >
64- }
65- export interface ComponentCustomOptions {
66- /**
67- * Calls `$rtdbBind` at created
68- */
69- firebase ?: FirebaseOption
70- }
71- }
72-
7331export type VueFirebaseObject = Record < string , Query | DatabaseReference >
7432export type FirebaseOption = VueFirebaseObject | ( ( ) => VueFirebaseObject )
7533
@@ -83,12 +41,15 @@ export const rtdbUnbinds = new WeakMap<
8341 * you use the Options API. If you **exclusively use the Composition API** (e.g. `useObject()` and `useList()`), you
8442 * should not add it.
8543 *
44+ * @deprecated Use `VueFire` and `VueFireDatabaseOptionsAPI` with the `modules` option instead.
45+ *
8646 * @param app
8747 * @param pluginOptions
8848 */
8949export function databasePlugin (
9050 app : App ,
91- pluginOptions ?: DatabasePluginOptions
51+ pluginOptions ?: DatabasePluginOptions ,
52+ firebaseApp ?: FirebaseApp ,
9253) {
9354 // TODO: implement
9455 // const strategies = Vue.config.optionMergeStrategies
@@ -103,7 +64,7 @@ export function databasePlugin(
10364
10465 GlobalTarget [ unbindName ] = function rtdbUnbind (
10566 key : string ,
106- reset ?: ResetOption
67+ reset ?: ResetOption ,
10768 ) {
10869 internalUnbind ( key , rtdbUnbinds . get ( this ) , reset )
10970 delete this . $firebaseRefs [ key ]
@@ -114,7 +75,7 @@ export function databasePlugin(
11475 this : ComponentPublicInstance ,
11576 key : string ,
11677 source : DatabaseReference | Query ,
117- userOptions ?: _DatabaseRefOptions
78+ userOptions ?: _DatabaseRefOptions ,
11879 ) {
11980 const options = Object . assign ( { } , globalOptions , userOptions )
12081 const target = toRef ( this . $data as any , key )
@@ -156,7 +117,7 @@ export function databasePlugin(
156117 // ts
157118 key ,
158119 bindings [ key ] ,
159- globalOptions
120+ globalOptions ,
160121 )
161122 }
162123 } ,
@@ -174,4 +135,70 @@ export function databasePlugin(
174135 } )
175136}
176137
138+ /**
139+ * VueFire Database Module to be added to the `VueFire` Vue plugin options.
140+ *
141+ * @example
142+ *
143+ * ```ts
144+ * import { createApp } from 'vue'
145+ * import { VueFire, VueFireDatabaseOptionsAPI } from 'vuefire'
146+ *
147+ * const app = createApp(App)
148+ * app.use(VueFire, {
149+ * modules: [VueFireDatabaseOptionsAPI()],
150+ * })
151+ * ```
152+ */
153+ export function VueFireDatabaseOptionsAPI (
154+ pluginOptions ?: DatabasePluginOptions ,
155+ ) {
156+ return ( firebaseApp : FirebaseApp , app : App ) => {
157+ return databasePlugin ( app , pluginOptions , firebaseApp )
158+ }
159+ }
160+
177161type Mutable < T > = { - readonly [ P in keyof T ] : T [ P ] }
162+
163+ declare module '@vue/runtime-core' {
164+ export interface ComponentCustomProperties {
165+ /**
166+ * Binds a reference
167+ *
168+ * @param name
169+ * @param reference
170+ * @param options
171+ */
172+ $rtdbBind (
173+ name : string ,
174+ reference : DatabaseReference | Query ,
175+ options ?: _DatabaseRefOptions ,
176+ ) : Promise < DataSnapshot >
177+
178+ /**
179+ * Unbinds a bound reference
180+ */
181+ $rtdbUnbind : ( name : string , reset ?: ResetOption ) => void
182+
183+ /**
184+ * Bound database references
185+ */
186+ $firebaseRefs : Readonly < Record < string , DatabaseReference > >
187+ // _firebaseSources: Readonly<
188+ // Record<string, Reference | Query>
189+ // >
190+ /**
191+ * Existing unbind functions that get automatically called when the component is unmounted
192+ * @internal
193+ */
194+ // _firebaseUnbinds: Readonly<
195+ // Record<string, UnbindWithReset>
196+ // >
197+ }
198+ export interface ComponentCustomOptions {
199+ /**
200+ * Calls `$rtdbBind` at created
201+ */
202+ firebase ?: FirebaseOption
203+ }
204+ }
0 commit comments