@yihuangdb/storage-object
Version:
A Node.js storage object layer library using Redis OM
302 lines • 9.56 kB
JavaScript
/**
* @module legacy
* @description Backward compatibility layer for the old functional API
*
* This module provides the original functional API that wraps the new OOP API.
* It ensures 100% backward compatibility for existing code while using the new
* architecture under the hood.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSchemaRegistry = void 0;
exports.createStorage = createStorage;
exports.getStorage = getStorage;
exports.deleteStorage = deleteStorage;
exports.getOrCreateStorage = getOrCreateStorage;
exports.listStorages = listStorages;
exports.getStorageMetadata = getStorageMetadata;
exports.validateStorage = validateStorage;
exports.getStorageStats = getStorageStats;
exports.backupStorages = backupStorages;
exports.restoreStorages = restoreStorages;
exports.cleanupStorages = cleanupStorages;
exports.getGlobalStats = getGlobalStats;
exports.monitorStorage = monitorStorage;
exports.migrateStorage = migrateStorage;
const storage_system_1 = require("./storage-system");
const storage_schema_1 = require("./storage-schema");
/**
* Creates a new storage instance (legacy API)
* @deprecated Use StorageSystem.create() instead
*
* @param name - Name of the storage
* @param schema - Schema configuration
* @param options - Storage options
* @returns Promise resolving to the storage instance
*
* @example
* ```typescript
* // Legacy way (still works)
* const storage = await createStorage('users', schema, options);
*
* // New way (recommended)
* const storage = await StorageSystem.create('users', schema, options);
* ```
*/
async function createStorage(name, schema, options) {
// Convert plain schema to EnhancedStorageSchema if needed
let enhancedSchema;
if (schema instanceof storage_schema_1.StorageSchema) {
enhancedSchema = schema;
}
else {
enhancedSchema = storage_schema_1.StorageSchema.define(schema, {
entityName: name,
useJSON: options?.useJSON !== false
});
}
return storage_system_1.StorageSystem.create(name, enhancedSchema.getFields(), options);
}
/**
* Gets an existing storage instance by name (legacy API)
* @deprecated Use StorageSystem.get() instead
*
* @param name - Name of the storage
* @returns Promise resolving to the storage instance or null if not found
*
* @example
* ```typescript
* // Legacy way (still works)
* const storage = await getStorage('users');
*
* // New way (recommended)
* const storage = await StorageSystem.get('users');
* ```
*/
async function getStorage(name) {
return storage_system_1.StorageSystem.get(name);
}
/**
* Deletes a storage instance (legacy API)
* @deprecated Use StorageSystem.delete() instead
*
* @param name - Name of the storage to delete
* @returns Promise resolving to true if deleted, false otherwise
*
* @example
* ```typescript
* // Legacy way (still works)
* const deleted = await deleteStorage('users');
*
* // New way (recommended)
* const deleted = await StorageSystem.delete('users');
* ```
*/
async function deleteStorage(name) {
return storage_system_1.StorageSystem.delete(name);
}
/**
* Gets or creates a storage instance (legacy API)
* @deprecated Use StorageSystem.getOrCreate() instead
*
* @param name - Name of the storage
* @param schema - Schema configuration (used if creating new)
* @param options - Storage options (used if creating new)
* @returns Promise resolving to the storage instance
*
* @example
* ```typescript
* // Legacy way (still works)
* const storage = await getOrCreateStorage('users', schema, options);
*
* // New way (recommended)
* const storage = await StorageSystem.getOrCreate('users', schema, options);
* ```
*/
async function getOrCreateStorage(name, schema, options) {
// Try to get existing storage first
const existing = await storage_system_1.StorageSystem.get(name);
if (existing) {
return existing;
}
// Create new storage
let enhancedSchema;
if (schema instanceof storage_schema_1.StorageSchema) {
enhancedSchema = schema;
}
else {
enhancedSchema = storage_schema_1.StorageSchema.define(schema, {
entityName: name,
useJSON: options?.useJSON !== false
});
}
return storage_system_1.StorageSystem.create(name, enhancedSchema.getFields(), options);
}
/**
* Lists all storage instances (legacy API)
* @deprecated Use StorageSystem.list() instead
*
* @returns Promise resolving to array of storage names
*
* @example
* ```typescript
* // Legacy way (still works)
* const storages = await listStorages();
*
* // New way (recommended)
* const storages = await StorageSystem.list();
* ```
*/
async function listStorages() {
const metadata = await storage_system_1.StorageSystem.list();
return metadata.map(m => m.name);
}
/**
* Gets metadata for a storage instance (legacy API)
* @deprecated Use StorageSystem.getMetadata() instead
*
* @param name - Name of the storage
* @returns Promise resolving to metadata or null if not found
*
* @example
* ```typescript
* // Legacy way (still works)
* const metadata = await getStorageMetadata('users');
*
* // New way (recommended)
* const metadata = await StorageSystem.getMetadata('users');
* ```
*/
async function getStorageMetadata(name) {
return storage_system_1.StorageSystem.getMetadata(name);
}
/**
* Validates a storage instance (legacy API)
* @deprecated Use StorageSystem.validate() instead
*
* @param name - Name of the storage
* @returns Promise resolving to validation result
*
* @example
* ```typescript
* // Legacy way (still works)
* const isValid = await validateStorage('users');
*
* // New way (recommended)
* const isValid = await StorageSystem.validate('users');
* ```
*/
async function validateStorage(name) {
const result = await storage_system_1.StorageSystem.validate(name);
return result.valid;
}
/**
* Gets statistics for a storage instance (legacy API)
*
* @param name - Name of the storage
* @returns Promise resolving to storage statistics
*/
async function getStorageStats(name) {
const storage = await storage_system_1.StorageSystem.get(name);
if (!storage) {
return null;
}
// Get count and other stats
const count = await storage.count();
const metadata = storage.getMetadata();
return {
name,
count,
...metadata
};
}
/**
* Backs up all storage instances (legacy API)
*
* @param path - Path to save backup
* @returns Promise resolving to backup result
*/
async function backupStorages(path) {
// This would require implementing backup functionality in StorageSystem
// For now, return a placeholder
throw new Error('Backup functionality not yet implemented in StorageSystem');
}
/**
* Restores storage instances from backup (legacy API)
*
* @param path - Path to restore from
* @returns Promise resolving to restore result
*/
async function restoreStorages(path) {
// This would require implementing restore functionality in StorageSystem
// For now, return a placeholder
throw new Error('Restore functionality not yet implemented in StorageSystem');
}
/**
* Cleans up unused storage instances (legacy API)
*
* @returns Promise resolving to cleanup result
*/
async function cleanupStorages() {
// This would require implementing cleanup functionality in StorageSystem
// For now, return a placeholder
const storages = await storage_system_1.StorageSystem.list();
return {
checked: storages.length,
cleaned: 0
};
}
/**
* Gets global statistics across all storages (legacy API)
*
* @returns Promise resolving to global statistics
*/
async function getGlobalStats() {
const storages = await storage_system_1.StorageSystem.list();
const stats = {
storageCount: storages.length,
totalEntities: 0,
storages: []
};
for (const metadata of storages) {
const storage = await storage_system_1.StorageSystem.get(metadata.name);
if (storage) {
const count = await storage.count();
stats.totalEntities += count;
stats.storages.push({
name: metadata.name,
count
});
}
}
return stats;
}
/**
* Monitors a storage instance (legacy API)
*
* @param name - Name of the storage
* @param callback - Callback function for monitoring events
* @returns Unsubscribe function
*/
function monitorStorage(name, callback) {
// This would require implementing monitoring functionality in StorageSystem
// For now, return a no-op unsubscribe function
console.warn('Monitor functionality not yet implemented in StorageSystem');
return () => { };
}
/**
* Migrates a storage instance to a new schema (legacy API)
*
* @param name - Name of the storage
* @param newSchema - New schema configuration
* @returns Promise resolving to migration result
*/
async function migrateStorage(name, newSchema) {
// This would require implementing migration functionality in StorageSystem
// For now, return a placeholder
throw new Error('Migration functionality not yet implemented in StorageSystem');
}
// Re-export the schema registry function for backward compatibility
var schema_registry_1 = require("./schema-registry");
Object.defineProperty(exports, "getSchemaRegistry", { enumerable: true, get: function () { return schema_registry_1.getSchemaRegistry; } });
//# sourceMappingURL=legacy.js.map
;