Skip to content

Commit

Permalink
Merge pull request #17 from epsilla-cloud/cloud-table
Browse files Browse the repository at this point in the history
support create/drop table with Epsilla Cloud;
  • Loading branch information
richard-epsilla authored Dec 5, 2023
2 parents 8cf4c0d + 91fe44d commit bdd80d2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "epsillajs",
"version": "0.2.0",
"version": "0.2.1",
"description": "A JS library to connect Epsilla vector database",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
41 changes: 37 additions & 4 deletions src/cloud.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import axios, { AxiosError } from 'axios';
import { DeleteRecordsConfig, EpsillaResponse, PreviewConfig, QueryConfig } from './models';
import { DeleteRecordsConfig, EpsillaResponse, PreviewConfig, QueryConfig, TableField } from './models';

export interface CloudClientConfig {
projectID: string;
apiKey: string;
}

const projectHost = 'https://dispatch.epsilla.com/api/v2/project/';
const vectordbPath = 'api/v2/project/${projectID}/vectordb';
const projectHost = 'https://dispatch.epsilla.com/api/v3/project';
const vectordbPath = 'api/v3/project/${projectID}/vectordb';
/**
* Cloud client config:
* - projectID: The project ID you used for your cloud client.
Expand Down Expand Up @@ -48,7 +48,7 @@ export class VectorDB {
async connect() {
try {
// Get public endpoint with project id.
const response = await axios.get(`${projectHost}${this.projectID}/vectordb/${this.dbID}`, { headers: this.headers });
const response = await axios.get(`${projectHost}/${this.projectID}/vectordb/${this.dbID}`, { headers: this.headers });
this.host = 'https://' + response.data.result.public_endpoint;

return response.data;
Expand All @@ -57,6 +57,39 @@ export class VectorDB {
}
}

async createTable(tableName: string, fields: TableField[]) {
try {
const response = await axios.post(
`${projectHost}/${this.projectID}/vectordb/${this.dbID}/table/create`,
{
table_name: tableName,
fields
},
{ headers: this.headers }
);
return response.data;
} catch (err) {
return (err as AxiosError).response?.data as EpsillaResponse;
}
}

async dropTable(tableName: string) {
try {
const response = await axios.delete(
`${projectHost}/${this.projectID}/vectordb/${this.dbID}/table/delete`,
{
params: {
table_name: tableName
},
headers: this.headers
}
);
return response.data;
} catch (err) {
return (err as AxiosError).response?.data as EpsillaResponse;
}
}

private checkConnection() {
if (!this.host) {
console.error('[ERROR] Please connect to vector database first!');
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { CloudClientConfig, EpsillaCloud, VectorDB } from './cloud';
import { EpsillaQueryResult, EpsillaResponse, LoadDBPayload, QueryConfig, QueryPayload } from './models';
import {
EpsillaQueryResult, EpsillaResponse, LoadDBPayload,
QueryConfig, QueryPayload, TableField
} from './models';
import EpsillaDB, { ClientConfig } from './vectordb';

export {
ClientConfig, CloudClientConfig,
QueryConfig as CloudQueryConfig, EpsillaCloud, EpsillaDB,
EpsillaQueryResult, EpsillaResponse, LoadDBPayload, QueryPayload, VectorDB
EpsillaQueryResult, EpsillaResponse, LoadDBPayload, QueryPayload, TableField, VectorDB
};

8 changes: 8 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ export interface LoadDBPayload {
walEnabled?: boolean;
}

export interface TableField {
name: string;
dataType: string;
primaryKey?: boolean;
dimensions?: number;
metricType?: string;
}

export interface SparseVector {
indices: number[];
values: number[];
Expand Down
5 changes: 3 additions & 2 deletions src/vectordb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios, { AxiosError } from 'axios';
import {
DeleteRecordsConfig, EpsillaResponse, LoadDBPayload, PreviewConfig, QueryConfig, QueryPayload
DeleteRecordsConfig, EpsillaResponse, LoadDBPayload,
PreviewConfig, QueryConfig, QueryPayload, TableField
} from './models';

export interface ClientConfig {
Expand Down Expand Up @@ -64,7 +65,7 @@ class EpsillaDB {
}
}

async createTable(tableName: string, fields: any[]): Promise<EpsillaResponse | Error> {
async createTable(tableName: string, fields: TableField[]): Promise<EpsillaResponse | Error> {
if (!this.db) {
console.error('[ERROR] Please useDB() first!');
return new Error('[ERROR] Please useDB() first!');
Expand Down

0 comments on commit bdd80d2

Please sign in to comment.