Skip to content

Commit 63673f0

Browse files
committed
Add support for endpoint/token constructor
1 parent 65de021 commit 63673f0

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/collections/client.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
import { Db } from "./db";
16-
import { createAstraUri, createNamespace, parseUri } from "./utils";
16+
import {createAstraUri, createNamespace, getKeyspaceName, parseUri} from "./utils";
1717
import { HTTPClient } from "@/src/client";
1818
import { logger } from "@/src/logger";
1919
import { Collection } from "./collection";
@@ -155,9 +155,16 @@ export class Client {
155155
}
156156

157157
export class AstraDB extends Client {
158-
constructor(token: string, dbId: string, region: string, keyspace?: string) {
159-
const keyspaceName = keyspace || "default_keyspace";
160-
const endpoint = createAstraUri(dbId, region, keyspaceName);
161-
super(endpoint, keyspaceName, { isAstra: true, applicationToken: token });
158+
constructor(...args: any[]) {
159+
// token: string, path: string
160+
if (args.length === 2) {
161+
const keyspaceName = getKeyspaceName(args[1]);
162+
super(args[1], keyspaceName, { isAstra: true, applicationToken: args[0] });
163+
} else {
164+
// token: string, dbId: string, region: string, keyspace?: string
165+
const keyspaceName = args[3] || "default_keyspace";
166+
const endpoint = createAstraUri(args[1], args[2], keyspaceName);
167+
super(endpoint, keyspaceName, { isAstra: true, applicationToken: args[0] });
168+
}
162169
}
163170
}

src/collections/utils.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// limitations under the License.
1414

1515
import url from "url";
16-
import { logger } from "@/src/logger";
16+
import {logger} from "@/src/logger";
1717
import axios from "axios";
18-
import { HTTPClient, handleIfErrorResponse } from "@/src/client/httpClient";
18+
import {handleIfErrorResponse, HTTPClient} from "@/src/client/httpClient";
1919

2020
interface ParsedUri {
2121
baseUrl: string;
@@ -66,6 +66,15 @@ function getBaseAPIPath(pathFromUrl?: string | null) {
6666
: baseApiPath.substring(1, baseApiPath.length - 1);
6767
}
6868

69+
// Get the keyspace name from the path
70+
export function getKeyspaceName(pathFromUrl?: string | null) {
71+
if (!pathFromUrl) {
72+
return "";
73+
}
74+
const pathElements = pathFromUrl.split("/");
75+
return pathElements[pathElements.length - 1];
76+
}
77+
6978
/**
7079
* Create an Astra connection URI while connecting to Astra JSON API
7180
* @param databaseId the database id of the Astra database
@@ -269,3 +278,4 @@ function _updateHasKey(update: Record<string, any>, key: string) {
269278
}
270279
return false;
271280
}
281+

0 commit comments

Comments
 (0)