1515 * limitations under the License.
1616 */
1717
18+ import { getLocalStore } from '../core/firestore_client' ;
1819import { fieldPathFromDotSeparatedString } from '../lite-api/user_data_reader' ;
20+ import { localStoreConfigureFieldIndexes } from '../local/local_store_impl' ;
1921import {
2022 FieldIndex ,
2123 IndexKind ,
@@ -24,6 +26,7 @@ import {
2426} from '../model/field_index' ;
2527import { Code , FirestoreError } from '../util/error' ;
2628import { cast } from '../util/input_validation' ;
29+ import { logWarn } from '../util/log' ;
2730
2831import { ensureFirestoreConfigured , Firestore } from './database' ;
2932
@@ -150,17 +153,29 @@ export function setIndexConfiguration(
150153 jsonOrConfiguration : string | IndexConfiguration
151154) : Promise < void > {
152155 firestore = cast ( firestore , Firestore ) ;
153- ensureFirestoreConfigured ( firestore ) ;
156+ const client = ensureFirestoreConfigured ( firestore ) ;
154157
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 [ ] {
155173 const indexConfiguration =
156174 typeof jsonOrConfiguration === 'string'
157175 ? ( tryParseJson ( jsonOrConfiguration ) as IndexConfiguration )
158176 : jsonOrConfiguration ;
159177 const parsedIndexes : FieldIndex [ ] = [ ] ;
160178
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-
164179 if ( Array . isArray ( indexConfiguration . indexes ) ) {
165180 for ( const index of indexConfiguration . indexes ) {
166181 const collectionGroup = tryGetString ( index , 'collectionGroup' ) ;
@@ -194,9 +209,7 @@ export function setIndexConfiguration(
194209 ) ;
195210 }
196211 }
197-
198- // TODO(indexing): Configure indexes
199- return Promise . resolve ( ) ;
212+ return parsedIndexes ;
200213}
201214
202215function tryParseJson ( json : string ) : Record < string , unknown > {
@@ -205,7 +218,7 @@ function tryParseJson(json: string): Record<string, unknown> {
205218 } catch ( e ) {
206219 throw new FirestoreError (
207220 Code . INVALID_ARGUMENT ,
208- 'Failed to parse JSON:' + ( e as Error ) ?. message
221+ 'Failed to parse JSON: ' + ( e as Error ) ?. message
209222 ) ;
210223 }
211224}
0 commit comments