@onchaindb/sdk 0.4.0 → 0.4.2

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 (98) hide show
  1. package/.DS_Store +0 -0
  2. package/.claude/settings.local.json +8 -0
  3. package/.gitignore +5 -0
  4. package/.idea/.gitignore +5 -0
  5. package/.idea/compiler.xml +6 -0
  6. package/.idea/inspectionProfiles/Project_Default.xml +6 -0
  7. package/.idea/jsLinters/eslint.xml +6 -0
  8. package/.idea/modules.xml +8 -0
  9. package/.idea/prettier.xml +7 -0
  10. package/.idea/sdk.iml +12 -0
  11. package/.idea/vcs.xml +6 -0
  12. package/.idea/workspace.xml +257 -0
  13. package/dist/client.d.ts.map +1 -1
  14. package/dist/client.js +11 -3
  15. package/dist/client.js.map +1 -1
  16. package/dist/database.d.ts +0 -20
  17. package/dist/database.d.ts.map +1 -1
  18. package/dist/database.js +0 -40
  19. package/dist/database.js.map +1 -1
  20. package/dist/query-sdk/tests/setup.d.ts +16 -0
  21. package/dist/query-sdk/tests/setup.d.ts.map +1 -0
  22. package/dist/query-sdk/tests/setup.js +49 -0
  23. package/dist/query-sdk/tests/setup.js.map +1 -0
  24. package/examples/basic-usage.ts +136 -0
  25. package/examples/blob-upload-example.ts +140 -0
  26. package/examples/collection-schema-example.ts +304 -0
  27. package/examples/server-side-joins.ts +201 -0
  28. package/examples/tweet-self-joins-example.ts +352 -0
  29. package/package-lock.json +3823 -0
  30. package/package.json +1 -1
  31. package/skills.md +1096 -0
  32. package/src/.env +1 -0
  33. package/src/batch.d.ts +121 -0
  34. package/src/batch.js +205 -0
  35. package/src/batch.ts +257 -0
  36. package/src/client.ts +1856 -0
  37. package/src/database.d.ts +268 -0
  38. package/src/database.js +294 -0
  39. package/src/database.ts +695 -0
  40. package/src/index.d.ts +160 -0
  41. package/src/index.js +186 -0
  42. package/src/index.ts +253 -0
  43. package/src/query-sdk/ConditionBuilder.ts +103 -0
  44. package/src/query-sdk/FieldConditionBuilder.ts +2 -0
  45. package/src/query-sdk/NestedBuilders.ts +186 -0
  46. package/src/query-sdk/OnChainDB.ts +294 -0
  47. package/src/query-sdk/QueryBuilder.ts +1191 -0
  48. package/src/query-sdk/QueryResult.ts +375 -0
  49. package/src/query-sdk/README.md +866 -0
  50. package/src/query-sdk/SelectionBuilder.ts +94 -0
  51. package/src/query-sdk/adapters/HttpClientAdapter.ts +249 -0
  52. package/src/query-sdk/dist/ConditionBuilder.d.ts +22 -0
  53. package/src/query-sdk/dist/ConditionBuilder.js +90 -0
  54. package/src/query-sdk/dist/FieldConditionBuilder.d.ts +1 -0
  55. package/src/query-sdk/dist/FieldConditionBuilder.js +6 -0
  56. package/src/query-sdk/dist/NestedBuilders.d.ts +43 -0
  57. package/src/query-sdk/dist/NestedBuilders.js +144 -0
  58. package/src/query-sdk/dist/OnChainDB.d.ts +19 -0
  59. package/src/query-sdk/dist/OnChainDB.js +123 -0
  60. package/src/query-sdk/dist/QueryBuilder.d.ts +70 -0
  61. package/src/query-sdk/dist/QueryBuilder.js +295 -0
  62. package/src/query-sdk/dist/QueryResult.d.ts +52 -0
  63. package/src/query-sdk/dist/QueryResult.js +293 -0
  64. package/src/query-sdk/dist/SelectionBuilder.d.ts +20 -0
  65. package/src/query-sdk/dist/SelectionBuilder.js +80 -0
  66. package/src/query-sdk/dist/adapters/HttpClientAdapter.d.ts +27 -0
  67. package/src/query-sdk/dist/adapters/HttpClientAdapter.js +170 -0
  68. package/src/query-sdk/dist/index.d.ts +36 -0
  69. package/src/query-sdk/dist/index.js +27 -0
  70. package/src/query-sdk/dist/operators.d.ts +56 -0
  71. package/src/query-sdk/dist/operators.js +289 -0
  72. package/src/query-sdk/dist/tests/setup.d.ts +15 -0
  73. package/src/query-sdk/dist/tests/setup.js +46 -0
  74. package/src/query-sdk/index.ts +59 -0
  75. package/src/query-sdk/jest.config.js +25 -0
  76. package/src/query-sdk/operators.ts +335 -0
  77. package/src/query-sdk/package.json +46 -0
  78. package/src/query-sdk/tests/FieldConditionBuilder.test.ts +84 -0
  79. package/src/query-sdk/tests/LogicalOperator.test.ts +85 -0
  80. package/src/query-sdk/tests/NestedBuilders.test.ts +321 -0
  81. package/src/query-sdk/tests/QueryBuilder.test.ts +348 -0
  82. package/src/query-sdk/tests/QueryResult.test.ts +464 -0
  83. package/src/query-sdk/tests/aggregations.test.ts +653 -0
  84. package/src/query-sdk/tests/comprehensive.test.ts +279 -0
  85. package/src/query-sdk/tests/integration.test.ts +608 -0
  86. package/src/query-sdk/tests/operators.test.ts +327 -0
  87. package/src/query-sdk/tests/setup.ts +59 -0
  88. package/src/query-sdk/tests/unit.test.ts +794 -0
  89. package/src/query-sdk/tsconfig.json +26 -0
  90. package/src/query-sdk/yarn.lock +3092 -0
  91. package/src/types.d.ts +131 -0
  92. package/src/types.js +46 -0
  93. package/src/types.ts +534 -0
  94. package/src/x402/index.ts +12 -0
  95. package/src/x402/types.ts +250 -0
  96. package/src/x402/utils.ts +332 -0
  97. package/tsconfig.json +20 -0
  98. package/yarn.lock +2309 -0
package/src/types.d.ts ADDED
@@ -0,0 +1,131 @@
1
+ import { QueryValue } from "./query-sdk";
2
+ export interface OnChainDBConfig {
3
+ endpoint: string;
4
+ apiKey?: string;
5
+ appId?: string;
6
+ timeout?: number;
7
+ retryCount?: number;
8
+ retryDelay?: number;
9
+ }
10
+ export interface StoreRequest {
11
+ root?: string;
12
+ collection?: string;
13
+ data: Record<string, any>[];
14
+ payment_proof?: {
15
+ tx_hash: string;
16
+ user_address: string;
17
+ amount_utia: number;
18
+ broker_address: string;
19
+ };
20
+ }
21
+ export interface StoreResponse {
22
+ id: string;
23
+ namespace: string;
24
+ block_height: number;
25
+ transaction_hash: string;
26
+ confirmed: boolean;
27
+ celestia_height: number;
28
+ }
29
+ export interface TransactionStatus {
30
+ id: string;
31
+ status: 'pending' | 'confirmed' | 'failed';
32
+ block_height?: number;
33
+ transaction_hash?: string;
34
+ celestia_height?: number;
35
+ error?: string;
36
+ }
37
+ export interface QueryRequest {
38
+ root?: string;
39
+ collection?: string;
40
+ app?: string;
41
+ limit?: number;
42
+ offset?: number;
43
+ sortBy?: string;
44
+ sortDirection?: string;
45
+ find?: QueryValue["find"];
46
+ select?: QueryValue["select"];
47
+ }
48
+ export interface AdvancedQueryRequest {
49
+ root?: string;
50
+ collection?: string;
51
+ find?: any;
52
+ select?: any;
53
+ filters?: Record<string, any>;
54
+ app?: string;
55
+ limit?: number;
56
+ offset?: number;
57
+ sort?: string[];
58
+ }
59
+ export interface QueryResponse<T extends Record<string, any>> {
60
+ records: T[];
61
+ total: number;
62
+ page: number;
63
+ limit: number;
64
+ }
65
+ export interface TransactionEvents {
66
+ 'transaction:pending': (tx: TransactionStatus) => void;
67
+ 'transaction:confirmed': (tx: TransactionStatus) => void;
68
+ 'transaction:failed': (tx: TransactionStatus) => void;
69
+ 'error': (error: Error) => void;
70
+ }
71
+ export declare class OnChainDBError extends Error {
72
+ code: string;
73
+ statusCode?: number | undefined;
74
+ details?: any | undefined;
75
+ constructor(message: string, code: string, statusCode?: number | undefined, details?: any | undefined);
76
+ }
77
+ export declare class TransactionError extends OnChainDBError {
78
+ transactionId: string;
79
+ constructor(message: string, transactionId: string, details?: any);
80
+ }
81
+ export declare class ValidationError extends OnChainDBError {
82
+ constructor(message: string, details?: any);
83
+ }
84
+ export declare class PaymentRequiredError extends OnChainDBError {
85
+ requiredAmount: number;
86
+ providedAmount?: number | undefined;
87
+ constructor(message: string, requiredAmount: number, providedAmount?: number | undefined, details?: any);
88
+ }
89
+ export declare class PaymentVerificationError extends OnChainDBError {
90
+ txHash: string;
91
+ constructor(message: string, txHash: string, details?: any);
92
+ }
93
+ export interface IndexRequest {
94
+ collection: string;
95
+ field: string;
96
+ type: 'string' | 'number' | 'boolean' | 'date';
97
+ }
98
+ export interface IndexResponse {
99
+ id: string;
100
+ collection: string;
101
+ field: string;
102
+ type: string;
103
+ status: 'active' | 'building' | 'failed';
104
+ }
105
+ export interface IndexPaymentProof {
106
+ tx_hash: string;
107
+ user_address: string;
108
+ broker_address: string;
109
+ amount_utia: number;
110
+ estimated_cost: IndexCostEstimate;
111
+ }
112
+ export interface IndexCostEstimate {
113
+ field_name: string;
114
+ index_type: string;
115
+ existing_records_count: number;
116
+ total_estimated_cost_utia: number;
117
+ ongoing_write_cost_per_record_utia: number;
118
+ index_size_estimate_mb: number;
119
+ breakdown: {
120
+ base_cost_utia: number;
121
+ per_record_cost_utia: number;
122
+ storage_cost_utia: number;
123
+ };
124
+ }
125
+ export interface StorageCostEstimate {
126
+ data_size_bytes: number;
127
+ total_cost_utia: number;
128
+ cost_per_kb_utia: number;
129
+ estimated_celestia_fee_utia: number;
130
+ broker_fee_utia: number;
131
+ }
package/src/types.js ADDED
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PaymentVerificationError = exports.PaymentRequiredError = exports.ValidationError = exports.TransactionError = exports.OnChainDBError = void 0;
4
+ // Error Types
5
+ class OnChainDBError extends Error {
6
+ constructor(message, code, statusCode, details) {
7
+ super(message);
8
+ this.code = code;
9
+ this.statusCode = statusCode;
10
+ this.details = details;
11
+ this.name = 'OnChainDBError';
12
+ }
13
+ }
14
+ exports.OnChainDBError = OnChainDBError;
15
+ class TransactionError extends OnChainDBError {
16
+ constructor(message, transactionId, details) {
17
+ super(message, 'TRANSACTION_ERROR', undefined, details);
18
+ this.transactionId = transactionId;
19
+ this.name = 'TransactionError';
20
+ }
21
+ }
22
+ exports.TransactionError = TransactionError;
23
+ class ValidationError extends OnChainDBError {
24
+ constructor(message, details) {
25
+ super(message, 'VALIDATION_ERROR', 400, details);
26
+ this.name = 'ValidationError';
27
+ }
28
+ }
29
+ exports.ValidationError = ValidationError;
30
+ class PaymentRequiredError extends OnChainDBError {
31
+ constructor(message, requiredAmount, providedAmount, details) {
32
+ super(message, 'PAYMENT_REQUIRED', 402, details);
33
+ this.requiredAmount = requiredAmount;
34
+ this.providedAmount = providedAmount;
35
+ this.name = 'PaymentRequiredError';
36
+ }
37
+ }
38
+ exports.PaymentRequiredError = PaymentRequiredError;
39
+ class PaymentVerificationError extends OnChainDBError {
40
+ constructor(message, txHash, details) {
41
+ super(message, 'PAYMENT_VERIFICATION_FAILED', 402, details);
42
+ this.txHash = txHash;
43
+ this.name = 'PaymentVerificationError';
44
+ }
45
+ }
46
+ exports.PaymentVerificationError = PaymentVerificationError;
package/src/types.ts ADDED
@@ -0,0 +1,534 @@
1
+ // Core OnChainDB Types
2
+ import {QueryValue} from "./query-sdk";
3
+ import {X402Quote} from "./x402/types";
4
+
5
+ export interface OnChainDBConfig {
6
+ endpoint: string;
7
+ apiKey?: string; // Deprecated: use appKey instead
8
+ appKey?: string; // App API key for write operations (X-App-Key header)
9
+ userKey?: string; // User API key for Auto-Pay (X-User-Key header)
10
+ appId?: string; // Application ID for automatic root building
11
+ timeout?: number;
12
+ retryCount?: number;
13
+ retryDelay?: number;
14
+ }
15
+
16
+ export interface StoreRequest {
17
+ root?: string; // Format: "app::collection" or just "collection" for system
18
+ collection?: string; // Collection name (will be combined with appId if provided)
19
+ data: Record<string, any>[];
20
+ // Payment is handled via x402 protocol (X-PAYMENT header)
21
+ // Use the paymentCallback parameter in store() method
22
+ }
23
+
24
+ export interface StoreResponse {
25
+ id: string;
26
+ namespace: string;
27
+ block_height: number;
28
+ transaction_hash: string;
29
+ confirmed: boolean;
30
+ celestia_height: number;
31
+ /** Ticket ID for async operations (when waitForConfirmation is false) */
32
+ ticket_id?: string;
33
+ }
34
+
35
+ // New async ticket-based response
36
+ export interface AsyncStoreResponse {
37
+ ticket_id: string;
38
+ status: string;
39
+ message: string;
40
+ }
41
+
42
+ // Task status types
43
+ export type TaskStatus =
44
+ | "Pending"
45
+ | "PaymentBroadcast"
46
+ | "PaymentConfirming"
47
+ | "PaymentConfirmed"
48
+ | "StoringData"
49
+ | "Completed"
50
+ | { Failed: { error: string } };
51
+
52
+ export interface TaskInfo {
53
+ ticket_id: string;
54
+ status: TaskStatus;
55
+ created_at: string;
56
+ updated_at: string;
57
+ operation_type: string;
58
+ user_address?: string;
59
+ transaction_hash?: string;
60
+ block_height?: number;
61
+ result?: any;
62
+ progress_log: string[];
63
+ }
64
+
65
+ export interface UserTasksResponse {
66
+ user_address: string;
67
+ tasks: TaskInfo[];
68
+ total_tasks: number;
69
+ }
70
+
71
+ export interface TransactionStatus {
72
+ id: string;
73
+ status: 'pending' | 'confirmed' | 'failed';
74
+ block_height?: number;
75
+ transaction_hash?: string;
76
+ celestia_height?: number;
77
+ error?: string;
78
+ }
79
+
80
+ export interface QueryRequest {
81
+ root?: string; // Format: "app::collection" - ONLY NEW THING
82
+ collection?: string; // Collection name (will be combined with appId if provided)
83
+ app?: string;
84
+ limit?: number;
85
+ offset?: number;
86
+ sortBy?: string;
87
+ sortDirection?: string,
88
+ find?: QueryValue["find"],
89
+ select?: QueryValue["select"],
90
+ }
91
+
92
+ // Enhanced query support for advanced query builder
93
+ export interface AdvancedQueryRequest {
94
+ root?: string; // Format: "app::collection" - ONLY NEW THING
95
+ collection?: string; // Collection name (will be combined with appId if provided)
96
+ find?: any; // Flexible find conditions from onChainDBSDK
97
+ select?: any; // Selection map from onChainDBSDK
98
+
99
+ // All existing functionality stays the same
100
+ filters?: Record<string, any>;
101
+ app?: string;
102
+
103
+ // Pagination and sorting
104
+ limit?: number;
105
+ offset?: number;
106
+ sort?: string[];
107
+ }
108
+
109
+
110
+ export interface QueryResponse<T extends Record<string, any>> {
111
+ records: T[];
112
+ total: number;
113
+ page: number;
114
+ limit: number;
115
+ }
116
+
117
+ export interface ReadQueryWithPayment {
118
+ // Original query fields
119
+ root?: string;
120
+ collection?: string;
121
+ find?: any;
122
+ select?: any;
123
+ limit?: number;
124
+ offset?: number;
125
+ sort?: string[];
126
+
127
+ // Payment is handled via x402 protocol (X-PAYMENT header)
128
+ // Use the paymentCallback parameter in query methods
129
+ quote_id: string;
130
+ }
131
+
132
+ // Union type for query responses - either data or quote
133
+ export type QueryResult<T extends Record<string, any>> =
134
+ | QueryResponse<T> // Normal response with data
135
+ | X402Quote; // Quote response when payment required (x402 format)
136
+
137
+ // Transaction Events
138
+ export interface TransactionEvents {
139
+ 'transaction:queued': (ticket: { ticket_id: string; status: string; message: string }) => void;
140
+ 'transaction:pending': (tx: TransactionStatus) => void;
141
+ 'transaction:confirmed': (tx: TransactionStatus) => void;
142
+ 'transaction:failed': (tx: TransactionStatus) => void;
143
+ 'error': (error: Error) => void;
144
+ }
145
+
146
+ // Error Types
147
+ export class OnChainDBError extends Error {
148
+ constructor(
149
+ message: string,
150
+ public code: string,
151
+ public statusCode?: number,
152
+ public details?: any
153
+ ) {
154
+ super(message);
155
+ this.name = 'OnChainDBError';
156
+ }
157
+ }
158
+
159
+ export class TransactionError extends OnChainDBError {
160
+ constructor(
161
+ message: string,
162
+ public transactionId: string,
163
+ details?: any
164
+ ) {
165
+ super(message, 'TRANSACTION_ERROR', undefined, details);
166
+ this.name = 'TransactionError';
167
+ }
168
+ }
169
+
170
+ export class ValidationError extends OnChainDBError {
171
+ constructor(message: string, details?: any) {
172
+ super(message, 'VALIDATION_ERROR', 400, details);
173
+ this.name = 'ValidationError';
174
+ }
175
+ }
176
+
177
+ export class PaymentRequiredError extends OnChainDBError {
178
+ constructor(
179
+ message: string,
180
+ public quote: X402Quote,
181
+ details?: any
182
+ ) {
183
+ super(message, 'PAYMENT_REQUIRED', 402, details);
184
+ this.name = 'PaymentRequiredError';
185
+ }
186
+ }
187
+
188
+ export class PaymentVerificationError extends OnChainDBError {
189
+ constructor(
190
+ message: string,
191
+ public txHash: string,
192
+ details?: any
193
+ ) {
194
+ super(message, 'PAYMENT_VERIFICATION_FAILED', 402, details);
195
+ this.name = 'PaymentVerificationError';
196
+ }
197
+ }
198
+
199
+ // Index Management
200
+ export interface IndexRequest {
201
+ collection: string;
202
+ field: string;
203
+ type: 'string' | 'number' | 'boolean' | 'date';
204
+ }
205
+
206
+ export interface IndexResponse {
207
+ id: string;
208
+ collection: string;
209
+ field: string;
210
+ type: string;
211
+ status: 'active' | 'building' | 'failed';
212
+ }
213
+
214
+ // Relation Management
215
+ export interface RelationRequest {
216
+ parent_collection: string;
217
+ parent_field: string;
218
+ child_collection: string;
219
+ child_field: string;
220
+ }
221
+
222
+ export interface RelationResponse {
223
+ parent_collection: string;
224
+ parent_field: string;
225
+ child_field: string;
226
+ created_at: string;
227
+ indexes_created: {
228
+ parent_index: boolean;
229
+ child_index: boolean;
230
+ };
231
+ }
232
+
233
+ // Blob Storage (CDN) Types
234
+ export interface UploadBlobRequest {
235
+ collection: string;
236
+ blob: File | Blob | Buffer; // File in browser, Buffer in Node.js
237
+ metadata?: Record<string, any>; // Additional custom fields for blob metadata
238
+ // Payment is handled via x402 protocol (X-PAYMENT header)
239
+ // Use the paymentCallback parameter in uploadBlob() method
240
+ }
241
+
242
+ export interface UploadBlobResponse {
243
+ ticket_id: string;
244
+ blob_id: string;
245
+ status: string;
246
+ message: string;
247
+ }
248
+
249
+ export interface RetrieveBlobRequest {
250
+ collection: string;
251
+ blob_id: string;
252
+ }
253
+
254
+ export interface BlobMetadata {
255
+ blob_id: string;
256
+ content_type: string;
257
+ size_bytes: number;
258
+ uploaded_at: string;
259
+ tx_hash: string;
260
+ celestia_height: number;
261
+
262
+ [key: string]: any; // Custom fields added by user
263
+ }
264
+
265
+ // Pricing Quote Types for cost estimation
266
+
267
+ export interface PricingQuoteRequest {
268
+ app_id: string;
269
+ operation_type: 'read' | 'write';
270
+ size_kb: number;
271
+ collection: string;
272
+ monthly_volume_kb?: number;
273
+ data?: any; // Sample data for price index calculation
274
+ }
275
+
276
+ export interface PricingQuoteResponse {
277
+ type: 'write_quote_with_indexing' | 'read_quote';
278
+ // Base costs
279
+ base_celestia_cost: number;
280
+ base_celestia_cost_utia: number;
281
+ broker_fee: number;
282
+ broker_fee_utia: number;
283
+ // Indexing costs per field
284
+ indexing_costs: Record<string, number>;
285
+ indexing_costs_utia: Record<string, number>;
286
+ // Total before price index fees
287
+ base_total_cost: number;
288
+ base_total_cost_utia: number;
289
+ // Final total (includes price index fees if any)
290
+ total_cost: number;
291
+ total_cost_utia: number;
292
+ // Metadata
293
+ indexed_fields_count: number;
294
+ request: PricingQuoteRequest;
295
+ monthly_volume_kb: number;
296
+ currency: string;
297
+ // Optional breakdowns
298
+ creator_premium?: {
299
+ premium_total: number;
300
+ premium_total_utia: number;
301
+ premium_type: string;
302
+ premium_amount: number;
303
+ creator_revenue: number;
304
+ creator_revenue_utia: number;
305
+ platform_revenue: number;
306
+ platform_revenue_utia: number;
307
+ revenue_split: string;
308
+ };
309
+ price?: any; // Price index breakdown (single or multiple fields)
310
+ }
311
+
312
+ // ============================================================================
313
+ // Collection Schema Types (for createCollection convenience method)
314
+ // ============================================================================
315
+
316
+ // Re-export from database.ts for consistency
317
+ export type {
318
+ Collection,
319
+ CollectionSchema,
320
+ FieldDefinition,
321
+ FieldValidation,
322
+ Relationship,
323
+ Index,
324
+ IndexOptions,
325
+ IndexStatus,
326
+ IndexStatistics,
327
+ PriceConfig,
328
+ CollectionMetadata
329
+ } from './database';
330
+
331
+ /**
332
+ * Simplified schema for createCollection convenience method
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * const schema: SimpleCollectionSchema = {
337
+ * name: 'users',
338
+ * fields: {
339
+ * email: { type: 'string', index: true },
340
+ * username: { type: 'string', index: true },
341
+ * age: { type: 'number' },
342
+ * // Nested fields use dot notation
343
+ * 'address.city': { type: 'string', index: true },
344
+ * 'address.country': { type: 'string', index: true }
345
+ * },
346
+ * useBaseFields: true
347
+ * };
348
+ * ```
349
+ */
350
+ export interface SimpleCollectionSchema {
351
+ /** Collection name */
352
+ name: string;
353
+ /** Field definitions */
354
+ fields: Record<string, SimpleFieldDefinition>;
355
+ /**
356
+ * Include base fields (id, createdAt, updatedAt, deletedAt)
357
+ * @default true
358
+ */
359
+ useBaseFields?: boolean;
360
+ }
361
+
362
+ /**
363
+ * Simplified field definition for createCollection
364
+ */
365
+ export interface SimpleFieldDefinition {
366
+ /** Field data type */
367
+ type: 'string' | 'number' | 'boolean' | 'date' | 'object' | 'array';
368
+ /** Create an index on this field */
369
+ index?: boolean;
370
+ /** Unique constraint - enables automatic deduplication (returns only latest version by this field) */
371
+ unique?: boolean;
372
+ /** Index type (defaults based on field type). Use 'price' for payment splits. */
373
+ indexType?: 'btree' | 'hash' | 'fulltext' | 'price';
374
+ /** Read pricing (for monetized fields) */
375
+ readPricing?: {
376
+ pricePerAccess?: number;
377
+ pricePerKb?: number;
378
+ };
379
+ }
380
+
381
+ // ============================================================================
382
+ // Sharding Types (for high-scale collections)
383
+ // ============================================================================
384
+
385
+ /**
386
+ * Sharding configuration for partitioning large collections
387
+ *
388
+ * Sharding splits a collection across multiple files based on field values,
389
+ * enabling efficient queries on massive datasets (e.g., 50+ markets × hourly data).
390
+ *
391
+ * @example
392
+ * ```typescript
393
+ * // Shard by market and hour for crypto orderbook data
394
+ * const sharding: ShardingStrategy = {
395
+ * keys: [
396
+ * { field: 'market', type: 'discrete' },
397
+ * { field: 'timestamp', type: 'time_range', granularity: 'hour' }
398
+ * ],
399
+ * enforce_in_queries: true,
400
+ * max_shard_size: 50_000_000 // 50MB max per shard
401
+ * };
402
+ * // Results in shards like: SUI/2025-01-15-12/metadata.jsonl
403
+ * // Query "market=SUI, hour=12" scans 600KB instead of 30GB (50,000x reduction)
404
+ * ```
405
+ */
406
+ export interface ShardingStrategy {
407
+ /**
408
+ * Ordered list of shard keys - each adds a level to the shard hierarchy
409
+ * Example: [market, timestamp] creates shards like "SUI/2025-01-15-12"
410
+ */
411
+ keys: ShardKey[];
412
+
413
+ /**
414
+ * If true, queries MUST include all shard keys to prevent full table scans
415
+ * Recommended: true for production to ensure optimal performance
416
+ */
417
+ enforce_in_queries: boolean;
418
+
419
+ /**
420
+ * Maximum size per shard in bytes (optional)
421
+ * When exceeded, suggests creating finer-grained time buckets
422
+ */
423
+ max_shard_size?: number;
424
+ }
425
+
426
+ /**
427
+ * Individual shard key definition
428
+ */
429
+ export interface ShardKey {
430
+ /**
431
+ * Field name to shard by (must be an indexed field)
432
+ */
433
+ field: string;
434
+
435
+ /**
436
+ * Sharding type:
437
+ * - 'discrete': Exact value matching (e.g., market symbols: SUI, BTC, ETH)
438
+ * - 'time_range': Time-based bucketing (e.g., hourly, daily partitions)
439
+ * - 'hash_distributed': Hash-based distribution for even spread
440
+ */
441
+ type: 'discrete' | 'time_range' | 'hash_distributed';
442
+
443
+ /**
444
+ * Time granularity (required for time_range sharding)
445
+ * - 'minute': YYYY-MM-DD-HH-MM
446
+ * - 'hour': YYYY-MM-DD-HH
447
+ * - 'day': YYYY-MM-DD
448
+ * - 'week': YYYY-WNN (ISO week)
449
+ * - 'month': YYYY-MM
450
+ */
451
+ granularity?: 'minute' | 'hour' | 'day' | 'week' | 'month';
452
+
453
+ /**
454
+ * Number of hash buckets (required for hash_distributed sharding)
455
+ * Example: 100 buckets for even user distribution
456
+ */
457
+ num_buckets?: number;
458
+ }
459
+
460
+ /**
461
+ * Extended SimpleCollectionSchema with sharding support
462
+ *
463
+ * @example
464
+ * ```typescript
465
+ * const schema: SimpleCollectionSchemaWithSharding = {
466
+ * name: 'orderbook_snapshots',
467
+ * fields: {
468
+ * market: { type: 'string', index: true },
469
+ * timestamp: { type: 'number', index: true },
470
+ * mid_price: { type: 'number' }
471
+ * },
472
+ * sharding: {
473
+ * keys: [
474
+ * { field: 'market', type: 'discrete' },
475
+ * { field: 'timestamp', type: 'time_range', granularity: 'hour' }
476
+ * ],
477
+ * enforce_in_queries: true
478
+ * }
479
+ * };
480
+ * ```
481
+ */
482
+ export interface SimpleCollectionSchemaWithSharding extends SimpleCollectionSchema {
483
+ /**
484
+ * Optional sharding configuration for high-scale collections
485
+ * When enabled, collection is partitioned across multiple files
486
+ */
487
+ sharding?: ShardingStrategy;
488
+ }
489
+
490
+ /**
491
+ * Result of createCollection operation
492
+ */
493
+ export interface CreateCollectionResult {
494
+ collection: string;
495
+ indexes: {
496
+ field: string;
497
+ type: string;
498
+ status: 'created' | 'updated' | 'failed';
499
+ error?: string;
500
+ }[];
501
+ success: boolean;
502
+ warnings?: string[];
503
+ }
504
+
505
+ /**
506
+ * Result of syncCollection operation
507
+ */
508
+ export interface SyncCollectionResult {
509
+ collection: string;
510
+ created: {
511
+ field: string;
512
+ type: string;
513
+ }[];
514
+ removed: {
515
+ field: string;
516
+ type: string;
517
+ }[];
518
+ unchanged: {
519
+ field: string;
520
+ type: string;
521
+ }[];
522
+ success: boolean;
523
+ errors?: string[];
524
+ }
525
+
526
+ /**
527
+ * Base document fields (auto-added when useBaseFields is true)
528
+ */
529
+ export interface BaseDocument {
530
+ id: string;
531
+ createdAt: string;
532
+ updatedAt: string;
533
+ deletedAt?: string | null;
534
+ }