Skip to content

Commit 8bc68ab

Browse files
committed
fix: an issue where a doc initialised with empty string would return the collection .data instead
1 parent 344b239 commit 8bc68ab

File tree

8 files changed

+18
-14
lines changed

8 files changed

+18
-14
lines changed

packages/plugin-firestore-admin/src/actions/fetch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import {
1010
getFirestoreDocPath,
1111
} from '@magnetarjs/utils-firestore'
1212
import type { DocumentSnapshot, QueryDocumentSnapshot } from 'firebase-admin/firestore'
13+
import { isString } from 'is-what'
1314
import { FirestoreAdminPluginOptions } from '../CreatePlugin.js'
1415
import { docSnapshotToDocMetadata, getQueryInstance } from '../helpers/getFirestore.js'
1516

1617
export function fetchActionFactory(
17-
firestorePluginOptions: Required<FirestoreAdminPluginOptions>
18+
firestorePluginOptions: Required<FirestoreAdminPluginOptions>,
1819
): PluginFetchAction {
1920
return async function ({
2021
payload,
@@ -25,7 +26,7 @@ export function fetchActionFactory(
2526
const { db } = firestorePluginOptions
2627
// in case of a doc module
2728
let snapshots: (DocumentSnapshot | QueryDocumentSnapshot)[] | undefined
28-
if (docId) {
29+
if (isString(docId)) {
2930
const documentPath = getFirestoreDocPath(collectionPath, docId, pluginModuleConfig, firestorePluginOptions) // prettier-ignore
3031
const docSnapshot = await db.doc(documentPath).get()
3132
snapshots = [docSnapshot]

packages/plugin-firestore-admin/src/actions/stream.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
getFirestoreDocPath,
1010
} from '@magnetarjs/utils-firestore'
1111
import type { DocumentChange, DocumentSnapshot, QuerySnapshot } from 'firebase-admin/firestore'
12+
import { isString } from 'is-what'
1213
import { FirestoreAdminPluginOptions } from '../CreatePlugin.js'
1314
import { docSnapshotToDocMetadata, getQueryInstance } from '../helpers/getFirestore.js'
1415

@@ -38,7 +39,7 @@ export function streamActionFactory(
3839
let firstDataReceived = false
3940

4041
// in case of a doc module
41-
if (docId) {
42+
if (isString(docId)) {
4243
const documentPath = getFirestoreDocPath(collectionPath, docId, pluginModuleConfig, firestorePluginOptions) // prettier-ignore
4344
closeStream = db
4445
.doc(documentPath)

packages/plugin-firestore/src/actions/fetch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import {
1212
} from '@magnetarjs/utils-firestore'
1313
import type { DocumentSnapshot, QueryDocumentSnapshot } from 'firebase/firestore'
1414
import { doc, getDoc, getDocs } from 'firebase/firestore'
15+
import { isString } from 'is-what'
1516
import { FirestorePluginOptions } from '../CreatePlugin.js'
1617
import { docSnapshotToDocMetadata, getQueryInstance } from '../helpers/getFirestore.js'
1718

1819
export function fetchActionFactory(
19-
firestorePluginOptions: Required<FirestorePluginOptions>
20+
firestorePluginOptions: Required<FirestorePluginOptions>,
2021
): PluginFetchAction {
2122
return async function ({
2223
payload,
@@ -27,7 +28,7 @@ export function fetchActionFactory(
2728
const { db, debug } = firestorePluginOptions
2829
// in case of a doc module
2930
let snapshots: (DocumentSnapshot | QueryDocumentSnapshot)[] | undefined
30-
if (docId) {
31+
if (isString(docId)) {
3132
const documentPath = getFirestoreDocPath(collectionPath, docId, pluginModuleConfig, firestorePluginOptions) // prettier-ignore
3233
const query = doc(db, documentPath)
3334
const warnNoResponse = debug

packages/plugin-firestore/src/actions/stream.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@magnetarjs/utils-firestore'
1111
import type { DocumentChange, DocumentSnapshot, QuerySnapshot } from 'firebase/firestore'
1212
import { doc, onSnapshot } from 'firebase/firestore'
13+
import { isString } from 'is-what'
1314
import { FirestorePluginOptions } from '../CreatePlugin.js'
1415
import { docSnapshotToDocMetadata, getQueryInstance } from '../helpers/getFirestore.js'
1516

@@ -39,7 +40,7 @@ export function streamActionFactory(
3940
let firstDataReceived = false
4041

4142
// in case of a doc module
42-
if (docId) {
43+
if (isString(docId)) {
4344
const documentPath = getFirestoreDocPath(collectionPath, docId, pluginModuleConfig, firestorePluginOptions) // prettier-ignore
4445
closeStream = onSnapshot(
4546
doc(db, documentPath),

packages/plugin-simple-store/src/CreatePlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getPathWhereIdentifier } from '@magnetarjs/types'
99
import { filterDataPerClauses } from '@magnetarjs/utils'
1010
import { copy } from 'copy-anything'
1111
import { mapGetOrSet, objGetOrSet } from 'getorset-anything'
12-
import { isArray, isNumber, isPlainObject } from 'is-what'
12+
import { isArray, isNumber, isPlainObject, isString } from 'is-what'
1313
import { deleteActionFactory } from './actions/delete.js'
1414
import { deletePropActionFactory } from './actions/deleteProp.js'
1515
import { fetchActionFactory } from './actions/fetch.js'
@@ -122,7 +122,7 @@ export const CreatePlugin: MagnetarPlugin<SimpleStoreOptions> = (
122122
}: PluginModuleSetupPayload<SimpleStoreModuleConfig>): any => {
123123
const dataCollectionMap = objGetOrSet(data, collectionPath, () => new Map())
124124
// if it's a doc, return the specific doc
125-
if (docId) return dataCollectionMap.get(docId)
125+
if (isString(docId)) return dataCollectionMap.get(docId)
126126
// if it's a collection, we must return the dataCollectionMap but with applied query clauses
127127
// but remember, the return type MUST be a map with id as keys and the docs as value
128128
const clauses: Clauses = pluginModuleConfig

packages/plugin-vue3/src/CreatePlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getPathWhereIdentifier } from '@magnetarjs/types'
99
import { filterDataPerClauses } from '@magnetarjs/utils'
1010
import { copy } from 'copy-anything'
1111
import { mapGetOrSet, objGetOrSet } from 'getorset-anything'
12-
import { isArray, isNumber, isPlainObject } from 'is-what'
12+
import { isArray, isNumber, isPlainObject, isString } from 'is-what'
1313
import { reactive } from 'vue'
1414
import { deleteActionFactory } from './actions/delete.js'
1515
import { deletePropActionFactory } from './actions/deleteProp.js'
@@ -130,7 +130,7 @@ export const CreatePlugin: MagnetarPlugin<Vue3StoreOptions> = (
130130
}: PluginModuleSetupPayload<Vue3StoreModuleConfig>): any => {
131131
const dataCollectionMap = objGetOrSet(data, collectionPath, () => reactive(new Map()))
132132
// if it's a doc, return the specific doc
133-
if (docId) return dataCollectionMap.get(docId)
133+
if (isString(docId)) return dataCollectionMap.get(docId)
134134
// if it's a collection, we must return the dataCollectionMap but with applied query clauses
135135
// but remember, the return type MUST be a map with id as keys and the docs as value
136136
const clauses: Clauses = pluginModuleConfig

packages/test-utils/src/PluginLocalMock/CreatePlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { getPathWhereIdentifier } from '@magnetarjs/types'
99
import { filterDataPerClauses } from '@magnetarjs/utils'
1010
import { copy } from 'copy-anything'
1111
import { mapGetOrSet, objGetOrSet } from 'getorset-anything'
12-
import { isArray, isNumber, isPlainObject } from 'is-what'
12+
import { isArray, isNumber, isPlainObject, isString } from 'is-what'
1313
import { deleteActionFactory } from './actions/delete.js'
1414
import { deletePropActionFactory } from './actions/deleteProp.js'
1515
import { fetchActionFactory } from './actions/fetch.js'
@@ -123,7 +123,7 @@ export const CreatePlugin: MagnetarPlugin<StorePluginOptions> = (
123123
}: PluginModuleSetupPayload<StorePluginModuleConfig>): any => {
124124
const dataCollectionMap = objGetOrSet(data, collectionPath, () => new Map())
125125
// if it's a doc, return the specific doc
126-
if (docId) return dataCollectionMap.get(docId)
126+
if (isString(docId)) return dataCollectionMap.get(docId)
127127
// if it's a collection, we must return the dataCollectionMap but with applied query clauses
128128
// but remember, the return type MUST be a map with id as keys and the docs as value
129129
const clauses: Clauses = pluginModuleConfig

packages/test-utils/src/PluginRemoteMock/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import type {
2626
StreamResponse,
2727
} from '@magnetarjs/types'
2828
import { filterDataPerClauses } from '@magnetarjs/utils'
29-
import { isFullArray, isFullString, isNumber, isPromise } from 'is-what'
29+
import { isFullArray, isFullString, isNumber, isPromise, isString } from 'is-what'
3030
import { getProp } from 'path-to-prop'
3131
import { generateRandomId, pokedexMap, throwIfEmulatedError, waitMs } from '../helpers/index.js'
3232
import { RemoteStoreOptions, StorePluginModuleConfig } from './index.js'
@@ -115,7 +115,7 @@ function mockDataRetrieval(
115115
if (collectionPath === 'pokedex') {
116116
const _pokedexMap = pokedexMap()
117117

118-
if (docId) {
118+
if (isString(docId)) {
119119
const result = _pokedexMap.get(docId)
120120
return result ? [result] : []
121121
}

0 commit comments

Comments
 (0)