react-native-firebase/lib/modules/firestore/Query.js

476 lines
12 KiB
JavaScript
Raw Normal View History

/**
* @flow
* Query representation wrapper
*/
2017-11-17 11:08:04 +00:00
import DocumentSnapshot from './DocumentSnapshot';
import FieldPath from './FieldPath';
import QuerySnapshot from './QuerySnapshot';
import { buildNativeArray, buildTypeMap } from './utils/serialize';
import { getAppEventName, SharedEventEmitter } from '../../utils/events';
import { getLogger } from '../../utils/log';
import { firestoreAutoId, isFunction, isObject } from '../../utils';
import { getNativeModule } from '../../utils/native';
import type Firestore from './';
import type Path from './Path';
import type {
MetadataChanges,
QueryDirection,
QueryOperator,
GetOptions,
} from './types';
const DIRECTIONS: { [QueryDirection]: string } = {
ASC: 'ASCENDING',
asc: 'ASCENDING',
DESC: 'DESCENDING',
desc: 'DESCENDING',
};
const OPERATORS: { [QueryOperator]: string } = {
'=': 'EQUAL',
'==': 'EQUAL',
'>': 'GREATER_THAN',
'>=': 'GREATER_THAN_OR_EQUAL',
'<': 'LESS_THAN',
'<=': 'LESS_THAN_OR_EQUAL',
};
type NativeFieldPath = {|
elements?: string[],
string?: string,
type: 'fieldpath' | 'string',
2018-01-25 18:25:39 +00:00
|};
type FieldFilter = {|
fieldPath: NativeFieldPath,
operator: string,
value: any,
2018-01-25 18:25:39 +00:00
|};
type FieldOrder = {|
direction: string,
fieldPath: NativeFieldPath,
2018-01-25 18:25:39 +00:00
|};
type QueryOptions = {
endAt?: any[],
endBefore?: any[],
limit?: number,
offset?: number,
selectFields?: string[],
startAfter?: any[],
startAt?: any[],
2018-01-25 18:25:39 +00:00
};
2018-01-25 18:25:39 +00:00
export type ObserverOnError = Object => void;
export type ObserverOnNext = QuerySnapshot => void;
export type Observer = {
error?: ObserverOnError,
next: ObserverOnNext,
2018-01-25 18:25:39 +00:00
};
2018-01-25 18:25:39 +00:00
const buildNativeFieldPath = (
fieldPath: string | FieldPath
): NativeFieldPath => {
if (fieldPath instanceof FieldPath) {
return {
elements: fieldPath._segments,
type: 'fieldpath',
};
}
return {
string: fieldPath,
type: 'string',
};
};
/**
* @class Query
*/
export default class Query {
_fieldFilters: FieldFilter[];
_fieldOrders: FieldOrder[];
_firestore: Firestore;
_iid: number;
_queryOptions: QueryOptions;
_referencePath: Path;
constructor(
firestore: Firestore,
path: Path,
2018-01-25 18:25:39 +00:00
fieldFilters?: FieldFilter[],
fieldOrders?: FieldOrder[],
2018-01-25 18:25:39 +00:00
queryOptions?: QueryOptions
) {
this._fieldFilters = fieldFilters || [];
this._fieldOrders = fieldOrders || [];
this._firestore = firestore;
this._queryOptions = queryOptions || {};
this._referencePath = path;
}
get firestore(): Firestore {
return this._firestore;
}
endAt(...snapshotOrVarArgs: any[]): Query {
const options = {
...this._queryOptions,
endAt: this._buildOrderByOption(snapshotOrVarArgs),
};
return new Query(
this.firestore,
this._referencePath,
this._fieldFilters,
this._fieldOrders,
2018-01-25 18:25:39 +00:00
options
);
}
endBefore(...snapshotOrVarArgs: any[]): Query {
const options = {
...this._queryOptions,
endBefore: this._buildOrderByOption(snapshotOrVarArgs),
};
return new Query(
this.firestore,
this._referencePath,
this._fieldFilters,
this._fieldOrders,
2018-01-25 18:25:39 +00:00
options
);
}
get(options?: GetOptions): Promise<QuerySnapshot> {
if (options) {
if (!isObject(options)) {
return Promise.reject(
new Error('Query.get failed: First argument must be an object.')
);
} else if (
options.source &&
(options.source !== 'default' &&
options.source !== 'server' &&
options.source !== 'cache')
) {
return Promise.reject(
new Error(
'Query.get failed: GetOptions.source must be one of `default`, `server` or `cache`.'
)
);
}
}
return getNativeModule(this._firestore)
.collectionGet(
this._referencePath.relativeName,
this._fieldFilters,
this._fieldOrders,
this._queryOptions,
options
)
.then(nativeData => new QuerySnapshot(this._firestore, this, nativeData));
}
limit(limit: number): Query {
// TODO: Validation
// validate.isInteger('n', n);
const options = {
...this._queryOptions,
limit,
};
return new Query(
this.firestore,
this._referencePath,
this._fieldFilters,
this._fieldOrders,
2018-01-25 18:25:39 +00:00
options
);
}
onSnapshot(
optionsOrObserverOrOnNext: MetadataChanges | Observer | ObserverOnNext,
observerOrOnNextOrOnError?: Observer | ObserverOnNext | ObserverOnError,
2018-01-25 18:25:39 +00:00
onError?: ObserverOnError
) {
let observer: Observer;
let metadataChanges = {};
// Called with: onNext, ?onError
if (isFunction(optionsOrObserverOrOnNext)) {
if (observerOrOnNextOrOnError && !isFunction(observerOrOnNextOrOnError)) {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: Second argument must be a valid function.'
);
}
2018-02-17 12:55:19 +00:00
// $FlowExpectedError: Not coping with the overloaded method signature
observer = {
next: optionsOrObserverOrOnNext,
error: observerOrOnNextOrOnError,
};
2018-01-25 18:25:39 +00:00
} else if (
optionsOrObserverOrOnNext &&
isObject(optionsOrObserverOrOnNext)
) {
// Called with: Observer
if (optionsOrObserverOrOnNext.next) {
if (isFunction(optionsOrObserverOrOnNext.next)) {
2018-01-25 18:25:39 +00:00
if (
optionsOrObserverOrOnNext.error &&
!isFunction(optionsOrObserverOrOnNext.error)
) {
throw new Error(
'Query.onSnapshot failed: Observer.error must be a valid function.'
);
}
2018-02-17 12:55:19 +00:00
// $FlowExpectedError: Not coping with the overloaded method signature
observer = {
next: optionsOrObserverOrOnNext.next,
error: optionsOrObserverOrOnNext.error,
};
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: Observer.next must be a valid function.'
);
}
2018-01-25 18:25:39 +00:00
} else if (
Object.prototype.hasOwnProperty.call(
optionsOrObserverOrOnNext,
'includeMetadataChanges'
2018-01-25 18:25:39 +00:00
)
) {
metadataChanges = optionsOrObserverOrOnNext;
// Called with: Options, onNext, ?onError
if (isFunction(observerOrOnNextOrOnError)) {
if (onError && !isFunction(onError)) {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: Third argument must be a valid function.'
);
}
2018-02-17 12:55:19 +00:00
// $FlowExpectedError: Not coping with the overloaded method signature
observer = {
next: observerOrOnNextOrOnError,
error: onError,
};
2018-01-25 18:25:39 +00:00
// Called with Options, Observer
} else if (
observerOrOnNextOrOnError &&
isObject(observerOrOnNextOrOnError) &&
observerOrOnNextOrOnError.next
) {
if (isFunction(observerOrOnNextOrOnError.next)) {
2018-01-25 18:25:39 +00:00
if (
observerOrOnNextOrOnError.error &&
!isFunction(observerOrOnNextOrOnError.error)
) {
throw new Error(
'Query.onSnapshot failed: Observer.error must be a valid function.'
);
}
observer = {
next: observerOrOnNextOrOnError.next,
error: observerOrOnNextOrOnError.error,
};
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: Observer.next must be a valid function.'
);
}
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: Second argument must be a function or observer.'
);
}
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: First argument must be a function, observer or options.'
);
}
} else {
2018-01-25 18:25:39 +00:00
throw new Error(
'Query.onSnapshot failed: Called with invalid arguments.'
);
}
const listenerId = firestoreAutoId();
2018-01-25 18:25:39 +00:00
const listener = nativeQuerySnapshot => {
const querySnapshot = new QuerySnapshot(
this._firestore,
this,
nativeQuerySnapshot
);
observer.next(querySnapshot);
};
// Listen to snapshot events
SharedEventEmitter.addListener(
getAppEventName(this._firestore, `onQuerySnapshot:${listenerId}`),
2018-01-25 18:25:39 +00:00
listener
);
// Listen for snapshot error events
if (observer.error) {
SharedEventEmitter.addListener(
getAppEventName(this._firestore, `onQuerySnapshotError:${listenerId}`),
2018-01-25 18:25:39 +00:00
observer.error
);
}
// Add the native listener
2018-01-25 18:25:39 +00:00
getNativeModule(this._firestore).collectionOnSnapshot(
this._referencePath.relativeName,
this._fieldFilters,
this._fieldOrders,
this._queryOptions,
listenerId,
metadataChanges
2018-01-25 18:25:39 +00:00
);
// Return an unsubscribe method
return this._offCollectionSnapshot.bind(this, listenerId, listener);
}
2018-01-25 18:25:39 +00:00
orderBy(
fieldPath: string | FieldPath,
directionStr?: QueryDirection = 'asc'
2018-01-25 18:25:39 +00:00
): Query {
// TODO: Validation
// validate.isFieldPath('fieldPath', fieldPath);
// validate.isOptionalFieldOrder('directionStr', directionStr);
2018-03-01 19:04:13 +00:00
if (
this._queryOptions.startAt ||
this._queryOptions.startAfter ||
this._queryOptions.endAt ||
this._queryOptions.endBefore
) {
2018-01-25 18:25:39 +00:00
throw new Error(
'Cannot specify an orderBy() constraint after calling ' +
'startAt(), startAfter(), endBefore() or endAt().'
);
}
const newOrder: FieldOrder = {
direction: DIRECTIONS[directionStr],
fieldPath: buildNativeFieldPath(fieldPath),
};
const combinedOrders = this._fieldOrders.concat(newOrder);
return new Query(
this.firestore,
this._referencePath,
this._fieldFilters,
combinedOrders,
2018-01-25 18:25:39 +00:00
this._queryOptions
);
}
startAfter(...snapshotOrVarArgs: any[]): Query {
const options = {
...this._queryOptions,
startAfter: this._buildOrderByOption(snapshotOrVarArgs),
};
return new Query(
this.firestore,
this._referencePath,
this._fieldFilters,
this._fieldOrders,
2018-01-25 18:25:39 +00:00
options
);
}
startAt(...snapshotOrVarArgs: any[]): Query {
const options = {
...this._queryOptions,
startAt: this._buildOrderByOption(snapshotOrVarArgs),
};
return new Query(
this.firestore,
this._referencePath,
this._fieldFilters,
this._fieldOrders,
2018-01-25 18:25:39 +00:00
options
);
}
2018-01-25 18:25:39 +00:00
where(
fieldPath: string | FieldPath,
opStr: QueryOperator,
2018-01-25 18:25:39 +00:00
value: any
): Query {
// TODO: Validation
// validate.isFieldPath('fieldPath', fieldPath);
// validate.isFieldFilter('fieldFilter', opStr, value);
const nativeValue = buildTypeMap(value);
const newFilter: FieldFilter = {
fieldPath: buildNativeFieldPath(fieldPath),
operator: OPERATORS[opStr],
value: nativeValue,
};
const combinedFilters = this._fieldFilters.concat(newFilter);
return new Query(
this.firestore,
this._referencePath,
combinedFilters,
this._fieldOrders,
2018-01-25 18:25:39 +00:00
this._queryOptions
);
}
/**
* INTERNALS
*/
_buildOrderByOption(snapshotOrVarArgs: any[]) {
// TODO: Validation
let values;
2018-01-25 18:25:39 +00:00
if (
snapshotOrVarArgs.length === 1 &&
snapshotOrVarArgs[0] instanceof DocumentSnapshot
) {
const docSnapshot: DocumentSnapshot = snapshotOrVarArgs[0];
values = [];
for (let i = 0; i < this._fieldOrders.length; i++) {
const fieldOrder = this._fieldOrders[i];
2018-01-25 18:25:39 +00:00
if (
fieldOrder.fieldPath.type === 'string' &&
fieldOrder.fieldPath.string
) {
values.push(docSnapshot.get(fieldOrder.fieldPath.string));
} else if (fieldOrder.fieldPath.fieldpath) {
const fieldPath = new FieldPath(...fieldOrder.fieldPath.fieldpath);
values.push(docSnapshot.get(fieldPath));
}
}
} else {
values = snapshotOrVarArgs;
}
return buildNativeArray(values);
}
/**
* Remove query snapshot listener
* @param listener
*/
_offCollectionSnapshot(listenerId: string, listener: Function) {
getLogger(this._firestore).info('Removing onQuerySnapshot listener');
2018-01-25 18:25:39 +00:00
SharedEventEmitter.removeListener(
getAppEventName(this._firestore, `onQuerySnapshot:${listenerId}`),
listener
);
SharedEventEmitter.removeListener(
getAppEventName(this._firestore, `onQuerySnapshotError:${listenerId}`),
listener
);
getNativeModule(this._firestore).collectionOffSnapshot(
this._referencePath.relativeName,
this._fieldFilters,
this._fieldOrders,
this._queryOptions,
listenerId
);
}
}