axiodb
Version:
The Pure JavaScript Alternative to SQLite. Embedded NoSQL database for Node.js with MongoDB-style queries, zero native dependencies, built-in InMemoryCache, and web GUI. Perfect for desktop apps, CLI tools, and embedded systems. No compilation, no platfor
111 lines • 5.27 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const Keys_1 = require("../../config/Keys/Keys");
const Index_service_1 = require("./Index.service");
const IndexCache_service_1 = require("./IndexCache.service");
/**
* Service for removing documents from indexes
*
* When a document is deleted, this service removes its reference from all
* relevant index entries to prevent stale references and maintain index integrity.
*
* @example
* ```typescript
* const deleteIndex = new DeleteIndex('/path/to/collection');
* await deleteIndex.RemoveFromIndex('doc123', { email: 'test@example.com' });
* ```
*/
class DeleteIndex extends Index_service_1.IndexManager {
constructor(path) {
super(path);
this.indexCache = new IndexCache_service_1.IndexCache(path);
}
/**
* Removes a document from all matching indexes
*
* For each indexed field present in the document:
* 1. Loads the index data from cache (or disk if not cached)
* 2. Removes the document filename from indexEntries[fieldValue]
* 3. Deletes the entry key if array becomes empty
* 4. Updates both memory cache and disk atomically
*
* @param documentId - The document ID to remove (without file extension)
* @param document - The document data containing indexed field values
* @returns True if at least one index was updated, false otherwise
*/
RemoveFromIndex(documentId, document) {
return __awaiter(this, void 0, void 0, function* () {
const matchedIndex = yield this.findMatchingIndexMeta(document);
if (!matchedIndex || matchedIndex.length === 0) {
return false;
}
let status = false;
const documentFileName = `${documentId}${Keys_1.General.DBMS_File_EXT}`;
for (const indexes of matchedIndex) {
const indexName = indexes.indexFieldName;
// Get current index data from memory cache (or load from disk)
let indexData = yield this.indexCache.getIndex(indexName);
if (!indexData) {
// Fallback: Load from disk if not in cache
const indexContent = yield this.fileManager.ReadFile(indexes.path);
if (indexContent.status) {
indexData = this.converter.ToObject(indexContent.data);
}
else {
continue;
}
}
if (!indexData) {
continue;
}
const fieldValue = document[indexName];
// Check if this field value exists in the index
if (indexData.indexEntries[fieldValue]) {
// Remove the document from the array
const docIndex = indexData.indexEntries[fieldValue].indexOf(documentFileName);
if (docIndex !== -1) {
indexData.indexEntries[fieldValue].splice(docIndex, 1);
// Remove the key entirely if array is now empty
if (indexData.indexEntries[fieldValue].length === 0) {
delete indexData.indexEntries[fieldValue];
}
// Update disk first (must await for data integrity)
const updateSuccess = yield this.indexCache.updateIndex(indexName, indexData);
// Fire-and-forget: Invalidate cache asynchronously
this.indexCache.invalidateIndex(indexName).catch(() => { });
status = status || updateSuccess;
}
}
}
return status;
});
}
/**
* Removes multiple documents from all matching indexes (batch operation)
*
* @param documents - Array of { documentId, document } pairs to remove
* @returns True if at least one index was updated, false otherwise
*/
RemoveMultipleFromIndex(documents) {
return __awaiter(this, void 0, void 0, function* () {
let status = false;
for (const { documentId, document } of documents) {
const result = yield this.RemoveFromIndex(documentId, document);
status = status || result;
}
return status;
});
}
}
exports.default = DeleteIndex;
//# sourceMappingURL=DeleteIndex.service.js.map