@boltic/sdk 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/sdk.mjs CHANGED
@@ -2296,6 +2296,209 @@ class ColumnResource extends BaseResource {
2296
2296
  return tableInfo?.id || null;
2297
2297
  }
2298
2298
  }
2299
+ const INDEX_ENDPOINTS = {
2300
+ create: {
2301
+ path: "/tables/indexes/{table_id}",
2302
+ method: "POST",
2303
+ authenticated: true
2304
+ },
2305
+ list: {
2306
+ path: "/tables/indexes/{table_id}/list",
2307
+ method: "POST",
2308
+ authenticated: true
2309
+ },
2310
+ delete: {
2311
+ path: "/tables/indexes/{table_id}",
2312
+ method: "DELETE",
2313
+ authenticated: true
2314
+ }
2315
+ };
2316
+ const buildIndexEndpointPath = (endpoint, params = {}) => {
2317
+ let path = endpoint.path;
2318
+ Object.entries(params).forEach(([key, value]) => {
2319
+ path = path.replace(`{${key}}`, encodeURIComponent(value));
2320
+ });
2321
+ const unreplacedParams = path.match(/\{([^}]+)\}/g);
2322
+ if (unreplacedParams) {
2323
+ throw new Error(`Missing path parameters: ${unreplacedParams.join(", ")}`);
2324
+ }
2325
+ return path;
2326
+ };
2327
+ class IndexesApiClient {
2328
+ constructor(apiKey, config = {}) {
2329
+ this.config = { apiKey, ...config };
2330
+ this.httpAdapter = createHttpAdapter();
2331
+ const environment = config.environment || "prod";
2332
+ const region = config.region || "asia-south1";
2333
+ this.baseURL = this.getBaseURL(environment, region);
2334
+ }
2335
+ getBaseURL(environment, region) {
2336
+ const regionConfig = REGION_CONFIGS[region];
2337
+ if (!regionConfig) {
2338
+ throw new Error(`Unsupported region: ${region}`);
2339
+ }
2340
+ const envConfig = regionConfig[environment];
2341
+ if (!envConfig) {
2342
+ throw new Error(
2343
+ `Unsupported environment: ${environment} for region: ${region}`
2344
+ );
2345
+ }
2346
+ return `${envConfig.baseURL}/v1`;
2347
+ }
2348
+ async addIndex(tableId, request) {
2349
+ try {
2350
+ const endpoint = INDEX_ENDPOINTS.create;
2351
+ const url = `${this.baseURL}${buildIndexEndpointPath(endpoint, { table_id: tableId })}`;
2352
+ const response = await this.httpAdapter.request({
2353
+ url,
2354
+ method: endpoint.method,
2355
+ headers: this.buildHeaders(),
2356
+ data: request,
2357
+ timeout: this.config.timeout
2358
+ });
2359
+ return response.data;
2360
+ } catch (error) {
2361
+ return this.formatErrorResponse(error);
2362
+ }
2363
+ }
2364
+ async listIndexes(tableId, query) {
2365
+ try {
2366
+ const endpoint = INDEX_ENDPOINTS.list;
2367
+ const url = `${this.baseURL}${buildIndexEndpointPath(endpoint, { table_id: tableId })}`;
2368
+ const response = await this.httpAdapter.request({
2369
+ url,
2370
+ method: endpoint.method,
2371
+ headers: this.buildHeaders(),
2372
+ data: query,
2373
+ timeout: this.config.timeout
2374
+ });
2375
+ return response.data;
2376
+ } catch (error) {
2377
+ return this.formatErrorResponse(error);
2378
+ }
2379
+ }
2380
+ async deleteIndex(tableId, request) {
2381
+ try {
2382
+ const endpoint = INDEX_ENDPOINTS.delete;
2383
+ const url = `${this.baseURL}${buildIndexEndpointPath(endpoint, { table_id: tableId })}`;
2384
+ const response = await this.httpAdapter.request({
2385
+ url,
2386
+ method: endpoint.method,
2387
+ headers: this.buildHeaders(),
2388
+ data: request,
2389
+ timeout: this.config.timeout
2390
+ });
2391
+ return response.data;
2392
+ } catch (error) {
2393
+ return this.formatErrorResponse(error);
2394
+ }
2395
+ }
2396
+ buildHeaders() {
2397
+ return {
2398
+ "Content-Type": "application/json",
2399
+ Accept: "application/json",
2400
+ "x-boltic-token": this.config.apiKey
2401
+ };
2402
+ }
2403
+ formatErrorResponse(error) {
2404
+ if (this.config.debug) {
2405
+ console.error("Indexes API Error:", error);
2406
+ }
2407
+ if (error && typeof error === "object" && "response" in error) {
2408
+ const apiError = error;
2409
+ if (apiError.response?.data?.error) {
2410
+ return apiError.response.data;
2411
+ }
2412
+ return {
2413
+ error: {
2414
+ code: "API_ERROR",
2415
+ message: error.message || "Unknown API error",
2416
+ meta: [`Status: ${apiError.response?.status || "unknown"}`]
2417
+ }
2418
+ };
2419
+ }
2420
+ if (error && typeof error === "object" && "message" in error) {
2421
+ return {
2422
+ error: {
2423
+ code: "CLIENT_ERROR",
2424
+ message: error.message,
2425
+ meta: ["Client-side error occurred"]
2426
+ }
2427
+ };
2428
+ }
2429
+ return {
2430
+ error: {
2431
+ code: "UNKNOWN_ERROR",
2432
+ message: "An unexpected error occurred",
2433
+ meta: ["Unknown error type"]
2434
+ }
2435
+ };
2436
+ }
2437
+ }
2438
+ class IndexResource {
2439
+ constructor(client) {
2440
+ this.client = client;
2441
+ const config = client.getConfig();
2442
+ this.apiClient = new IndexesApiClient(config.apiKey, {
2443
+ environment: config.environment,
2444
+ region: config.region,
2445
+ timeout: config.timeout,
2446
+ debug: config.debug,
2447
+ retryAttempts: config.retryAttempts,
2448
+ retryDelay: config.retryDelay,
2449
+ headers: config.headers
2450
+ });
2451
+ this.tableResource = new TableResource(client);
2452
+ }
2453
+ async resolveTableId(tableName) {
2454
+ const tableResult = await this.tableResource.findByName(tableName);
2455
+ if (!tableResult.data) throw new Error(`Table not found: ${tableName}`);
2456
+ return tableResult.data.id;
2457
+ }
2458
+ async addIndex(tableName, request) {
2459
+ try {
2460
+ const tableId = await this.resolveTableId(tableName);
2461
+ return await this.apiClient.addIndex(tableId, request);
2462
+ } catch (error) {
2463
+ return {
2464
+ error: {
2465
+ code: "CLIENT_ERROR",
2466
+ message: error?.message || "Failed to add index",
2467
+ meta: ["IndexResource.addIndex"]
2468
+ }
2469
+ };
2470
+ }
2471
+ }
2472
+ async listIndexes(tableName, query) {
2473
+ try {
2474
+ const tableId = await this.resolveTableId(tableName);
2475
+ return await this.apiClient.listIndexes(tableId, query);
2476
+ } catch (error) {
2477
+ return {
2478
+ error: {
2479
+ code: "CLIENT_ERROR",
2480
+ message: error?.message || "Failed to list indexes",
2481
+ meta: ["IndexResource.listIndexes"]
2482
+ }
2483
+ };
2484
+ }
2485
+ }
2486
+ async deleteIndex(tableName, indexName) {
2487
+ try {
2488
+ const tableId = await this.resolveTableId(tableName);
2489
+ const request = { index_name: indexName };
2490
+ return await this.apiClient.deleteIndex(tableId, request);
2491
+ } catch (error) {
2492
+ return {
2493
+ error: {
2494
+ code: "CLIENT_ERROR",
2495
+ message: error?.message || "Failed to delete index",
2496
+ meta: ["IndexResource.deleteIndex"]
2497
+ }
2498
+ };
2499
+ }
2500
+ }
2501
+ }
2299
2502
  const RECORD_ENDPOINTS = {
2300
2503
  insert: {
2301
2504
  path: "/tables/{table_id}/records",
@@ -3762,6 +3965,7 @@ class BolticClient {
3762
3965
  this.columnResource = new ColumnResource(this.baseClient);
3763
3966
  this.recordResource = new RecordResource(this.baseClient);
3764
3967
  this.sqlResource = new SqlResource(this.baseClient);
3968
+ this.indexResource = new IndexResource(this.baseClient);
3765
3969
  this.currentDatabase = {
3766
3970
  databaseName: "Default"
3767
3971
  };
@@ -3795,6 +3999,14 @@ class BolticClient {
3795
3999
  delete: (tableName, columnName) => this.columnResource.delete(tableName, columnName)
3796
4000
  };
3797
4001
  }
4002
+ // Direct index operations
4003
+ get indexes() {
4004
+ return {
4005
+ addIndex: (tableName, payload) => this.indexResource.addIndex(tableName, payload),
4006
+ listIndexes: (tableName, query) => this.indexResource.listIndexes(tableName, query),
4007
+ deleteIndex: (tableName, indexName) => this.indexResource.deleteIndex(tableName, indexName)
4008
+ };
4009
+ }
3798
4010
  // Fluent table operations
3799
4011
  table(name) {
3800
4012
  const tableBuilder = createTableBuilder({ name });
@@ -3827,6 +4039,12 @@ class BolticClient {
3827
4039
  record: () => createRecordBuilder({
3828
4040
  tableName,
3829
4041
  recordResource: this.recordResource
4042
+ }),
4043
+ // Indexes - Method 2: Function chaining under from(tableName)
4044
+ indexes: () => ({
4045
+ addIndex: (payload) => this.indexResource.addIndex(tableName, payload),
4046
+ listIndexes: (query) => this.indexResource.listIndexes(tableName, query),
4047
+ deleteIndex: (indexName) => this.indexResource.deleteIndex(tableName, indexName)
3830
4048
  })
3831
4049
  };
3832
4050
  }
@@ -3937,6 +4155,7 @@ class BolticClient {
3937
4155
  this.columnResource = new ColumnResource(this.baseClient);
3938
4156
  this.recordResource = new RecordResource(this.baseClient);
3939
4157
  this.sqlResource = new SqlResource(this.baseClient);
4158
+ this.indexResource = new IndexResource(this.baseClient);
3940
4159
  }
3941
4160
  // Security methods to prevent API key exposure
3942
4161
  toString() {