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