@onchaindb/sdk 0.4.4 → 1.0.0
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/.claude/settings.local.json +10 -2
- package/README.md +422 -355
- package/dist/batch.d.ts +1 -10
- package/dist/batch.d.ts.map +1 -1
- package/dist/batch.js +4 -26
- package/dist/batch.js.map +1 -1
- package/dist/client.d.ts +29 -43
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +199 -323
- package/dist/client.js.map +1 -1
- package/dist/database.d.ts +14 -131
- package/dist/database.d.ts.map +1 -1
- package/dist/database.js +35 -131
- package/dist/database.js.map +1 -1
- package/dist/index.d.ts +6 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -15
- package/dist/index.js.map +1 -1
- package/dist/query-sdk/ConditionBuilder.d.ts +3 -11
- package/dist/query-sdk/ConditionBuilder.d.ts.map +1 -1
- package/dist/query-sdk/ConditionBuilder.js +10 -48
- package/dist/query-sdk/ConditionBuilder.js.map +1 -1
- package/dist/query-sdk/NestedBuilders.d.ts +33 -30
- package/dist/query-sdk/NestedBuilders.d.ts.map +1 -1
- package/dist/query-sdk/NestedBuilders.js +46 -43
- package/dist/query-sdk/NestedBuilders.js.map +1 -1
- package/dist/query-sdk/QueryBuilder.d.ts +4 -2
- package/dist/query-sdk/QueryBuilder.d.ts.map +1 -1
- package/dist/query-sdk/QueryBuilder.js +47 -169
- package/dist/query-sdk/QueryBuilder.js.map +1 -1
- package/dist/query-sdk/QueryResult.d.ts +0 -38
- package/dist/query-sdk/QueryResult.d.ts.map +1 -1
- package/dist/query-sdk/QueryResult.js +1 -227
- package/dist/query-sdk/QueryResult.js.map +1 -1
- package/dist/query-sdk/index.d.ts +1 -1
- package/dist/query-sdk/index.d.ts.map +1 -1
- package/dist/query-sdk/index.js.map +1 -1
- package/dist/query-sdk/operators.d.ts +32 -28
- package/dist/query-sdk/operators.d.ts.map +1 -1
- package/dist/query-sdk/operators.js +45 -155
- package/dist/query-sdk/operators.js.map +1 -1
- package/dist/types.d.ts +153 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/jest.config.js +4 -0
- package/package.json +1 -1
- package/skills.md +0 -1
- package/src/client.ts +243 -745
- package/src/database.ts +70 -493
- package/src/index.ts +40 -193
- package/src/query-sdk/ConditionBuilder.ts +37 -89
- package/src/query-sdk/NestedBuilders.ts +90 -92
- package/src/query-sdk/QueryBuilder.ts +59 -218
- package/src/query-sdk/QueryResult.ts +4 -330
- package/src/query-sdk/README.md +214 -583
- package/src/query-sdk/index.ts +1 -1
- package/src/query-sdk/operators.ts +91 -200
- package/src/query-sdk/tests/FieldConditionBuilder.test.ts +70 -71
- package/src/query-sdk/tests/LogicalOperator.test.ts +43 -82
- package/src/query-sdk/tests/NestedBuilders.test.ts +229 -309
- package/src/query-sdk/tests/QueryBuilder.test.ts +5 -5
- package/src/query-sdk/tests/QueryResult.test.ts +41 -435
- package/src/query-sdk/tests/comprehensive.test.ts +4 -185
- package/src/tests/client-requests.test.ts +280 -0
- package/src/tests/client-validation.test.ts +80 -0
- package/src/types.ts +229 -8
- package/src/batch.ts +0 -257
- package/src/query-sdk/dist/ConditionBuilder.d.ts +0 -22
- package/src/query-sdk/dist/ConditionBuilder.js +0 -90
- package/src/query-sdk/dist/FieldConditionBuilder.d.ts +0 -1
- package/src/query-sdk/dist/FieldConditionBuilder.js +0 -6
- package/src/query-sdk/dist/NestedBuilders.d.ts +0 -43
- package/src/query-sdk/dist/NestedBuilders.js +0 -144
- package/src/query-sdk/dist/OnChainDB.d.ts +0 -19
- package/src/query-sdk/dist/OnChainDB.js +0 -123
- package/src/query-sdk/dist/QueryBuilder.d.ts +0 -70
- package/src/query-sdk/dist/QueryBuilder.js +0 -295
- package/src/query-sdk/dist/QueryResult.d.ts +0 -52
- package/src/query-sdk/dist/QueryResult.js +0 -293
- package/src/query-sdk/dist/SelectionBuilder.d.ts +0 -20
- package/src/query-sdk/dist/SelectionBuilder.js +0 -80
- package/src/query-sdk/dist/adapters/HttpClientAdapter.d.ts +0 -27
- package/src/query-sdk/dist/adapters/HttpClientAdapter.js +0 -170
- package/src/query-sdk/dist/index.d.ts +0 -36
- package/src/query-sdk/dist/index.js +0 -27
- package/src/query-sdk/dist/operators.d.ts +0 -56
- package/src/query-sdk/dist/operators.js +0 -289
- package/src/query-sdk/dist/tests/setup.d.ts +0 -15
- package/src/query-sdk/dist/tests/setup.js +0 -46
- package/src/query-sdk/jest.config.js +0 -25
- package/src/query-sdk/package.json +0 -46
- package/src/query-sdk/tests/aggregations.test.ts +0 -653
- package/src/query-sdk/tests/integration.test.ts +0 -608
- package/src/query-sdk/tests/operators.test.ts +0 -327
- package/src/query-sdk/tests/unit.test.ts +0 -794
- package/src/query-sdk/tsconfig.json +0 -26
- package/src/query-sdk/yarn.lock +0 -3092
package/src/types.ts
CHANGED
|
@@ -7,12 +7,68 @@ export interface OnChainDBConfig {
|
|
|
7
7
|
apiKey?: string; // Deprecated: use appKey instead
|
|
8
8
|
appKey?: string; // App API key for write operations (X-App-Key header)
|
|
9
9
|
userKey?: string; // User API key for Auto-Pay (X-User-Key header)
|
|
10
|
+
agentKey?: string; // Agent Key with Pay permission (X-Agent-Key header)
|
|
10
11
|
appId?: string; // Application ID for automatic root building
|
|
11
12
|
timeout?: number;
|
|
12
13
|
retryCount?: number;
|
|
13
14
|
retryDelay?: number;
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
// ============================================================================
|
|
18
|
+
// App Key Management Types
|
|
19
|
+
// ============================================================================
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Permissions that can be granted to an app key.
|
|
23
|
+
* A key with "Pay" permission is an Agent Key — it can pay other apps inline
|
|
24
|
+
* when accessing their data using USDC via EIP-3009.
|
|
25
|
+
*/
|
|
26
|
+
export type AppKeyPermission = 'Read' | 'Write' | 'Admin' | 'Pay';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Spend limits for Agent Keys (keys with "Pay" permission).
|
|
30
|
+
* All fields are optional — omitting a field means no limit on that dimension.
|
|
31
|
+
*/
|
|
32
|
+
export interface PayLimits {
|
|
33
|
+
/** Optional expiry time (ISO 8601). Key stops working after this. */
|
|
34
|
+
expires_at?: string;
|
|
35
|
+
/** Max USDC per single transaction, in base units (6 decimals). e.g. 5000000 = 5 USDC */
|
|
36
|
+
max_payment_per_tx?: number;
|
|
37
|
+
/** Lifetime spend cap in USDC base units. */
|
|
38
|
+
spend_allowance?: number;
|
|
39
|
+
/** Whitelist of target app IDs this key may pay. Omit to allow all apps. */
|
|
40
|
+
allowed_target_apps?: string[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Request body for POST /api/apps/:app_id/regenerate-key
|
|
45
|
+
*/
|
|
46
|
+
export interface CreateKeyRequest {
|
|
47
|
+
name?: string;
|
|
48
|
+
permissions?: AppKeyPermission[];
|
|
49
|
+
/** Only used when permissions includes "Pay". Ignored otherwise. */
|
|
50
|
+
pay_limits?: PayLimits;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* App key entry returned by POST /api/apps/:app_id/keys/list
|
|
55
|
+
*/
|
|
56
|
+
export interface AppKeyInfo {
|
|
57
|
+
key_hash: string;
|
|
58
|
+
name: string;
|
|
59
|
+
permissions: AppKeyPermission[];
|
|
60
|
+
/** ISO 8601 expiry (only present on keys with pay_limits.expires_at set) */
|
|
61
|
+
expires_at?: string;
|
|
62
|
+
/** Max USDC per transaction in base units (6 decimals) */
|
|
63
|
+
max_payment_per_tx?: number;
|
|
64
|
+
/** Lifetime spend cap in USDC base units */
|
|
65
|
+
spend_allowance?: number;
|
|
66
|
+
/** Cumulative USDC spent so far in base units — increments after each on-chain payment */
|
|
67
|
+
spend_allowance_used?: number;
|
|
68
|
+
/** Whitelisted target app IDs (undefined = no restriction) */
|
|
69
|
+
allowed_target_apps?: string[];
|
|
70
|
+
}
|
|
71
|
+
|
|
16
72
|
export interface StoreRequest {
|
|
17
73
|
root?: string; // Format: "app::collection" or just "collection" for system
|
|
18
74
|
collection?: string; // Collection name (will be combined with appId if provided)
|
|
@@ -315,17 +371,9 @@ export interface PricingQuoteResponse {
|
|
|
315
371
|
|
|
316
372
|
// Re-export from database.ts for consistency
|
|
317
373
|
export type {
|
|
318
|
-
Collection,
|
|
319
|
-
CollectionSchema,
|
|
320
|
-
FieldDefinition,
|
|
321
|
-
FieldValidation,
|
|
322
|
-
Relationship,
|
|
323
374
|
Index,
|
|
324
375
|
IndexOptions,
|
|
325
|
-
IndexStatus,
|
|
326
|
-
IndexStatistics,
|
|
327
376
|
PriceConfig,
|
|
328
|
-
CollectionMetadata
|
|
329
377
|
} from './database';
|
|
330
378
|
|
|
331
379
|
/**
|
|
@@ -519,10 +567,183 @@ export interface SyncCollectionResult {
|
|
|
519
567
|
field: string;
|
|
520
568
|
type: string;
|
|
521
569
|
}[];
|
|
570
|
+
sharding_configured?: boolean;
|
|
522
571
|
success: boolean;
|
|
523
572
|
errors?: string[];
|
|
524
573
|
}
|
|
525
574
|
|
|
575
|
+
// ============================================================================
|
|
576
|
+
// Collection Management
|
|
577
|
+
// ============================================================================
|
|
578
|
+
|
|
579
|
+
export interface UpdateCollectionRequest {
|
|
580
|
+
description?: string;
|
|
581
|
+
public?: boolean;
|
|
582
|
+
default_sort_column?: string;
|
|
583
|
+
retention_days?: number | null;
|
|
584
|
+
collection_type?: 'local' | 'offline' | 'api' | 'hybrid';
|
|
585
|
+
api_config?: ApiConfig;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
export interface ApiConfig {
|
|
589
|
+
base_url: string;
|
|
590
|
+
authentication: any;
|
|
591
|
+
query_mapping: any;
|
|
592
|
+
response_mapping: any;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
export interface CollectionResponse {
|
|
596
|
+
id: string;
|
|
597
|
+
name: string;
|
|
598
|
+
namespace: string;
|
|
599
|
+
primary_column: string;
|
|
600
|
+
sort_column?: string;
|
|
601
|
+
status: string;
|
|
602
|
+
collection_type: string;
|
|
603
|
+
document_count: number;
|
|
604
|
+
size_kb: number;
|
|
605
|
+
created_at: string;
|
|
606
|
+
last_modified: string;
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
export interface SetRetentionRequest {
|
|
610
|
+
retention_days: number | null;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
export interface RetentionConfig {
|
|
614
|
+
collection: string;
|
|
615
|
+
retention_days?: number;
|
|
616
|
+
monthly_cost_per_kb: number;
|
|
617
|
+
last_cleanup?: string;
|
|
618
|
+
status: string;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
export interface CollectionRetentionCost {
|
|
622
|
+
collection: string;
|
|
623
|
+
retention_days?: number;
|
|
624
|
+
size_kb: number;
|
|
625
|
+
monthly_cost_per_kb: number;
|
|
626
|
+
monthly_cost_tia: number;
|
|
627
|
+
projected_size_kb: number;
|
|
628
|
+
projected_monthly_cost_tia: number;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
export interface RetentionCostResponse {
|
|
632
|
+
app_id: string;
|
|
633
|
+
collections: CollectionRetentionCost[];
|
|
634
|
+
total_monthly_cost_tia: number;
|
|
635
|
+
projected_next_month_cost_tia: number;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
// ============================================================================
|
|
639
|
+
// SQL Interface
|
|
640
|
+
// ============================================================================
|
|
641
|
+
|
|
642
|
+
export interface SqlQueryRequest {
|
|
643
|
+
sql: string;
|
|
644
|
+
include_history?: boolean;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
export interface SqlQueryResponse {
|
|
648
|
+
data: any[];
|
|
649
|
+
count: number;
|
|
650
|
+
query: string;
|
|
651
|
+
app_id: string;
|
|
652
|
+
collection: string;
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
export interface SqlInsertRequest {
|
|
656
|
+
sql: string;
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// ============================================================================
|
|
660
|
+
// Predefined Queries
|
|
661
|
+
// ============================================================================
|
|
662
|
+
|
|
663
|
+
export interface QueryParameter {
|
|
664
|
+
/** Parameter name (used in URL query params) */
|
|
665
|
+
name: string;
|
|
666
|
+
/** Field path in the query (e.g., "market" or "price.$gte") */
|
|
667
|
+
field_path: string;
|
|
668
|
+
/** Default value if not provided */
|
|
669
|
+
default?: any;
|
|
670
|
+
/** Whether this parameter is required (no default, must be provided) */
|
|
671
|
+
required?: boolean;
|
|
672
|
+
/** Description of the parameter */
|
|
673
|
+
description?: string;
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
export interface QueryDefinition {
|
|
677
|
+
name: string;
|
|
678
|
+
source_collection: string;
|
|
679
|
+
base_query: any;
|
|
680
|
+
parameters?: QueryParameter[];
|
|
681
|
+
created_at: string;
|
|
682
|
+
description?: string;
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
export interface CreateQueryRequest {
|
|
686
|
+
name: string;
|
|
687
|
+
source_collection: string;
|
|
688
|
+
base_query: any;
|
|
689
|
+
parameters?: QueryParameter[];
|
|
690
|
+
description?: string;
|
|
691
|
+
version?: number;
|
|
692
|
+
response_schema?: any;
|
|
693
|
+
sample_response?: any;
|
|
694
|
+
example_request?: any;
|
|
695
|
+
example_response?: any;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
export interface CreateQueryResponse {
|
|
699
|
+
success: boolean;
|
|
700
|
+
message: string;
|
|
701
|
+
query_name: string;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
export interface QueryDataResponse {
|
|
705
|
+
success: boolean;
|
|
706
|
+
query_name: string;
|
|
707
|
+
data: any[];
|
|
708
|
+
count: number;
|
|
709
|
+
query_time_ms: number;
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// ============================================================================
|
|
713
|
+
// Views
|
|
714
|
+
// ============================================================================
|
|
715
|
+
|
|
716
|
+
export interface SqlCreateViewRequest {
|
|
717
|
+
sql: string;
|
|
718
|
+
refresh_mode?: 'live' | 'lazy';
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
export interface ViewDataResponse<T = any> {
|
|
722
|
+
success: boolean;
|
|
723
|
+
view_name: string;
|
|
724
|
+
data: T[];
|
|
725
|
+
count: number;
|
|
726
|
+
query_time_ms: number;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
export interface ViewQueryRequest {
|
|
730
|
+
find?: any;
|
|
731
|
+
select?: string[];
|
|
732
|
+
sort_by?: string[];
|
|
733
|
+
limit?: number;
|
|
734
|
+
offset?: number;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// ============================================================================
|
|
738
|
+
// API Collections
|
|
739
|
+
// ============================================================================
|
|
740
|
+
|
|
741
|
+
export interface CreateApiCollectionRequest {
|
|
742
|
+
name: string;
|
|
743
|
+
description: string;
|
|
744
|
+
api_config: ApiConfig;
|
|
745
|
+
}
|
|
746
|
+
|
|
526
747
|
/**
|
|
527
748
|
* Base document fields (auto-added when useBaseFields is true)
|
|
528
749
|
*/
|
package/src/batch.ts
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
import { OnChainDBClient } from './client';
|
|
2
|
-
import { StoreRequest, StoreResponse, TransactionStatus } from './types';
|
|
3
|
-
import { EventEmitter } from 'eventemitter3';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Batch operations for OnChainDB
|
|
7
|
-
*
|
|
8
|
-
* Allows efficient batch insertion with transaction management
|
|
9
|
-
* and progress tracking for large datasets.
|
|
10
|
-
*/
|
|
11
|
-
export class BatchOperations extends EventEmitter {
|
|
12
|
-
constructor(private client: OnChainDBClient) {
|
|
13
|
-
super();
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Batch store multiple records
|
|
18
|
-
*
|
|
19
|
-
* @param requests - Array of store requests
|
|
20
|
-
* @param options - Batch options including payment configuration
|
|
21
|
-
* @returns Promise resolving to array of store responses
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```typescript
|
|
25
|
-
* const batch = new BatchOperations(db);
|
|
26
|
-
*
|
|
27
|
-
* const results = await batch.store([
|
|
28
|
-
* { data: { message: 'Tweet 1' }, collection: 'tweets' },
|
|
29
|
-
* { data: { message: 'Tweet 2' }, collection: 'tweets' },
|
|
30
|
-
* { data: { message: 'Tweet 3' }, collection: 'tweets' }
|
|
31
|
-
* ], {
|
|
32
|
-
* concurrency: 5,
|
|
33
|
-
* waitForConfirmation: true,
|
|
34
|
-
* paymentOptions: {
|
|
35
|
-
* userWallet: keplrWallet,
|
|
36
|
-
* brokerAddress: 'celestia1xyz...'
|
|
37
|
-
* }
|
|
38
|
-
* });
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
async store(
|
|
42
|
-
requests: StoreRequest[],
|
|
43
|
-
options: {
|
|
44
|
-
concurrency?: number;
|
|
45
|
-
waitForConfirmation?: boolean;
|
|
46
|
-
onProgress?: (completed: number, total: number) => void;
|
|
47
|
-
paymentOptions?: {
|
|
48
|
-
userWallet?: any;
|
|
49
|
-
brokerAddress?: string;
|
|
50
|
-
};
|
|
51
|
-
} = {}
|
|
52
|
-
): Promise<StoreResponse[]> {
|
|
53
|
-
const {
|
|
54
|
-
concurrency = 10,
|
|
55
|
-
waitForConfirmation = false,
|
|
56
|
-
onProgress,
|
|
57
|
-
paymentOptions
|
|
58
|
-
} = options;
|
|
59
|
-
|
|
60
|
-
const results: StoreResponse[] = [];
|
|
61
|
-
const errors: Array<{ index: number; error: Error }> = [];
|
|
62
|
-
let completed = 0;
|
|
63
|
-
|
|
64
|
-
// Process requests in batches with concurrency limit
|
|
65
|
-
for (let i = 0; i < requests.length; i += concurrency) {
|
|
66
|
-
const batch = requests.slice(i, i + concurrency);
|
|
67
|
-
const batchPromises = batch.map(async (request, batchIndex) => {
|
|
68
|
-
const globalIndex = i + batchIndex;
|
|
69
|
-
try {
|
|
70
|
-
const result = await this.client.store(request, undefined, waitForConfirmation);
|
|
71
|
-
results[globalIndex] = result;
|
|
72
|
-
completed++;
|
|
73
|
-
onProgress?.(completed, requests.length);
|
|
74
|
-
this.emit('progress', { completed, total: requests.length, success: true });
|
|
75
|
-
return result;
|
|
76
|
-
} catch (error) {
|
|
77
|
-
errors.push({ index: globalIndex, error: error as Error });
|
|
78
|
-
completed++;
|
|
79
|
-
onProgress?.(completed, requests.length);
|
|
80
|
-
this.emit('progress', { completed, total: requests.length, success: false, error });
|
|
81
|
-
throw error;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// Wait for current batch to complete before starting next
|
|
86
|
-
await Promise.allSettled(batchPromises);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (errors.length > 0) {
|
|
90
|
-
this.emit('batchComplete', { results, errors });
|
|
91
|
-
throw new Error(`Batch operation failed: ${errors.length}/${requests.length} requests failed`);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
this.emit('batchComplete', { results, errors: [] });
|
|
95
|
-
return results;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Batch store with retry logic
|
|
100
|
-
*
|
|
101
|
-
* @param requests - Store requests
|
|
102
|
-
* @param options - Batch options with retry configuration and payment support
|
|
103
|
-
* @returns Store responses
|
|
104
|
-
*/
|
|
105
|
-
async storeWithRetry(
|
|
106
|
-
requests: StoreRequest[],
|
|
107
|
-
options: {
|
|
108
|
-
concurrency?: number;
|
|
109
|
-
waitForConfirmation?: boolean;
|
|
110
|
-
maxRetries?: number;
|
|
111
|
-
retryDelay?: number;
|
|
112
|
-
onProgress?: (completed: number, total: number) => void;
|
|
113
|
-
paymentOptions?: {
|
|
114
|
-
userWallet?: any;
|
|
115
|
-
brokerAddress?: string;
|
|
116
|
-
};
|
|
117
|
-
} = {}
|
|
118
|
-
): Promise<StoreResponse[]> {
|
|
119
|
-
const {
|
|
120
|
-
maxRetries = 3,
|
|
121
|
-
retryDelay = 1000,
|
|
122
|
-
...batchOptions
|
|
123
|
-
} = options;
|
|
124
|
-
|
|
125
|
-
let attempt = 0;
|
|
126
|
-
let lastError: Error;
|
|
127
|
-
|
|
128
|
-
while (attempt <= maxRetries) {
|
|
129
|
-
try {
|
|
130
|
-
return await this.store(requests, batchOptions);
|
|
131
|
-
} catch (error) {
|
|
132
|
-
lastError = error as Error;
|
|
133
|
-
attempt++;
|
|
134
|
-
|
|
135
|
-
if (attempt <= maxRetries) {
|
|
136
|
-
this.emit('retryAttempt', { attempt, maxRetries, error });
|
|
137
|
-
await this.sleep(retryDelay * attempt); // Exponential backoff
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
throw lastError!;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Monitor multiple transactions for confirmation
|
|
147
|
-
*
|
|
148
|
-
* @param transactionIds - Array of transaction IDs to monitor
|
|
149
|
-
* @param maxWaitTime - Maximum wait time per transaction
|
|
150
|
-
* @returns Promise resolving when all transactions are confirmed
|
|
151
|
-
*/
|
|
152
|
-
async waitForBatchConfirmation(
|
|
153
|
-
transactionIds: string[],
|
|
154
|
-
maxWaitTime: number = 300000
|
|
155
|
-
): Promise<TransactionStatus[]> {
|
|
156
|
-
const confirmationPromises = transactionIds.map(async (id, index) => {
|
|
157
|
-
try {
|
|
158
|
-
const result = await this.client.waitForConfirmation(id, maxWaitTime);
|
|
159
|
-
this.emit('transactionConfirmed', { id, index, result });
|
|
160
|
-
return {
|
|
161
|
-
id,
|
|
162
|
-
status: 'confirmed' as const,
|
|
163
|
-
block_height: result.block_height,
|
|
164
|
-
transaction_hash: result.transaction_hash,
|
|
165
|
-
celestia_height: result.celestia_height
|
|
166
|
-
};
|
|
167
|
-
} catch (error) {
|
|
168
|
-
this.emit('transactionFailed', { id, index, error });
|
|
169
|
-
return {
|
|
170
|
-
id,
|
|
171
|
-
status: 'failed' as const,
|
|
172
|
-
error: (error as Error).message
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
return Promise.all(confirmationPromises);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private sleep(ms: number): Promise<void> {
|
|
181
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Utility class for building bulk operations
|
|
187
|
-
*
|
|
188
|
-
* @example
|
|
189
|
-
* ```typescript
|
|
190
|
-
* const builder = new BulkBuilder()
|
|
191
|
-
* .collection('tweets')
|
|
192
|
-
* .add({ message: 'Tweet 1', author: 'alice' })
|
|
193
|
-
* .add({ message: 'Tweet 2', author: 'bob' })
|
|
194
|
-
* .add({ message: 'Tweet 3', author: 'charlie' });
|
|
195
|
-
*
|
|
196
|
-
* const results = await db.batch().store(builder.build());
|
|
197
|
-
* ```
|
|
198
|
-
*/
|
|
199
|
-
export class BulkBuilder {
|
|
200
|
-
private requests: StoreRequest[] = [];
|
|
201
|
-
private defaultCollection?: string;
|
|
202
|
-
private defaultApp?: string;
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Set default collection for all subsequent operations
|
|
206
|
-
*/
|
|
207
|
-
collection(name: string): this {
|
|
208
|
-
this.defaultCollection = name;
|
|
209
|
-
return this;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Set default app for all subsequent operations
|
|
214
|
-
*/
|
|
215
|
-
app(name: string): this {
|
|
216
|
-
this.defaultApp = name;
|
|
217
|
-
return this;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Add a data record to the bulk operation
|
|
222
|
-
*/
|
|
223
|
-
add(data: Record<string, any>, collection?: string, app?: string): this {
|
|
224
|
-
this.requests.push({data: [data], root: `${app}::${collection}`});
|
|
225
|
-
return this;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Add multiple data records
|
|
230
|
-
*/
|
|
231
|
-
addMany(records: Record<string, any>[], collection?: string, app?: string): this {
|
|
232
|
-
records.forEach(data => this.add(data, collection, app));
|
|
233
|
-
return this;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* Build the final array of store requests
|
|
238
|
-
*/
|
|
239
|
-
build(): StoreRequest[] {
|
|
240
|
-
return [...this.requests];
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* Get the number of operations in the builder
|
|
245
|
-
*/
|
|
246
|
-
count(): number {
|
|
247
|
-
return this.requests.length;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
/**
|
|
251
|
-
* Clear all operations
|
|
252
|
-
*/
|
|
253
|
-
clear(): this {
|
|
254
|
-
this.requests = [];
|
|
255
|
-
return this;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { LogicalOperator, FieldConditionBuilder } from './operators';
|
|
2
|
-
import { NestedConditionBuilder } from './NestedBuilders';
|
|
3
|
-
export declare class ConditionBuilder {
|
|
4
|
-
private conditions;
|
|
5
|
-
private currentOperator?;
|
|
6
|
-
constructor();
|
|
7
|
-
static new(): ConditionBuilder;
|
|
8
|
-
field(fieldName: string): FieldConditionBuilder;
|
|
9
|
-
nested(fieldName: string, builderFn: (builder: NestedConditionBuilder) => LogicalOperator): LogicalOperator;
|
|
10
|
-
and(): ConditionBuilder;
|
|
11
|
-
or(): ConditionBuilder;
|
|
12
|
-
not(): ConditionBuilder;
|
|
13
|
-
andGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator;
|
|
14
|
-
orGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator;
|
|
15
|
-
notGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator;
|
|
16
|
-
addCondition(condition: LogicalOperator): ConditionBuilder;
|
|
17
|
-
build(): LogicalOperator[];
|
|
18
|
-
buildSingle(): LogicalOperator;
|
|
19
|
-
clear(): ConditionBuilder;
|
|
20
|
-
isEmpty(): boolean;
|
|
21
|
-
getConditionCount(): number;
|
|
22
|
-
}
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConditionBuilder = void 0;
|
|
4
|
-
const operators_1 = require("./operators");
|
|
5
|
-
const NestedBuilders_1 = require("./NestedBuilders");
|
|
6
|
-
// Builder for creating complex conditions with logical operators
|
|
7
|
-
class ConditionBuilder {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.conditions = [];
|
|
10
|
-
}
|
|
11
|
-
static new() {
|
|
12
|
-
return new ConditionBuilder();
|
|
13
|
-
}
|
|
14
|
-
// Create a field condition builder
|
|
15
|
-
field(fieldName) {
|
|
16
|
-
return new operators_1.FieldConditionBuilder(fieldName);
|
|
17
|
-
}
|
|
18
|
-
// Create a nested field condition with fluent API (like Rust SDK)
|
|
19
|
-
nested(fieldName, builderFn) {
|
|
20
|
-
const nestedBuilder = new NestedBuilders_1.NestedConditionBuilder(fieldName);
|
|
21
|
-
return builderFn(nestedBuilder);
|
|
22
|
-
}
|
|
23
|
-
// Add an AND operator context
|
|
24
|
-
and() {
|
|
25
|
-
this.currentOperator = 'and';
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
// Add an OR operator context
|
|
29
|
-
or() {
|
|
30
|
-
this.currentOperator = 'or';
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
// Add a NOT operator context
|
|
34
|
-
not() {
|
|
35
|
-
this.currentOperator = 'not';
|
|
36
|
-
return this;
|
|
37
|
-
}
|
|
38
|
-
// Group conditions with AND logic
|
|
39
|
-
andGroup(builderFn) {
|
|
40
|
-
const nestedBuilder = new ConditionBuilder();
|
|
41
|
-
const conditions = builderFn(nestedBuilder);
|
|
42
|
-
return operators_1.LogicalOperator.And(conditions);
|
|
43
|
-
}
|
|
44
|
-
// Group conditions with OR logic
|
|
45
|
-
orGroup(builderFn) {
|
|
46
|
-
const nestedBuilder = new ConditionBuilder();
|
|
47
|
-
const conditions = builderFn(nestedBuilder);
|
|
48
|
-
return operators_1.LogicalOperator.Or(conditions);
|
|
49
|
-
}
|
|
50
|
-
// Group conditions with NOT logic
|
|
51
|
-
notGroup(builderFn) {
|
|
52
|
-
const nestedBuilder = new ConditionBuilder();
|
|
53
|
-
const conditions = builderFn(nestedBuilder);
|
|
54
|
-
return operators_1.LogicalOperator.Not(conditions);
|
|
55
|
-
}
|
|
56
|
-
// Add a condition to the current builder
|
|
57
|
-
addCondition(condition) {
|
|
58
|
-
this.conditions.push(condition);
|
|
59
|
-
return this;
|
|
60
|
-
}
|
|
61
|
-
// Build and return all conditions
|
|
62
|
-
build() {
|
|
63
|
-
return [...this.conditions];
|
|
64
|
-
}
|
|
65
|
-
// Build as a single logical operator (combines all conditions with AND by default)
|
|
66
|
-
buildSingle() {
|
|
67
|
-
if (this.conditions.length === 0) {
|
|
68
|
-
throw new Error('No conditions to build');
|
|
69
|
-
}
|
|
70
|
-
if (this.conditions.length === 1) {
|
|
71
|
-
return this.conditions[0];
|
|
72
|
-
}
|
|
73
|
-
return operators_1.LogicalOperator.And(this.conditions);
|
|
74
|
-
}
|
|
75
|
-
// Clear all conditions
|
|
76
|
-
clear() {
|
|
77
|
-
this.conditions = [];
|
|
78
|
-
this.currentOperator = undefined;
|
|
79
|
-
return this;
|
|
80
|
-
}
|
|
81
|
-
// Check if any conditions have been added
|
|
82
|
-
isEmpty() {
|
|
83
|
-
return this.conditions.length === 0;
|
|
84
|
-
}
|
|
85
|
-
// Get the number of conditions
|
|
86
|
-
getConditionCount() {
|
|
87
|
-
return this.conditions.length;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
exports.ConditionBuilder = ConditionBuilder;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { FieldConditionBuilder } from './operators';
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FieldConditionBuilder = void 0;
|
|
4
|
-
// Re-export from operators for convenience
|
|
5
|
-
var operators_1 = require("./operators");
|
|
6
|
-
Object.defineProperty(exports, "FieldConditionBuilder", { enumerable: true, get: function () { return operators_1.FieldConditionBuilder; } });
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { LogicalOperator, Condition } from './operators';
|
|
2
|
-
export declare class NestedConditionBuilder {
|
|
3
|
-
private path;
|
|
4
|
-
constructor(rootField: string);
|
|
5
|
-
field(fieldName: string): NestedFieldConditionBuilder;
|
|
6
|
-
nested(fieldName: string, builderFn: (builder: NestedConditionBuilder) => LogicalOperator): LogicalOperator;
|
|
7
|
-
andGroup(builderFn: (builder: NestedConditionBuilder) => LogicalOperator[]): LogicalOperator;
|
|
8
|
-
orGroup(builderFn: (builder: NestedConditionBuilder) => LogicalOperator[]): LogicalOperator;
|
|
9
|
-
notGroup(builderFn: (builder: NestedConditionBuilder) => LogicalOperator[]): LogicalOperator;
|
|
10
|
-
}
|
|
11
|
-
export declare class NestedFieldConditionBuilder {
|
|
12
|
-
private fieldPath;
|
|
13
|
-
constructor(fieldPath: string[]);
|
|
14
|
-
private createCondition;
|
|
15
|
-
equals(value: any): Condition;
|
|
16
|
-
notEquals(value: any): Condition;
|
|
17
|
-
in(values: any[]): Condition;
|
|
18
|
-
notIn(values: any[]): Condition;
|
|
19
|
-
isNull(): Condition;
|
|
20
|
-
isNotNull(): Condition;
|
|
21
|
-
exists(): Condition;
|
|
22
|
-
notExists(): Condition;
|
|
23
|
-
startsWith(value: string): Condition;
|
|
24
|
-
endsWith(value: string): Condition;
|
|
25
|
-
contains(value: string): Condition;
|
|
26
|
-
regExpMatches(pattern: string): Condition;
|
|
27
|
-
includesCaseInsensitive(value: string): Condition;
|
|
28
|
-
startsWithCaseInsensitive(value: string): Condition;
|
|
29
|
-
endsWithCaseInsensitive(value: string): Condition;
|
|
30
|
-
greaterThan(value: any): Condition;
|
|
31
|
-
lessThan(value: any): Condition;
|
|
32
|
-
greaterThanOrEqual(value: any): Condition;
|
|
33
|
-
lessThanOrEqual(value: any): Condition;
|
|
34
|
-
isLocalIp(): Condition;
|
|
35
|
-
isExternalIp(): Condition;
|
|
36
|
-
b64(value: string): Condition;
|
|
37
|
-
inDataset(dataset: string): Condition;
|
|
38
|
-
inCountry(countryCode: string): Condition;
|
|
39
|
-
cidr(cidr: string): Condition;
|
|
40
|
-
between(min: any, max: any): Condition;
|
|
41
|
-
isTrue(): Condition;
|
|
42
|
-
isFalse(): Condition;
|
|
43
|
-
}
|