Save, validate and parse geometries on your @moleculer/database
project
Table of Contents
About the Project
The moleculer-postgis
is designed to support Postgis functions for @moleculer/database
mixin.
Getting Started
To get started, install moleculer-postgis
package to your project.
npm i moleculer-postgis
yarn add moleculer-postgis
Usage
import PostgisMixin, { GeometryType } from 'moleculer-postgis';
module.exports = {
mixins: [
PostgisMixin({
srid: 3346,
}),
],
settings: {
fields: {
geom: {
type: 'any',
geom: {
types: [GeometryType.POINT, GeometryType.LINE_STRING],
},
},
area: {
type: 'number',
virtual: true,
geom: {
type: 'area',
},
},
},
},
};
Documentation
Mixin
Using mixin is simple. Import and define it as a function. To the function you can pass opts
such as global srid
.
import PostgisMixin from 'moleculer-postgis';
module.exports = {
mixins: [
PostgisMixin({
srid: 3346,
geojson: {
maxDecimalDigits: 0
options: 0
}
}),
],
};
Fields
module.exports = {
settings: {
fields: {
geom: {
columnName: 'geomfield',
geom: {
type: 'geom',
types: [],
multi: true,
validate({ ctx, params, value, field }) {
return true;
},
},
},
},
defaultPopulates: ['geom'],
},
};
module.exports = {
settings: {
fields: {
geom: {
geom: true,
},
},
},
};
Options:
Option | Default value | Type (available values) |
---|
type | geom | String - area , geom |
multi | false | Boolean |
types | All types | Array<string> or string |
validate | - | Function or string |
Types - Point
, LineString
, Polygon
, MultiLineString
, MultiPoint
, MultiPolygon
Queries
areaQuery
import { areaQuery } from 'moleculer-postgis';
const field = 'geomfield';
const fieldAs = 'geom';
const srid = 3346;
areaQuery(geom, fieldAs, srid);
distanceQuery
import { distanceQuery } from 'moleculer-postgis';
const field1 = 'geomfield';
const field2 = 'geomfield2';
const resultAs = 'distance';
const srid = 3346;
distanceQuery(field1, field2, resultAs, srid);
asGeoJsonQuery
import { asGeoJsonQuery } from 'moleculer-postgis';
const field = 'geomfield';
const resultAs = 'geom';
const srid = 3346;
const opts = {
digits: 0,
options: 0,
};
asGeoJsonQuery(field, resultAs, srid, opts);
geometriesAsTextQuery
import { geometriesAsTextQuery } from 'moleculer-postgis';
const geometry = {
type: 'Point',
coordinates: [11, 22],
};
geometriesAsTextQuery(geometry);
const geometry = {
type: 'Point',
coordinates: [11, 22],
crs: { type: 'name', properties: { name: 'EPSG:4326' } },
};
const srid = 3346;
geometriesAsTextQuery(geometry, srid);
geomFromText
import { geomFromText, geometriesAsTextQuery } from 'moleculer-postgis';
const geometry = {
type: 'Point',
coordinates: [11, 22],
};
const srid = 3346;
const text = geometriesAsTextQuery(geometry);
geomFromText(text, srid);
intersectsQuery
import { intersectsQuery } from 'moleculer-postgis';
const geometry = {
type: 'Point',
coordinates: [11, 22],
};
const field = 'geomfield';
const srid = 3346;
intersectsQuery(field, geometry, srid);
Contributing
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a
pull request. For more information, see the contribution guidelines.
License
This project is licensed under the MIT License.