Skip to content

Commit

Permalink
feat(database): add databasePlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Oct 20, 2022
1 parent 3f56f10 commit 058d7dc
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// TODO: remove this file (used to be @vuefire/core)
export { walkSet } from './shared'
export type { OperationsType, ResetOption } from './shared'
export * from './database/subscribe'
Expand Down
9 changes: 3 additions & 6 deletions src/database/optionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,12 @@ function getRef(refOrQuery: DatabaseReference | Query): DatabaseReference {
return refOrQuery.ref
}

interface PluginOptions {
export interface DatabasePluginOptions extends RTDBOptions {
bindName?: string
unbindName?: string
serialize?: RTDBOptions['serialize']
reset?: RTDBOptions['reset']
wait?: RTDBOptions['wait']
}

const defaultOptions: Readonly<Required<PluginOptions>> = {
const defaultOptions: Readonly<Required<DatabasePluginOptions>> = {
bindName: '$rtdbBind',
unbindName: '$rtdbUnbind',
serialize: rtdbOptions.serialize,
Expand Down Expand Up @@ -94,7 +91,7 @@ export const rtdbUnbinds = new WeakMap<
*/
export function rtdbPlugin(
app: App,
pluginOptions: PluginOptions = defaultOptions
pluginOptions: DatabasePluginOptions = defaultOptions
) {
// TODO: implement
// const strategies = Vue.config.optionMergeStrategies
Expand Down
1 change: 1 addition & 0 deletions src/database/subscribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
onChildRemoved,
} from 'firebase/database'

// TODO: rename to match where it's used
export interface RTDBOptions {
reset?: ResetOption
serialize?: RTDBSerializer
Expand Down
6 changes: 3 additions & 3 deletions src/firestore/optionsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ export const firestoreUnbinds = new WeakMap<
Record<string, ReturnType<typeof bindCollection | typeof bindDocument>>
>()

export interface PluginOptions {
export interface FirestorePluginOptions {
bindName?: string
unbindName?: string
converter?: FirestoreOptions['converter']
reset?: FirestoreOptions['reset']
wait?: FirestoreOptions['wait']
}

const defaultOptions: Readonly<Required<PluginOptions>> = {
const defaultOptions: Readonly<Required<FirestorePluginOptions>> = {
bindName: '$bind',
unbindName: '$unbind',
converter: firestoreOptions.converter,
Expand All @@ -104,7 +104,7 @@ const defaultOptions: Readonly<Required<PluginOptions>> = {
*/
export const firestorePlugin = function firestorePlugin(
app: App,
pluginOptions: PluginOptions = defaultOptions
pluginOptions: FirestorePluginOptions = defaultOptions
) {
// const strategies = app.config.optionMergeStrategies
// TODO: implement
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
export {
rtdbPlugin,
bind as rtdbBind,
unbind as rtdbUnbind,
useList,
useObject,
} from './database'

export { rtdbPlugin } from './database/optionsApi'
export type { DatabasePluginOptions } from './database/optionsApi'

// TODO: rename and move to database
export type { RTDBOptions } from './core'

export {
bind as firestoreBind,
unbind as firestoreUnbind,
Expand All @@ -20,7 +26,7 @@ export type {

export { firestorePlugin } from './firestore/optionsApi'
export type {
PluginOptions,
FirestorePluginOptions,
VueFirestoreObject,
FirestoreOption,
} from './firestore/optionsApi'
91 changes: 91 additions & 0 deletions tests/database/options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { defineComponent } from 'vue'
import { mount } from '@vue/test-utils'
import { describe, expect, it, vi } from 'vitest'
import { rtdbPlugin, DatabasePluginOptions } from '../../src'
import { setupDatabaseRefs } from '../utils'
import { push } from 'firebase/database'

const component = defineComponent({ template: 'no' })

describe('RTDB: plugin options', () => {
const { databaseRef } = setupDatabaseRefs()

it('allows customizing $rtdbBind', () => {
const wrapper = mount(component, {
global: {
plugins: [
[
rtdbPlugin,
{
bindName: '$myBind',
unbindName: '$myUnbind',
},
],
],
},
})
expect(typeof (wrapper.vm as any).$myBind).toBe('function')
expect(typeof (wrapper.vm as any).$myUnbind).toBe('function')
})

it('calls custom serialize function with a ref', async () => {
const pluginOptions: DatabasePluginOptions = {
serialize: vi.fn(() => ({ foo: 'bar' })),
}
const { vm } = mount(
{
template: 'no',
data: () => ({ items: [] }),
},
{
global: {
plugins: [[rtdbPlugin, pluginOptions]],
},
}
)

const itemListRef = databaseRef()

const p = vm.$rtdbBind('items', itemListRef)
push(itemListRef, { text: 'foo' })

await p

expect(pluginOptions.serialize).toHaveBeenCalledTimes(1)
expect(pluginOptions.serialize).toHaveBeenCalledWith(
expect.objectContaining({ val: expect.any(Function) })
)
expect(vm.items).toEqual([{ foo: 'bar' }])
})

it('can override serialize with local option', async () => {
const pluginOptions = {
serialize: vi.fn(() => ({ foo: 'bar' })),
}

const items = databaseRef()
const { vm } = mount(
{
template: 'no',
data: () => ({ items: [] }),
},
{
global: {
plugins: [[rtdbPlugin, pluginOptions]],
},
}
)

const spy = vi.fn(() => ({ bar: 'bar' }))

vm.$rtdbBind('items', items, { serialize: spy })
await push(items, { text: 'foo' })

expect(pluginOptions.serialize).not.toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ val: expect.any(Function) })
)
expect(vm.items).toEqual([{ bar: 'bar' }])
})
})
10 changes: 4 additions & 6 deletions tests/firestore/options.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { defineComponent } from 'vue'
import { mount } from '@vue/test-utils'
import { describe, expect, it, vi } from 'vitest'
import { firestorePlugin, PluginOptions, useCollection } from '../../src'
import { firestorePlugin, FirestorePluginOptions } from '../../src'
import { addDoc, DocumentData } from 'firebase/firestore'
import { expectType, setupFirestoreRefs, tds, firestore } from '../utils'
import { usePendingPromises } from '../../src/firestore'
import { type Ref } from 'vue'
import { setupFirestoreRefs } from '../utils'

const component = defineComponent({ template: 'no' })

Expand Down Expand Up @@ -34,7 +32,7 @@ describe('Firestore: Options API', () => {
})

it('calls custom serialize function with collection', async () => {
const pluginOptions: PluginOptions = {
const pluginOptions: FirestorePluginOptions = {
converter: {
fromFirestore: vi.fn((snapshot, options?) => ({
foo: 'bar',
Expand Down Expand Up @@ -70,7 +68,7 @@ describe('Firestore: Options API', () => {
})

it('can be overridden by local option', async () => {
const pluginOptions: PluginOptions = {
const pluginOptions: FirestorePluginOptions = {
converter: {
fromFirestore: vi.fn((snapshot, options?) => ({
foo: 'bar',
Expand Down
6 changes: 4 additions & 2 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
query as databaseQuery,
orderByChild,
remove,
push as databaseAdd,
} from 'firebase/database'
import {
getFirestore,
Expand Down Expand Up @@ -132,8 +133,9 @@ export function setupDatabaseRefs() {
await remove(testsCollection)
})

function databaseRef(path: string) {
return ref(database, testsCollection.key + '/' + path)
function databaseRef(path?: string) {
const data = databaseAdd(testsCollection, path)
return data.ref
}

return { itemRef, listRef, orderedListRef, testId, databaseRef }
Expand Down

0 comments on commit 058d7dc

Please sign in to comment.