Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates firestore indexes api calls to apiv2 #4571

Merged
merged 2 commits into from May 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 17 additions & 38 deletions src/firestore/indexes.ts
@@ -1,6 +1,5 @@
import * as clc from "cli-color";

import * as api from "../api";
import { logger } from "../logger";
import * as utils from "../utils";
import * as validator from "./validator";
Expand All @@ -10,8 +9,12 @@ import * as Spec from "./indexes-spec";
import * as sort from "./indexes-sort";
import * as util from "./util";
import { promptOnce } from "../prompt";
import { firestoreOrigin } from "../api";
import { Client } from "../apiv2";

export class FirestoreIndexes {
apiClient = new Client({ urlPrefix: firestoreOrigin, apiVersion: "v1" });

/**
* Deploy an index specification to the specified project.
* @param options the CLI options.
Expand Down Expand Up @@ -163,13 +166,8 @@ export class FirestoreIndexes {
* @param project the Firebase project id.
*/
async listIndexes(project: string): Promise<API.Index[]> {
const url = `projects/${project}/databases/(default)/collectionGroups/-/indexes`;

const res = await api.request("GET", `/v1/${url}`, {
auth: true,
origin: api.firestoreOrigin,
});

const url = `/projects/${project}/databases/(default)/collectionGroups/-/indexes`;
const res = await this.apiClient.get<{ indexes?: API.Index[] }>(url);
const indexes = res.body.indexes;
if (!indexes) {
return [];
Expand Down Expand Up @@ -197,14 +195,10 @@ export class FirestoreIndexes {
*/
async listFieldOverrides(project: string): Promise<API.Field[]> {
const parent = `projects/${project}/databases/(default)/collectionGroups/-`;
const url = `${parent}/fields?filter=indexConfig.usesAncestorConfig=false`;

const res = await api.request("GET", `/v1/${url}`, {
auth: true,
origin: api.firestoreOrigin,
});
const url = `/${parent}/fields?filter=indexConfig.usesAncestorConfig=false`;

const fields = res.body.fields as API.Field[];
const res = await this.apiClient.get<{ fields?: API.Field[] }>(url);
const fields = res.body.fields;

// This should never be the case, since the API always returns the __default__
// configuration, but this is a defensive check.
Expand Down Expand Up @@ -370,7 +364,7 @@ export class FirestoreIndexes {
* @param spec the new field override specification.
*/
async patchField(project: string, spec: Spec.FieldOverride): Promise<any> {
const url = `projects/${project}/databases/(default)/collectionGroups/${spec.collectionGroup}/fields/${spec.fieldPath}`;
const url = `/projects/${project}/databases/(default)/collectionGroups/${spec.collectionGroup}/fields/${spec.fieldPath}`;

const indexes = spec.indexes.map((index) => {
return {
Expand All @@ -391,11 +385,7 @@ export class FirestoreIndexes {
},
};

await api.request("PATCH", `/v1/${url}`, {
auth: true,
origin: api.firestoreOrigin,
data,
});
await this.apiClient.patch(url, data);
}

/**
Expand All @@ -405,25 +395,17 @@ export class FirestoreIndexes {
const url = field.name;
const data = {};

return api.request("PATCH", "/v1/" + url + "?updateMask=indexConfig", {
auth: true,
origin: api.firestoreOrigin,
data,
});
return this.apiClient.patch(`/${url}`, data, { queryParams: { updateMask: "indexConfig" } });
}

/**
* Create a new index on the specified project.
*/
createIndex(project: string, index: Spec.Index): Promise<any> {
const url = `projects/${project}/databases/(default)/collectionGroups/${index.collectionGroup}/indexes`;
return api.request("POST", "/v1/" + url, {
auth: true,
data: {
fields: index.fields,
queryScope: index.queryScope,
},
origin: api.firestoreOrigin,
const url = `/projects/${project}/databases/(default)/collectionGroups/${index.collectionGroup}/indexes`;
return this.apiClient.post(url, {
fields: index.fields,
queryScope: index.queryScope,
});
}

Expand All @@ -432,10 +414,7 @@ export class FirestoreIndexes {
*/
deleteIndex(index: API.Index): Promise<any> {
const url = index.name!;
return api.request("DELETE", "/v1/" + url, {
auth: true,
origin: api.firestoreOrigin,
});
return this.apiClient.delete(`/${url}`);
}

/**
Expand Down