Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to init mikroConfig to MikroORM with typescript and i got this error message

The error message:

Argument of type '{ readonly entities: readonly [typeof Post]; readonly dbName: "lireddit"; readonly type: "postgresql"; readonly debug: boolean; }' is not assignable to parameter of type 'Configuration<IDatabaseDriver> | Options<IDatabaseDriver> | undefined'. Type '{ readonly entities: readonly [typeof Post]; readonly dbName: "lireddit"; readonly type: "postgresql"; readonly debug: boolean; }' is not assignable to type 'Options<IDatabaseDriver>'. Type '{ readonly entities: readonly [typeof Post]; readonly dbName: "lireddit"; readonly type: "postgresql"; readonly debug: boolean; }' is not assignable to type 'Partial<MikroORMOptions<IDatabaseDriver>>'. Types of property 'entities' are incompatible. The type 'readonly [typeof Post]' is 'readonly' and cannot be assigned to the mutable type '(string | EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema<any, undefined>)[]'.ts(2345)

The index.ts:

import { MikroORM } from '@mikro-orm/core';
import { __prod__ } from './constants';
import { Post } from './entities/Post';
import mikroConfig from './mikro-orm.config';

const main = async() => {
    const orm = await MikroORM.init(mikroConfig);
    const post = orm.em.create(Post, {title:'ez az első posztom hehe'})
    await orm.em.persistAndFlush(post)
}

main().catch((err) => {
    console.error(err)
})

And the mikro-orm.config.ts:

import { Post } from "./entities/Post";
import { __prod__ } from "./constants";

export default {
    entities:[Post],
    dbName: "lireddit",
    type: "postgresql",
    debug : !__prod__,
} as const;

Thank you for the help i you can, its so painful

like image 533
LansPeti Avatar asked Mar 27 '21 11:03

LansPeti


People also ask

How do I install typescript on my project?

With your project directory set up, you can install TypeScript: It is important to include the --save-dev flag because it saves TypeScript as a development dependency. This means that TypeScript is absolutely required for the development of your project.

What is Mikro Orm?

TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. MikroORM allows handling transactions automatically. When you call em.flush (), all computed changes are wrapped inside a database transaction. Uses source code analysis so you do not have to repeat yourself when defining entities.

How to handle transactions in mikroorm?

MikroORM allows handling transactions automatically. When you call em.flush (), all computed changes are wrapped inside a database transaction.

What is the TSC command in typescript?

The tsc command is used here because it is the built-in TypeScript compiler. When you write code in TypeScript, running tsc will transform or compile your code into JavaScript. Using the --init flag in the above command will initialize your project by creating a tsconfig.json file in your typescript-project project directory.


1 Answers

The way you are defining your ORM config is wrong, you should use Options type from the core package instead of const assertion. Define the config this way to have the best intellisense support (as well as to get rid of that TS error):

import { Options } from '@mikro-orm/core';
import { Post } from "./entities/Post";
import { __prod__ } from "./constants";

const config: Options = {
    entities: [Post],
    dbName: "lireddit",
    type: "postgresql",
    debug : !__prod__,
};
export default config;
like image 63
Martin Adámek Avatar answered Oct 21 '22 22:10

Martin Adámek