tersejson 0.3.0 → 0.3.1
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/README.md +39 -0
- package/dist/mongodb.d.mts +107 -0
- package/dist/mongodb.d.ts +107 -0
- package/dist/mongodb.js +472 -0
- package/dist/mongodb.js.map +1 -0
- package/dist/mongodb.mjs +465 -0
- package/dist/mongodb.mjs.map +1 -0
- package/package.json +15 -2
package/README.md
CHANGED
|
@@ -181,6 +181,45 @@ If you access 3 fields from a 21-field object:
|
|
|
181
181
|
- Protobuf: All 21 fields deserialized into memory
|
|
182
182
|
- TerseJSON: Only 3 fields materialize
|
|
183
183
|
|
|
184
|
+
## MongoDB Integration (Zero-Config)
|
|
185
|
+
|
|
186
|
+
**NEW:** Automatic memory-efficient queries with MongoDB native driver.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
import { terseMongo } from 'tersejson/mongodb';
|
|
190
|
+
import { MongoClient } from 'mongodb';
|
|
191
|
+
|
|
192
|
+
// Call once at app startup
|
|
193
|
+
await terseMongo();
|
|
194
|
+
|
|
195
|
+
// All queries automatically return Proxy-wrapped results
|
|
196
|
+
const client = new MongoClient(uri);
|
|
197
|
+
const users = await client.db('mydb').collection('users').find().toArray();
|
|
198
|
+
|
|
199
|
+
// Access properties normally - 70% less memory
|
|
200
|
+
console.log(users[0].firstName); // Works transparently!
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
**What gets patched:**
|
|
204
|
+
- `find().toArray()` - arrays of documents
|
|
205
|
+
- `find().next()` - single document iteration
|
|
206
|
+
- `for await (const doc of cursor)` - async iteration
|
|
207
|
+
- `findOne()` - single document queries
|
|
208
|
+
- `aggregate().toArray()` - aggregation results
|
|
209
|
+
|
|
210
|
+
**Options:**
|
|
211
|
+
```typescript
|
|
212
|
+
await terseMongo({
|
|
213
|
+
minArrayLength: 5, // Only compress arrays with 5+ items
|
|
214
|
+
skipSingleDocs: true, // Don't wrap findOne results
|
|
215
|
+
minKeyLength: 4, // Only compress keys with 4+ chars
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// Restore original behavior
|
|
219
|
+
import { unterse } from 'tersejson/mongodb';
|
|
220
|
+
await unterse();
|
|
221
|
+
```
|
|
222
|
+
|
|
184
223
|
## Server-Side Memory Optimization
|
|
185
224
|
|
|
186
225
|
TerseJSON includes utilities for memory-efficient server-side data handling:
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { C as CompressOptions } from './types-BTonKlz8.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TerseJSON MongoDB Integration
|
|
5
|
+
*
|
|
6
|
+
* Zero-config integration with MongoDB native driver.
|
|
7
|
+
* Call terseMongo() once at startup and all queries automatically
|
|
8
|
+
* return memory-efficient Proxy-wrapped results.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { terseMongo } from 'tersejson/mongodb';
|
|
13
|
+
* import { MongoClient } from 'mongodb';
|
|
14
|
+
*
|
|
15
|
+
* terseMongo(); // Patch once at startup
|
|
16
|
+
*
|
|
17
|
+
* const client = new MongoClient(uri);
|
|
18
|
+
* const users = await client.db('mydb').collection('users').find().toArray();
|
|
19
|
+
* // users is automatically Proxy-wrapped - 70% less memory
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @packageDocumentation
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for terseMongo initialization
|
|
27
|
+
*/
|
|
28
|
+
interface TerseMongoOptions extends Partial<CompressOptions> {
|
|
29
|
+
/**
|
|
30
|
+
* Enable/disable compression (default: true)
|
|
31
|
+
*/
|
|
32
|
+
enabled?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Minimum array length to compress (default: 1)
|
|
35
|
+
* Single documents are always wrapped for consistency
|
|
36
|
+
*/
|
|
37
|
+
minArrayLength?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Skip compression for single document queries like findOne (default: false)
|
|
40
|
+
* Set to true if you don't want overhead on single doc fetches
|
|
41
|
+
*/
|
|
42
|
+
skipSingleDocs?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Initialize TerseJSON integration with MongoDB native driver.
|
|
46
|
+
*
|
|
47
|
+
* Call this once at application startup, before any MongoDB queries.
|
|
48
|
+
* All subsequent queries will automatically return Proxy-wrapped results.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { terseMongo } from 'tersejson/mongodb';
|
|
53
|
+
*
|
|
54
|
+
* // Basic usage
|
|
55
|
+
* await terseMongo();
|
|
56
|
+
*
|
|
57
|
+
* // With options
|
|
58
|
+
* await terseMongo({
|
|
59
|
+
* minKeyLength: 4, // Only compress keys with 4+ characters
|
|
60
|
+
* minArrayLength: 5, // Only compress arrays with 5+ items
|
|
61
|
+
* skipSingleDocs: true, // Don't wrap findOne results
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param options - Configuration options
|
|
66
|
+
*/
|
|
67
|
+
declare function terseMongo(options?: TerseMongoOptions): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Synchronous version of terseMongo for CommonJS compatibility.
|
|
70
|
+
*
|
|
71
|
+
* Note: This requires mongodb to already be loaded in the module cache.
|
|
72
|
+
* For best results, use the async terseMongo() instead.
|
|
73
|
+
*
|
|
74
|
+
* @param options - Configuration options
|
|
75
|
+
*/
|
|
76
|
+
declare function terseMongoSync(options?: TerseMongoOptions): void;
|
|
77
|
+
/**
|
|
78
|
+
* Remove TerseJSON patches from MongoDB driver.
|
|
79
|
+
*
|
|
80
|
+
* Useful for testing or when you need to temporarily disable compression.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* import { terseMongo, unterse } from 'tersejson/mongodb';
|
|
85
|
+
*
|
|
86
|
+
* await terseMongo();
|
|
87
|
+
* // ... queries are Proxy-wrapped
|
|
88
|
+
*
|
|
89
|
+
* await unterse();
|
|
90
|
+
* // ... queries return normal documents
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
declare function unterse(): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Check if TerseJSON MongoDB patching is active
|
|
96
|
+
*/
|
|
97
|
+
declare function isTerseMongoActive(): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Get current TerseJSON MongoDB options
|
|
100
|
+
*/
|
|
101
|
+
declare function getTerseMongoOptions(): TerseMongoOptions;
|
|
102
|
+
/**
|
|
103
|
+
* Update TerseJSON MongoDB options without re-patching
|
|
104
|
+
*/
|
|
105
|
+
declare function setTerseMongoOptions(options: Partial<TerseMongoOptions>): void;
|
|
106
|
+
|
|
107
|
+
export { type TerseMongoOptions, getTerseMongoOptions, isTerseMongoActive, setTerseMongoOptions, terseMongo, terseMongoSync, unterse };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { C as CompressOptions } from './types-BTonKlz8.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TerseJSON MongoDB Integration
|
|
5
|
+
*
|
|
6
|
+
* Zero-config integration with MongoDB native driver.
|
|
7
|
+
* Call terseMongo() once at startup and all queries automatically
|
|
8
|
+
* return memory-efficient Proxy-wrapped results.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { terseMongo } from 'tersejson/mongodb';
|
|
13
|
+
* import { MongoClient } from 'mongodb';
|
|
14
|
+
*
|
|
15
|
+
* terseMongo(); // Patch once at startup
|
|
16
|
+
*
|
|
17
|
+
* const client = new MongoClient(uri);
|
|
18
|
+
* const users = await client.db('mydb').collection('users').find().toArray();
|
|
19
|
+
* // users is automatically Proxy-wrapped - 70% less memory
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @packageDocumentation
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for terseMongo initialization
|
|
27
|
+
*/
|
|
28
|
+
interface TerseMongoOptions extends Partial<CompressOptions> {
|
|
29
|
+
/**
|
|
30
|
+
* Enable/disable compression (default: true)
|
|
31
|
+
*/
|
|
32
|
+
enabled?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Minimum array length to compress (default: 1)
|
|
35
|
+
* Single documents are always wrapped for consistency
|
|
36
|
+
*/
|
|
37
|
+
minArrayLength?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Skip compression for single document queries like findOne (default: false)
|
|
40
|
+
* Set to true if you don't want overhead on single doc fetches
|
|
41
|
+
*/
|
|
42
|
+
skipSingleDocs?: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Initialize TerseJSON integration with MongoDB native driver.
|
|
46
|
+
*
|
|
47
|
+
* Call this once at application startup, before any MongoDB queries.
|
|
48
|
+
* All subsequent queries will automatically return Proxy-wrapped results.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* import { terseMongo } from 'tersejson/mongodb';
|
|
53
|
+
*
|
|
54
|
+
* // Basic usage
|
|
55
|
+
* await terseMongo();
|
|
56
|
+
*
|
|
57
|
+
* // With options
|
|
58
|
+
* await terseMongo({
|
|
59
|
+
* minKeyLength: 4, // Only compress keys with 4+ characters
|
|
60
|
+
* minArrayLength: 5, // Only compress arrays with 5+ items
|
|
61
|
+
* skipSingleDocs: true, // Don't wrap findOne results
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* @param options - Configuration options
|
|
66
|
+
*/
|
|
67
|
+
declare function terseMongo(options?: TerseMongoOptions): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Synchronous version of terseMongo for CommonJS compatibility.
|
|
70
|
+
*
|
|
71
|
+
* Note: This requires mongodb to already be loaded in the module cache.
|
|
72
|
+
* For best results, use the async terseMongo() instead.
|
|
73
|
+
*
|
|
74
|
+
* @param options - Configuration options
|
|
75
|
+
*/
|
|
76
|
+
declare function terseMongoSync(options?: TerseMongoOptions): void;
|
|
77
|
+
/**
|
|
78
|
+
* Remove TerseJSON patches from MongoDB driver.
|
|
79
|
+
*
|
|
80
|
+
* Useful for testing or when you need to temporarily disable compression.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* import { terseMongo, unterse } from 'tersejson/mongodb';
|
|
85
|
+
*
|
|
86
|
+
* await terseMongo();
|
|
87
|
+
* // ... queries are Proxy-wrapped
|
|
88
|
+
*
|
|
89
|
+
* await unterse();
|
|
90
|
+
* // ... queries return normal documents
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
declare function unterse(): Promise<void>;
|
|
94
|
+
/**
|
|
95
|
+
* Check if TerseJSON MongoDB patching is active
|
|
96
|
+
*/
|
|
97
|
+
declare function isTerseMongoActive(): boolean;
|
|
98
|
+
/**
|
|
99
|
+
* Get current TerseJSON MongoDB options
|
|
100
|
+
*/
|
|
101
|
+
declare function getTerseMongoOptions(): TerseMongoOptions;
|
|
102
|
+
/**
|
|
103
|
+
* Update TerseJSON MongoDB options without re-patching
|
|
104
|
+
*/
|
|
105
|
+
declare function setTerseMongoOptions(options: Partial<TerseMongoOptions>): void;
|
|
106
|
+
|
|
107
|
+
export { type TerseMongoOptions, getTerseMongoOptions, isTerseMongoActive, setTerseMongoOptions, terseMongo, terseMongoSync, unterse };
|