所以我尝试使用 postgresql 设置 mikroorm,但我在类型上遇到了这个奇怪的错误:
这是代码
import { MikroORM, Options} from "@mikro-orm/core";
import { _prod_ } from "./constants";
import { Post } from "./entities/Post";
const main = async () => {
const config: Options =
const orm = await MikroORM.init({
entities: [Post],
dbName: "readit",
type: "postgresql", <<-- having error here
debug: !_prod_,
});
const post = orm.em.create(Post, {title:'First post'})
}
main();
我收到此错误
Object literal may only specify known properties, and 'type' does not exist in type 'Options<IDatabaseDriver<Connection>, EntityManager<IDatabaseDriver<Connection>>>'.
我正在尝试遵循文档,但该错误根本没有意义。 这是打字稿的问题吗?
我尝试了打字稿上的一些设置,但它没有改变任何东西。 由于我还不熟悉 mikroorm postgrestql 和 typescript,我尝试添加一些代码但没有成功。
我的 tsconfig.json
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": ["dom", "es6", "es2017", "esnext.asynciterable"],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
"removeComments": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"resolveJsonModule": true,
"baseUrl": "."
},
"exclude": ["node_modules"],
"include": ["./src/**/*.ts"]
}
type
选项已在 v6 中删除。您现在可以从驱动程序包中导入 MikroORM
类,系统将自动从中推断出该类。
import { MikroORM} from "@mikro-orm/postgresql";
import { _prod_ } from "./constants";
import { Post } from "./entities/Post";
const main = async () => {
const orm = await MikroORM.init({
entities: [Post],
dbName: "readit",
debug: !_prod_,
});
const post = orm.em.create(Post, {title:'First post'})
await orm.em.flush(); // also added this
}
main();
请参阅升级指南更多更多选择。
另请注意,如果不调用
em.flush()
,您的代码将不会执行任何操作,它只是在内存中创建实体实例。