Replies: 1 comment 2 replies
-
The docs explicitly mention how to create numeric arrays: @Property({ type: new ArrayType(i => +i), nullable: true })
numericArray?: number[]; https://mikro-orm.io/docs/custom-types#arraytype Unless you use ts-morph this needs to be explicit, with ts-morph we infer the array type and do it automatically. Or are you doing it this way already? Hard to guess, provide some code and then we can talk about why it works the way it works. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
We have entities that have columns that are array types (a mixture of arrays with either numbers, booleans or strings). Mikro-orm seems to handle this perfectly fine when running against postgres, inferring the right database column type and mapping to/from Javascript. However, we currently run our unit tests (using vitest) with sqlite. Under sqlite by default (and per documentation) mikro-orm creates the table schemas as text columns and when inserting records, the columns are written as comma separated strings. This works totally fine for our what we need to be testing (e.g. don't care if an array of string splits on comma). However, by default when it is loading a row into an entity, it returns the values as arrays of strings even if the declared type is an array of something not a string, e.g.
number[]
. So, for example, if we write an array[1, 2, 3]
to a column, it writes the string'1,2,3'
. When that column is read back it turns into['1','2','3']
in javascript.I think this is correct per the documentation, specifically the custom types section on overriding the array type. However, we don't need to override the type for postgres so I didn't want to reference the custom type in the entity class itself. I ended up doing some hacky "detect if running under vitest" stuff to get our unit tests to work correctly (that is get it to read back
[1, 2, 3]
from a loaded entity). I couldn't figure out how to really make a custom type only apply for certain database drivers or how to customize this kind of mapping just for better-sqlite (by subclassing something in that package?)Is there a better way to do this? Obviously we could just run postgres for unit test but that changes how the CI system works and is quite a bit slower.
Beta Was this translation helpful? Give feedback.
All reactions