Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 68 additions & 3 deletions lib/type-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const BOOLEAN = { type: 'boolean' };
const INTEGER = { type: 'integer' };
const NUMBER = { type: 'number' };
const STRING = { type: 'string' };
const GEOMETRY = { type: 'object' };
const ANY = { anyOf: [OBJECT, ARRAY, BOOLEAN, INTEGER, NUMBER, STRING] };

/**
Expand Down Expand Up @@ -186,9 +187,73 @@ class TypeMapper {
// @todo GEOMETRY('POINT', 4326)
// ----------------------------------------------------------------------
case 'GEOMETRY': {
throw new TypeError(
'sequelize-to-json-schemas has not yet implemented the GEOMETRY DataType',
);
const geometrySubType = properties.type?.options?.type?.toUpperCase();

result = {
...GEOMETRY,
required: ['type', 'coordinates'],
properties: {
type: {
type: 'string',
enum: geometrySubType
? [geometrySubType[0] + geometrySubType.slice(1).toLowerCase()]
: [
'Point',
'LineString',
'Polygon',
'MultiPoint',
'MultiLineString',
'MultiPolygon',
'GeometryCollection',
],
},
coordinates: { type: 'array' }, // refined below
},
};

switch (geometrySubType) {
case 'POINT':
result.properties.coordinates = {
type: 'array',
items: { type: 'number' },
minItems: 2,
};
break;

case 'MULTIPOINT':
case 'LINESTRING':
result.properties.coordinates = {
type: 'array',
items: { type: 'array', items: { type: 'number' }, minItems: 2 },
};
break;

case 'MULTILINESTRING':
case 'POLYGON':
result.properties.coordinates = {
type: 'array',
items: {
type: 'array',
items: { type: 'array', items: { type: 'number' }, minItems: 2 },
},
};
break;

case 'MULTIPOLYGON':
result.properties.coordinates = {
type: 'array',
items: {
type: 'array',
items: {
type: 'array',
items: { type: 'array', items: { type: 'number' }, minItems: 2 },
},
},
};
break;
}

break;
}

// ----------------------------------------------------------------------
Expand Down