npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

moleculer-postgis

v0.3.3

Published

Postgis mixin and functions for @moleculer/database

Readme

Save, validate and parse geometries on your @moleculer/database project

License GitHub issues GitHub stars

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: {
      // example with geom and validating types
      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, // default 3346
      geojson: { // optional - Documentation: https://postgis.net/docs/ST_AsGeoJSON.html
        maxDecimalDigits: 0 // default
        options: 0 // default
      }
    }),
  ],
};

Fields

module.exports = {
  settings: {
    fields: {
      geom: {
        columnName: 'geomfield', // optional
        geom: {
          type: 'geom', // define type - defaults to "geom"
          types: [], // defining types,
          multi: true, // defines handling multi geometries
          validate({ ctx, params, value, field }) {
            // validation function (same as @moleculer/database)
            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

| Query | | | ----------------------- | ----------------------------------- | | areaQuery | More info | | distanceQuery | More info | | asGeoJsonQuery | More info | | geometriesAsTextQuery | More info | | geomFromText | More info | | intersectsQuery | More info |

areaQuery

import { areaQuery } from 'moleculer-postgis';
const field = 'geomfield';
const fieldAs = 'geom';
// optional
const srid = 3346;

// ROUND(ST_Area("geomfield")) as "geom"
// If srid is passed, ST_Transform is applied
areaQuery(geom, fieldAs, srid);

distanceQuery

import { distanceQuery } from 'moleculer-postgis';
const field1 = 'geomfield';
const field2 = 'geomfield2';
const resultAs = 'distance';
// optional
const srid = 3346;

// ROUND(ST_Distance("geomfield", "geomfield2")) as "distance"
// If srid is passed, ST_Transform is applied for each field
distanceQuery(field1, field2, resultAs, srid);

asGeoJsonQuery

import { asGeoJsonQuery } from 'moleculer-postgis';
const field = 'geomfield';
const resultAs = 'geom';
// optional
const srid = 3346;
const opts = {
  digits: 0,
  options: 0,
};

// ST_AsGeoJSON("geomfield")::json as "geom"
// If srid is passed, ST_Transform is applied for each field. Options are not applied if not passed
asGeoJsonQuery(field, resultAs, srid, opts);

geometriesAsTextQuery

import { geometriesAsTextQuery } from 'moleculer-postgis';
const geometry = {
  type: 'Point',
  coordinates: [11, 22],
};

// ST_AsText(...)
// If passed multi geometries - it will use ST_Collect
geometriesAsTextQuery(geometry);

// Using crs with transofrms
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;
// ST_AsText(...)
const text = geometriesAsTextQuery(geometry);

// ST_GeomFromText(...)
// If srid is passed - ST_Transform is applied
geomFromText(text, srid);

intersectsQuery

import { intersectsQuery } from 'moleculer-postgis';
// any type of geometry can be passed (feature collection, feature, array of feature collections, etc)
const geometry = {
  type: 'Point',
  coordinates: [11, 22],
};
const field = 'geomfield';
const srid = 3346;

// ST_intersects(...)
// If srid is passed - ST_Transform is applied
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.