98 lines
2.6 KiB
Markdown
98 lines
2.6 KiB
Markdown
# Migration Guide: From packages to libs
|
|
|
|
This guide will help you migrate your service to use the new library structure for better separation of concerns.
|
|
|
|
## Steps for each service
|
|
|
|
1. Update your `package.json` dependencies to use the new libraries:
|
|
|
|
```diff
|
|
"dependencies": {
|
|
- "@stock-bot/shared-types": "workspace:*",
|
|
+ "@stock-bot/shared-types": "workspace:*",
|
|
+ "@stock-bot/utils": "workspace:*",
|
|
+ "@stock-bot/event-bus": "workspace:*",
|
|
+ "@stock-bot/api-client": "workspace:*",
|
|
...
|
|
}
|
|
```
|
|
|
|
2. Update your imports to use the domain-specific modules:
|
|
|
|
```diff
|
|
- import { OHLCV, Strategy, Order } from '@stock-bot/shared-types';
|
|
+ import { OHLCV } from '@stock-bot/shared-types';
|
|
+ import { Strategy } from '@stock-bot/shared-types';
|
|
+ import { Order } from '@stock-bot/shared-types';
|
|
```
|
|
|
|
For logging:
|
|
```diff
|
|
- // Custom logging or console.log usage
|
|
+ import { createLogger, LogLevel } from '@stock-bot/utils';
|
|
+
|
|
+ const logger = createLogger('your-service-name');
|
|
+ logger.info('Message');
|
|
```
|
|
|
|
For API client usage:
|
|
```diff
|
|
- // Manual axios calls
|
|
+ import { createBacktestClient, createStrategyClient } from '@stock-bot/api-client';
|
|
+
|
|
+ const backtestClient = createBacktestClient();
|
|
+ const result = await backtestClient.runBacktest(config);
|
|
```
|
|
|
|
For event-based communication:
|
|
```diff
|
|
- // Manual Redis/Dragonfly usage
|
|
+ import { createEventBus } from '@stock-bot/event-bus';
|
|
+ import { MarketDataEvent } from '@stock-bot/shared-types';
|
|
+
|
|
+ const eventBus = createEventBus({
|
|
+ redisHost: process.env.REDIS_HOST || 'localhost',
|
|
+ redisPort: parseInt(process.env.REDIS_PORT || '6379')
|
|
+ });
|
|
+
|
|
+ eventBus.subscribe('market.data', async (event) => {
|
|
+ // Handle event
|
|
+ });
|
|
```
|
|
|
|
## Example: Updating BacktestEngine
|
|
|
|
```typescript
|
|
// Before
|
|
import { Strategy, BacktestConfig } from '@stock-bot/shared-types';
|
|
import Redis from 'ioredis';
|
|
|
|
// After
|
|
import { Strategy } from '@stock-bot/shared-types';
|
|
import { BacktestConfig } from '@stock-bot/shared-types';
|
|
import { createLogger } from '@stock-bot/utils';
|
|
import { createEventBus } from '@stock-bot/event-bus';
|
|
|
|
const logger = createLogger('backtest-engine');
|
|
const eventBus = createEventBus({
|
|
redisHost: process.env.REDIS_HOST || 'localhost',
|
|
redisPort: parseInt(process.env.REDIS_PORT || '6379')
|
|
});
|
|
```
|
|
|
|
## Updating build scripts
|
|
|
|
If your turbo.json configuration references specific packages, update the dependencies:
|
|
|
|
```diff
|
|
"backtest": {
|
|
"dependsOn": [
|
|
"^build",
|
|
- "packages/shared-types#build"
|
|
+ "libs/shared-types#build",
|
|
+ "libs/utils#build",
|
|
+ "libs/event-bus#build",
|
|
+ "libs/api-client#build"
|
|
],
|
|
}
|
|
```
|