appwrite-utils-cli
Version:
Appwrite Utility Functions to help with database management, data conversion, data import, migrations, and much more. Meant to be used as a CLI tool, I do not recommend installing this in frontend environments.
58 lines (57 loc) • 1.65 kB
JavaScript
import { logger } from "../shared/logging.js";
/**
* Type guard and validation utilities
*/
/**
* Type guard to check if object is a DatabaseAdapter
* @param db - Object to check
* @returns True if object is DatabaseAdapter
*/
export function isDatabaseAdapter(db) {
const isAdapter = 'getMetadata' in db && typeof db.getMetadata === 'function';
logger.debug('Adapter type detection', {
isAdapter,
hasGetMetadata: 'getMetadata' in db,
getMetadataType: typeof db.getMetadata,
operation: 'isDatabaseAdapter'
});
return isAdapter;
}
/**
* Type guard to check if object is legacy Databases instance
* @param db - Object to check
* @returns True if object is Databases (not DatabaseAdapter)
*/
export function isLegacyDatabases(db) {
return !isDatabaseAdapter(db);
}
/**
* Checks if object has metadata property
* @param obj - Object to check
* @returns True if object has metadata
*/
export function hasMetadata(obj) {
return obj && typeof obj === 'object' && 'metadata' in obj;
}
/**
* Type guard for collection create objects
* @param obj - Object to check
* @returns True if object looks like CollectionCreate
*/
export function isCollectionCreate(obj) {
return obj &&
typeof obj === 'object' &&
'name' in obj &&
('$id' in obj || 'collectionId' in obj);
}
/**
* Type guard for table create objects
* @param obj - Object to check
* @returns True if object looks like TableCreate
*/
export function isTableCreate(obj) {
return obj &&
typeof obj === 'object' &&
'name' in obj &&
('tableId' in obj || '$id' in obj);
}