- 😄 Welcome to visit my open source projects.
hackycy / egg-typeorm Goto Github PK
View Code? Open in Web Editor NEW🔥 TypeORM plugin for Egg.js
License: MIT License
🔥 TypeORM plugin for Egg.js
License: MIT License
egg 程序运行时必须要配置 config.typeorm
项目中使用 typeorm 迁移时, 又必须配置 ormconfig.yaml
这两个重复了, 建议同时支持 ormconfig.json, 这样方便直接导入到 config.typeorm 中使用
declare module 'egg' {
interface Context {
entity: {
AdminInfo: typeof EntityAdminInfo
}
repo: {
AdminInfo: Repository
}
}
}
这是自动生成的,代码在VS code中,没有任何错误,正常启动后,连接数据库正常。但是在service中,this.ctx 上没有 repo
按照文档配好之后,可以在dev环境下正常启动,但是在跑ci的时候会报错导致跑不过。
报错如下:
2021-08-12 15:36:53,219 ERROR 73 nodejs.Error: please config typeorm in config file
1) "before all" hook in "{root}"
at new AppBootHook (/github/workspace/node_modules/@hackycy/egg-typeorm/app.js:13:13)
at /github/workspace/node_modules/egg-core/lib/lifecycle.js:102:45
at Array.map (<anonymous>)
at Lifecycle.init (/github/workspace/node_modules/egg-core/lib/lifecycle.js:102:36)
at AppWorkerLoader.[Loader#loadBootHook] (/github/workspace/node_modules/egg-core/lib/loader/mixin/custom.js:74:20)
at AppWorkerLoader.loadCustomApp (/github/workspace/node_modules/egg-core/lib/loader/mixin/custom.js:36:25)
at AppWorkerLoader.load (/github/workspace/node_modules/egg/lib/loader/app_worker_loader.js:35:10)
at new Application (/github/workspace/node_modules/egg/lib/application.js:66:19)
at new MessengerApplication (/github/workspace/node_modules/egg-mock/lib/app.js:256:7)
at MockApplication.[init] (/github/workspace/node_modules/egg-mock/lib/app.js:90:29)
at Generator.next (<anonymous>)
at onFulfilled (/github/workspace/node_modules/co/index.js:65:19)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
pid: 73
hostname: f856c6ed2e3c
0 passing (2s)
1 failing
1) "before all" hook in "{root}":
Error: please config typeorm in config file
at new AppBootHook (node_modules/@hackycy/egg-typeorm/app.js:13:13)
at /github/workspace/node_modules/egg-core/lib/lifecycle.js:102:45
at Array.map (<anonymous>)
at Lifecycle.init (node_modules/egg-core/lib/lifecycle.js:102:36)
at AppWorkerLoader.[Loader#loadBootHook] (node_modules/egg-core/lib/loader/mixin/custom.js:74:20)
at AppWorkerLoader.loadCustomApp (node_modules/egg-core/lib/loader/mixin/custom.js:36:25)
at AppWorkerLoader.load (node_modules/egg/lib/loader/app_worker_loader.js:35:10)
at new Application (node_modules/egg/lib/application.js:66:19)
at new MessengerApplication (node_modules/egg-mock/lib/app.js:256:7)
at MockApplication.[init] (node_modules/egg-mock/lib/app.js:90:29)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
[use `--full-trace` to display the full stack trace]
我没有ignore掉config文件,所以说跑github action插件启动的时候config应该是有值的,但还是报错
本机跑ci也会这样...求指教
如标题所示,下面是一个简单的代码展示
依赖如下:
"@hackycy/egg-typeorm": "^0.3.8",
"egg": "^2.6.1",
"egg-scripts": "^2.6.0",
"mysql2": "^2.2.5"
"egg-bin": "^4.11.0",
@Entity('sys_app_config')
class SysAppConfig {
@PrimaryGeneratedColumn()
id: number;
@Column()
appName: string;
@Column({ nullable: true })
appIcon: string;
@Column()
appStatus: number;
@Column({ nullable: true })
remark: string;
}
import { Service } from 'egg';
export default class SysAppConfigService extends Service {
async save(appConfig) {
await this.ctx.repo.SysAppConfig.save(appConfig);
}
}
import { app, assert } from 'egg-mock/bootstrap';
describe('test/app/service/SysAppConfig.test.js', () => {
// afterEach(mock.restore);
it('SysAppConfig create', async () => {
const ctx = app.mockContext();
// console.log('ctx :>> ', ctx);
const result = await ctx.service.sysAppConfig.save({ appName: 'test', appStatus: 1 });
assert(result);
});
});
#运行结果
yarn run v1.22.5
$ egg-bin test
test/app/service/SysAppConfig.test.js
1) SysAppConfig create
0 passing (555ms)
1 failing
1) test/app/service/SysAppConfig.test.js
SysAppConfig create:
TypeError: Cannot read property 'save' of undefined
at SysAppConfigService.save (app/service/sysAppConfig.ts:6:38)
at Context.<anonymous> (test/app/service/sysAppConfig.test.ts:10:51)
at processImmediate (internal/timers.js:456:21)
[use `--full-trace` to display the full stack trace]
请大佬告知!
小白鼓捣了好久,看您的example一下子就弄清楚了。谢谢!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.