11import axios , { AxiosError } from 'axios' ;
22import {
3- DeleteRecordsConfig , EpsillaResponse , LoadDBPayload ,
3+ DeleteRecordsConfig , EpsillaResponse , Index , LoadDBPayload ,
44 PreviewConfig , QueryConfig , QueryPayload , TableField
55} from './models' ;
66
77export interface ClientConfig {
88 protocol ?: string ;
99 host ?: string ;
1010 port ?: number ;
11+ headers ?: { [ key : string ] : string } ;
1112}
1213
1314class EpsillaDB {
@@ -18,13 +19,16 @@ class EpsillaDB {
1819 private baseurl : string ;
1920 private headers : any ;
2021
21- constructor ( { protocol = 'http' , host = 'localhost' , port = 8888 } : ClientConfig = { } ) {
22+ constructor ( { protocol = 'http' , host = 'localhost' , port = 8888 , headers = { } } : ClientConfig = { } ) {
2223 this . protocol = protocol ;
2324 this . host = host ;
2425 this . port = port ;
2526 this . db = null ;
2627 this . baseurl = `${ this . protocol } ://${ this . host } :${ this . port } ` ;
2728 this . headers = { 'Content-type' : 'application/json' } ;
29+ if ( headers ) {
30+ this . headers = { ...this . headers , ...headers } ;
31+ }
2832 }
2933
3034 useDB ( dbName : string ) {
@@ -65,17 +69,21 @@ class EpsillaDB {
6569 }
6670 }
6771
68- async createTable ( tableName : string , fields : TableField [ ] ) : Promise < EpsillaResponse | Error > {
72+ async createTable ( tableName : string , fields : TableField [ ] , indices ?: Index [ ] ) : Promise < EpsillaResponse | Error > {
6973 if ( ! this . db ) {
7074 console . error ( '[ERROR] Please useDB() first!' ) ;
7175 return new Error ( '[ERROR] Please useDB() first!' ) ;
7276 }
7377 try {
78+ let payload : any = {
79+ name : tableName ,
80+ fields
81+ } ;
82+ if ( indices ) {
83+ payload [ 'indices' ] = indices ;
84+ }
7485 const response = await axios . post ( `${ this . baseurl } /api/${ this . db } /schema/tables` ,
75- {
76- name : tableName ,
77- fields
78- } ,
86+ payload ,
7987 { headers : this . headers }
8088 ) ;
8189 return response . data ;
0 commit comments