@harperfast/rocksdb-js 0.1.2 → 0.1.4

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 CHANGED
@@ -91,6 +91,26 @@ RocksDatabase.config({
91
91
  });
92
92
  ```
93
93
 
94
+ ### `db.isOpen(): boolean`
95
+
96
+ Returns `true` if the database is open, otherwise false.
97
+
98
+ ```typescript
99
+ console.log(db.isOpen()); // true or false
100
+ ```
101
+
102
+ ### `db.name: string`
103
+
104
+ Returns the database column family's name.
105
+
106
+ ```typescript
107
+ const db = new RocksDatabase('path/to/db');
108
+ console.log(db.name); // 'default'
109
+
110
+ const db2 = new RocksDatabase('path/to/db', { name: 'users' });
111
+ console.log(db.name); // 'users'
112
+ ```
113
+
94
114
  ### `db.open(): RocksDatabase`
95
115
 
96
116
  Opens the database at the given path. This must be called before performing any data operations.
@@ -108,16 +128,12 @@ There's also a static `open()` method for convenience that performs the same thi
108
128
  const db = RocksDatabase.open('path/to/db');
109
129
  ```
110
130
 
111
- ### `db.name: string`
131
+ ### `db.status: 'opened' | 'closed'`
112
132
 
113
- Returns the database column family's name.
133
+ Returns a string `'opened'` or `'closed'` indicating if the database is opened or closed.
114
134
 
115
135
  ```typescript
116
- const db = new RocksDatabase('path/to/db');
117
- console.log(db.name); // 'default'
118
-
119
- const db2 = new RocksDatabase('path/to/db', { name: 'users' });
120
- console.log(db.name); // 'users'
136
+ console.log(db.status);
121
137
  ```
122
138
 
123
139
  ## Data Operations
@@ -156,6 +172,16 @@ const entriesRemoved = db.clearSync();
156
172
  console.log(entriesRemoved); // 10
157
173
  ```
158
174
 
175
+ ### `db.destroy(): void`
176
+
177
+ Completely removes a database based on the `db` instance's path including all data, column families,
178
+ and files on disk.
179
+
180
+ ```typescript
181
+ db.destroy();
182
+ console.log(fs.existsSync(db.path)); // false
183
+ ```
184
+
159
185
  ### `db.drop(): Promise<void>`
160
186
 
161
187
  Removes all entries in the database. If the database was opened with a `name`, the database will be
@@ -847,6 +873,24 @@ Returns an object containing all of the information in the log file.
847
873
  - `length: number` The size of the entry data.
848
874
  - `timestamp: number` The entry timestamp.
849
875
 
876
+ ### `registryStatus(): RegistryStatus`
877
+
878
+ Returns an array containing that status of all active RocksDB instances.
879
+
880
+ - `path: string` The database path.
881
+ - `refCount: number` The number of JavaScript database instances plus the registry's reference.
882
+ - `columnFamiles: string[]` A list of the database's column families.
883
+ - `transactions: number` The count of active transactions.
884
+ - `closables: number` The count of active database, transactions, and iterators.
885
+ - `locks: number` The count of active locks.
886
+ - `userSharedBuffers: number` The count of active user shared buffers.
887
+ - `listenerCallbacks: number` The count of in-flight callbacks.
888
+
889
+ ```typescript
890
+ import { registryStatus } from '@harperfast/rocksdb-js';
891
+ console.log(registryStatus());
892
+ ```
893
+
850
894
  ### `shutdown(): void`
851
895
 
852
896
  The `shutdown()` will flush all in-memory data to disk and wait for any outstanding compactions to
@@ -858,6 +902,15 @@ import { shutdown } from '@harperfast/rocksdb-js';
858
902
  process.on('exit', shutdown);
859
903
  ```
860
904
 
905
+ ### `versions: { 'rocksdb': string; 'rocksdb-js': string }`
906
+
907
+ Returns the `rocksdb-js` and RocksDB version.
908
+
909
+ ```typescript
910
+ import { versions } from '@harperfast/rocksdb-js';
911
+ console.log(versions); // { "rocksdb": "10.10.1", "rocksdb-js": "0.1.2" }
912
+ ```
913
+
861
914
  ## Custom Store
862
915
 
863
916
  The store is a class that sits between the `RocksDatabase` or `Transaction` instance and the native
@@ -874,6 +927,8 @@ The default `Store` contains the following methods which can be overridden:
874
927
  - `encodeValue(value)`
875
928
  - `get(context, key, resolve, reject, txnId?)`
876
929
  - `getCount(context, options?, txnId?)`
930
+ - `getKeys(context, options?)`
931
+ - `getKeysCount(context, options?)`
877
932
  - `getRange(context, options?)`
878
933
  - `getSync(context, key, options?)`
879
934
  - `getUserSharedBuffer(key, defaultBuffer?)`
package/dist/index.cjs CHANGED
@@ -1,4 +1,5 @@
1
- //#region rolldown:runtime
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2
+ //#region \0rolldown/runtime.js
2
3
  var __create = Object.create;
3
4
  var __defProp = Object.defineProperty;
4
5
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -79,8 +80,9 @@ const NativeDatabase = binding.Database;
79
80
  const NativeIterator = binding.Iterator;
80
81
  const NativeTransaction = binding.Transaction;
81
82
  const TransactionLog = binding.TransactionLog;
82
- const version = binding.version;
83
+ const registryStatus = binding.registryStatus;
83
84
  const shutdown = binding.shutdown;
85
+ const version = binding.version;
84
86
 
85
87
  //#endregion
86
88
  //#region src/util.ts
@@ -168,7 +170,7 @@ var DBI = class DBI {
168
170
  /**
169
171
  * The RocksDB context for `get()`, `put()`, and `remove()`.
170
172
  */
171
- #context;
173
+ _context;
172
174
  /**
173
175
  * The database store instance. The store instance is tied to the database
174
176
  * instance and shared with transaction instances.
@@ -183,7 +185,7 @@ var DBI = class DBI {
183
185
  constructor(store, transaction) {
184
186
  if (new.target === DBI) throw new Error("DBI is an abstract class and cannot be instantiated directly");
185
187
  this.store = store;
186
- this.#context = transaction || store.db;
188
+ this._context = transaction || store.db;
187
189
  }
188
190
  /**
189
191
  * Adds a listener for the given key.
@@ -214,14 +216,14 @@ var DBI = class DBI {
214
216
  */
215
217
  getBinary(key, options) {
216
218
  if (!this.store.isOpen()) return Promise.reject(/* @__PURE__ */ new Error("Database not open"));
217
- return this.store.get(this.#context, key, true, this.store.getTxnId(options));
219
+ return this.store.get(this._context, key, true, this.store.getTxnId(options));
218
220
  }
219
221
  /**
220
222
  * Synchronously retrieves the binary data for the given key.
221
223
  */
222
224
  getBinarySync(key, options) {
223
225
  if (!this.store.isOpen()) throw new Error("Database not open");
224
- return this.store.getSync(this.#context, key, true, options);
226
+ return this.store.getSync(this._context, key, true, options);
225
227
  }
226
228
  /**
227
229
  * Retrieves the binary data for the given key using a preallocated,
@@ -235,7 +237,7 @@ var DBI = class DBI {
235
237
  */
236
238
  getBinaryFast(key, options) {
237
239
  if (!this.store.isOpen()) return Promise.reject(/* @__PURE__ */ new Error("Database not open"));
238
- return this.store.get(this.#context, key, false, this.store.getTxnId(options));
240
+ return this.store.get(this._context, key, false, this.store.getTxnId(options));
239
241
  }
240
242
  /**
241
243
  * Synchronously retrieves the binary data for the given key using a
@@ -244,16 +246,13 @@ var DBI = class DBI {
244
246
  */
245
247
  getBinaryFastSync(key, options) {
246
248
  if (!this.store.isOpen()) throw new Error("Database not open");
247
- return this.store.getSync(this.#context, key, false, options);
249
+ return this.store.getSync(this._context, key, false, options);
248
250
  }
249
251
  /**
250
252
  * Retrieves all keys within a range.
251
253
  */
252
254
  getKeys(options) {
253
- return this.store.getRange(this.#context, {
254
- ...options,
255
- values: false
256
- }).map((item) => item.key);
255
+ return this.store.getKeys(this._context, options);
257
256
  }
258
257
  /**
259
258
  * Retrieves the number of keys within a range.
@@ -268,7 +267,7 @@ var DBI = class DBI {
268
267
  * ```
269
268
  */
270
269
  getKeysCount(options) {
271
- return this.store.getCount(this.#context, options);
270
+ return this.store.getKeysCount(this._context, options);
272
271
  }
273
272
  /**
274
273
  * Retrieves a range of keys and their values.
@@ -288,7 +287,7 @@ var DBI = class DBI {
288
287
  * ```
289
288
  */
290
289
  getRange(options) {
291
- return this.store.getRange(this.#context, options);
290
+ return this.store.getRange(this._context, options);
292
291
  }
293
292
  /**
294
293
  * Synchronously retrieves the value for the given key, then returns the
@@ -305,7 +304,7 @@ var DBI = class DBI {
305
304
  return result ? this.store.decodeValue(result) : void 0;
306
305
  }
307
306
  if (!this.store.isOpen()) throw new Error("Database not open");
308
- return this.store.decodeValue(this.store.getSync(this.#context, key, true, options));
307
+ return this.store.decodeValue(this.store.getSync(this._context, key, true, options));
309
308
  }
310
309
  /**
311
310
  * Gets the number of listeners for the given key.
@@ -374,7 +373,7 @@ var DBI = class DBI {
374
373
  * ```
375
374
  */
376
375
  async put(key, value, options) {
377
- return this.store.putSync(this.#context, key, value, options);
376
+ return this.store.putSync(this._context, key, value, options);
378
377
  }
379
378
  /**
380
379
  * Synchronously stores a value for the given key.
@@ -390,7 +389,7 @@ var DBI = class DBI {
390
389
  * ```
391
390
  */
392
391
  putSync(key, value, options) {
393
- return this.store.putSync(this.#context, key, value, options);
392
+ return this.store.putSync(this._context, key, value, options);
394
393
  }
395
394
  /**
396
395
  * Removes a value for the given key. If the key does not exist, it will
@@ -406,7 +405,7 @@ var DBI = class DBI {
406
405
  * ```
407
406
  */
408
407
  async remove(key, options) {
409
- return this.store.removeSync(this.#context, key, options);
408
+ return this.store.removeSync(this._context, key, options);
410
409
  }
411
410
  /**
412
411
  * Removes a value for the given key. If the key does not exist, it will
@@ -422,7 +421,7 @@ var DBI = class DBI {
422
421
  * ```
423
422
  */
424
423
  removeSync(key, options) {
425
- return this.store.removeSync(this.#context, key, options);
424
+ return this.store.removeSync(this._context, key, options);
426
425
  }
427
426
  /**
428
427
  * Removes an event listener. You must specify the exact same callback that was
@@ -441,7 +440,7 @@ var DBI = class DBI {
441
440
  * @returns The transaction log.
442
441
  */
443
442
  useLog(name) {
444
- return this.store.useLog(this.#context, name);
443
+ return this.store.useLog(this._context, name);
445
444
  }
446
445
  };
447
446
 
@@ -798,6 +797,15 @@ var Store = class {
798
797
  }
799
798
  return context.getCount(options, this.getTxnId(options));
800
799
  }
800
+ getKeys(context, options) {
801
+ return this.getRange(context, {
802
+ ...options,
803
+ values: false
804
+ }).map((item) => item.key);
805
+ }
806
+ getKeysCount(context, options) {
807
+ return this.getCount(context, options);
808
+ }
801
809
  getRange(context, options) {
802
810
  if (!this.db.opened) throw new Error("Database not open");
803
811
  options = { ...options };
@@ -1099,7 +1107,6 @@ var RocksDatabase = class RocksDatabase extends DBI {
1099
1107
  * ```
1100
1108
  */
1101
1109
  clear() {
1102
- if (!this.store.db.opened) return Promise.reject(/* @__PURE__ */ new Error("Database not open"));
1103
1110
  if (this.store.encoder?.structures !== void 0) this.store.encoder.structures = [];
1104
1111
  return new Promise((resolve, reject) => {
1105
1112
  this.store.db.clear(resolve, reject);
@@ -1115,9 +1122,8 @@ var RocksDatabase = class RocksDatabase extends DBI {
1115
1122
  * ```
1116
1123
  */
1117
1124
  clearSync() {
1118
- if (!this.store.db.opened) throw new Error("Database not open");
1119
1125
  if (this.store.encoder?.structures !== void 0) this.store.encoder.structures = [];
1120
- return this.store.db.clearSync();
1126
+ this.store.db.clearSync();
1121
1127
  }
1122
1128
  /**
1123
1129
  * Closes the database.
@@ -1144,14 +1150,15 @@ var RocksDatabase = class RocksDatabase extends DBI {
1144
1150
  static config(options) {
1145
1151
  config(options);
1146
1152
  }
1153
+ destroy() {
1154
+ this.store.db.destroy();
1155
+ }
1147
1156
  async drop() {
1148
- if (!this.store.db.opened) return Promise.reject(/* @__PURE__ */ new Error("Database not open"));
1149
1157
  return new Promise((resolve, reject) => {
1150
1158
  this.store.db.drop(resolve, reject);
1151
1159
  });
1152
1160
  }
1153
1161
  dropSync() {
1154
- if (!this.store.db.opened) throw new Error("Database not open");
1155
1162
  return this.store.db.dropSync();
1156
1163
  }
1157
1164
  get encoder() {
@@ -1410,6 +1417,12 @@ var RocksDatabase = class RocksDatabase extends DBI {
1410
1417
  return this.store.db.purgeLogs(options);
1411
1418
  }
1412
1419
  /**
1420
+ * The status of the database.
1421
+ */
1422
+ get status() {
1423
+ return this.store.isOpen() ? "open" : "closed";
1424
+ }
1425
+ /**
1413
1426
  * Executes all operations in the callback as a single transaction.
1414
1427
  *
1415
1428
  * @param callback - A async function that receives the transaction as an argument.
@@ -1798,10 +1811,11 @@ function loadLastPosition(transactionLog, readUncommitted) {
1798
1811
  //#region src/index.ts
1799
1812
  const versions = {
1800
1813
  rocksdb: version,
1801
- "rocksdb-js": "0.1.2"
1814
+ "rocksdb-js": "0.1.4"
1802
1815
  };
1803
1816
 
1804
1817
  //#endregion
1818
+ exports.DBI = DBI;
1805
1819
  exports.DBIterator = DBIterator;
1806
1820
  exports.RocksDatabase = RocksDatabase;
1807
1821
  exports.Store = Store;
@@ -1809,6 +1823,7 @@ exports.Transaction = Transaction;
1809
1823
  exports.TransactionLog = TransactionLog;
1810
1824
  exports.constants = constants;
1811
1825
  exports.parseTransactionLog = parseTransactionLog;
1826
+ exports.registryStatus = registryStatus;
1812
1827
  exports.shutdown = shutdown;
1813
1828
  exports.versions = versions;
1814
1829
  //# sourceMappingURL=index.cjs.map