Skip to content

Allow nested arrays for IN queries #2346

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 3 commits into from
Nov 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1489,7 +1489,9 @@ export class Query implements firestore.Query {
}
fieldValue = this.firestore._dataConverter.parseQueryValue(
'Query.where',
value
value,
// We only allow nested arrays for IN queries.
/** allowArrays = */ operator === Operator.IN ? true : false
);
}
const filter = FieldFilter.create(fieldPath, operator, fieldValue);
Expand Down
24 changes: 20 additions & 4 deletions packages/firestore/src/api/user_data_converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,11 @@ enum UserDataSource {
* Indicates the source is a where clause, cursor bound, arrayUnion()
* element, etc. Of note, isWrite(source) will return false.
*/
Argument
Argument,
/**
* Indicates that the source is an Argument that allows for nested arrays.
*/
ArrayArgument
}

function isWrite(dataSource: UserDataSource): boolean {
Expand All @@ -144,6 +148,7 @@ function isWrite(dataSource: UserDataSource): boolean {
case UserDataSource.Update:
return true;
case UserDataSource.Argument:
case UserDataSource.ArrayArgument:
return false;
default:
throw fail(`Unexpected case for UserDataSource: ${dataSource}`);
Expand Down Expand Up @@ -478,10 +483,16 @@ export class UserDataConverter {
/**
* Parse a "query value" (e.g. value in a where filter or a value in a cursor
* bound).
*
* @param allowArrays Whether the dataContext should allow nested arrays.
*/
parseQueryValue(methodName: string, input: unknown): FieldValue {
parseQueryValue(
methodName: string,
input: unknown,
allowArrays = false
): FieldValue {
const context = new ParseContext(
UserDataSource.Argument,
allowArrays ? UserDataSource.ArrayArgument : UserDataSource.Argument,
methodName,
FieldPath.EMPTY_PATH
);
Expand Down Expand Up @@ -536,7 +547,12 @@ export class UserDataConverter {
if (input instanceof Array) {
// TODO(b/34871131): Include the path containing the array in the error
// message.
if (context.arrayElement) {
// Nested arrays are allowed when making IN queries, so we make the
// exception here.
if (
context.arrayElement &&
context.dataSource !== UserDataSource.ArrayArgument
) {
throw context.createError('Nested arrays are not supported');
}
return this.parseArray(input as unknown[], context);
Expand Down
51 changes: 25 additions & 26 deletions packages/firestore/test/integration/api/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import { EventsAccumulator } from '../util/events_accumulator';
import firebase from '../util/firebase_export';
import {
apiDescribe,
isRunningAgainstEmulator,
toChangesArray,
toDataArray,
withTestCollection,
Expand Down Expand Up @@ -799,14 +798,18 @@ apiDescribe('Queries', (persistence: boolean) => {
c: { zip: 98103 },
d: { zip: [98101] },
e: { zip: ['98101', { zip: 98101 }] },
f: { zip: { code: 500 } }
f: { zip: { code: 500 } },
g: { zip: [98101, 98102] }
};

await withTestCollection(persistence, testDocs, async coll => {
const snapshot = await coll.where('zip', 'in', [98101, 98103]).get();
const snapshot = await coll
.where('zip', 'in', [98101, 98103, [98101, 98102]])
.get();
expect(toDataArray(snapshot)).to.deep.equal([
{ zip: 98101 },
{ zip: 98103 }
{ zip: 98103 },
{ zip: [98101, 98102] }
]);

// With objects.
Expand All @@ -815,28 +818,24 @@ apiDescribe('Queries', (persistence: boolean) => {
});
});

// eslint-disable-next-line no-restricted-properties,
(isRunningAgainstEmulator() ? it : it.skip)(
'can use IN filters by document ID',
async () => {
const testDocs = {
aa: { key: 'aa' },
ab: { key: 'ab' },
ba: { key: 'ba' },
bb: { key: 'bb' }
};
await withTestCollection(persistence, testDocs, async coll => {
const snapshot = await coll
.where(FieldPath.documentId(), 'in', ['aa', 'ab'])
.get();

expect(toDataArray(snapshot)).to.deep.equal([
{ key: 'aa' },
{ key: 'ab' }
]);
});
}
);
it('can use IN filters by document ID', async () => {
const testDocs = {
aa: { key: 'aa' },
ab: { key: 'ab' },
ba: { key: 'ba' },
bb: { key: 'bb' }
};
await withTestCollection(persistence, testDocs, async coll => {
const snapshot = await coll
.where(FieldPath.documentId(), 'in', ['aa', 'ab'])
.get();

expect(toDataArray(snapshot)).to.deep.equal([
{ key: 'aa' },
{ key: 'ab' }
]);
});
});

it('can use array-contains-any filters', async () => {
const testDocs = {
Expand Down