loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Support GeoPoint type

Open JorisPalings opened this issue 5 years ago • 42 comments

Description/Steps to reproduce

I'm attempting to start my project by using npm start and get the following output:

> [email protected] prestart /Users/joris/Documents/Personal/Projects/project/project-api
> npm run build


> [email protected] build /Users/joris/Documents/Personal/Projects/project/project-api
> lb-tsc es2017 --outDir dist


> [email protected] start /Users/joris/Documents/Personal/Projects/project/project-api
> node .

Cannot start the application. Error: Unsupported type: geopoint
    at stringTypeToWrapper (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/repository-json-schema/dist/src/build-schema.js:68:19)
    at metaToJsonProperty (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/repository-json-schema/dist/src/build-schema.js:101:25)
    at modelToJsonSchema (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/repository-json-schema/dist/src/build-schema.js:151:32)
    at Object.getJsonSchema (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/repository-json-schema/dist/src/build-schema.js:22:27)
    at generateOpenAPISchema (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/openapi-v3/dist/src/controller-spec.js:181:49)
    at resolveControllerSpec (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/openapi-v3/dist/src/controller-spec.js:128:17)
    at Object.getControllerSpec (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/openapi-v3/dist/src/controller-spec.js:205:16)
    at RestServer._setupHandlerIfNeeded (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/rest/dist/src/rest.server.js:195:42)
    at RestServer.start (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/rest/dist/src/rest.server.js:498:14)
    at _forEachServer.s (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/core/dist/src/application.js:123:42)
    at Promise.all.bindings.map (/Users/joris/Documents/Personal/Projects/project/project-api/node_modules/@loopback/core/dist/src/application.js:145:26)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/joris/.npm/_logs/2018-11-05T15_06_38_766Z-debug.log

In one of my models, I have a property of type 'geopoint':

  @property({
    type: 'geopoint',
    required: true,
  })
  location: string;

Upon closer inspection, no case is defined for the 'geopoint' type in the switch statement in @loopback/repository-json-schema/dist/src/build-schema.js.

Expected result

For the Loopback 4 project to start.

Additional information

  • node -e 'console.log(process.platform, process.arch, process.versions.node)' shows darwin x64 10.12.0
  • npm ls --prod --depth 0 | grep loopback shows no output

JorisPalings avatar Nov 05 '18 19:11 JorisPalings

The support for GeoPoint is not finished yet. I suggest turn this issue to be a feature.

We can either leverage the GeoPoint class in juggler https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/geo.js#L164

Or implement a GeoPoint type in loopback 4: see https://github.com/strongloop/loopback-next/tree/master/packages/repository/src/types

jannyHou avatar Nov 07 '18 21:11 jannyHou

Added "feature" label.

@raymondfeng what's your take on the direction we should take here? I see two options:

  1. A quick fix: keep using the current GeoPoint class from juggler and/or loopback-datatype-geopoint. @loopback/repository should export GeoPoint constructor. We may need to describe GeoPoint in juggler typings.

  2. A rewrite/redesign: create a new implementation of GeoPoint to be used in LB4. While this type can live in @loopback/repository, I'd personally prefer to see it in it's own package, e.g. @loopback/geopoint.

Either way, we need to improve @loopback/repository-json-schema to correctly handle this new type. If GeoPoint is maintained outside of @loopback/repository, then we need to define & implement an extension point allowing GeoPoint to contribute JSON Schema mapping.

Personally, I'd prefer the second option, as it provides a great opportunity to build infrastructure that will allow LB4 community to contribute further types in the future.

bajtos avatar Nov 08 '18 11:11 bajtos

@JorisPalings your model is storing GeoPoint types as string. I find that a bit unusual, shouldn't GeoPoints be represented as objects?

bajtos avatar Nov 08 '18 11:11 bajtos

@bajtos I created it through the lb4 model CLI command, which for some reason defined all model properties as strings.

JorisPalings avatar Nov 08 '18 13:11 JorisPalings

I created it through the lb4 model CLI command, which for some reason defined all model properties as strings.

Here is the code generating TypeScript types in lb4 model:

https://github.com/strongloop/loopback-next/blob/7966ee8e8a0b9129ed980d79be06a8e8e907b2ee/packages/cli/generators/model/index.js#L297-L319

AFAICT, it falls back to string for types it does not understand (e.g. geopoint).

bajtos avatar Nov 23 '18 13:11 bajtos

Shall we do a spike story for adding types like GeoPoint?

jannyHou avatar Dec 13 '18 01:12 jannyHou

IIRC, @hacksparrow is already looking into this issue.

bajtos avatar Dec 13 '18 07:12 bajtos

@hacksparrow, since you're looking into this issue already, could you please add the acceptance criteria, so that the team can estimate? Thanks!

dhmlau avatar Jan 08 '19 18:01 dhmlau

@dhmlau the acceptance criteria for this issue would be simply, "App should start with GeoPoint support". We will miss the implementation and feature details of the as-yet-non-supportedGeoPoint data type. Maybe we should track the GeoPoint support on a separate dedicated issue?

We also need to decide on the approaches suggested by @bajtos - https://github.com/strongloop/loopback-next/issues/1981#issuecomment-436966284.

hacksparrow avatar Jan 10 '19 07:01 hacksparrow

This issue is labelled as a feature, the goal is to add support for GeoPoint to LB4. See my comments above for more details about the problems we need to fix and places to modify.

"App should start with GeoPoint support".

IMO, the app should not only start, but also correctly handle GeoPoint properties in incoming requests and outgoing responses.

In the acceptance criteria, I'd like to see a description of the approach we decided to take and a (high-level) list of places in our code that need to be modified.

bajtos avatar Jan 11 '19 16:01 bajtos

@strongloop/loopback-maintainers, before we can get to the acceptance criteria, I think we probably need to discuss how we should enable the support of GeoPoint. There are 2 options proposed by @bajtos in https://github.com/strongloop/loopback-next/issues/1981#issuecomment-436966284. PTAL.

dhmlau avatar Jan 26 '19 03:01 dhmlau

I have always found the LB3 GeoPoint type limiting, and have often desired support for other geospatial types (e.g. lines, polygons. etc.) and other geospatial relationship comparison operators (e.g. other than just near). I suggest LB4 (insead of / in addtion to) support GeoJSON types (i.e. GeoJsonObject, GeometryObject, or Geometry) as there are many support packages which simplify the implementation (e.g. for the Memory Connector). See:

  • https://www.npmjs.com/package/geojson
  • https://www.npmjs.com/package/@types/geojson
  • https://www.npmjs.com/browse/depended/@types/geojson
  • https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/geojson
  • https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/geojson/index.d.ts

P.S. Several weeks ago I tried implementing this as a value object and had difficulties because of how arrays of types are not well supported in LB4 (i.e. an array of numbers).

ewrayjohnson avatar Apr 02 '19 17:04 ewrayjohnson

had difficulties because of how arrays of types are not well supported in LB4 (i.e. an array of numbers).

Could you please open a new issue and provide more details? @b-admike is improving handling of nested properties (including arrays) at the moment, some of the issues may be already fixed by now.

bajtos avatar Apr 16 '19 13:04 bajtos

There is also an issue with type any

Cannot start the application. Error: Unsupported type: any
    at stringTypeToWrapper (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\repository-json-schema\dist\build-schema.js:69:19)
    at metaToJsonProperty (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\repository-json-schema\dist\build-schema.js:102:25)
    at modelToJsonSchema (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\repository-json-schema\dist\build-schema.js:160:32)
    at Object.getJsonSchema (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\repository-json-schema\dist\build-schema.js:23:27)
    at generateOpenAPISchema (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\openapi-v3\dist\controller-spec.js:194:49)
    at resolveTSType (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\openapi-v3\dist\controller-spec.js:161:13)
    at resolveControllerSpec (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\openapi-v3\dist\controller-spec.js:66:17)
    at Object.getControllerSpec (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\openapi-v3\dist\controller-spec.js:218:16)
    at RestServer._setupHandlerIfNeeded (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\rest\dist\rest.server.js:215:42)
    at RestServer.start (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\rest\dist\rest.server.js:530:14)
    at LifeCycleObserverRegistry.invokeObserver (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\core\dist\lifecycle-registry.js:136:34)
    at observers.map (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\core\dist\lifecycle-registry.js:125:25)
    at Array.map (<anonymous>)
    at LifeCycleObserverRegistry.notifyObservers (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\core\dist\lifecycle-registry.js:123:37)
    at LifeCycleObserverRegistry.notifyGroups (C:\Users\user\Desktop\happyfitt\node_modules\@loopback\core\dist\lifecycle-registry.js:162:28)
    at process._tickCallback (internal/process/next_tick.js:68:7)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `node .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\user\AppData\Roaming\npm-cache\_logs\2019-05-04T21_39_50_979Z-debug.log

hadasiddhant avatar May 04 '19 21:05 hadasiddhant

@hadasiddhant please open a new issue to discuss the type any, so that the discussion here can stay focused on GeoPoint. Thanks!

bajtos avatar May 09 '19 11:05 bajtos

Guys any update on GeoPoint?

riyastir avatar Oct 08 '19 03:10 riyastir

Hello, any news on this subject ?

malek0512 avatar Jan 22 '20 09:01 malek0512

I am working on creating tests to show the bug and confirm the fix

ewrayjohnson avatar Jan 28 '20 06:01 ewrayjohnson

Hello @ewrayjohnson @bajtos, is there any progress on this feature ?

malek0512 avatar Mar 24 '20 14:03 malek0512

@strongloop/loopback-maintainers, would like to get your input on the approach to support geopoint. Thanks!

To summarize, @bajtos had 2 proposals mentioned in https://github.com/strongloop/loopback-next/issues/1981#issuecomment-436966284, and @ewrayjohnson also mentioned the limitation of the juggler geopoint implementation.

dhmlau avatar Apr 15 '20 13:04 dhmlau

Yes

Wray Johnson

On Mar 24, 2020, at 10:38 AM, Malek Mammar [email protected] wrote:

 Hello @ewrayjohnson, is there any progress on this feature ?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

ewrayjohnson avatar Apr 16 '20 01:04 ewrayjohnson

@JorisPalings @riyastir @malek0512 @ewrayjohnson can you give some details on the usecases you need this feature for ? Is this necessary for a specific backend or db querying ? we need some more details on this issue , to scope it and estimate,

deepakrkris avatar Apr 16 '20 18:04 deepakrkris

Hi, I anyhow tried to insert the geopont data with mysql-connector. first in your model : add Interface <YourInterfaceName> eg:

interface GeoObject {
  lat: number;
  lng: number;
}

than in side your class in property decorator

...Model
 @property({
    type: 'object',
    required: false,
    mysql: {
      dataType: 'point',
    },
  })
  location: GeoObject;
...restModel

Now to insert from rest 'post' endpoint, in our model repository, we will be using 2 hooks

  1. persist
  2. after save to save the geopoint type data in database. The process is divided in to two steps 1)The basic idea is to pull GeoPoint from 'location' and save in a variable and deleting the node loc from the current context and save the data. using 'persist' hook
  3. after that updating the instance with the 'location' by using Update the instance by 'this.datasource.execute()' mentioned in @bajtos loopback-datasource-juggler/pull/1671 using aftersave hook.

Both hook should be called inside the constructor.Example code

...constructor
 let loc: any = null;
    (this.modelClass as any).observe('persist', async (ctx: any) => {
      console.log(ctx.data);
      loc = ctx.data.location;
      delete ctx.data.location;
      console.log(ctx.data);
    });
    (this.modelClass as any).observe('after save', async (ctx: any) => {
      console.log(ctx);
      const id = ctx.instance.id;
      this.dataSource.execute(
        "UPDATE `Store` SET `location` = ST_GeomFromText('Point(" +
          loc.lat +
          ' ' +
          loc.lng +
          ")') WHERE `Store`.`id` = '" +
          id +
          "'",
      );
    });
...restConstructor code

madaky avatar Apr 16 '20 18:04 madaky

@JorisPalings @riyastir @malek0512 @ewrayjohnson can you give some details on the usecases you need this feature for ? Is this necessary for a specific backend or db querying ? we need some more details on this issue , to scope it and estimate, @deepakrkris : the details are as follows :

Problem Definitation : AFAIK, When we need to create a Spatial data type in db, we would be needing the datatype

  • Point,
  • LineString,
  • Polygon,
  • MultiPoint,
  • MultiLineString,
  • MultiPolygon,
  • GeometryCollection

Here we are focusing on inserting the data in 'Point' type data type. -For the purpose we need a type named 'geopoint' which may be a object type consisting of two coordinates. { x:number, y:number } -This help in reducing the manual leverage of the developers to define the Spatial data.

  • While sending request to rest endpoints, we can supply the coordinates in the property, and those can be inserted in the database.

  • as in my previous comment as I also used a function ST_GeomFromText which is a mysql function in this case and a Point function as a string supplied to it to be executed in mysql.

Also I have not tried other queries like st_distance to find the distance from PointA to Point B

Another approach can be we can store GeoJson data in data source, and can implement GeoJson schema.

Please let me know if this helps. Also please let me know how to approach this.

madaky avatar Apr 16 '20 18:04 madaky

It says that GeoPoint is support here https://apidocs.strongloop.com/loopback-datasource-juggler/#geopoint

Is it not actually supported? I'm using postgresql. If it is supported now, what packages should I update?

vikramkh avatar May 04 '20 16:05 vikramkh

I will try to implement this in the next few days. I think it is necessary to have this functionality

frbuceta avatar Jun 09 '20 10:06 frbuceta

Hi guys, any news on the support for geopoint?

GianlucaCesari avatar Jul 03 '20 10:07 GianlucaCesari

Hi guys, any news on the support for geopoint?

Nothing advanced yet. Sorry for the inconvenience

frbuceta avatar Jul 03 '20 10:07 frbuceta

@frbuceta Is now the loopback 4 support geopoint?

lakshanmamalgaha avatar Jul 19 '20 12:07 lakshanmamalgaha

@dhmlau @bajtos @frbuceta Any new updates on LB4 with GeoPoint data support.

pktippa avatar Oct 25 '20 11:10 pktippa