@harperfast/rocksdb-js 1.0.1 → 1.1.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.
package/dist/index.d.cts CHANGED
@@ -75,6 +75,11 @@ interface StoreOptions extends Omit<NativeDatabaseOptions, "mode" | "transaction
75
75
  * If `true`, the encoder will use a random access structure.
76
76
  */
77
77
  randomAccessStructure?: boolean;
78
+ /**
79
+ * When `true`, the database is opened in read-only mode. Write operations
80
+ * will throw an error with code `ERR_DATABASE_READONLY`.
81
+ */
82
+ readOnly?: boolean;
78
83
  sharedStructuresKey?: symbol;
79
84
  /**
80
85
  * A string containing the amount of time or the number of milliseconds to
@@ -180,6 +185,11 @@ declare class Store {
180
185
  */
181
186
  pessimistic: boolean;
182
187
  /**
188
+ * Whether the database is open in readonly mode. When `true`, write
189
+ * operations will throw an error with code `ERR_DATABASE_READONLY`.
190
+ */
191
+ readOnly: boolean;
192
+ /**
183
193
  * Encoder specific flag used to signal that the encoder should use a random
184
194
  * access structure.
185
195
  */
@@ -416,7 +426,7 @@ type TransactionEntry = {
416
426
  endTxn: boolean;
417
427
  };
418
428
  type TransactionLog = {
419
- new (name: string): TransactionLog;
429
+ new (db: NativeDatabase, name: string): TransactionLog;
420
430
  addEntry(data: Buffer | Uint8Array, txnId?: number): void;
421
431
  getLogFileSize(sequenceId?: number): number;
422
432
  name: string;
@@ -438,6 +448,7 @@ type NativeDatabaseOptions = {
438
448
  name?: string;
439
449
  noBlockCache?: boolean;
440
450
  parallelismThreads?: number;
451
+ readOnly?: boolean;
441
452
  statsLevel?: (typeof stats.StatsLevel)[keyof typeof stats.StatsLevel];
442
453
  transactionLogMaxAgeThreshold?: number;
443
454
  transactionLogMaxSize?: number;
@@ -911,6 +922,8 @@ interface RocksDatabaseOptions extends StoreOptions {
911
922
  */
912
923
  name?: string;
913
924
  }
925
+ type RocksDBStat = number | StatsHistogramData;
926
+ type RocksDBStats = Record<string, RocksDBStat>;
914
927
  /**
915
928
  * The main class for interacting with a RocksDB database.
916
929
  *
@@ -1032,7 +1045,7 @@ declare class RocksDatabase extends DBI<DBITransactional> {
1032
1045
  * @param statName - The name of the statistic to retrieve.
1033
1046
  * @returns The statistic value.
1034
1047
  */
1035
- getStat(statName: string): number | StatsHistogramData;
1048
+ getStat(statName: string): RocksDBStat;
1036
1049
  /**
1037
1050
  * Gets the RocksDB statistics. Requires statistics to be enabled.
1038
1051
  *
@@ -1042,7 +1055,7 @@ declare class RocksDatabase extends DBI<DBITransactional> {
1042
1055
  * const stats = db.getStats();
1043
1056
  * ```
1044
1057
  */
1045
- getStats(all?: boolean): Record<string, number | StatsHistogramData>;
1058
+ getStats(all?: boolean): RocksDBStats;
1046
1059
  /**
1047
1060
  * Gets or creates a buffer that can be shared across worker threads.
1048
1061
  *
@@ -1096,6 +1109,10 @@ declare class RocksDatabase extends DBI<DBITransactional> {
1096
1109
  */
1097
1110
  get name(): string;
1098
1111
  /**
1112
+ * Whether the database is open in readonly mode.
1113
+ */
1114
+ get readOnly(): boolean;
1115
+ /**
1099
1116
  * Sugar method for opening a database.
1100
1117
  *
1101
1118
  * @param pathOrStore - The filesystem path to the database or a custom store.
@@ -1246,5 +1263,5 @@ declare const versions: {
1246
1263
  "rocksdb-js": string;
1247
1264
  };
1248
1265
  //#endregion
1249
- export { DBI, DBIterator, type IteratorOptions, type Key, RocksDatabase, type RocksDatabaseOptions, type StatsHistogramData, Store, type StoreContext, type StoreGetOptions, type StoreIteratorOptions, type StorePutOptions, type StoreRangeOptions, type StoreRemoveOptions, Transaction, type TransactionEntry, TransactionLog, constants, currentThreadId, parseTransactionLog, registryStatus, shutdown, stats, versions };
1266
+ export { DBI, DBIterator, type IteratorOptions, type Key, type RocksDBStat, type RocksDBStats, RocksDatabase, type RocksDatabaseOptions, type StatsHistogramData, Store, type StoreContext, type StoreGetOptions, type StoreIteratorOptions, type StorePutOptions, type StoreRangeOptions, type StoreRemoveOptions, Transaction, type TransactionEntry, TransactionLog, constants, currentThreadId, parseTransactionLog, registryStatus, shutdown, stats, versions };
1250
1267
  //# sourceMappingURL=index.d.cts.map
package/dist/index.d.mts CHANGED
@@ -75,6 +75,11 @@ interface StoreOptions extends Omit<NativeDatabaseOptions, "mode" | "transaction
75
75
  * If `true`, the encoder will use a random access structure.
76
76
  */
77
77
  randomAccessStructure?: boolean;
78
+ /**
79
+ * When `true`, the database is opened in read-only mode. Write operations
80
+ * will throw an error with code `ERR_DATABASE_READONLY`.
81
+ */
82
+ readOnly?: boolean;
78
83
  sharedStructuresKey?: symbol;
79
84
  /**
80
85
  * A string containing the amount of time or the number of milliseconds to
@@ -180,6 +185,11 @@ declare class Store {
180
185
  */
181
186
  pessimistic: boolean;
182
187
  /**
188
+ * Whether the database is open in readonly mode. When `true`, write
189
+ * operations will throw an error with code `ERR_DATABASE_READONLY`.
190
+ */
191
+ readOnly: boolean;
192
+ /**
183
193
  * Encoder specific flag used to signal that the encoder should use a random
184
194
  * access structure.
185
195
  */
@@ -416,7 +426,7 @@ type TransactionEntry = {
416
426
  endTxn: boolean;
417
427
  };
418
428
  type TransactionLog = {
419
- new (name: string): TransactionLog;
429
+ new (db: NativeDatabase, name: string): TransactionLog;
420
430
  addEntry(data: Buffer | Uint8Array, txnId?: number): void;
421
431
  getLogFileSize(sequenceId?: number): number;
422
432
  name: string;
@@ -438,6 +448,7 @@ type NativeDatabaseOptions = {
438
448
  name?: string;
439
449
  noBlockCache?: boolean;
440
450
  parallelismThreads?: number;
451
+ readOnly?: boolean;
441
452
  statsLevel?: (typeof stats.StatsLevel)[keyof typeof stats.StatsLevel];
442
453
  transactionLogMaxAgeThreshold?: number;
443
454
  transactionLogMaxSize?: number;
@@ -911,6 +922,8 @@ interface RocksDatabaseOptions extends StoreOptions {
911
922
  */
912
923
  name?: string;
913
924
  }
925
+ type RocksDBStat = number | StatsHistogramData;
926
+ type RocksDBStats = Record<string, RocksDBStat>;
914
927
  /**
915
928
  * The main class for interacting with a RocksDB database.
916
929
  *
@@ -1032,7 +1045,7 @@ declare class RocksDatabase extends DBI<DBITransactional> {
1032
1045
  * @param statName - The name of the statistic to retrieve.
1033
1046
  * @returns The statistic value.
1034
1047
  */
1035
- getStat(statName: string): number | StatsHistogramData;
1048
+ getStat(statName: string): RocksDBStat;
1036
1049
  /**
1037
1050
  * Gets the RocksDB statistics. Requires statistics to be enabled.
1038
1051
  *
@@ -1042,7 +1055,7 @@ declare class RocksDatabase extends DBI<DBITransactional> {
1042
1055
  * const stats = db.getStats();
1043
1056
  * ```
1044
1057
  */
1045
- getStats(all?: boolean): Record<string, number | StatsHistogramData>;
1058
+ getStats(all?: boolean): RocksDBStats;
1046
1059
  /**
1047
1060
  * Gets or creates a buffer that can be shared across worker threads.
1048
1061
  *
@@ -1096,6 +1109,10 @@ declare class RocksDatabase extends DBI<DBITransactional> {
1096
1109
  */
1097
1110
  get name(): string;
1098
1111
  /**
1112
+ * Whether the database is open in readonly mode.
1113
+ */
1114
+ get readOnly(): boolean;
1115
+ /**
1099
1116
  * Sugar method for opening a database.
1100
1117
  *
1101
1118
  * @param pathOrStore - The filesystem path to the database or a custom store.
@@ -1246,5 +1263,5 @@ declare const versions: {
1246
1263
  "rocksdb-js": string;
1247
1264
  };
1248
1265
  //#endregion
1249
- export { DBI, DBIterator, type IteratorOptions, type Key, RocksDatabase, type RocksDatabaseOptions, type StatsHistogramData, Store, type StoreContext, type StoreGetOptions, type StoreIteratorOptions, type StorePutOptions, type StoreRangeOptions, type StoreRemoveOptions, Transaction, type TransactionEntry, TransactionLog, constants, currentThreadId, parseTransactionLog, registryStatus, shutdown, stats, versions };
1266
+ export { DBI, DBIterator, type IteratorOptions, type Key, type RocksDBStat, type RocksDBStats, RocksDatabase, type RocksDatabaseOptions, type StatsHistogramData, Store, type StoreContext, type StoreGetOptions, type StoreIteratorOptions, type StorePutOptions, type StoreRangeOptions, type StoreRemoveOptions, Transaction, type TransactionEntry, TransactionLog, constants, currentThreadId, parseTransactionLog, registryStatus, shutdown, stats, versions };
1250
1267
  //# sourceMappingURL=index.d.mts.map
package/dist/index.mjs CHANGED
@@ -629,6 +629,11 @@ var Store = class {
629
629
  */
630
630
  pessimistic;
631
631
  /**
632
+ * Whether the database is open in readonly mode. When `true`, write
633
+ * operations will throw an error with code `ERR_DATABASE_READONLY`.
634
+ */
635
+ readOnly;
636
+ /**
632
637
  * Encoder specific flag used to signal that the encoder should use a random
633
638
  * access structure.
634
639
  */
@@ -697,6 +702,7 @@ var Store = class {
697
702
  this.parallelismThreads = options?.parallelismThreads ?? 1;
698
703
  this.path = path;
699
704
  this.pessimistic = options?.pessimistic ?? false;
705
+ this.readOnly = options?.readOnly ?? false;
700
706
  this.randomAccessStructure = options?.randomAccessStructure ?? false;
701
707
  this.readKey = readKey;
702
708
  this.sharedStructuresKey = options?.sharedStructuresKey;
@@ -767,6 +773,7 @@ var Store = class {
767
773
  const keyParam = getKeyParam(this.encodeKey(key));
768
774
  let flags = 0;
769
775
  if (alwaysCreateNewBuffer) flags |= ALWAYS_CREATE_NEW_BUFFER_FLAG;
776
+ if (this.readOnly) txnId = void 0;
770
777
  const result = context.getSync(keyParam, flags | ONLY_IF_IN_MEMORY_CACHE_FLAG, txnId);
771
778
  if (typeof result === "number") {
772
779
  if (result === NOT_IN_MEMORY_CACHE_FLAG) return new Promise((resolve, reject) => {
@@ -839,7 +846,7 @@ var Store = class {
839
846
  */
840
847
  getTxnId(options) {
841
848
  let txnId;
842
- if (options?.transaction) {
849
+ if (!this.readOnly && options?.transaction) {
843
850
  txnId = options.transaction.id;
844
851
  if (txnId === void 0) throw new TypeError("Invalid transaction");
845
852
  }
@@ -907,6 +914,7 @@ var Store = class {
907
914
  name: this.name,
908
915
  noBlockCache: this.noBlockCache,
909
916
  parallelismThreads: this.parallelismThreads,
917
+ readOnly: this.readOnly,
910
918
  statsLevel: this.statsLevel,
911
919
  transactionLogMaxAgeThreshold: this.transactionLogMaxAgeThreshold,
912
920
  transactionLogMaxSize: this.transactionLogMaxSize,
@@ -1022,9 +1030,17 @@ var Transaction = class extends DBI {
1022
1030
  * @param options - The options for the transaction.
1023
1031
  */
1024
1032
  constructor(store, options) {
1025
- const txn = new NativeTransaction(store.db, options);
1026
- super(store, txn);
1027
- this.#txn = txn;
1033
+ if (store.readOnly) {
1034
+ super(store);
1035
+ this.#txn = { id: 0 };
1036
+ this.abort = this.commitSync = this.setTimestamp = () => {};
1037
+ this.commit = async () => {};
1038
+ this.getTimestamp = () => 0;
1039
+ } else {
1040
+ const txn = new NativeTransaction(store.db, options);
1041
+ super(store, txn);
1042
+ this.#txn = txn;
1043
+ }
1028
1044
  }
1029
1045
  /**
1030
1046
  * Abort the transaction.
@@ -1354,6 +1370,12 @@ var RocksDatabase = class RocksDatabase extends DBI {
1354
1370
  return this.#name;
1355
1371
  }
1356
1372
  /**
1373
+ * Whether the database is open in readonly mode.
1374
+ */
1375
+ get readOnly() {
1376
+ return this.store.readOnly;
1377
+ }
1378
+ /**
1357
1379
  * Sugar method for opening a database.
1358
1380
  *
1359
1381
  * @param pathOrStore - The filesystem path to the database or a custom store.
@@ -1884,7 +1906,7 @@ function loadLastPosition(transactionLog, readUncommitted) {
1884
1906
  //#region src/index.ts
1885
1907
  const versions = {
1886
1908
  rocksdb: version,
1887
- "rocksdb-js": "1.0.1"
1909
+ "rocksdb-js": "1.1.0"
1888
1910
  };
1889
1911
 
1890
1912
  //#endregion