@memory-bank/mcp
Version:
Memory-enabled Co-Pilot (MCP) server for managing project documentation and context
164 lines • 5.72 kB
JavaScript
import { DocumentId } from "../../../domain/entities/DocumentId.js";
import { DocumentPath } from "../../../domain/entities/DocumentPath.js";
import { JsonDocument } from "../../../domain/entities/JsonDocument.js";
/**
* In-memory implementation of the JSON document repository
* Used for testing purposes
*/
export class MockJsonDocumentRepository {
documents = new Map();
/**
* Find a document by its ID
* @param id Document ID
* @returns Promise resolving to the document if found, or null if not found
*/
async findById(id) {
for (const [, branchDocuments] of this.documents.entries()) {
for (const document of branchDocuments.values()) {
if (document.id.equals(id)) {
return document;
}
}
}
return null;
}
/**
* Find a document by its path
* @param branchInfo Branch information
* @param path Document path
* @returns Promise resolving to the document if found, or null if not found
*/
async findByPath(branchInfo, path) {
const branchDocuments = this.getBranchDocuments(branchInfo);
return branchDocuments.get(path.value) || null;
}
/**
* Find documents by tags
* @param branchInfo Branch information
* @param tags Tags to search for
* @param matchAll If true, documents must have all tags; if false, any tag is sufficient
* @returns Promise resolving to array of matching documents
*/
async findByTags(branchInfo, tags, matchAll = false) {
if (tags.length === 0) {
return [];
}
const branchDocuments = this.getBranchDocuments(branchInfo);
const result = [];
for (const document of branchDocuments.values()) {
if (matchAll) {
if (tags.every((tag) => document.hasTag(tag))) {
result.push(document);
}
}
else {
if (tags.some((tag) => document.hasTag(tag))) {
result.push(document);
}
}
}
return result;
}
/**
* Find documents by document type
* @param branchInfo Branch information
* @param documentType Document type to search for
* @returns Promise resolving to array of matching documents
*/
async findByType(branchInfo, documentType) {
const branchDocuments = this.getBranchDocuments(branchInfo);
const result = [];
for (const document of branchDocuments.values()) {
if (document.documentType === documentType) {
result.push(document);
}
}
return result;
}
/**
* Save a document
* @param branchInfo Branch information
* @param document Document to save
* @returns Promise resolving to the saved document
*/
async save(branchInfo, document) {
const branchDocuments = this.getBranchDocuments(branchInfo);
branchDocuments.set(document.path.value, document);
return document;
}
/**
* Delete a document
* @param branchInfo Branch information
* @param document Document to delete (or document ID or path)
* @returns Promise resolving to boolean indicating success
*/
async delete(branchInfo, document) {
const branchDocuments = this.getBranchDocuments(branchInfo);
if (document instanceof JsonDocument) {
return branchDocuments.delete(document.path.value);
}
else if (document instanceof DocumentPath) {
return branchDocuments.delete(document.value);
}
else if (document instanceof DocumentId) {
for (const [path, doc] of branchDocuments.entries()) {
if (doc.id.equals(document)) {
return branchDocuments.delete(path);
}
}
return false;
}
return false;
}
/**
* List all documents
* @param branchInfo Branch information
* @returns Promise resolving to array of all documents
*/
async listAll(branchInfo) {
const branchDocuments = this.getBranchDocuments(branchInfo);
return Array.from(branchDocuments.values());
}
/**
* Check if a document exists by path
* @param branchInfo Branch information
* @param path Document path
* @returns Promise resolving to boolean indicating existence
*/
async exists(branchInfo, path) {
const branchDocuments = this.getBranchDocuments(branchInfo);
return branchDocuments.has(path.value);
}
/**
* Helper method to get the documents map for a branch
* Creates the map if it doesn't exist
* @param branchInfo Branch information
* @returns Map of document path to document
*/
getBranchDocuments(branchInfo) {
let branchDocuments = this.documents.get(branchInfo.name);
if (!branchDocuments) {
branchDocuments = new Map();
this.documents.set(branchInfo.name, branchDocuments);
}
return branchDocuments;
}
/**
* Clear all documents
*/
clear() {
this.documents.clear();
}
/**
* Initialize with a set of documents
* @param documents Documents to initialize with
* @param branchInfo Branch information
*/
async initialize(documents, branchInfo) {
const branchDocuments = this.getBranchDocuments(branchInfo);
for (const document of documents) {
branchDocuments.set(document.path.value, document);
}
}
}
//# sourceMappingURL=MockJsonDocumentRepository.js.map