|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import { fieldPathFromDotSeparatedString } from '../lite-api/user_data_reader'; |
| 19 | +import { FieldIndex, Kind, Segment } from '../model/field_index'; |
| 20 | +import { Code, FirestoreError } from '../util/error'; |
| 21 | +import { cast } from '../util/input_validation'; |
| 22 | + |
| 23 | +import { ensureFirestoreConfigured, Firestore } from './database'; |
| 24 | + |
| 25 | +export { |
| 26 | + connectFirestoreEmulator, |
| 27 | + EmulatorMockTokenOptions |
| 28 | +} from '../lite-api/database'; |
| 29 | + |
| 30 | +// TODO(indexing): Remove "@internal" from the API. |
| 31 | + |
| 32 | +/** |
| 33 | + * A single field element in an index configuration. |
| 34 | + * |
| 35 | + * @internal |
| 36 | + */ |
| 37 | +export interface IndexField { |
| 38 | + /** The field path to index. */ |
| 39 | + readonly fieldPath: string; |
| 40 | + /** |
| 41 | + * What type of array index to create. Set to `CONTAINS` for `array-contains` |
| 42 | + * and `array-contains-any` indexes. |
| 43 | + * |
| 44 | + * Only one of `arrayConfig` or `order` should be set; |
| 45 | + */ |
| 46 | + readonly arrayConfig?: 'CONTAINS'; |
| 47 | + /** |
| 48 | + * What type of array index to create. Set to `ASCENDING` or 'DESCENDING` for |
| 49 | + * `==`, `!=`, `<=`, `<=`, `in` and `not-in` filters. |
| 50 | + * |
| 51 | + * Only one of `arrayConfig` or `order` should be set. |
| 52 | + */ |
| 53 | + readonly order?: 'ASCENDING' | 'DESCENDING'; |
| 54 | + |
| 55 | + [key: string]: unknown; |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * The SDK definition of a Firestore index. |
| 60 | + * |
| 61 | + * @internal |
| 62 | + */ |
| 63 | +export interface Index { |
| 64 | + /** The ID of the collection to index. */ |
| 65 | + readonly collectionGroup: string; |
| 66 | + /** A list of fields to index. */ |
| 67 | + readonly fields?: IndexField[]; |
| 68 | + |
| 69 | + [key: string]: unknown; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * A list of Firestore indexes to speed up local query execution. |
| 74 | + * |
| 75 | + * See {@link https://firebase.google.com/docs/reference/firestore/indexes/#json_format | JSON Format} |
| 76 | + * for a description of the format of the index definition. |
| 77 | + * |
| 78 | + * @internal |
| 79 | + */ |
| 80 | +export interface IndexConfiguration { |
| 81 | + /** A list of all Firestore indexes. */ |
| 82 | + readonly indexes?: Index[]; |
| 83 | + |
| 84 | + [key: string]: unknown; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * Configures indexing for local query execution. Any previous index |
| 89 | + * configuration is overridden. The `Promise` resolves once the index |
| 90 | + * configuration has been persisted. |
| 91 | + * |
| 92 | + * The index entries themselves are created asynchronously. You can continue to |
| 93 | + * use queries that require indexing even if the indices are not yet available. |
| 94 | + * Query execution will automatically start using the index once the index |
| 95 | + * entries have been written. |
| 96 | + * |
| 97 | + * Indexes are only supported with IndexedDb persistence. Invoke either |
| 98 | + * `enableIndexedDbPersistence()` or `enableMultiTabIndexedDbPersistence()` |
| 99 | + * before setting an index configuration. If IndexedDb is not enabled, any |
| 100 | + * index configuration is ignored. |
| 101 | + * |
| 102 | + * @internal |
| 103 | + * @param firestore - The {@link Firestore} instance to configure indexes for. |
| 104 | + * @param configuration -The index definition. |
| 105 | + * @throws FirestoreError if the JSON format is invalid. |
| 106 | + * @returns A `Promise` that resolves once all indices are successfully |
| 107 | + * configured. |
| 108 | + */ |
| 109 | +export function setIndexConfiguration( |
| 110 | + firestore: Firestore, |
| 111 | + configuration: IndexConfiguration |
| 112 | +): Promise<void>; |
| 113 | +/** |
| 114 | + * Configures indexing for local query execution. Any previous index |
| 115 | + * configuration is overridden. The `Promise` resolves once the index |
| 116 | + * configuration has been persisted. |
| 117 | + * |
| 118 | + * The index entries themselves are created asynchronously. You can continue to |
| 119 | + * use queries that require indexing even if the indices are not yet available. |
| 120 | + * Query execution will automatically start using the index once the index |
| 121 | + * entries have been written. |
| 122 | + * |
| 123 | + * Indexes are only supported with IndexedDb persistence. Invoke either |
| 124 | + * `enableIndexedDbPersistence()` or `enableMultiTabIndexedDbPersistence()` |
| 125 | + * before setting an index configuration. If IndexedDb is not enabled, any |
| 126 | + * index configuration is ignored. |
| 127 | + * |
| 128 | + * The method accepts the JSON format exported by the Firebase CLI (`firebase |
| 129 | + * firestore:indexes`). If the JSON format is invalid, this method throws an |
| 130 | + * error. |
| 131 | + * |
| 132 | + * @internal |
| 133 | + * @param firestore - The {@link Firestore} instance to configure indexes for. |
| 134 | + * @param json -The JSON format exported by the Firebase CLI. |
| 135 | + * @throws FirestoreError if the JSON format is invalid. |
| 136 | + * @returns A `Promise` that resolves once all indices are successfully |
| 137 | + * configured. |
| 138 | + */ |
| 139 | +export function setIndexConfiguration( |
| 140 | + firestore: Firestore, |
| 141 | + json: string |
| 142 | +): Promise<void>; |
| 143 | +export function setIndexConfiguration( |
| 144 | + firestore: Firestore, |
| 145 | + jsonOrConfiguration: string | IndexConfiguration |
| 146 | +): Promise<void> { |
| 147 | + firestore = cast(firestore, Firestore); |
| 148 | + ensureFirestoreConfigured(firestore); |
| 149 | + |
| 150 | + const indexConfiguration = |
| 151 | + typeof jsonOrConfiguration === 'string' |
| 152 | + ? (tryParseJson(jsonOrConfiguration) as IndexConfiguration) |
| 153 | + : jsonOrConfiguration; |
| 154 | + const parsedIndexes: FieldIndex[] = []; |
| 155 | + |
| 156 | + // PORTING NOTE: We don't return an error if the user has not enabled |
| 157 | + // persistence since `enableIndexeddbPersistence()` can fail on the Web. |
| 158 | + |
| 159 | + if (Array.isArray(indexConfiguration.indexes)) { |
| 160 | + for (const index of indexConfiguration.indexes) { |
| 161 | + const collectionGroup = tryGetString(index, 'collectionGroup'); |
| 162 | + |
| 163 | + const segments: Segment[] = []; |
| 164 | + if (Array.isArray(index.fields)) { |
| 165 | + for (const field of index.fields) { |
| 166 | + const fieldPathString = tryGetString(field, 'fieldPath'); |
| 167 | + const fieldPath = fieldPathFromDotSeparatedString( |
| 168 | + 'setIndexConfiguration', |
| 169 | + fieldPathString |
| 170 | + ); |
| 171 | + |
| 172 | + if (field.arrayConfig === 'CONTAINS') { |
| 173 | + segments.push(new Segment(fieldPath, Kind.CONTAINS)); |
| 174 | + } else if (field.order === 'ASCENDING') { |
| 175 | + segments.push(new Segment(fieldPath, Kind.ASCENDING)); |
| 176 | + } else if (field.order === 'DESCENDING') { |
| 177 | + segments.push(new Segment(fieldPath, Kind.DESCENDING)); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + parsedIndexes.push( |
| 183 | + new FieldIndex(FieldIndex.UNKNOWN_ID, collectionGroup, segments) |
| 184 | + ); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + // TODO(indexing): Configure indexes |
| 189 | + return Promise.resolve(); |
| 190 | +} |
| 191 | + |
| 192 | +function tryParseJson(json: string): Record<string, unknown> { |
| 193 | + try { |
| 194 | + return JSON.parse(json); |
| 195 | + } catch (e) { |
| 196 | + throw new FirestoreError( |
| 197 | + Code.INVALID_ARGUMENT, |
| 198 | + 'Failed to parse JSON:' + e.message |
| 199 | + ); |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +function tryGetString(data: Record<string, unknown>, property: string): string { |
| 204 | + if (typeof data[property] !== 'string') { |
| 205 | + throw new FirestoreError( |
| 206 | + Code.INVALID_ARGUMENT, |
| 207 | + 'Missing string value for: ' + property |
| 208 | + ); |
| 209 | + } |
| 210 | + return data[property] as string; |
| 211 | +} |
0 commit comments