mdbxmou 0.3.5 → 0.3.7
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 +96 -40
- package/package.json +1 -1
- package/src/dbi.cpp +0 -1
- package/src/txnmou.cpp +1 -0
package/README.md
CHANGED
|
@@ -24,29 +24,33 @@ CommonJS:
|
|
|
24
24
|
```javascript
|
|
25
25
|
const { MDBX_Env, MDBX_Param } = require('mdbxmou');
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
async function main() {
|
|
28
|
+
// Create environment
|
|
29
|
+
const env = new MDBX_Env();
|
|
30
|
+
await env.open({
|
|
31
|
+
path: './data',
|
|
32
|
+
keyFlag: MDBX_Param.keyFlag.string, // Default key encoding (optional)
|
|
33
|
+
valueFlag: MDBX_Param.valueFlag.string // Default value encoding (optional)
|
|
34
|
+
});
|
|
34
35
|
|
|
35
|
-
// Write data
|
|
36
|
-
const txn = env.startWrite();
|
|
37
|
-
const dbi = txn.createMap(MDBX_Param.keyMode.ordinal);
|
|
38
|
-
dbi.put(txn, 1, "hello");
|
|
39
|
-
dbi.put(txn, 2, "world");
|
|
40
|
-
txn.commit();
|
|
36
|
+
// Write data
|
|
37
|
+
const txn = env.startWrite();
|
|
38
|
+
const dbi = txn.createMap(MDBX_Param.keyMode.ordinal);
|
|
39
|
+
dbi.put(txn, 1, "hello");
|
|
40
|
+
dbi.put(txn, 2, "world");
|
|
41
|
+
txn.commit();
|
|
41
42
|
|
|
42
|
-
// Read data
|
|
43
|
-
const readTxn = env.startRead();
|
|
44
|
-
const readDbi = readTxn.openMap(BigInt(MDBX_Param.keyMode.ordinal));
|
|
45
|
-
const value = readDbi.get(readTxn, 1);
|
|
46
|
-
console.log(value); // "hello"
|
|
47
|
-
readTxn.commit();
|
|
43
|
+
// Read data
|
|
44
|
+
const readTxn = env.startRead();
|
|
45
|
+
const readDbi = readTxn.openMap(BigInt(MDBX_Param.keyMode.ordinal));
|
|
46
|
+
const value = readDbi.get(readTxn, 1);
|
|
47
|
+
console.log(value); // "hello"
|
|
48
|
+
readTxn.commit();
|
|
48
49
|
|
|
49
|
-
await env.close();
|
|
50
|
+
await env.close();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
main().catch(console.error);
|
|
50
54
|
```
|
|
51
55
|
|
|
52
56
|
ESM:
|
|
@@ -185,8 +189,9 @@ const result = await env.query([
|
|
|
185
189
|
#### Methods
|
|
186
190
|
|
|
187
191
|
**createMap([db_name | keyMode], [keyMode | valueMode], [valueMode]) → DBI**
|
|
192
|
+
**createMap({ name, keyFlag, valueFlag, keyMode, valueMode }) → DBI**
|
|
188
193
|
```javascript
|
|
189
|
-
// No arguments - default DB with
|
|
194
|
+
// No arguments - default DB with env key/value flags (buffer if not set)
|
|
190
195
|
const dbi = txn.createMap();
|
|
191
196
|
|
|
192
197
|
// One argument - keyMode only
|
|
@@ -203,13 +208,23 @@ const namedDbi = txn.createMap("my-table", MDBX_Param.keyMode.ordinal);
|
|
|
203
208
|
|
|
204
209
|
// Three arguments - db_name + keyMode + valueMode
|
|
205
210
|
const namedDbi = txn.createMap("my-table", MDBX_Param.keyMode.ordinal, MDBX_Param.valueMode.multi);
|
|
211
|
+
|
|
212
|
+
// Object form - explicit flags/modes
|
|
213
|
+
const dbi = txn.createMap({
|
|
214
|
+
name: "my-table",
|
|
215
|
+
keyFlag: MDBX_Param.keyFlag.string,
|
|
216
|
+
valueFlag: MDBX_Param.valueFlag.string,
|
|
217
|
+
keyMode: MDBX_Param.keyMode.reverse,
|
|
218
|
+
valueMode: MDBX_Param.valueMode.multi
|
|
219
|
+
});
|
|
206
220
|
```
|
|
207
221
|
|
|
208
222
|
> **Note**: Use `createMap` in write transactions - it will create the database if it doesn't exist, or open it if it does. This is safer for new environments.
|
|
209
223
|
|
|
210
224
|
**openMap([db_name | keyMode], [keyMode]) → DBI**
|
|
225
|
+
**openMap({ name, keyFlag, valueFlag, keyMode, valueMode }) → DBI**
|
|
211
226
|
```javascript
|
|
212
|
-
// No arguments - default DB with
|
|
227
|
+
// No arguments - default DB with env key/value flags (buffer if not set)
|
|
213
228
|
const dbi = txn.openMap();
|
|
214
229
|
|
|
215
230
|
// One argument - keyMode only
|
|
@@ -224,6 +239,15 @@ const namedDbi = txn.openMap("my-table");
|
|
|
224
239
|
// Two arguments - db_name + keyMode
|
|
225
240
|
const namedDbi = txn.openMap("my-table", MDBX_Param.keyMode.ordinal);
|
|
226
241
|
const namedDbiBigInt = txn.openMap("my-table", BigInt(MDBX_Param.keyMode.ordinal));
|
|
242
|
+
|
|
243
|
+
// Object form - explicit flags/modes
|
|
244
|
+
const dbi = txn.openMap({
|
|
245
|
+
name: "my-table",
|
|
246
|
+
keyFlag: MDBX_Param.keyFlag.string,
|
|
247
|
+
valueFlag: MDBX_Param.valueFlag.string,
|
|
248
|
+
keyMode: MDBX_Param.keyMode.reverse,
|
|
249
|
+
valueMode: MDBX_Param.valueMode.multi
|
|
250
|
+
});
|
|
227
251
|
```
|
|
228
252
|
|
|
229
253
|
> **Note**: Use `openMap` in read transactions or when you're sure the database already exists. For write transactions on new environments, prefer `createMap`.
|
|
@@ -231,6 +255,7 @@ const namedDbiBigInt = txn.openMap("my-table", BigInt(MDBX_Param.keyMode.ordinal
|
|
|
231
255
|
> **Note**: When using ordinal keyMode, the key type in results depends on how you specify keyMode:
|
|
232
256
|
> - `keyMode: number` → keys returned as `number`
|
|
233
257
|
> - `keyMode: BigInt(number)` → keys returned as `BigInt`
|
|
258
|
+
> - When you pass `keyMode.ordinal` as a positional argument (Number/BigInt), it also updates `keyFlag` to number/bigint unless a numeric keyFlag was already set in env or explicitly provided.
|
|
234
259
|
|
|
235
260
|
**commit()**
|
|
236
261
|
```javascript
|
|
@@ -607,6 +632,9 @@ async function asyncExample() {
|
|
|
607
632
|
readTxn.commit();
|
|
608
633
|
await env.close();
|
|
609
634
|
}
|
|
635
|
+
|
|
636
|
+
syncExample();
|
|
637
|
+
asyncExample().catch(console.error);
|
|
610
638
|
```
|
|
611
639
|
|
|
612
640
|
### Key Type Behavior
|
|
@@ -649,6 +677,8 @@ function keyTypesExample() {
|
|
|
649
677
|
|
|
650
678
|
env.closeSync();
|
|
651
679
|
}
|
|
680
|
+
|
|
681
|
+
keyTypesExample();
|
|
652
682
|
```
|
|
653
683
|
|
|
654
684
|
### Cursor Operations
|
|
@@ -702,6 +732,8 @@ function cursorExample() {
|
|
|
702
732
|
readTxn.commit();
|
|
703
733
|
env.closeSync();
|
|
704
734
|
}
|
|
735
|
+
|
|
736
|
+
cursorExample();
|
|
705
737
|
```
|
|
706
738
|
|
|
707
739
|
### Query API (Advanced Async)
|
|
@@ -721,7 +753,7 @@ async function queryExample() {
|
|
|
721
753
|
// Async query with DBI object (not database name)
|
|
722
754
|
const results = await env.query([
|
|
723
755
|
{
|
|
724
|
-
dbi
|
|
756
|
+
dbi,
|
|
725
757
|
mode: MDBX_Param.queryMode.insertUnique,
|
|
726
758
|
item: [
|
|
727
759
|
{ key: 1, value: JSON.stringify({ name: "Alice" }) },
|
|
@@ -729,7 +761,7 @@ async function queryExample() {
|
|
|
729
761
|
]
|
|
730
762
|
},
|
|
731
763
|
{
|
|
732
|
-
dbi
|
|
764
|
+
dbi,
|
|
733
765
|
mode: MDBX_Param.queryMode.get,
|
|
734
766
|
item: [
|
|
735
767
|
{ key: 1 },
|
|
@@ -741,6 +773,8 @@ async function queryExample() {
|
|
|
741
773
|
console.log('Query results:', JSON.stringify(results, null, 2));
|
|
742
774
|
await env.close();
|
|
743
775
|
}
|
|
776
|
+
|
|
777
|
+
queryExample().catch(console.error);
|
|
744
778
|
```
|
|
745
779
|
|
|
746
780
|
### Async Keys API
|
|
@@ -767,7 +801,7 @@ async function keysExample() {
|
|
|
767
801
|
console.log("All keys:", allKeys);
|
|
768
802
|
|
|
769
803
|
// Get keys with DBI object parameter
|
|
770
|
-
const allKeys2 = await env.keys({ dbi
|
|
804
|
+
const allKeys2 = await env.keys({ dbi });
|
|
771
805
|
console.log("All keys (object):", allKeys2);
|
|
772
806
|
|
|
773
807
|
// Get keys from multiple DBIs
|
|
@@ -776,32 +810,54 @@ async function keysExample() {
|
|
|
776
810
|
|
|
777
811
|
// Get limited keys from specific position
|
|
778
812
|
const limitedKeys = await env.keys([
|
|
779
|
-
{ dbi
|
|
813
|
+
{ dbi, limit: 3, from: 5 }
|
|
780
814
|
]);
|
|
781
815
|
console.log("Limited keys:", limitedKeys);
|
|
782
816
|
|
|
783
817
|
await env.close();
|
|
784
818
|
}
|
|
819
|
+
|
|
820
|
+
keysExample().catch(console.error);
|
|
785
821
|
```
|
|
786
822
|
|
|
787
823
|
## Error Handling
|
|
788
824
|
|
|
789
825
|
```javascript
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
826
|
+
async function errorHandlingExample() {
|
|
827
|
+
let txn;
|
|
828
|
+
try {
|
|
829
|
+
const env = new MDBX_Env();
|
|
830
|
+
await env.open({ path: './data' });
|
|
831
|
+
|
|
832
|
+
txn = env.startWrite();
|
|
833
|
+
const dbi = txn.createMap(MDBX_Param.keyMode.ordinal);
|
|
834
|
+
|
|
835
|
+
// This might throw if key already exists with MDBX_NOOVERWRITE
|
|
836
|
+
dbi.put(txn, 123, "value");
|
|
837
|
+
|
|
838
|
+
txn.commit();
|
|
839
|
+
} catch (error) {
|
|
840
|
+
console.error('Database error:', error.message);
|
|
841
|
+
if (txn) txn.abort();
|
|
842
|
+
}
|
|
804
843
|
}
|
|
844
|
+
|
|
845
|
+
errorHandlingExample().catch(console.error);
|
|
846
|
+
```
|
|
847
|
+
|
|
848
|
+
## Runnable README Tests
|
|
849
|
+
|
|
850
|
+
The examples above have matching runnable tests in `test/`. You can execute them directly:
|
|
851
|
+
|
|
852
|
+
```bash
|
|
853
|
+
node test/readme-quick-start.js
|
|
854
|
+
node test/readme-sync-example.js
|
|
855
|
+
node test/readme-async-example.js
|
|
856
|
+
node test/readme-key-types.js
|
|
857
|
+
node test/readme-cursor-example.js
|
|
858
|
+
node test/readme-query-example.js
|
|
859
|
+
node test/readme-keys-example.js
|
|
860
|
+
node test/readme-error-handling.js
|
|
805
861
|
```
|
|
806
862
|
|
|
807
863
|
## Configuration Options
|
package/package.json
CHANGED
package/src/dbi.cpp
CHANGED
package/src/txnmou.cpp
CHANGED
|
@@ -102,6 +102,7 @@ Napi::Value txnmou::get_dbi(const Napi::Object& arg0, db_mode db_mode)
|
|
|
102
102
|
{
|
|
103
103
|
auto env = arg0.Env();
|
|
104
104
|
auto conf = get_env_userctx(*env_);
|
|
105
|
+
// параметры по умолчанию из окружения
|
|
105
106
|
auto key_flag = conf->key_flag;
|
|
106
107
|
auto value_flag = conf->value_flag;
|
|
107
108
|
key_mode key_mode{};
|