am2222 / strapi-plugin-postgis

Add native postgis support to strapi.
https://am2222.github.io/strapi-plugin-postgis/
40 stars 14 forks source link

Populate going wrong when Geom is set on #2

Open qmidy opened 1 year ago

qmidy commented 1 year ago

Hi,

Thanks a lot for your awesome plugin which works very well.

I got an issue when i wanna populate an object on my user on which i have added a geom.

So when i wanna do this :

var user = await strapi.entityService.findOne( 'plugin::users-permissions.user', id, { populate: ['music_entities'] } );

I got the following exception - I add the logs in the node_modules.

So when it comes to read 'geom', it's typed as a string field and so a JSON.parse is done on it and so it crashes but i don't know what can be done to fix it, sorry.

[2022-07-28 23:17:11.748] info: Welcome to Strapi Postgis 🚀 + 🐘 + 🗺️ | 3.2 USE_GEOS=1 USE_PROJ=1 USE_STATS=1

geom { id: 1, name: 'xxxx', description: 'xxxxx', entity_type: 'band', created_at: 2022-07-26T18:55:31.797Z, updated_at: 2022-07-26T19:23:11.546Z, published_at: 2022-07-26T19:23:11.532Z, created_by_id: 1, updated_by_id: 1, url_encode_name: 'xxxx', location_name: 'xxxxx', socials_url: null, geom: '0101000020E610000047AAEFFCA24356C03FC8B260E22D5040', user_id: 3 } [2022-07-28 23:10:39.079] error: Unexpected number in JSON at position 1 SyntaxError: Unexpected number in JSON at position 1 at JSON.parse (<anonymous>) at JSONField.fromDB (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\fields.js:37:48) at fromRow (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\helpers\transform.js:34:55) at C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\helpers\transform.js:10:33 at Array.map (<anonymous>) at fromRow (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\helpers\transform.js:10:16) at fromTargetRow (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\helpers\populate.js:133:40) at C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\helpers\populate.js:409:23 at Array.forEach (<anonymous>) at Object.applyPopulate (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\helpers\populate.js:408:15) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async Object.execute (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\query\query-builder.js:357:11) at async Object.findOne (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\database\lib\entity-manager.js:118:22) at async Object.findOne (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\strapi\lib\services\entity-service\index.js:67:20) at async Object.fetch (C:\Projets\Local Stage\local-scene-back-strapi-v4\src\extensions\users-permissions\server\services\user.js:70:16) at async Object.findOne (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\plugin-users-permissions\server\controllers\user.js:152:16) at async returnBodyMiddleware (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\strapi\lib\services\server\compose-endpoint.js:52:18) at async policiesMiddleware (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\strapi\lib\services\server\policy.js:24:5) at async serve (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\koa-static\index.js:59:5) at async returnBodyMiddleware (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\strapi\lib\services\server\compose-endpoint.js:52:18) at async policiesMiddleware (C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\strapi\lib\services\server\policy.js:24:5) at async C:\Projets\Local Stage\local-scene-back-strapi-v4\node_modules\@strapi\strapi\lib\middlewares\body.js:51:9

Is this enough information ? 
am2222 commented 1 year ago

@qmidy Thanks for the bug report. I noticed that when strapi populates fields it does not call database lifecycles methods such as beforeFind and so on. As a result in your query geom field will remain in wkb format

geom { id: 1, name: 'xxxx', description: 'xxxxx', entity_type: 'band', created_at: 2022-07-26T18:55:31.797Z, updated_at: 2022-07-26T19:23:11.546Z, published_at: 2022-07-26T19:23:11.532Z, created_by_id: 1, updated_by_id: 1, url_encode_name: 'xxxx', location_name: 'xxxxx', socials_url: null, geom: '0101000020E610000047AAEFFCA24356C03FC8B260E22D5040', user_id: 3 }

I need to find a way to find a workaround related to this limitation or think of patching strapi's qurybuilder and related helper classes. https://github.com/strapi/strapi/blob/21b95f9954a3ad622ff42dfdb34172bee7483284/packages/core/database/lib/query/helpers/populate.js#L114

qmidy commented 1 year ago

For now, the workaround is to request the nested class' service and filters on the user id (in my case obviously). But it means to request the db twice.

Also, it seems to be hard to fix without touching the core of strapi -- maybe a regex on the string to know if it's a wkb or not and so do Json.parse or not.

Maybe we can ask strapi directly ?

am2222 commented 1 year ago

@qmidy The problem is that we need to touch core strapi query builder. I have actually asked them a couple of times and seems it is not in their plans. I was thinking to add a beforeExcuteQuery and afterExcureQuery hooks to the query builder and then do the query modifications there. For example if we want to sort by distance or add geographic where filters we will face many issues.

vrutik-21 commented 1 year ago

Facing the same issue right now, can't populate the relation field consisting locations from another collection type. It just keeps reloading forever and fails to get items in the api as well as admin panel. Is there any temporary fix for this issue?

qmidy commented 1 year ago

The workaround is to not use populate and request the nested service

vrutik-21 commented 1 year ago

@qmidy Thank you for the response. Can you please somehow explain or show me an example how you overcame this issue. That would be much helpful to me as I've been stuck at this issue since a week now.

qmidy commented 1 year ago

Can you send your code ?

Envoyé à partir de Outlook pour Androidhttps://aka.ms/AAb9ysg


From: vrutik-21 @.> Sent: Friday, December 16, 2022 12:02:09 PM To: am2222/strapi-plugin-postgis @.> Cc: qmidy @.>; Mention @.> Subject: Re: [am2222/strapi-plugin-postgis] Populate going wrong when Geom is set on (Issue #2)

@qmidyhttps://github.com/qmidy Thank you for the response. Can you please somehow explain or show me an example how you overcame this issue. That would be much helpful to me as I've been stuck at this issue since a week now.

— Reply to this email directly, view it on GitHubhttps://github.com/am2222/strapi-plugin-postgis/issues/2#issuecomment-1354558054, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABK55V2OD4HOUXGWFSRAFLLWNRD3DANCNFSM546VMPGA. You are receiving this because you were mentioned.Message ID: @.***>

vrutik-21 commented 1 year ago

@qmidy @am2222 This is the attribute I've set in the location collection type schema -

"point": { "columnType": { "type": "specificType", "args": ["geometry(POINT,4326)"] }, "type": "json", "default": { "type": "Point", "coordinates": [] }, "fieldRenderer": "postgis", "coord_dimension": 2, "f_table_schema": "public", "geoType": "POINT", "required": true, "isSpatial": true, "srid": 4326 }

This is the location collection type -

locations

All I want is to use this field as a relation with the Quest collection type and populate in it's api too, but in return I'm getting this as a error whenever I click on the items field in admin panel-

quest

Quest route - GET http://localhost:1337/api/quests?populate=* Response I'm getting - { "data": [], "meta": { "pagination": { "page": 1, "pageSize": 25, "pageCount": 0, "total": 0 } } }

Also I've not made a build after installing strapi postgis plugin because strapi gave me an error and a white screen whenever I made build with this plugin.

qmidy commented 1 year ago

Maybe try to override controller and service with a sql raw but on your specific id.