# knex-postgis

> postgis extension for knex

Latest version **0.14.3** (published 2022-03-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install knex-postgis
pnpm add knex-postgis
yarn add knex-postgis
bun add knex-postgis
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.14.3 |
| Published | 2022-03-22 |
| First published | 2014-10-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 62.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 188 |
| Author | Jorge Godoy |
| Maintainers | jfgodoy |
| Keywords | knex, postgres, postgis, sql, sql builder |

## Links

- npm: https://www.npmjs.com/package/knex-postgis
- Repository: https://github.com/jfgodoy/knex-postgis
- Homepage: https://github.com/jfgodoy/knex-postgis#readme
- Issues: https://github.com/jfgodoy/knex-postgis/issues
- npm.io page: https://npm.io/package/knex-postgis

## Dependencies (2)

- [@types/geojson](https://npm.io/package/@types/geojson.md) ^7946.0.7
- [@placemarkio/check-geojson](https://npm.io/package/@placemarkio/check-geojson.md) 0.1.7

## Recent versions

- 0.14.3 (latest) — 2022-03-22
- 0.14.2 — 2022-03-10
- 0.14.1 — 2021-07-05
- 0.14.0 — 2021-07-04
- 0.13.0 — 2021-02-27
- 0.12.0 — 2020-04-28
- 0.11.0 — 2020-03-25
- 0.10.0 — 2019-11-15
- 0.9.0 — 2019-11-12
- 0.8.1 — 2019-07-03
- 0.8.0 — 2019-04-09
- 0.7.0 — 2018-08-03
- 0.6.0 — 2018-03-19
- 0.5.0 — 2017-11-15
- 0.4.0 — 2017-10-12
- … 17 more at https://npm.io/package/knex-postgis/versions

## README

# knex-postgis
[![npm version](http://img.shields.io/npm/v/knex-postgis.svg)](https://npmjs.org/package/knex-postgis)
[![npm downloads](https://img.shields.io/npm/dm/knex-postgis.svg)](https://npmjs.org/package/knex-postgis)
[![Build Status](https://www.travis-ci.com/jfgodoy/knex-postgis.svg?branch=master)](https://www.travis-ci.com/jfgodoy/knex-postgis)
[![Dependencies Status](https://img.shields.io/david/jfgodoy/knex-postgis)](https://david-dm.org/jfgodoy/knex-postgis)
[![License](https://img.shields.io/github/license/jfgodoy/knex-postgis)](https://github.com/jfgodoy/knex-postgis/blob/master/LICENSE)

Extension for use postgis functions in [knex](http://knexjs.org) SQL query builder.



## Example
This example show the sql generated by the extension.


```js
const knex = require('knex');
const knexPostgis = require('knex-postgis');

const db = knex({
  client: 'postgres'
});

// install postgis functions in knex.postgis;
const st = knexPostgis(db);
/* or:
 * knexPostgis(db);
 * const st = db.postgis;
 */

// insert a point
const sql1 = db.insert({
  id: 1,
  geom: st.geomFromText('Point(0 0)', 4326)
}).into('points').toString();
console.log(sql1);
// insert into "points" ("geom", "id") values (ST_geomFromText('Point(0 0)'), '1')

// find all points return point in wkt format
const sql2 = db.select('id', st.asText('geom')).from('points').toString();
console.log(sql2);
// select "id", ST_asText("geom") as "geom" from "points"

// all methods support alias
const sql3 = db.select('id', st.asText(st.centroid('geom')).as('centroid')).from('geometries').toString();
console.log(sql3);
// select "id", ST_asText(ST_centroid("geom")) as "centroid" from "geometries"

```

## Currently supported functions

- area(geom), see [postgis documentation](https://postgis.net/docs/ST_Area.html)
- asText(column), see [postgis documentation](https://postgis.net/docs/ST_AsText.html)
- asGeoJSON(column), see [postgis documentation](https://postgis.net/docs/ST_AsGeoJSON.html)
- asEWKT(column), see [postgis documentation](http://www.postgis.net/docs/ST_AsEWKT.html)
- buffer(geom, radius), see [postgis documentation](http://www.postgis.net/docs/ST_Buffer.html)
- centroid(geom), see [postgis documentation](https://postgis.net/docs/ST_Centroid.html)
- distance(geom, geom), see [postgis documentation](https://postgis.net/docs/ST_Distance.html)
- distanceSphere(geom, geom), see [postgis documentation](https://postgis.net/docs/manual-1.4/ST_Distance_Sphere.html)
- dwithin(geom, geom, distance, /* optional bool spheroid */), see [postgis documentation](https://postgis.net/docs/ST_DWithin.html)
- intersection(geom1, geom2), see [postgis documentation](https://postgis.net/docs/ST_Intersection.html)
- intersects(geom1, geom2), see [postgis documentation](https://postgis.net/docs/ST_Intersects.html)
- geography(geom)
- geographyFromText(ewkt), see [postgis documentation](https://postgis.net/docs/ST_GeographyFromText.html)
- geometry(geography)
- geomFromText(geom, srid), see [postgis documentation](http://www.postgis.net/docs/ST_GeomFromText.html)
- geomFromGeoJSON(geojson /*object, string or column name*/), see [postgis documentation](https://postgis.net/docs/ST_GeomFromGeoJSON.html)
- makeEnvelope(minlon, minlat, maxlon, maxlat, /* optional integer SRID */), see [postgis documentation](http://www.postgis.net/docs/ST_MakeEnvelope.html)
- makePoint(lon, lat, /* optional z */, /* optional measure */), see [postgis documentation](https://postgis.net/docs/ST_MakePoint.html)
- makeValid(geom), see [postgis documentation](https://postgis.net/docs/ST_MakeValid.html)
- point(lon, lat), see [postgis documentation](https://postgis.net/docs/ST_Point.html)
- transform(geom, srid), see [postgis documentation](https://postgis.net/docs/ST_Transform.html)
- within(geom, geom), see [postgis documentation](https://postgis.net/docs/ST_Within.html)
- setSRID(geom, srid), see [postgis documentation](https://postgis.net/docs/ST_SetSRID.html)
- x, see [postgis documentation](https://postgis.net/docs/ST_X.html)
- y, see [postgis documentation](https://postgis.net/docs/ST_Y.html)
- z, see [postgis documentation](https://postgis.net/docs/ST_Z.html)
- m, see [postgis documentation](https://postgis.net/docs/ST_M.html)
- boundingBoxIntersects(geom a, geom b), represented as `a && b`, see [postgis documentation](http://postgis.net/docs/manual-2.0/geometry_overlaps.html)
- boundingBoxContained(geom a, geom b), represented as `a @ b`, see [postgis documentation](http://postgis.net/docs/manual-2.0/ST_Geometry_Contained.html)
- boundingBoxContains(geom a, geom b), represented as `a ~ b`, see [postgis documentation](http://postgis.net/docs/manual-2.0/ST_Geometry_Contain.html)
- multi(geom), see [postgis documentation](https://postgis.net/docs/ST_Multi.html)
## Define extra functions

```js
const knex = require('knex');
const knexPostgis = require('knex-postgis');

const db = knex({
  client: 'postgres'
});

knexPostgis(db);

db.postgisDefineExtras((knex, formatter) => ({
  utmzone(geom) {
    return knex.raw('utmzone(?)', [formatter.wrapWKT(geom)]);
  }
}));
//now you can use st.utmzone function in the same way as predefined functions
```

---
_Source: https://npm.io/package/knex-postgis · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
