From a98c9e03ce10a7b6520936e5cdd60361dbdad9f7 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sat, 31 Dec 2022 22:30:26 +0800 Subject: [PATCH] fix(map-plugin): cannot save because the value is null (#1309) * fix: cannot save because the value is null * test: update --- packages/plugins/map/src/server/__tests__/fields.test.ts | 7 ++++++- packages/plugins/map/src/server/fields/circle.ts | 3 ++- packages/plugins/map/src/server/fields/lineString.ts | 7 ++++--- packages/plugins/map/src/server/fields/point.ts | 7 ++++--- packages/plugins/map/src/server/fields/polygon.ts | 9 +++++---- 5 files changed, 21 insertions(+), 12 deletions(-) diff --git a/packages/plugins/map/src/server/__tests__/fields.test.ts b/packages/plugins/map/src/server/__tests__/fields.test.ts index ef2625848..798653b97 100644 --- a/packages/plugins/map/src/server/__tests__/fields.test.ts +++ b/packages/plugins/map/src/server/__tests__/fields.test.ts @@ -143,7 +143,12 @@ describe('fields', () => { it('empty', async () => { const Test = await createCollection(); - const model = await Test.model.create(); + const model = await Test.model.create({ + circle: null, + lineString: null, + point: null, + polygon: null, + }); await model.save(); const findOne = () => diff --git a/packages/plugins/map/src/server/fields/circle.ts b/packages/plugins/map/src/server/fields/circle.ts index b518aef48..8e58e78cb 100644 --- a/packages/plugins/map/src/server/fields/circle.ts +++ b/packages/plugins/map/src/server/fields/circle.ts @@ -24,7 +24,8 @@ export class CircleField extends Field { } }, set(value) { - if (isPg(context)) { + if (!value?.length) value = null + else if (isPg(context)) { value = value.join(',') } this.setDataValue(name, value) diff --git a/packages/plugins/map/src/server/fields/lineString.ts b/packages/plugins/map/src/server/fields/lineString.ts index 9d8b46825..878333339 100644 --- a/packages/plugins/map/src/server/fields/lineString.ts +++ b/packages/plugins/map/src/server/fields/lineString.ts @@ -22,13 +22,14 @@ export class LineStringField extends Field { } }, set(value) { - if (isPg(context)) { + if (!value?.length) value = null + else if (isPg(context)) { value = joinComma(value.map(joinComma)) } else if (isMysql(context)) { - value = value?.length ? { + value = { type: 'LineString', coordinates: value - } : null + } } this.setDataValue(name, value) }, diff --git a/packages/plugins/map/src/server/fields/point.ts b/packages/plugins/map/src/server/fields/point.ts index e1d135993..1aaf8e8a3 100644 --- a/packages/plugins/map/src/server/fields/point.ts +++ b/packages/plugins/map/src/server/fields/point.ts @@ -25,13 +25,14 @@ export class PointField extends Field { } }, set(value) { - if (isPg(context)) { + if (!value?.length) value = null + else if (isPg(context)) { value = joinComma(value) } else if (isMysql(context)) { - value = value?.length ? { + value = { type: 'Point', coordinates: value - } : null + } } this.setDataValue(name, value) }, diff --git a/packages/plugins/map/src/server/fields/polygon.ts b/packages/plugins/map/src/server/fields/polygon.ts index 145d44cc5..11097e809 100644 --- a/packages/plugins/map/src/server/fields/polygon.ts +++ b/packages/plugins/map/src/server/fields/polygon.ts @@ -22,13 +22,14 @@ export class PolygonField extends Field { } }, set(value) { - if (isPg(context)) { - value = value ? joinComma(value.map((item: any) => joinComma(item))) : null + if (!value?.length) value = null + else if (isPg(context)) { + value = joinComma(value.map((item: any) => joinComma(item))) } else if (isMysql(context)) { - value = value?.length ? { + value = { type: 'Polygon', coordinates: [value.concat([value[0]])] - } : null + } } this.setDataValue(name, value) },