@onchaindb/sdk 0.4.5 → 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.
Files changed (97) hide show
  1. package/.claude/settings.local.json +10 -2
  2. package/README.md +422 -355
  3. package/dist/batch.d.ts +1 -10
  4. package/dist/batch.d.ts.map +1 -1
  5. package/dist/batch.js +4 -26
  6. package/dist/batch.js.map +1 -1
  7. package/dist/client.d.ts +29 -43
  8. package/dist/client.d.ts.map +1 -1
  9. package/dist/client.js +198 -323
  10. package/dist/client.js.map +1 -1
  11. package/dist/database.d.ts +14 -131
  12. package/dist/database.d.ts.map +1 -1
  13. package/dist/database.js +35 -131
  14. package/dist/database.js.map +1 -1
  15. package/dist/index.d.ts +6 -9
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +1 -15
  18. package/dist/index.js.map +1 -1
  19. package/dist/query-sdk/ConditionBuilder.d.ts +3 -11
  20. package/dist/query-sdk/ConditionBuilder.d.ts.map +1 -1
  21. package/dist/query-sdk/ConditionBuilder.js +10 -48
  22. package/dist/query-sdk/ConditionBuilder.js.map +1 -1
  23. package/dist/query-sdk/NestedBuilders.d.ts +33 -30
  24. package/dist/query-sdk/NestedBuilders.d.ts.map +1 -1
  25. package/dist/query-sdk/NestedBuilders.js +46 -43
  26. package/dist/query-sdk/NestedBuilders.js.map +1 -1
  27. package/dist/query-sdk/QueryBuilder.d.ts +4 -2
  28. package/dist/query-sdk/QueryBuilder.d.ts.map +1 -1
  29. package/dist/query-sdk/QueryBuilder.js +47 -169
  30. package/dist/query-sdk/QueryBuilder.js.map +1 -1
  31. package/dist/query-sdk/QueryResult.d.ts +0 -38
  32. package/dist/query-sdk/QueryResult.d.ts.map +1 -1
  33. package/dist/query-sdk/QueryResult.js +1 -227
  34. package/dist/query-sdk/QueryResult.js.map +1 -1
  35. package/dist/query-sdk/index.d.ts +1 -1
  36. package/dist/query-sdk/index.d.ts.map +1 -1
  37. package/dist/query-sdk/index.js.map +1 -1
  38. package/dist/query-sdk/operators.d.ts +32 -28
  39. package/dist/query-sdk/operators.d.ts.map +1 -1
  40. package/dist/query-sdk/operators.js +45 -155
  41. package/dist/query-sdk/operators.js.map +1 -1
  42. package/dist/types.d.ts +153 -1
  43. package/dist/types.d.ts.map +1 -1
  44. package/dist/types.js.map +1 -1
  45. package/jest.config.js +4 -0
  46. package/package.json +1 -1
  47. package/skills.md +0 -1
  48. package/src/client.ts +242 -745
  49. package/src/database.ts +70 -493
  50. package/src/index.ts +40 -193
  51. package/src/query-sdk/ConditionBuilder.ts +37 -89
  52. package/src/query-sdk/NestedBuilders.ts +90 -92
  53. package/src/query-sdk/QueryBuilder.ts +59 -218
  54. package/src/query-sdk/QueryResult.ts +4 -330
  55. package/src/query-sdk/README.md +214 -583
  56. package/src/query-sdk/index.ts +1 -1
  57. package/src/query-sdk/operators.ts +91 -200
  58. package/src/query-sdk/tests/FieldConditionBuilder.test.ts +70 -71
  59. package/src/query-sdk/tests/LogicalOperator.test.ts +43 -82
  60. package/src/query-sdk/tests/NestedBuilders.test.ts +229 -309
  61. package/src/query-sdk/tests/QueryBuilder.test.ts +5 -5
  62. package/src/query-sdk/tests/QueryResult.test.ts +41 -435
  63. package/src/query-sdk/tests/comprehensive.test.ts +4 -185
  64. package/src/tests/client-requests.test.ts +280 -0
  65. package/src/tests/client-validation.test.ts +80 -0
  66. package/src/types.ts +229 -8
  67. package/src/batch.ts +0 -257
  68. package/src/query-sdk/dist/ConditionBuilder.d.ts +0 -22
  69. package/src/query-sdk/dist/ConditionBuilder.js +0 -90
  70. package/src/query-sdk/dist/FieldConditionBuilder.d.ts +0 -1
  71. package/src/query-sdk/dist/FieldConditionBuilder.js +0 -6
  72. package/src/query-sdk/dist/NestedBuilders.d.ts +0 -43
  73. package/src/query-sdk/dist/NestedBuilders.js +0 -144
  74. package/src/query-sdk/dist/OnChainDB.d.ts +0 -19
  75. package/src/query-sdk/dist/OnChainDB.js +0 -123
  76. package/src/query-sdk/dist/QueryBuilder.d.ts +0 -70
  77. package/src/query-sdk/dist/QueryBuilder.js +0 -295
  78. package/src/query-sdk/dist/QueryResult.d.ts +0 -52
  79. package/src/query-sdk/dist/QueryResult.js +0 -293
  80. package/src/query-sdk/dist/SelectionBuilder.d.ts +0 -20
  81. package/src/query-sdk/dist/SelectionBuilder.js +0 -80
  82. package/src/query-sdk/dist/adapters/HttpClientAdapter.d.ts +0 -27
  83. package/src/query-sdk/dist/adapters/HttpClientAdapter.js +0 -170
  84. package/src/query-sdk/dist/index.d.ts +0 -36
  85. package/src/query-sdk/dist/index.js +0 -27
  86. package/src/query-sdk/dist/operators.d.ts +0 -56
  87. package/src/query-sdk/dist/operators.js +0 -289
  88. package/src/query-sdk/dist/tests/setup.d.ts +0 -15
  89. package/src/query-sdk/dist/tests/setup.js +0 -46
  90. package/src/query-sdk/jest.config.js +0 -25
  91. package/src/query-sdk/package.json +0 -46
  92. package/src/query-sdk/tests/aggregations.test.ts +0 -653
  93. package/src/query-sdk/tests/integration.test.ts +0 -608
  94. package/src/query-sdk/tests/operators.test.ts +0 -327
  95. package/src/query-sdk/tests/unit.test.ts +0 -794
  96. package/src/query-sdk/tsconfig.json +0 -26
  97. package/src/query-sdk/yarn.lock +0 -3092
package/src/index.ts CHANGED
@@ -1,14 +1,9 @@
1
1
  // Main SDK exports
2
- export {OnChainDBClient} from './client';
3
- export {BatchOperations, BulkBuilder} from './batch';
2
+ export { OnChainDBClient } from './client';
3
+ export { QueryBuilder } from './query-sdk';
4
+ export { DatabaseManager, createDatabaseManager } from './database';
4
5
 
5
- // Query builder exports from ./query-sdk
6
- export {QueryBuilder, SelectionBuilder, FieldConditionBuilder, LogicalOperator, ConditionBuilder, JoinBuilder, OnChainDB} from './query-sdk';
7
-
8
- // Database management exports
9
- export {DatabaseManager, createDatabaseManager} from './database';
10
-
11
- // Type exports
6
+ // Core type exports
12
7
  export type {
13
8
  OnChainDBConfig,
14
9
  StoreRequest,
@@ -30,55 +25,58 @@ export type {
30
25
  PricingQuoteResponse,
31
26
  SimpleCollectionSchema,
32
27
  SimpleFieldDefinition,
28
+ SimpleCollectionSchemaWithSharding,
29
+ ShardingStrategy,
30
+ ShardKey,
33
31
  CreateCollectionResult,
34
32
  SyncCollectionResult,
33
+ UpdateCollectionRequest,
34
+ SetRetentionRequest,
35
+ ApiConfig,
36
+ SqlQueryRequest,
37
+ SqlQueryResponse,
38
+ SqlInsertRequest,
39
+ SqlCreateViewRequest,
40
+ ViewDataResponse,
41
+ ViewQueryRequest,
42
+ QueryParameter,
43
+ QueryDefinition,
44
+ CreateQueryRequest,
45
+ CreateQueryResponse,
46
+ QueryDataResponse,
47
+ RetentionConfig,
48
+ CollectionRetentionCost,
49
+ RetentionCostResponse,
50
+ CollectionResponse,
51
+ CreateApiCollectionRequest,
35
52
  BaseDocument,
36
- ShardingStrategy,
37
- ShardKey,
38
- SimpleCollectionSchemaWithSharding
53
+ AppKeyPermission,
54
+ PayLimits,
55
+ CreateKeyRequest,
56
+ AppKeyInfo,
39
57
  } from './types';
40
58
 
41
- // Query type exports from ./query-sdk
42
- export type {
43
- SelectionMap,
44
- QueryRequest as RawQuery,
45
- QueryResponse as OnChainQueryResponse,
46
- QueryValue,
47
- Val
48
- } from './query-sdk';
49
-
50
- // Re-export operator types from ./query-sdk
51
- export type {Condition} from './query-sdk';
59
+ // Query type exports (useful for advanced typing)
60
+ export type { QueryResponse as RawQueryResponse, SelectionMap, Val } from './query-sdk';
52
61
 
53
- // x402 payment types
62
+ // x402 payment types (needed for payment callbacks)
54
63
  export type {
55
64
  X402Quote,
56
65
  X402PaymentRequirement,
57
66
  X402PaymentResult,
67
+ X402PaymentCallbackResult,
58
68
  ChainType,
59
- PaymentMethod
69
+ PaymentMethod,
60
70
  } from './x402';
61
71
 
62
72
  // Database management type exports
63
73
  export type {
64
- Collection,
65
- CollectionSchema,
66
- FieldDefinition,
67
- FieldValidation,
68
- Relationship,
69
74
  Index,
70
75
  IndexOptions,
71
- IndexStatus,
72
- IndexStatistics,
73
76
  PriceConfig,
74
- CollectionMetadata,
75
- DatabaseStats,
76
- QueryPlan,
77
- BatchOperation,
78
- BatchResult,
79
77
  MaterializedView,
80
78
  ViewInfo,
81
- ListViewsResponse
79
+ ListViewsResponse,
82
80
  } from './database';
83
81
 
84
82
  // Error exports
@@ -87,167 +85,16 @@ export {
87
85
  TransactionError,
88
86
  ValidationError,
89
87
  PaymentRequiredError,
90
- PaymentVerificationError
88
+ PaymentVerificationError,
91
89
  } from './types';
92
90
 
93
- // Import types for factory function
94
- import {OnChainDBConfig} from './types';
95
- import {OnChainDBClient} from './client';
96
-
97
91
  // Convenience factory function
92
+ import { OnChainDBConfig } from './types';
93
+ import { OnChainDBClient } from './client';
94
+
98
95
  export function createClient(config: OnChainDBConfig): OnChainDBClient {
99
96
  return new OnChainDBClient(config);
100
97
  }
101
98
 
102
- // Re-export HTTP client utilities from ./query-sdk for advanced usage
103
- export {AxiosHttpClient, FetchHttpClient, NodeHttpClient, createHttpClient} from './query-sdk';
104
-
105
99
  // SDK version
106
100
  export const VERSION = '1.0.0';
107
-
108
- /**
109
- * OnChainDB TypeScript SDK
110
- *
111
- * A complete TypeScript SDK for OnChainDB - the decentralized database
112
- * built on Celestia blockchain.
113
- *
114
- * Features:
115
- * - ✅ Full type safety with TypeScript
116
- * - ✅ Automatic transaction management
117
- * - ✅ Real-time confirmation tracking
118
- * - ✅ Batch operations with progress tracking
119
- * - ✅ Built-in error handling and retries
120
- * - ✅ Event-driven architecture
121
- * - ✅ Query building and filtering
122
- * - ✅ Complete database management (collections, indexes, schemas)
123
- * - ✅ Performance monitoring and optimization
124
- * - ✅ Schema validation and migration
125
- *
126
- * @example Basic Usage
127
- * ```typescript
128
- * import { createClient } from '@onchaindb/sdk';
129
- *
130
- * const db = createClient({
131
- * endpoint: 'http://localhost:9092',
132
- * apiKey: 'your-api-key'
133
- * });
134
- *
135
- * // Store data
136
- * const result = await db.store({
137
- * data: { message: 'Hello OnChainDB!', user: 'alice' },
138
- * collection: 'messages'
139
- * });
140
- *
141
- * console.log('Stored with ID:', result.id);
142
- * console.log('Transaction:', result.transaction_hash);
143
- *
144
- * // Query data
145
- * const messages = await db.query({
146
- * collection: 'messages',
147
- * limit: 10
148
- * });
149
- *
150
- * console.log(`Found ${messages.total} messages`);
151
- * ```
152
- *
153
- * @example Advanced Usage with Events
154
- * ```typescript
155
- * import { OnChainDBClient } from '@onchaindb/sdk';
156
- *
157
- * const db = new OnChainDBClient({
158
- * endpoint: 'http://localhost:9092'
159
- * });
160
- *
161
- * // Listen for transaction events
162
- * db.on('transaction:pending', (tx) => {
163
- * console.log('Transaction pending:', tx.id);
164
- * });
165
- *
166
- * db.on('transaction:confirmed', (tx) => {
167
- * console.log('Transaction confirmed:', tx.id);
168
- * });
169
- *
170
- * // Store and wait for confirmation
171
- * const confirmed = await db.storeAndConfirm({
172
- * data: { important: 'data that needs confirmation' },
173
- * collection: 'critical'
174
- * });
175
- * ```
176
- *
177
- * @example Batch Operations
178
- * ```typescript
179
- * import { OnChainDBClient, BulkBuilder } from '@onchaindb/sdk';
180
- *
181
- * const db = new OnChainDBClient({ endpoint: 'http://localhost:9092' });
182
- * const batch = db.batch();
183
- *
184
- * // Build bulk operation
185
- * const builder = new BulkBuilder()
186
- * .collection('tweets')
187
- * .add({ message: 'Tweet 1', author: 'alice' })
188
- * .add({ message: 'Tweet 2', author: 'bob' })
189
- * .add({ message: 'Tweet 3', author: 'charlie' });
190
- *
191
- * // Execute with progress tracking
192
- * const results = await batch.store(builder.build(), {
193
- * concurrency: 5,
194
- * waitForConfirmation: true,
195
- * onProgress: (completed, total) => {
196
- * console.log(`Progress: ${completed}/${total}`);
197
- * }
198
- * });
199
- * ```
200
- *
201
- * @example Database Management
202
- * ```typescript
203
- * import { createClient } from '@onchaindb/sdk';
204
- *
205
- * const client = createClient({
206
- * endpoint: 'http://localhost:9092',
207
- * apiKey: 'your-api-key'
208
- * });
209
- *
210
- * // Get database manager for an app
211
- * const db = client.database('app_12345');
212
- *
213
- * // Create collections with schemas
214
- * await db.createCollection('users', {
215
- * fields: {
216
- * id: { type: 'string', unique: true, required: true },
217
- * name: { type: 'string', required: true },
218
- * email: { type: 'string', unique: true, required: true },
219
- * created_at: { type: 'date', default: () => new Date() }
220
- * }
221
- * });
222
- *
223
- * // Create optimized indexes
224
- * await db.createIndex({
225
- * name: 'users_email_index',
226
- * collection: 'users',
227
- * field_name: 'email',
228
- * index_type: 'btree',
229
- * options: { unique: true }
230
- * });
231
- *
232
- * // Monitor performance
233
- * const stats = await db.getDatabaseStats();
234
- * console.log(`Collections: ${stats.total_collections}, Indexes: ${stats.total_indexes}`);
235
- * ```
236
- *
237
- * @example Error Handling
238
- * ```typescript
239
- * import { OnChainDBError, TransactionError, ValidationError } from '@onchaindb/sdk';
240
- *
241
- * try {
242
- * await db.store({ data: { test: 'data' } });
243
- * } catch (error) {
244
- * if (error instanceof ValidationError) {
245
- * console.log('Validation failed:', error.message);
246
- * } else if (error instanceof TransactionError) {
247
- * console.log('Transaction failed:', error.transactionId);
248
- * } else if (error instanceof OnChainDBError) {
249
- * console.log('OnChainDB error:', error.code);
250
- * }
251
- * }
252
- * ```
253
- */
@@ -1,103 +1,51 @@
1
1
  import { LogicalOperator, FieldConditionBuilder } from './operators';
2
2
  import { NestedConditionBuilder } from './NestedBuilders';
3
3
 
4
- // Builder for creating complex conditions with logical operators
4
+ // Builder for creating complex conditions with logical operators.
5
+ // Received as the argument inside .find() and .on() callbacks.
5
6
  export class ConditionBuilder {
6
- private conditions: LogicalOperator[] = [];
7
- private currentOperator?: 'and' | 'or' | 'not';
7
+ constructor() {}
8
8
 
9
- constructor() {}
10
-
11
- static new(): ConditionBuilder {
12
- return new ConditionBuilder();
13
- }
14
-
15
- // Create a field condition builder
16
- field(fieldName: string): FieldConditionBuilder {
17
- return new FieldConditionBuilder(fieldName);
18
- }
19
-
20
- // Create a nested field condition with fluent API (like Rust SDK)
21
- nested(fieldName: string, builderFn: (builder: NestedConditionBuilder) => LogicalOperator): LogicalOperator {
22
- const nestedBuilder = new NestedConditionBuilder(fieldName);
23
- return builderFn(nestedBuilder);
24
- }
25
-
26
- // Add an AND operator context
27
- and(): ConditionBuilder {
28
- this.currentOperator = 'and';
29
- return this;
30
- }
31
-
32
- // Add an OR operator context
33
- or(): ConditionBuilder {
34
- this.currentOperator = 'or';
35
- return this;
36
- }
37
-
38
- // Add a NOT operator context
39
- not(): ConditionBuilder {
40
- this.currentOperator = 'not';
41
- return this;
42
- }
43
-
44
- // Group conditions with AND logic
45
- andGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator {
46
- const nestedBuilder = new ConditionBuilder();
47
- const conditions = builderFn(nestedBuilder);
48
- return LogicalOperator.And(conditions);
49
- }
50
-
51
- // Group conditions with OR logic
52
- orGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator {
53
- const nestedBuilder = new ConditionBuilder();
54
- const conditions = builderFn(nestedBuilder);
55
- return LogicalOperator.Or(conditions);
56
- }
9
+ static new(): ConditionBuilder {
10
+ return new ConditionBuilder();
11
+ }
57
12
 
58
- // Group conditions with NOT logic
59
- notGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator {
60
- const nestedBuilder = new ConditionBuilder();
61
- const conditions = builderFn(nestedBuilder);
62
- return LogicalOperator.Not(conditions);
63
- }
13
+ // Create a field condition builder for a single field
14
+ field(fieldName: string): FieldConditionBuilder {
15
+ return new FieldConditionBuilder(fieldName);
16
+ }
64
17
 
65
- // Add a condition to the current builder
66
- addCondition(condition: LogicalOperator): ConditionBuilder {
67
- this.conditions.push(condition);
68
- return this;
69
- }
18
+ // Create a nested field condition (dot-notation path)
19
+ nested(fieldName: string, builderFn: (builder: NestedConditionBuilder) => LogicalOperator): LogicalOperator {
20
+ const nestedBuilder = new NestedConditionBuilder(fieldName);
21
+ return builderFn(nestedBuilder);
22
+ }
70
23
 
71
- // Build and return all conditions
72
- build(): LogicalOperator[] {
73
- return [...this.conditions];
74
- }
24
+ // Combine conditions with AND — the default when no group is specified
25
+ and(...conditions: LogicalOperator[]): LogicalOperator {
26
+ return LogicalOperator.And(conditions);
27
+ }
75
28
 
76
- // Build as a single logical operator (combines all conditions with AND by default)
77
- buildSingle(): LogicalOperator {
78
- if (this.conditions.length === 0) {
79
- throw new Error('No conditions to build');
29
+ // Combine conditions with OR
30
+ or(...conditions: LogicalOperator[]): LogicalOperator {
31
+ return LogicalOperator.Or(conditions);
80
32
  }
81
- if (this.conditions.length === 1) {
82
- return this.conditions[0];
33
+
34
+ // Negate conditions with NOT
35
+ not(...conditions: LogicalOperator[]): LogicalOperator {
36
+ return LogicalOperator.Not(conditions);
83
37
  }
84
- return LogicalOperator.And(this.conditions);
85
- }
86
38
 
87
- // Clear all conditions
88
- clear(): ConditionBuilder {
89
- this.conditions = [];
90
- this.currentOperator = undefined;
91
- return this;
92
- }
39
+ // Callback-style group helpers (kept for compatibility)
40
+ andGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator {
41
+ return LogicalOperator.And(builderFn(new ConditionBuilder()));
42
+ }
93
43
 
94
- // Check if any conditions have been added
95
- isEmpty(): boolean {
96
- return this.conditions.length === 0;
97
- }
44
+ orGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator {
45
+ return LogicalOperator.Or(builderFn(new ConditionBuilder()));
46
+ }
98
47
 
99
- // Get the number of conditions
100
- getConditionCount(): number {
101
- return this.conditions.length;
102
- }
103
- }
48
+ notGroup(builderFn: (builder: ConditionBuilder) => LogicalOperator[]): LogicalOperator {
49
+ return LogicalOperator.Not(builderFn(new ConditionBuilder()));
50
+ }
51
+ }