Skip to content

chore: update to firebase 8x #940

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"eslint": "^6.1.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-prettier": "^3.1.0",
"firebase": "^7.2.3",
"firebase": "^8.1.1",
"jest": "^25.1.0",
"lerna": "^3.16.2",
"prettier": "^1.18.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bindCollection } from '../../src'
import { db, createOps, spyUnbind } from '@posva/vuefire-test-helpers'
import { firestore } from 'firebase'
import firebase from 'firebase/app'
import { OperationsType } from '../../src/shared'

describe('collections', () => {
let collection: firestore.CollectionReference,
let collection: firebase.firestore.CollectionReference,
vm: Record<string, any>,
resolve: (data: any) => void,
reject: (error: any) => void,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { bindDocument } from '../../src'
import { db, spyUnbind, createOps } from '@posva/vuefire-test-helpers'
import { firestore } from 'firebase'
import firebase from 'firebase/app'
import { OperationsType } from '../../src/shared'

describe('documents', () => {
let collection: firestore.CollectionReference,
document: firestore.DocumentReference,
let collection: firebase.firestore.CollectionReference,
document: firebase.firestore.DocumentReference,
vm: Record<string, any>,
resolve: (data: any) => void,
reject: (error: any) => void,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { bindDocument, firestoreOptions, bindCollection } from '../../src'
import { db, createOps, CollectionReference, DocumentReference } from '@posva/vuefire-test-helpers'
import { firestore } from 'firebase'
import { db, createOps } from '@posva/vuefire-test-helpers'
import firebase from 'firebase/app'

describe('options', () => {
let collection: firestore.CollectionReference,
document: firestore.DocumentReference,
let collection: firebase.firestore.CollectionReference,
document: firebase.firestore.DocumentReference,
vm: Record<string, any>,
resolve: (data: any) => void,
reject: (error: any) => void
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { bindCollection, FirestoreOptions } from '../../src'
import { db, delay, spyUnbind, delayUpdate, createOps } from '@posva/vuefire-test-helpers'
import { OperationsType } from '../../src/shared'
import { firestore } from 'firebase'
import firebase from 'firebase/app'

describe('refs in collections', () => {
let collection: firestore.CollectionReference,
a: firestore.DocumentReference,
b: firestore.DocumentReference,
let collection: firebase.firestore.CollectionReference,
a: firebase.firestore.DocumentReference,
b: firebase.firestore.DocumentReference,
vm: Record<string, any>,
bind: (
key: string,
collection: firestore.CollectionReference,
collection: firebase.firestore.CollectionReference,
options?: FirestoreOptions
) => void,
unbind: () => void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@ import {
spyOnSnapshotCallback,
createOps,
} from '@posva/vuefire-test-helpers'
import { firestore } from 'firebase'
import firebase from 'firebase/app'
import { OperationsType } from '../../src/shared'

describe('refs in documents', () => {
// a and c existing objects { isA: true }
// item is an empty ready to use object
// empty is an empty object that is left empty
// d has a ref to c
let collection: firestore.CollectionReference,
a: firestore.DocumentReference,
b: firestore.DocumentReference,
c: firestore.DocumentReference,
d: firestore.DocumentReference,
empty: firestore.DocumentReference,
item: firestore.DocumentReference,
let collection: firebase.firestore.CollectionReference,
a: firebase.firestore.DocumentReference,
b: firebase.firestore.DocumentReference,
c: firebase.firestore.DocumentReference,
d: firebase.firestore.DocumentReference,
empty: firebase.firestore.DocumentReference,
item: firebase.firestore.DocumentReference,
vm: Record<string, any>,
bind: (key: string, document: firestore.DocumentReference, options?: FirestoreOptions) => void,
bind: (
key: string,
document: firebase.firestore.DocumentReference,
options?: FirestoreOptions
) => void,
unbind: () => void,
ops: OperationsType

Expand Down
24 changes: 12 additions & 12 deletions packages/@posva/vuefire-core/src/firestore/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSnapshot, extractRefs, FirestoreSerializer } from './utils'
import { walkGet, callOnceWithArg, OperationsType } from '../shared'
import { firestore } from 'firebase'
import firebase from 'firebase/app'

export interface FirestoreOptions {
maxRefDepth?: number
Expand All @@ -21,7 +21,7 @@ interface FirestoreSubscription {
unsub: () => void
// Firestore unique key eg: items/12
path: string
data: () => firestore.DocumentData | null
data: () => firebase.firestore.DocumentData | null
// // path inside the object to access the data items.3
// key: string
}
Expand All @@ -36,7 +36,7 @@ function updateDataFromDocumentSnapshot(
options: Required<FirestoreOptions>,
target: CommonBindOptionsParameter['vm'],
path: string,
snapshot: firestore.DocumentSnapshot,
snapshot: firebase.firestore.DocumentSnapshot,
subs: Record<string, FirestoreSubscription>,
ops: CommonBindOptionsParameter['ops'],
depth: number,
Expand All @@ -53,7 +53,7 @@ interface SubscribeToDocumentParamater {
depth: number
resolve: () => void
ops: CommonBindOptionsParameter['ops']
ref: firestore.DocumentReference
ref: firebase.firestore.DocumentReference
}

function subscribeToDocument(
Expand All @@ -79,7 +79,7 @@ function subscribeToDocument(
interface SubscribeToRefsParameter {
subs: Record<string, FirestoreSubscription>
target: CommonBindOptionsParameter['vm']
refs: Record<string, firestore.DocumentReference>
refs: Record<string, firebase.firestore.DocumentReference>
path: string | number
depth: number
resolve: CommonBindOptionsParameter['resolve']
Expand All @@ -94,7 +94,7 @@ function subscribeToRefs(
target: CommonBindOptionsParameter['vm'],
path: string | number,
subs: Record<string, FirestoreSubscription>,
refs: Record<string, firestore.DocumentReference>,
refs: Record<string, firebase.firestore.DocumentReference>,
ops: CommonBindOptionsParameter['ops'],
depth: number,
resolve: CommonBindOptionsParameter['resolve']
Expand Down Expand Up @@ -159,7 +159,7 @@ interface CommonBindOptionsParameter {
}

interface BindCollectionParamater extends CommonBindOptionsParameter {
collection: firestore.CollectionReference | firestore.Query
collection: firebase.firestore.CollectionReference | firebase.firestore.Query
}

// TODO: refactor without using an object to improve size like the other functions
Expand All @@ -179,14 +179,14 @@ export function bindCollection(
const arraySubs: Record<string, FirestoreSubscription>[] = []

const change = {
added: ({ newIndex, doc }: firestore.DocumentChange) => {
added: ({ newIndex, doc }: firebase.firestore.DocumentChange) => {
arraySubs.splice(newIndex, 0, Object.create(null))
const subs = arraySubs[newIndex]
const [data, refs] = extractRefs(options.serialize(doc), undefined, subs)
ops.add(array, newIndex, data)
subscribeToRefs(options, array, newIndex, subs, refs, ops, 0, resolve.bind(null, doc))
},
modified: ({ oldIndex, newIndex, doc }: firestore.DocumentChange) => {
modified: ({ oldIndex, newIndex, doc }: firebase.firestore.DocumentChange) => {
const subs = arraySubs[oldIndex]
const oldData = array[oldIndex]
const [data, refs] = extractRefs(options.serialize(doc), oldData, subs)
Expand All @@ -197,7 +197,7 @@ export function bindCollection(
ops.add(array, newIndex, data)
subscribeToRefs(options, array, newIndex, subs, refs, ops, 0, resolve)
},
removed: ({ oldIndex }: firestore.DocumentChange) => {
removed: ({ oldIndex }: firebase.firestore.DocumentChange) => {
ops.remove(array, oldIndex)
unsubscribeAll(arraySubs.splice(oldIndex, 1)[0])
},
Expand All @@ -215,7 +215,7 @@ export function bindCollection(
typeof snapshot.docChanges === 'function'
? snapshot.docChanges()
: /* istanbul ignore next to support firebase < 5*/
((snapshot.docChanges as unknown) as firestore.DocumentChange[])
((snapshot.docChanges as unknown) as firebase.firestore.DocumentChange[])

if (!isResolved && docChanges.length) {
// isResolved is only meant to make sure we do the check only once
Expand Down Expand Up @@ -263,7 +263,7 @@ export function bindCollection(
}

interface BindDocumentParamater extends CommonBindOptionsParameter {
document: firestore.DocumentReference
document: firebase.firestore.DocumentReference
}

/**
Expand Down
34 changes: 17 additions & 17 deletions packages/@posva/vuefire-core/src/firestore/utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { firestore } from 'firebase'
import firebase from 'firebase/app'
import { isTimestamp, isObject, isDocumentRef, TODO } from '../shared'

export type FirestoreReference =
| firestore.Query
| firestore.DocumentReference
| firestore.CollectionReference
| firebase.firestore.Query
| firebase.firestore.DocumentReference
| firebase.firestore.CollectionReference

// TODO: fix type not to be any
export function createSnapshot(doc: firestore.DocumentSnapshot): TODO {
export function createSnapshot(doc: firebase.firestore.DocumentSnapshot): TODO {
// TODO: it should create a deep copy instead because otherwise we will modify internal data
// defaults everything to false, so no need to set
return Object.defineProperty(doc.data() || {}, 'id', { value: doc.id })
Expand All @@ -16,26 +16,26 @@ export function createSnapshot(doc: firestore.DocumentSnapshot): TODO {
export type FirestoreSerializer = typeof createSnapshot

export function extractRefs(
doc: firestore.DocumentData,
oldDoc: firestore.DocumentData | void,
subs: Record<string, { path: string; data: () => firestore.DocumentData | null }>
): [firestore.DocumentData, Record<string, firestore.DocumentReference>] {
const dataAndRefs: [firestore.DocumentData, Record<string, firestore.DocumentReference>] = [
{},
{},
]
doc: firebase.firestore.DocumentData,
oldDoc: firebase.firestore.DocumentData | void,
subs: Record<string, { path: string; data: () => firebase.firestore.DocumentData | null }>
): [firebase.firestore.DocumentData, Record<string, firebase.firestore.DocumentReference>] {
const dataAndRefs: [
firebase.firestore.DocumentData,
Record<string, firebase.firestore.DocumentReference>
] = [{}, {}]

const subsByPath = Object.keys(subs).reduce((resultSubs, subKey) => {
const sub = subs[subKey]
resultSubs[sub.path] = sub.data()
return resultSubs
}, {} as Record<string, firestore.DocumentData | null>)
}, {} as Record<string, firebase.firestore.DocumentData | null>)

function recursiveExtract(
doc: firestore.DocumentData,
oldDoc: firestore.DocumentData | void,
doc: firebase.firestore.DocumentData,
oldDoc: firebase.firestore.DocumentData | void,
path: string,
result: [firestore.DocumentData, Record<string, firestore.DocumentReference>]
result: [firebase.firestore.DocumentData, Record<string, firebase.firestore.DocumentReference>]
): void {
// make it easier to later on access the value
oldDoc = oldDoc || {}
Expand Down
6 changes: 3 additions & 3 deletions packages/@posva/vuefire-core/src/rtdb/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { database } from 'firebase'
import firebase from 'firebase/app'
import { createRecordFromRTDBSnapshot, indexForKey, RTDBSerializer } from './utils'
import { OperationsType, ResetOption } from '../shared'

Expand Down Expand Up @@ -27,7 +27,7 @@ interface CommonBindOptionsParameter {
// TODO: refactor using normal arguments instead of an array to improve size

interface BindAsObjectParameter extends CommonBindOptionsParameter {
document: database.Reference | database.Query
document: firebase.database.Reference | firebase.database.Query
}

/**
Expand Down Expand Up @@ -60,7 +60,7 @@ export function rtdbBindAsObject(
}

interface BindAsArrayParameter extends CommonBindOptionsParameter {
collection: database.Reference | database.Query
collection: firebase.database.Reference | firebase.database.Query
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/@posva/vuefire-core/src/rtdb/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { database } from 'firebase'
import firebase from 'firebase/app'
import { isObject } from '../shared'

/**
Expand All @@ -7,7 +7,7 @@ import { isObject } from '../shared'
* @param snapshot
* @return
*/
export function createRecordFromRTDBSnapshot(snapshot: database.DataSnapshot): any {
export function createRecordFromRTDBSnapshot(snapshot: firebase.database.DataSnapshot): any {
const value = snapshot.val()
const res = isObject(value) ? value : Object.defineProperty({}, '.value', { value })
// if (isObject(value)) {
Expand Down
4 changes: 2 additions & 2 deletions packages/@posva/vuefire-core/src/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { firestore } from 'firebase'
import firebase from 'firebase/app'

export interface OperationsType {
set: (target: Record<string, any>, key: string | number, value: any) => any
Expand Down Expand Up @@ -62,7 +62,7 @@ export function isTimestamp(o: any): o is Date {
* Checks if a variable is a Firestore Document Reference
* @param o
*/
export function isDocumentRef(o: any): o is firestore.DocumentReference {
export function isDocumentRef(o: any): o is firebase.firestore.DocumentReference {
return o && o.onSnapshot
}

Expand Down
10 changes: 5 additions & 5 deletions packages/@posva/vuefire-test-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Vue from 'vue'
import { MockFirebase, MockedReference } from 'firebase-mock'
import { firestore } from '../../../../node_modules/firebase/index'
import firebase from '../../../../node_modules/firebase/index'
import { walkSet } from '@posva/vuefire-core'

Vue.config.productionTip = false
Expand All @@ -10,9 +10,9 @@ export { Vue, MockFirebase, MockedReference }
export * from './mock'

type FirestoreReference =
| firestore.CollectionReference
| firestore.DocumentReference
| firestore.Query
| firebase.firestore.CollectionReference
| firebase.firestore.DocumentReference
| firebase.firestore.Query

export function spyUnbind(ref: FirestoreReference) {
const unbindSpy = jest.fn()
Expand Down Expand Up @@ -49,7 +49,7 @@ export function spyOnSnapshotCallback(ref: FirestoreReference) {
}

// This makes sure some tests fail by delaying callbacks
export function delayUpdate(ref: firestore.DocumentReference, time = 0) {
export function delayUpdate(ref: firebase.firestore.DocumentReference, time = 0) {
const onSnapshot = ref.onSnapshot.bind(ref)
// @ts-ignore
ref.onSnapshot = fn =>
Expand Down
5 changes: 2 additions & 3 deletions packages/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
declare module 'firebase-mock' {
import { database } from 'firebase'

import firebase from 'firebase/app'
type TODO = any

interface MockFirebaseConstructor {
new (): MockFirebaseI
}

export interface MockedReference extends database.Reference {
export interface MockedReference extends firebase.database.Reference {
flush: () => void
forceCancel: (error: any) => void
autoFlush: () => void
Expand Down
Loading