3.x Upgrade Guide
This article will introduce how to upgrade from midway v3 to midway v4.
Upgrading from Midway v3 to Midway v4 will have some Breaking Changes. This document only lists these Breaking changes to let users know the changes in advance and make responses.
Node.js Changes
midway v4 supports from node v20 onwards, it's best to use the LTS version.
Package Version Updates
All component packages and core packages will be upgraded to 4.x versions.
Currently in beta stage
{
"dependencies": {
- "@midwayjs/bootstrap": "^3.0.0",
- "@midwayjs/core": "^3.0.0",
- "@midwayjs/koa": "^3.0.0",
- "@midwayjs/logger": "^3.0.0",
+ "@midwayjs/bootstrap": "^4.0.0-beta.1",
+ "@midwayjs/core": "^4.0.0-beta.1",
+ "@midwayjs/koa": "^4.0.0-beta.1",
+ "@midwayjs/logger": "^4.0.0",
},
"devDependencies": {
- "@midwayjs/mock": "^3.0.0",
+ "@midwayjs/mock": "^4.0.0-beta.1",
// ...
}
}
The version of @midwyajs/luckeye is excluded.
Remove @midwayjs/decorator
Starting from v4, the @midwayjs/decorator package has been removed, users can directly use @midwayjs/core instead.
- import { Controller } from '@midwayjs/decorator';
+ import { Controller } from '@midwayjs/core';
Entry Auto-scanning Capability
Starting from v4, the framework removes the implicit auto-scanning capability, users need to explicitly declare the directories that need to be scanned.
import { Configuration, CommonJSFileDetector } from '@midwayjs/core';
@Configuration({
detector: new CommonJSFileDetector(),
// ...
})
export class MainContainer {
// ...
}
You can also specify directories to ignore.
import { Configuration, CommonJSFileDetector } from '@midwayjs/core';
@Configuration({
detector: new CommonJSFileDetector({
ignore: [
'**/logs/**',
],
}),
// ...
})
export class MainContainer {
// ...
}
Duplicate check configuration is also moved to parameters.
import { Configuration, CommonJSFileDetector } from '@midwayjs/core';
@Configuration({
detector: new CommonJSFileDetector({
conflictCheck: true,
}),
// ...
})
export class MainContainer {
// ...
}
Main Framework Logging Configuration
Starting from v4, the main framework logging format returns to the midwayLogger configuration.
- Koa
- Express
- Egg
- Bull
// *.config.ts
export default {
koa: {
- contextLoggerFormat: info => {
- const ctx = info.ctx;
- return `${info.timestamp} ${info.LEVEL} ${info.pid} [${ctx.userId} - ${Date.now() - ctx.startTime}ms ${ctx.method}] ${info.message}`;
- }
},
midwayLogger: {
clients: {
appLogger: {
+ contextFormat: info => {
+ const ctx = info.ctx;
+ return `${info.timestamp} ${info.LEVEL} ${info.pid} [${ctx.userId} - ${Date.now() - ctx.startTime}ms ${ctx.method}] ${info.message}`;
+ }
}
}
},
} as MidwayConfig;
// *.config.ts
export default {
express: {
- contextLoggerFormat: info => {
- const ctx = info.ctx;
- return `${info.timestamp} ${info.LEVEL} ${info.pid} [${ctx.userId} - ${Date.now() - ctx.startTime}ms ${ctx.method}] ${info.message}`;
- }
},
midwayLogger: {
clients: {
appLogger: {
+ contextFormat: info => {
+ const ctx = info.ctx;
+ return `${info.timestamp} ${info.LEVEL} ${info.pid} [${ctx.userId} - ${Date.now() - ctx.startTime}ms ${ctx.method}] ${info.message}`;
+ }
}
}
},
} as MidwayConfig;
// *.config.ts
export default {
egg: {
- contextLoggerFormat: info => {
- const ctx = info.ctx;
- return `${info.timestamp} ${info.LEVEL} ${info.pid} [${ctx.userId} - ${Date.now() - ctx.startTime}ms ${ctx.method}] ${info.message}`;
- }
},
midwayLogger: {
clients: {
appLogger: {
+ contextFormat: info => {
+ const ctx = info.ctx;
+ return `${info.timestamp} ${info.LEVEL} ${info.pid} [${ctx.userId} - ${Date.now() - ctx.startTime}ms ${ctx.method}] ${info.message}`;
+ }
}
}
},
} as MidwayConfig;
// *.config.ts
export default {
bull: {
- contextLoggerFormat: info => {
- const { jobId, from } = info.ctx;
- return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}] ${info.message}`;
- }
},
midwayLogger: {
clients: {
bullLogger: {
+ contextFormat: info => {
+ const { jobId, from } = info.ctx;
+ return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}] ${info.message}`;`;
+ }
}
}
},
} as MidwayConfig;
Other components like cron, mqtt, kafka, etc., if they have related configurations, are similar to the above.
Optional Change Reference
The following is a comparison table of Breaking Changes for each package, showing specific change content classified by package name.
@midwayjs/core
@App() Decorator Changes
Use @MainApp() instead of @App() with empty parameters:
- @App()
+ @MainApp()
private app;
@Config() Decorator Changes
Use @AllConfig() to replace @Config(ALL):
- @Config(ALL)
+ @AllConfig()
private allConfig;