feat(database): add database layer architecture (#410)

- Add @esengine/database-drivers for MongoDB/Redis connection management
- Add @esengine/database for Repository pattern with CRUD, pagination, soft delete
- Refactor @esengine/transaction MongoStorage to use shared connection
- Add comprehensive documentation in Chinese and English
This commit is contained in:
YHH
2025-12-31 16:26:53 +08:00
committed by GitHub
parent 87f71e2251
commit 71022abc99
41 changed files with 5226 additions and 186 deletions

View File

@@ -0,0 +1,48 @@
---
"@esengine/database-drivers": minor
"@esengine/database": minor
"@esengine/transaction": minor
---
feat: add database layer architecture
Added new database packages with layered architecture:
**@esengine/database-drivers (Layer 1)**
- MongoDB connection with pool management, auto-reconnect, events
- Redis connection with auto-reconnect, key prefix
- Type-safe `IMongoCollection<T>` interface decoupled from mongodb types
- Service tokens for dependency injection (`MongoConnectionToken`, `RedisConnectionToken`)
**@esengine/database (Layer 2)**
- Generic `Repository<T>` with CRUD, pagination, soft delete
- `UserRepository` with registration, authentication, role management
- Password hashing utilities using scrypt
- Query operators: `$eq`, `$ne`, `$gt`, `$gte`, `$lt`, `$lte`, `$in`, `$nin`, `$like`, `$regex`
**@esengine/transaction**
- Refactored `MongoStorage` to use shared connection from `@esengine/database-drivers`
- Removed factory pattern in favor of shared connection (breaking change)
- Simplified API: `createMongoStorage(connection, options?)`
Example usage:
```typescript
import { createMongoConnection } from '@esengine/database-drivers'
import { UserRepository } from '@esengine/database'
import { createMongoStorage, TransactionManager } from '@esengine/transaction'
// Create shared connection
const mongo = createMongoConnection({
uri: 'mongodb://localhost:27017',
database: 'game'
})
await mongo.connect()
// Use for database operations
const userRepo = new UserRepository(mongo)
await userRepo.register({ username: 'john', password: '123456' })
// Use for transactions (same connection)
const storage = createMongoStorage(mongo)
const txManager = new TransactionManager({ storage })
```