Search code examples
mysqlnode.jstypescripttypeormgeojson

FUNCTION Database.GeomFromText doesnt exist in TypeORM


I use mysql with typeorm and typescript. I have the following entity.

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { Geometry } from "geojson";

@Entity({ name: 'user' })
export class User {
    @PrimaryGeneratedColumn()
    id!: number;

    @Column({ unique: true })
    username: string;

    @Column({ select: false })
    password: string;

    @Column({
        type: "geometry",
        nullable: true,
        srid: 4326
    })
    location: Geometry;
}

and a function to create user from an incoming http request

async createUser(req: Request, res: Response) {
    const { username, password, location } = req.body
    let validLocation: Geometry

    if (!username || !password || !location)
        return noGoodRequest(res, 'bad parameter')

    try {
        const l = JSON.parse(location)
        validLocation = {
            type: "Point",
            coordinates: [l.longitude, l.latitude]
        }
    } catch {
        return noGoodRequest(res, 'bad parameter')
    }

    const newUser = await UserRepo
        .create({ username, password, location: validLocation })

    await UserRepo.save(newUser)

    goodRequest(res)
}

when invoking the createUser function i get the following error and I'm stuck.

QueryFailedError: FUNCTION mydb.GeomFromText does not exist

I need help on this matter.

I tried changing location: Geometry to location: string and @Column({ type: "geometry" }) to @Column({ type: "point" })


Solution

  • You can set connection option with legacySpatialSupport: false

    zhe source code in typeorm