我在一个node.js (+Express)后端工作,我尝试使用Mikro-orm而不使用类型记录。
我遵循这个项目作为参考+ Mikro-orm文档,但是我一直收到这个错误:
MetadataError: Only abstract entities were discovered, maybe you forgot to use @Entity() decorator?有没有办法避免使用打字本的装潢工?或者一个更好的问题是,是否可以在纯JS项目中使用Mikro-orm?
配置文件mikro-orm.config.js如下所示:
module.exports = {
entities: [EntityX, EntitiyZ],
type: 'mysql',
dbName: 'example',
highlighter: new SqlHighlighter(),
debug: true
};我安装的版本如下:
"@mikro-orm/core": "^4.5.10",
"@mikro-orm/mysql": "^4.5.10"任何帮助都将不胜感激,谢谢!
发布于 2022-01-16 16:40:32
当我发现这个问题时,我正遇到这个问题(谢谢你的发帖)。
我能够通过确保来自我的每个实体文件的默认导出具有Mikro-ORM所期望的形状来解决这个问题,在您的例子中,该形状如下所示:
export default {
EntityX,
schema,
entity: EntityX
}**编辑
再看一看,我意识到您使用的是CommonJS语法,所以如果没有看到实体文件,我就会认为您可能缺少这样的内容:
module.exports.EntityX = EntityX;
module.exports.entity = EntityX;
module.exports.schema = schema;在实体文件中,或者忘记连接entities/index.js,如链接的示例所示。
发布于 2022-09-13 08:14:36
const appDataSource = {
type: 'mysql',
entities: [EntityX, EntityZ],
dbName: 'yourDBName',
port: 3307,
highlighter: new SqlHighlighter(),
debug: true
};
export default appDataSource as Options;发布于 2022-09-13 08:33:40
首先是npm i @nestjs/config。
其次,在appModule.ts内部
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: `.env.${process.env.NODE_ENV}`,
}),
ModuleX,
ModuleY
],
controllers: [AppController],
providers: [AppService],
})第三,在Root项目中,env.development将所有的env放在那里。
最后在mikro-orm.config.ts中
const appDataSource = {
entities: [EntityX, EntityY]
};
const configService = new ConfigService();
switch (process.env.NODE_ENV) {
case 'development':
Object.assign(appDataSource, {
type: configService.get('MIKRO_ORM_TYPE'),
dbName: configService.get('MIKRO_ORM_DB_NAME'),
host: configService.get('MIKRO_ORM_HOST'),
user: configService.get('MIKRO_ORM_USER'),
password: configService.get('MIKRO_ORM_PASSWORD'),
port: parseInt(configService.get('MIKRO_ORM_PORT')),
});
break;
}
//continue with the switch statement on diff environment
//if test env create .env.test at the project root
export default appDataSource as Options;https://stackoverflow.com/questions/70707241
复制相似问题