Add native postgis support to strapi.
Add native postgis support to strapi.
Since Strapi does not support native database formats I convert requests before they being sent to the querybuilder and convert all the geometry objects to the geojson
.
Strapi Version 4.5.0 and up.
Setup your strapi app as usual
1npx create-strapi-app@latest my-project --quickstart
Install pg
and strapi-plugin-postgis
1npm install pg --save
2npm i strapi-plugin-postgis
Make sure to config your strapi to use postgrs
database as backend, Use this link in case you need any help with this step (https://strapi.io/blog/postgre-sql-and-strapi-setup)
Make sure to install postgis on your database server.
Modify your middlewares as following to let strapi load osm
tiles. Add '*.tile.openstreetmap.org'
to the img-src
as follows
1// ./config/middlewares.js
2module.exports = [
3 'strapi::errors',
4 {
5 name: 'strapi::security',
6 config: {
7 contentSecurityPolicy: {
8 useDefaults: true,
9 directives: {
10 'img-src': ["'self'", 'data:', 'blob:', '*.tile.openstreetmap.org'],
11 upgradeInsecureRequests: null,
12 },
13 },
14 },
15 },
16 'strapi::cors',
17 'strapi::poweredBy',
18 'strapi::logger',
19 'strapi::query',
20 'strapi::body',
21 'strapi::session',
22 'strapi::favicon',
23 'strapi::public',
24];
Run strapi and you should see the following line
1[2022-06-03 10:47:25.194] info: Welcome to Strapi Postgis 🚀 + 🐘 + 🗺️ | 3.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
Now in your content-types api folder modify schema.json
and add a new column with the following format
1{
2 "kind": "collectionType",
3 .
4 .
5 .
6 "attributes": {
7 .
8 .
9 .
10 "geom": { //--> your column name. you can change to anything
11 "columnType": {
12 "type": "specificType",
13 "args": [
14 "geometry(POINT,4326)" //-> change this line according to the Supported Data Types section
15 ]
16 },
17 "type": "customField", //->don't change this
18 "customField": "plugin::postgis.map" //->don't change this
19 }
20 }
21}
1"geom": {
2 "columnType": {
3 "type": "specificType",
4 "args": [
5 "geometry(POINT,4326)"
6 ]
7 },
8 "type": "customField",
9 "customField": "plugin::postgis.map"
10 }
POINT Z (0 0 0)
POINT ZM (0 0 0 0)
POINT EMPTY
1 "g_line": {
2 "columnType": {
3 "type": "specificType",
4 "args": [
5 "geometry(LINESTRING,4326)"
6 ]
7 },
8 "type": "customField",
9 "customField": "plugin::postgis.map"
10 }
1 "g_polygon": {
2 "columnType": {
3 "type": "specificType",
4 "args": [
5 "geometry(POLYGON,4326)"
6 ]
7 },
8 "type": "customField",
9 "customField": "plugin::postgis.map"
10 }
MULTIPOINT((0 0),(1 2))
MULTIPOINT Z ((0 0 0),(1 2 3))
MULTIPOINT EMPTY
MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))
MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)), ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))
GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))
GEOMETRYCOLLECTION EMPTY
npm install strapi-plugin-postgis
Check out the available plugin resources that will help you to develop your plugin or provider and get it listed on the marketplace.