You can define custom types by extending
Type abstract class. It has several optional methods:
convertToDatabaseValue(value: any, platform: Platform): any
Converts a value from its JS representation to its database representation of this type. By default returns unchanged
convertToJSValue(value: any, platform: Platform): any
Converts a value from its database representation to its JS representation of this type. By default returns unchanged
toJSON(value: any, platform: Platform): any
Converts a value from its JS representation to its serialized JSON form of this type. By default uses the runtime value.
getColumnType(prop: EntityProperty, platform: Platform): string
Gets the SQL declaration snippet for a field of this type. By default returns
columnTypeof given property.
convertToDatabaseValueSQL(key: string, platform: Platform): string
Converts a value from its JS representation to its database representation of this type. (added in v4.4.2)
convertToJSValueSQL?(key: string, platform: Platform): string
Modifies the SQL expression (identifier, parameter) to convert to a JS value. (added in v4.4.2)
Then you can use this type when defining your entity properties:
In this example we will combine mapping values via database as well as during runtime.
The Point type is part of the Spatial extension of MySQL and enables you to store a single location in a coordinate space by using x and y coordinates. You can use the Point type to store a longitude/latitude pair to represent a geographic location.
First let's define the
Point class that will be used to represent the value during runtime:
Then the mapping type:
Now let's define an entity:
...and use it:
This will result in following queries:
We do a 2-step conversion here. In the first step, we convert the Point object into a string representation before saving to the database (in the convertToDatabaseValue method) and back into an object after fetching the value from the database (in the convertToJSValue method).
The format of the string representation format is called Well-known text (WKT). The advantage of this format is, that it is both human readable and parsable by MySQL.
Internally, MySQL stores geometry values in a binary format that is not identical to the WKT format. So, we need to let MySQL transform the WKT representation into its internal format.
This is where the
convertToDatabaseValueSQL methods come
This methods wrap a sql expression (the WKT representation of the Point) into MySQL functions ST_PointFromText and ST_AsText which convert WKT strings to and from the internal format of MySQL.
When using DQL queries, the
convertToDatabaseValueSQLmethods only apply to identification variables and path expressions in SELECT clauses. Expressions in WHERE clauses are not wrapped!
There are few types provided by MikroORM. All of them aim to provide similar experience among all the drivers, even if the particular feature is not supported out of box by the driver.
In PostgreSQL and MongoDB, it uses native arrays, otherwise it concatenates the
values into string separated by commas. This means that you can't use values that
contain comma with the
ArrayType (but you can create custom array type that will
handle this case, e.g. by using different separator).
By default array of strings is returned from the type. You can also have arrays
of numbers or other data types - to do so, you will need to implement custom
hydrate method that is used for converting the array values to the right type.
ArrayTypewill be used automatically if
typeis set to
array(default behaviour of reflect-metadata) or
number(either manually or via ts-morph). In case of
numberit will automatically handle the conversion to numbers. This means that the following examples would both have the
ArrayTypeused automatically (but with reflect-metadata we would have a string array for both unless we specify the type manually as `type: 'number')
You can use
BigIntType to support
bigints. By default, it will represent the
value as a
Blob type can be used to store binary data in the database.
BlobTypewill be used automatically if you specify the type hint as
Buffer. This means that the following example should work even without the explicit
type: BlobTypeoption (with both reflect-metadata and ts-morph providers).
To store objects we can use
JsonType. As some drivers are handling objects
automatically and some don't, this type will handle the serialization in a driver
independent way (calling
stringify only when needed).
To store dates without time information, we can use
DateType. It does use
column type and maps it to the
As opposed to the
DateType, to store only the time information, we can use
TimeType. It will use the
time column type, the runtime type is string.