15
15
* limitations under the License.
16
16
*/
17
17
18
+ import { getLocalStore } from '../core/firestore_client' ;
18
19
import { fieldPathFromDotSeparatedString } from '../lite-api/user_data_reader' ;
20
+ import { localStoreConfigureFieldIndexes } from '../local/local_store_impl' ;
19
21
import {
20
22
FieldIndex ,
21
23
IndexKind ,
@@ -24,6 +26,7 @@ import {
24
26
} from '../model/field_index' ;
25
27
import { Code , FirestoreError } from '../util/error' ;
26
28
import { cast } from '../util/input_validation' ;
29
+ import { logWarn } from '../util/log' ;
27
30
28
31
import { ensureFirestoreConfigured , Firestore } from './database' ;
29
32
@@ -150,17 +153,29 @@ export function setIndexConfiguration(
150
153
jsonOrConfiguration : string | IndexConfiguration
151
154
) : Promise < void > {
152
155
firestore = cast ( firestore , Firestore ) ;
153
- ensureFirestoreConfigured ( firestore ) ;
156
+ const client = ensureFirestoreConfigured ( firestore ) ;
154
157
158
+ // PORTING NOTE: We don't return an error if the user has not enabled
159
+ // persistence since `enableIndexeddbPersistence()` can fail on the Web.
160
+ if ( ! client . offlineComponents ?. indexBackfillerScheduler ) {
161
+ logWarn ( 'Cannot enable indexes when persistence is disabled' ) ;
162
+ return Promise . resolve ( ) ;
163
+ }
164
+ const parsedIndexes = parseIndexes ( jsonOrConfiguration ) ;
165
+ return getLocalStore ( client ) . then ( localStore =>
166
+ localStoreConfigureFieldIndexes ( localStore , parsedIndexes )
167
+ ) ;
168
+ }
169
+
170
+ export function parseIndexes (
171
+ jsonOrConfiguration : string | IndexConfiguration
172
+ ) : FieldIndex [ ] {
155
173
const indexConfiguration =
156
174
typeof jsonOrConfiguration === 'string'
157
175
? ( tryParseJson ( jsonOrConfiguration ) as IndexConfiguration )
158
176
: jsonOrConfiguration ;
159
177
const parsedIndexes : FieldIndex [ ] = [ ] ;
160
178
161
- // PORTING NOTE: We don't return an error if the user has not enabled
162
- // persistence since `enableIndexeddbPersistence()` can fail on the Web.
163
-
164
179
if ( Array . isArray ( indexConfiguration . indexes ) ) {
165
180
for ( const index of indexConfiguration . indexes ) {
166
181
const collectionGroup = tryGetString ( index , 'collectionGroup' ) ;
@@ -194,9 +209,7 @@ export function setIndexConfiguration(
194
209
) ;
195
210
}
196
211
}
197
-
198
- // TODO(indexing): Configure indexes
199
- return Promise . resolve ( ) ;
212
+ return parsedIndexes ;
200
213
}
201
214
202
215
function tryParseJson ( json : string ) : Record < string , unknown > {
@@ -205,7 +218,7 @@ function tryParseJson(json: string): Record<string, unknown> {
205
218
} catch ( e ) {
206
219
throw new FirestoreError (
207
220
Code . INVALID_ARGUMENT ,
208
- 'Failed to parse JSON:' + ( e as Error ) ?. message
221
+ 'Failed to parse JSON: ' + ( e as Error ) ?. message
209
222
) ;
210
223
}
211
224
}
0 commit comments