jexidb 2.2.0 → 2.2.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jexidb",
3
- "version": "2.2.0",
3
+ "version": "2.2.1",
4
4
  "type": "module",
5
5
  "description": "JexiDB is a pure JS NPM library for managing data on disk efficiently, without the need for a server.",
6
6
  "main": "./dist/Database.cjs",
package/src/Database.mjs CHANGED
@@ -4056,12 +4056,23 @@ class Database extends EventEmitter {
4056
4056
  this.termManager.termMappingFields &&
4057
4057
  this.termManager.termMappingFields.includes(fieldName)
4058
4058
 
4059
+ // CRITICAL FIX: Ensure index is loaded before accessing data
4060
+ // After idle unload, index.data[fieldName] becomes {} (truthy but empty)
4061
+ // Use _ensureLazyIndexLoaded() to reload from disk instead of _rebuildIndexesIfNeeded()
4062
+ // which requires allowIndexRebuild=true and rebuilds from scratch
4063
+ if (!this.indexManager.indexLoaded) {
4064
+ await this._ensureLazyIndexLoaded()
4065
+ }
4066
+
4059
4067
  // Access the index for this field
4060
4068
  const fieldIndex = this.indexManager.index.data[fieldName]
4061
- if (!fieldIndex) {
4069
+ if (!fieldIndex || Object.keys(fieldIndex).length === 0) {
4062
4070
  return []
4063
4071
  }
4064
4072
 
4073
+ // Reset idle unload timer since we're using the index
4074
+ this.indexManager.markIndexUsed()
4075
+
4065
4076
  // Accumulate scores for each line number
4066
4077
  const scoreMap = new Map()
4067
4078
  const countMap = opts.mode === 'avg' ? new Map() : null
@@ -103,6 +103,11 @@ export default class IndexManager {
103
103
  console.log(`🧹 IndexManager.unload: Unloading index data after ${this.indexIdleUnloadMs}ms idle`)
104
104
  }
105
105
 
106
+ // Mark rebuild as needed so score() can trigger lazy reload
107
+ if (this.database) {
108
+ this.database._indexRebuildNeeded = true
109
+ }
110
+
106
111
  this._clearIdleUnloadTimer()
107
112
  const newData = {}
108
113
  for (const field of this.indexedFields) {