backend v4 half
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
import { AppDataSource } from './ormconfig';
|
||||
import { RepositoryManager } from './src/Repository/RepositoryManager';
|
||||
import { appLogger } from './src/Utils/EnhancedLogger';
|
||||
|
||||
// Example usage of the repository system with integrated logging
|
||||
async function demonstrateRepositorySystem() {
|
||||
try {
|
||||
await appLogger.info('Starting repository system demonstration');
|
||||
|
||||
// Initialize database connection
|
||||
if (!AppDataSource.isInitialized) {
|
||||
await AppDataSource.initialize();
|
||||
await appLogger.info('Database connection initialized');
|
||||
}
|
||||
|
||||
// Create repository manager
|
||||
const repositoryManager = new RepositoryManager(AppDataSource);
|
||||
|
||||
// Example 1: User Repository Operations
|
||||
const userRepository = repositoryManager.getUserRepository();
|
||||
|
||||
// Find all users (automatically logged)
|
||||
const users = await userRepository.findAll();
|
||||
await appLogger.info(`Found ${users.length} users`);
|
||||
|
||||
// Example 2: Company Repository Operations
|
||||
const companyRepository = repositoryManager.getCompanyRepository();
|
||||
|
||||
// Find companies with pagination (automatically logged)
|
||||
const companiesPage = await companyRepository.findWithPagination(1, 10);
|
||||
await appLogger.info(`Found ${companiesPage.total} companies, showing page 1 with ${companiesPage.data.length} items`);
|
||||
|
||||
// Example 3: Cards Repository Operations
|
||||
const cardsRepository = repositoryManager.getCardsRepository();
|
||||
|
||||
// Count all cards (automatically logged)
|
||||
const cardCount = await cardsRepository.count();
|
||||
await appLogger.info(`Total cards in system: ${cardCount}`);
|
||||
|
||||
// Example 4: Transaction example (automatically logged)
|
||||
const result = await userRepository.withTransaction(async (manager) => {
|
||||
// Transaction operations would go here
|
||||
return 'Transaction completed successfully';
|
||||
});
|
||||
|
||||
await appLogger.info(`Transaction result: ${result}`);
|
||||
|
||||
// Close connection
|
||||
await repositoryManager.closeConnection();
|
||||
await appLogger.info('Repository system demonstration completed');
|
||||
|
||||
} catch (error) {
|
||||
await appLogger.errorEvent(
|
||||
error instanceof Error ? error.message : String(error),
|
||||
'repository_demo',
|
||||
{
|
||||
operation: 'demonstration',
|
||||
stack: error instanceof Error ? error.stack : undefined
|
||||
}
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// Export for potential usage
|
||||
export { demonstrateRepositorySystem };
|
||||
|
||||
// Example of how to use in your application
|
||||
/*
|
||||
Usage example:
|
||||
|
||||
import { demonstrateRepositorySystem } from './demo/repository-demo';
|
||||
|
||||
// In your main application file
|
||||
async function main() {
|
||||
try {
|
||||
await demonstrateRepositorySystem();
|
||||
} catch (error) {
|
||||
console.error('Demo failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
||||
*/
|
||||
Reference in New Issue
Block a user