feat: comprehensive metadata namespace architecture and cleanup system
BREAKING CHANGE: Remove hard delete option from deleteVerb() for consistent API - Add complete metadata namespace architecture with O(1) soft delete performance - Implement periodic cleanup system for old soft-deleted items - Add restore methods for both nouns and verbs - Require metadata contracts for all augmentations - Eliminate namespace collisions with clean separation (_brainy, _augmentations, _audit) - Optimize index performance using flattened dot-notation for O(1) lookups - Add comprehensive augmentation safety system with type-safe access control - Maintain full backward compatibility for existing data - Add enterprise-grade cleanup with configurable age thresholds and batch processing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
9220ca6748
commit
7e243b6f2b
35 changed files with 2700 additions and 125 deletions
88
src/utils/ensureDeleted.ts
Normal file
88
src/utils/ensureDeleted.ts
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
/**
|
||||
* Utility to ensure all metadata has the deleted field set properly
|
||||
* This is CRITICAL for O(1) soft delete filtering performance
|
||||
*
|
||||
* Uses _brainy namespace to avoid conflicts with user metadata
|
||||
*/
|
||||
|
||||
const BRAINY_NAMESPACE = '_brainy'
|
||||
|
||||
/**
|
||||
* Ensure metadata has internal Brainy fields set
|
||||
* @param metadata The metadata object (could be null/undefined)
|
||||
* @param preserveExisting If true, preserve existing deleted value
|
||||
* @returns Metadata with internal fields guaranteed
|
||||
*/
|
||||
export function ensureDeletedField(metadata: any, preserveExisting: boolean = true): any {
|
||||
// Handle null/undefined metadata
|
||||
if (!metadata) {
|
||||
return {
|
||||
[BRAINY_NAMESPACE]: {
|
||||
deleted: false,
|
||||
version: 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clone to avoid mutation
|
||||
const result = { ...metadata }
|
||||
|
||||
// Ensure _brainy namespace exists
|
||||
if (!result[BRAINY_NAMESPACE]) {
|
||||
result[BRAINY_NAMESPACE] = {}
|
||||
}
|
||||
|
||||
// Set deleted field if not present
|
||||
if (!('deleted' in result[BRAINY_NAMESPACE])) {
|
||||
result[BRAINY_NAMESPACE].deleted = false
|
||||
} else if (!preserveExisting) {
|
||||
// Force to false if not preserving
|
||||
result[BRAINY_NAMESPACE].deleted = false
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark an item as soft deleted
|
||||
* @param metadata The metadata object
|
||||
* @returns Metadata with _brainy.deleted=true
|
||||
*/
|
||||
export function markAsDeleted(metadata: any): any {
|
||||
const result = ensureDeletedField(metadata)
|
||||
result[BRAINY_NAMESPACE].deleted = true
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark an item as restored (not deleted)
|
||||
* @param metadata The metadata object
|
||||
* @returns Metadata with _brainy.deleted=false
|
||||
*/
|
||||
export function markAsRestored(metadata: any): any {
|
||||
const result = ensureDeletedField(metadata)
|
||||
result[BRAINY_NAMESPACE].deleted = false
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an item is deleted
|
||||
* @param metadata The metadata object
|
||||
* @returns true if deleted, false otherwise (including if field missing)
|
||||
*/
|
||||
export function isDeleted(metadata: any): boolean {
|
||||
return metadata?.[BRAINY_NAMESPACE]?.deleted === true
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if an item is active (not deleted)
|
||||
* @param metadata The metadata object
|
||||
* @returns true if not deleted (default), false if deleted
|
||||
*/
|
||||
export function isActive(metadata: any): boolean {
|
||||
// If no deleted field or deleted=false, item is active
|
||||
return !isDeleted(metadata)
|
||||
}
|
||||
|
||||
// Export the namespace constant for use in queries
|
||||
export const BRAINY_DELETED_FIELD = `${BRAINY_NAMESPACE}.deleted`
|
||||
Loading…
Add table
Add a link
Reference in a new issue