Skip to content

Commit 9b1c738

Browse files
committed
refactor: deprecate old bind rtdb names
1 parent eca3031 commit 9b1c738

File tree

4 files changed

+83
-25
lines changed

4 files changed

+83
-25
lines changed

docs/cookbook/migration-v2-v3.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,24 @@ Therefore, these are the requirements to upgrade to VueFire v3:
1111

1212
VueFire 3 introduces a Composition API that is more flexible and powerful than the Options API. However, it keeps the existing Options API as close as possible to the existing version in v2. Internally, it is implemented as a wrapper around the Composition API.
1313

14+
Terms starting with _rtdb_ are now prefixed with _database_ to match the Firebase SDK. For example, `rtdbBindAsArray` is now `databaseBindAsArray`. The ones starting with _rtdb_ are still available but marked as deprecated.
15+
16+
## Deprecations
17+
18+
The `firestorePlugin` and `rtdbPlugin` are now deprecated in favor of _modules_. They are still available but will be removed in the next major version. You should use `VueFire`, `VueFireFirestoreOptionsAPI` and `VueFireDatabaseOptionsAPI` instead:
19+
20+
```diff
21+
const app = createApp({})
22+
23+
// for firestore
24+
-app.use(firestorePlugin)
25+
+app.use(VueFire, { modules: [VueFireFirestoreOptionsAPI] })
26+
27+
// for database
28+
-app.use(rtdbPlugin)
29+
+app.use(VueFire, { modules: [VueFireFirestoreOptionsAPI] })
30+
````
31+
1432
## Breaking changes
1533

1634
### Removal of `serialize` option for Firestore

src/database/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
_Nullable,
2323
_RefWithState,
2424
} from '../shared'
25-
import { rtdbUnbinds } from './optionsApi'
25+
import { databaseUnbinds } from './optionsApi'
2626
import {
2727
bindAsArray,
2828
bindAsObject,
@@ -203,7 +203,7 @@ export function useObject<T = unknown>(
203203
}
204204

205205
export const unbind = (target: Ref, reset?: ResetOption) =>
206-
internalUnbind('', rtdbUnbinds.get(target), reset)
206+
internalUnbind('', databaseUnbinds.get(target), reset)
207207

208208
/**
209209
* Retrieves the Database instance.

src/database/optionsApi.ts

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,40 +6,41 @@ import { useFirebaseApp } from '../app'
66
import { getGlobalScope } from '../globals'
77
import { ResetOption, UnbindWithReset } from '../shared'
88
import { internalUnbind, _useDatabaseRef } from './index'
9-
import {
10-
bindAsArray,
11-
bindAsObject,
12-
rtdbOptions,
13-
_DatabaseRefOptions,
14-
_GlobalDatabaseRefOptions,
15-
} from './subscribe'
9+
import { _DatabaseRefOptions, _GlobalDatabaseRefOptions } from './subscribe'
1610

1711
/**
1812
* Options for the Firebase Database Plugin that enables the Options API such as `$rtdbBind` and `$rtdbUnbind`.
1913
*/
2014
export interface DatabasePluginOptions
2115
extends Partial<_GlobalDatabaseRefOptions> {
16+
/**
17+
* @deprecated: was largely unused and not very useful. Please open an issue with use cases if you need this.
18+
*/
2219
bindName?: string
20+
21+
/**
22+
* @deprecated: was largely unused and not very useful. Please open an issue with use cases if you need this.
23+
*/
2324
unbindName?: string
2425
}
2526

2627
const databasePluginDefaults: Readonly<
2728
Required<Omit<DatabasePluginOptions, keyof _GlobalDatabaseRefOptions>>
2829
> = {
29-
bindName: '$rtdbBind',
30-
unbindName: '$rtdbUnbind',
30+
bindName: '$databaseBind',
31+
unbindName: '$databaseUnbind',
3132
}
3233

3334
export type VueFirebaseObject = Record<string, Query | DatabaseReference>
3435
export type FirebaseOption = VueFirebaseObject | (() => VueFirebaseObject)
3536

36-
export const rtdbUnbinds = new WeakMap<
37+
export const databaseUnbinds = new WeakMap<
3738
object,
3839
Record<string, UnbindWithReset>
3940
>()
4041

4142
/**
42-
* Install this plugin if you want to add `$rtdbBind` and `$rtdbUnbind` functions. Note this plugin is only necessary if
43+
* Install this plugin if you want to add `$databaseBind` and `$databaseUnbind` functions. Note this plugin is only necessary if
4344
* you use the Options API. If you **exclusively use the Composition API** (e.g. `useObject()` and `useList()`), you
4445
* should not add it.
4546
*
@@ -64,32 +65,44 @@ export function databasePlugin(
6465
? app.config.globalProperties
6566
: (app as any).prototype
6667

67-
GlobalTarget[unbindName] = function rtdbUnbind(
68+
GlobalTarget[unbindName] = function databaseUnbind(
69+
this: ComponentPublicInstance,
6870
key: string,
6971
reset?: ResetOption
7072
) {
71-
internalUnbind(key, rtdbUnbinds.get(this), reset)
73+
internalUnbind(key, databaseUnbinds.get(this), reset)
74+
// @ts-expect-error: readonly for the users
7275
delete this.$firebaseRefs[key]
7376
}
7477

75-
// add $rtdbBind and $rtdbUnbind methods
76-
GlobalTarget[bindName] = function rtdbBind(
78+
GlobalTarget[bindName] = function databaseBind(
7779
this: ComponentPublicInstance,
7880
key: string,
7981
source: DatabaseReference | Query,
8082
userOptions?: _DatabaseRefOptions
8183
) {
8284
const options = Object.assign({}, globalOptions, userOptions)
8385
const target = toRef(this.$data as any, key)
84-
if (!rtdbUnbinds.has(this)) {
85-
rtdbUnbinds.set(this, {})
86+
if (!databaseUnbinds.has(this)) {
87+
databaseUnbinds.set(this, {})
8688
}
87-
const unbinds = rtdbUnbinds.get(this)!
89+
const unbinds = databaseUnbinds.get(this)!
8890

8991
if (unbinds[key]) {
9092
unbinds[key](options.reset)
9193
}
9294

95+
// add the old rtdb* methods if the user was using the defaults
96+
if (pluginOptions) {
97+
if (!pluginOptions.bindName) {
98+
GlobalTarget['$rtdbBind'] = GlobalTarget[bindName]
99+
}
100+
if (!pluginOptions.unbindName) {
101+
GlobalTarget['$rtdbUnbind'] = GlobalTarget[unbindName]
102+
}
103+
}
104+
105+
// we create a local scope to avoid leaking the effect since it's created outside of the component
93106
const scope = getGlobalScope(firebaseApp || useFirebaseApp(), app).run(() =>
94107
effectScope()
95108
)!
@@ -117,6 +130,7 @@ export function databasePlugin(
117130
beforeCreate(this: ComponentPublicInstance) {
118131
this.$firebaseRefs = Object.create(null)
119132
},
133+
120134
created(this: ComponentPublicInstance) {
121135
let bindings = this.$options.firebase
122136
if (typeof bindings === 'function') {
@@ -136,7 +150,7 @@ export function databasePlugin(
136150
},
137151

138152
beforeUnmount(this: ComponentPublicInstance) {
139-
const unbinds = rtdbUnbinds.get(this)
153+
const unbinds = databaseUnbinds.get(this)
140154
if (unbinds) {
141155
for (const key in unbinds) {
142156
unbinds[key]()
@@ -149,7 +163,8 @@ export function databasePlugin(
149163
}
150164

151165
/**
152-
* VueFire Database Module to be added to the `VueFire` Vue plugin options.
166+
* VueFire Database Module to be added to the `VueFire` Vue plugin options. If you **exclusively use the Composition
167+
* API** (e.g. `useObject()` and `useList()`), you should not add it.
153168
*
154169
* @example
155170
*
@@ -182,6 +197,16 @@ declare module '@vue/runtime-core' {
182197
* @param reference
183198
* @param options
184199
*/
200+
$databaseBind(
201+
name: string,
202+
reference: DatabaseReference | Query,
203+
options?: _DatabaseRefOptions
204+
): Promise<DataSnapshot>
205+
206+
/**
207+
* {@inheritDoc ComponentCustomProperties.$databaseBind}
208+
* @deprecated Use `$databaseBind` instead.
209+
*/
185210
$rtdbBind(
186211
name: string,
187212
reference: DatabaseReference | Query,
@@ -191,6 +216,12 @@ declare module '@vue/runtime-core' {
191216
/**
192217
* Unbinds a bound reference
193218
*/
219+
$databaseUnbind: (name: string, reset?: ResetOption) => void
220+
221+
/**
222+
* {@inheritDoc ComponentCustomProperties.$databaseUnbind}
223+
* @deprecated Use `$databaseUnbind` instead.
224+
*/
194225
$rtdbUnbind: (name: string, reset?: ResetOption) => void
195226

196227
/**
@@ -210,7 +241,7 @@ declare module '@vue/runtime-core' {
210241
}
211242
export interface ComponentCustomOptions {
212243
/**
213-
* Calls `$rtdbBind` at created
244+
* Calls `$databaseBind` at created
214245
*/
215246
firebase?: FirebaseOption
216247
}

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FirebaseApp } from 'firebase/app'
22
import type { App } from 'vue'
3+
import { databasePlugin } from '../dist'
34
import { _FirebaseAppInjectionKey } from './app'
45

56
// Database
@@ -28,15 +29,23 @@ export type {
2829
} from './firestore'
2930

3031
// Database options API
31-
export { databasePlugin } from './database/optionsApi'
32+
export {
33+
databasePlugin,
34+
// To ease migration
35+
databasePlugin as rtdbPlugin,
36+
VueFireDatabaseOptionsAPI,
37+
} from './database/optionsApi'
3238
export type {
3339
DatabasePluginOptions,
3440
VueFirebaseObject,
3541
FirebaseOption,
3642
} from './database/optionsApi'
3743

3844
// Firestore options API
39-
export { firestorePlugin } from './firestore/optionsApi'
45+
export {
46+
firestorePlugin,
47+
VueFireFirestoreOptionsAPI,
48+
} from './firestore/optionsApi'
4049
export type {
4150
FirestorePluginOptions,
4251
VueFirestoreObject,

0 commit comments

Comments
 (0)