Skip to content
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

fix: #116 #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
73 changes: 72 additions & 1 deletion packages/core/test/external/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pokedex, waitMs } from '@magnetarjs/test-utils'
import test from 'ava'
import { createMagnetarInstance } from '../helpers/createMagnetarInstance'
import { pokedex, waitMs } from '@magnetarjs/test-utils'

test('fetch (collection)', async (t) => {
/// 'fetch' resolves once all stores have given a response with data
Expand Down Expand Up @@ -219,6 +219,77 @@ test('fetch (collection) where-filter: array-contains-any', async (t) => {
}
})

test('fetch (collection) query-filter: and', async (t) => {
const { pokedexModule } = createMagnetarInstance()
try {
const queryModuleRef = pokedexModule.query({
and: [
['type', 'array-contains', 'Fire'],
['base.Speed', '>=', 100],
],
})
await queryModuleRef.fetch({ force: true })
const actual = [...queryModuleRef.data.values()]
const expected = [pokedex(6), pokedex(38), pokedex(78)]
t.deepEqual(actual, expected as any)
} catch (error) {
t.fail(JSON.stringify(error))
}
})

test('fetch (collection) query-filter: or', async (t) => {
const { pokedexModule } = createMagnetarInstance()
try {
const queryModuleRef = pokedexModule.query({
or: [
['name', '==', 'Bulbasaur'],
['name', '==', 'Ivysaur'],
['name', '==', 'Venusaur'],
],
})
await queryModuleRef.fetch({ force: true })
const actual = [...queryModuleRef.data.values()]
const expected = [pokedex(1), pokedex(2), pokedex(3)]
t.deepEqual(actual, expected as any)
} catch (error) {
t.fail(JSON.stringify(error))
}
})

test('fetch (collection) query-filter: combine or, and', async (t) => {
const { pokedexModule } = createMagnetarInstance()
try {
const queryModuleRef = pokedexModule.query({
or: [
{
and: [
['name', '==', 'Bulbasaur'],
['base.Speed', '==', 45],
],
},
{
and: [
['name', '==', 'Ivysaur'],
['base.Speed', '==', 60],
],
},
{
and: [
['name', '==', 'Venusaur'],
['base.Speed', '==', 80],
],
},
],
})
await queryModuleRef.fetch({ force: true })
const actual = [...queryModuleRef.data.values()]
const expected = [pokedex(1), pokedex(2), pokedex(3)]
t.deepEqual(actual, expected as any)
} catch (error) {
t.fail(JSON.stringify(error))
}
})

test('fetch (collection) compound queries', async (t) => {
const { pokedexModule } = createMagnetarInstance()
try {
Expand Down
25 changes: 16 additions & 9 deletions packages/plugin-firestore-admin/src/helpers/getFirestore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DocMetadata, QueryClause } from '@magnetarjs/types'
import { isWhereClause } from '@magnetarjs/types'
import type { FirestoreModuleConfig } from '@magnetarjs/utils-firestore'
import type {
CollectionReference,
Expand All @@ -10,7 +11,7 @@ import type {
WriteBatch,
} from 'firebase-admin/firestore'
import { FieldValue, Filter } from 'firebase-admin/firestore'
import { isArray, isNumber } from 'is-what'
import { isNumber } from 'is-what'
export type {
CollectionReference,
DocumentReference,
Expand All @@ -36,16 +37,22 @@ function queryToFilter(
queryClause: QueryClause
): ReturnType<(typeof Filter)['or']> | ReturnType<(typeof Filter)['and']> {
if ('and' in queryClause) {
if (isArray(queryClause.and)) {
return Filter.and(...queryClause.and.map((whereClause) => Filter.where(...whereClause)))
}
return queryToFilter(queryClause.and)
return Filter.and(
...queryClause.and.map((whereClauseOrQueryClause) =>
isWhereClause(whereClauseOrQueryClause)
? Filter.where(...whereClauseOrQueryClause)
: queryToFilter(queryClause)
)
)
}
// if ('or' in queryClause)
if (isArray(queryClause.or)) {
return Filter.or(...queryClause.or.map((whereClause) => Filter.where(...whereClause)))
}
return queryToFilter(queryClause.or)
return Filter.or(
...queryClause.or.map((whereClauseOrQueryClause) =>
isWhereClause(whereClauseOrQueryClause)
? Filter.where(...whereClauseOrQueryClause)
: queryToFilter(queryClause)
)
)
}

/**
Expand Down
93 changes: 92 additions & 1 deletion packages/plugin-firestore-admin/test/external/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { pokedex } from '@magnetarjs/test-utils'
import test from 'ava'
import { createMagnetarInstance } from '../helpers/createMagnetarInstance'
import { pokedex } from '@magnetarjs/test-utils'

{
const testName = 'fetch (collection)'
Expand Down Expand Up @@ -276,6 +276,97 @@ import { pokedex } from '@magnetarjs/test-utils'
}
})
}
{
const testName = 'fetch (collection) query-filter: and'
test(testName, async (t) => {
const { pokedexModule } = await createMagnetarInstance('read')
try {
const queryModuleRef = pokedexModule
.query({
and: [
['type', 'array-contains', 'Fire'],
['base.Speed', '>=', 100],
],
})
.orderBy('base.Speed', 'asc')
await queryModuleRef.fetch({ force: true }, { onError: 'stop' })
const actual = [...queryModuleRef.data.values()]
const expected = [pokedex(6), pokedex(38), pokedex(78)]
t.deepEqual(actual, expected as any)
// also check the collection without query
const actualDocCountWithoutQuery = pokedexModule.data.size
const expectedDocCountWithoutQuery = expected.length
t.deepEqual(actualDocCountWithoutQuery, expectedDocCountWithoutQuery)
} catch (error) {
t.fail(JSON.stringify(error))
}
})
}
{
const testName = 'fetch (collection) query-filter: or'
test(testName, async (t) => {
const { pokedexModule } = await createMagnetarInstance('read')
try {
const queryModuleRef = pokedexModule.query({
or: [
['name', '==', 'Bulbasaur'],
['name', '==', 'Ivysaur'],
['name', '==', 'Venusaur'],
],
})
await queryModuleRef.fetch({ force: true }, { onError: 'stop' })
const actual = [...queryModuleRef.data.values()]
const expected = [pokedex(1), pokedex(2), pokedex(3)]
t.deepEqual(actual, expected as any)
// also check the collection without query
const actualDocCountWithoutQuery = pokedexModule.data.size
const expectedDocCountWithoutQuery = expected.length
t.deepEqual(actualDocCountWithoutQuery, expectedDocCountWithoutQuery)
} catch (error) {
t.fail(JSON.stringify(error))
}
})
}
{
const testName = 'fetch (collection) query-filter: combine or, and'
test(testName, async (t) => {
const { pokedexModule } = await createMagnetarInstance('read')
try {
const queryModuleRef = pokedexModule.query({
or: [
{
and: [
['name', '==', 'Bulbasaur'],
['base.Speed', '==', 45],
],
},
{
and: [
['name', '==', 'Ivysaur'],
['base.Speed', '==', 60],
],
},
{
and: [
['name', '==', 'Venusaur'],
['base.Speed', '==', 80],
],
},
],
})
await queryModuleRef.fetch({ force: true }, { onError: 'stop' })
const actual = [...queryModuleRef.data.values()]
const expected = [pokedex(1), pokedex(2), pokedex(3)]
t.deepEqual(actual, expected as any)
// also check the collection without query
const actualDocCountWithoutQuery = pokedexModule.data.size
const expectedDocCountWithoutQuery = expected.length
t.deepEqual(actualDocCountWithoutQuery, expectedDocCountWithoutQuery)
} catch (error) {
t.fail(JSON.stringify(error))
}
})
}
{
const testName = 'fetch (collection) compound queries'
test(testName, async (t) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { initializeApp, cert } from 'firebase-admin/app'
import { cert, initializeApp } from 'firebase-admin/app'
import { getFirestore } from 'firebase-admin/firestore'

const config = {
Expand Down
35 changes: 20 additions & 15 deletions packages/plugin-firestore/src/helpers/getFirestore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { DocMetadata, QueryClause } from '@magnetarjs/types'
import { DocMetadata, isWhereClause, QueryClause } from '@magnetarjs/types'
import type { FirestoreModuleConfig } from '@magnetarjs/utils-firestore'
import type {
CollectionReference,
DocumentSnapshot,
Firestore,
Query,
QueryCompositeFilterConstraint,
QueryDocumentSnapshot,
} from 'firebase/firestore'
import {
Expand All @@ -18,22 +19,26 @@ import {
startAfter,
where,
} from 'firebase/firestore'
import { isArray, isNumber } from 'is-what'
import { isNumber } from 'is-what'

function applyQuery(q: CollectionReference | Query, queryClause: QueryClause): Query {
function applyQuery(queryClause: QueryClause): QueryCompositeFilterConstraint {
if ('and' in queryClause) {
if (isArray(queryClause.and)) {
return query(q, and(...queryClause.and.map((whereClause) => where(...whereClause))))
}
return applyQuery(q, queryClause.and)
}
if ('or' in queryClause) {
if (isArray(queryClause.or)) {
return query(q, or(...queryClause.or.map((whereClause) => where(...whereClause))))
}
return applyQuery(q, queryClause.or)
return and(
...queryClause.and.map((whereClauseOrQueryClause) =>
isWhereClause(whereClauseOrQueryClause)
? where(...whereClauseOrQueryClause)
: applyQuery(whereClauseOrQueryClause)
)
)
}
return q
// if ('or' in queryClause)
return or(
...queryClause.or.map((whereClauseOrQueryClause) =>
isWhereClause(whereClauseOrQueryClause)
? where(...whereClauseOrQueryClause)
: applyQuery(whereClauseOrQueryClause)
)
)
}

/**
Expand All @@ -49,7 +54,7 @@ export function getQueryInstance(
? collectionGroup(db, collectionPath.split('*/')[1])
: collection(db, collectionPath)
for (const queryClause of config.query || []) {
q = applyQuery(q, queryClause)
q = query(q, applyQuery(queryClause))
}
for (const whereClause of config.where || []) {
q = query(q, where(...whereClause))
Expand Down
Loading