typescriptpostgresqlmikro-orm

mikroorm postgresql setup error with type


So i'm trying to setup a mikroorm with postgresql, but I get this weird error on the type:

This is the code

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();

And I'm getting this error

Object literal may only specify known properties, and 'type' does not exist in type 'Options<IDatabaseDriver<Connection>, EntityManager<IDatabaseDriver<Connection>>>'.

I'm trying to follow the documentation but the error just doesn't make sense at all. is this an issue with typescript?

I tried to some settings on the typescript but it's not changing anything. Since I'm not familiar with mikroorm postgrestql and typescript yet, I've tried adding some codes but no luck.

My 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"]
}


Solution

  • The type option has been removed in v6. You can now import the MikroORM class from your driver package and it will be automatically inferred from that.

    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();
    

    See the upgrading guide more more alternatives.


    Also note that your code won't do anything without calling em.flush(), it just creates entity instance in memory.