I have an entity that is storing a serialized json string. I initially had it declared as string
type but started getting errors when it went over 255 characters. Postgres said it should be declared as a text
type, so I referenced the MIKRO-ORM docs and see that they have a a text: TextType
custom type.
I updated my entitities, and manually changed my db types as the migration wasn't picking up the change, but Typescript wasn't liking the new changes as trying to store a serialized json string into a text type yielded a Argument of type 'string' is not assignable to parameter of type 'TextType'
error.
I was on a deadline so I just put a ts-ignore
statement above, but I want to figure out what I'm doing wrong. The docs aren't clear to me and googling hasn't yielded any benefits.
Perhaps someone has insight into this?
import { TextType } from '@mikro-orm/core';
@Entity()
export class Event {
...
@Property({ nullable: true })
meta: TextType;
...
constructor(
...
meta?: TextType,
) {
...
this.meta = meta || null;
}
}
Thanks in advance!
CodePudding user response:
You are confusing two things - the TS property type, which is what your runtime values will be mapped to - and that is still a string
- and the mapped type that will be used to process the values and define the database column type - which is TextType
.
The docs page you linked describes the t
variable, that is a map of all provided mapped types, it just describes what you can use as an alias. All of the following should work the same (note that I also made the property optional on TS level, given you wanna mark is as nullable).
@Property({ type: TextType, nullable: true })
meta?: string;
@Property({ type: t.text, nullable: true })
meta?: string;
@Property({ type: 'text', nullable: true })
meta?: string;
CodePudding user response:
So a closer look at the docs showed me what I was missing.
import { Property, types } from '@mikro-orm/core';
@Property({ type: types.text, nullable: true })
meta?: string;
It should still have been a string, but in the decorator, I needed to pass types.text.
Guess this is the issue with deadlines, you miss obvious things.