typeorm 几何类型未定义类型错误
typeorm geometry type Undefined type error
各位。这次我正在使用 postgresql postgis 创建一个使用几何数据的项目。所以我想在列中声明几何并使用它,但是出现错误。你能告诉我为什么会出错吗?
查了很多官方文档,没找到方法。
注释坐标栏将正常创建代码。
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn
} from 'typeorm';
import { Location_Group } from './location_group.entity';
import { Geometry } from 'geojson';
import { Field, ID, Int, ObjectType } from '@nestjs/graphql';
@ObjectType()
@Entity()
export class Location {
@Field(() => ID)
@PrimaryGeneratedColumn('increment')
id: number;
@Field(() => String)
@Column({ type: 'varchar' })
name: string;
@Field()
@Column({
type: 'geometry',
nullable: true,
spatialFeatureType: 'Point',
srid: 4326
})
coordinate: Geometry;
@Field(() => Int)
@Column({ type: 'int' })
order_number: number;
@Field()
@CreateDateColumn({ type: 'timestamptz' })
created_at: Date;
@Field(() => Location_Group)
@ManyToOne(
() => Location_Group,
(location_group) => location_group.location
)
@JoinColumn([{ name: 'location_group_id', referencedColumnName: 'id' }])
location_group: Location_Group;
}
有人要我分享我做的标量,所以写在这里。希望这段代码对你有帮助。
import { GraphQLScalarType } from 'graphql';
export const GeoJSONPoint = new GraphQLScalarType({
name: 'GeoJSONPoint',
description: 'Geometry scalar type',
parseValue(value) {
return value;
},
serialize(value) {
return value;
},
parseLiteral(ast) {
const geometryData = {
type: '',
coordinates: []
};
for (const i in ast['fields']) {
if (ast['fields'][i]['name']['value'] == 'type') {
if (ast['fields'][i]['value']['value'] != 'point') {
return null;
}
geometryData.type = ast['fields'][i]['value']['value'];
}
if (ast['fields'][i]['name']['value'] == 'coordinate') {
for (const j in ast['fields'][i]['value']['values']) {
geometryData.coordinates.push(
parseFloat(
ast['fields'][i]['value']['values'][j]['value']
)
);
}
}
}
return geometryData;
}
});
各位。这次我正在使用 postgresql postgis 创建一个使用几何数据的项目。所以我想在列中声明几何并使用它,但是出现错误。你能告诉我为什么会出错吗?
查了很多官方文档,没找到方法。 注释坐标栏将正常创建代码。
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn
} from 'typeorm';
import { Location_Group } from './location_group.entity';
import { Geometry } from 'geojson';
import { Field, ID, Int, ObjectType } from '@nestjs/graphql';
@ObjectType()
@Entity()
export class Location {
@Field(() => ID)
@PrimaryGeneratedColumn('increment')
id: number;
@Field(() => String)
@Column({ type: 'varchar' })
name: string;
@Field()
@Column({
type: 'geometry',
nullable: true,
spatialFeatureType: 'Point',
srid: 4326
})
coordinate: Geometry;
@Field(() => Int)
@Column({ type: 'int' })
order_number: number;
@Field()
@CreateDateColumn({ type: 'timestamptz' })
created_at: Date;
@Field(() => Location_Group)
@ManyToOne(
() => Location_Group,
(location_group) => location_group.location
)
@JoinColumn([{ name: 'location_group_id', referencedColumnName: 'id' }])
location_group: Location_Group;
}
有人要我分享我做的标量,所以写在这里。希望这段代码对你有帮助。
import { GraphQLScalarType } from 'graphql';
export const GeoJSONPoint = new GraphQLScalarType({
name: 'GeoJSONPoint',
description: 'Geometry scalar type',
parseValue(value) {
return value;
},
serialize(value) {
return value;
},
parseLiteral(ast) {
const geometryData = {
type: '',
coordinates: []
};
for (const i in ast['fields']) {
if (ast['fields'][i]['name']['value'] == 'type') {
if (ast['fields'][i]['value']['value'] != 'point') {
return null;
}
geometryData.type = ast['fields'][i]['value']['value'];
}
if (ast['fields'][i]['name']['value'] == 'coordinate') {
for (const j in ast['fields'][i]['value']['values']) {
geometryData.coordinates.push(
parseFloat(
ast['fields'][i]['value']['values'][j]['value']
)
);
}
}
}
return geometryData;
}
});