0.5.1 • Published 5 months ago

kysely-postgis v0.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

kysely-postgis

npm version npm downloads License

Extension to use postgis functions with kysely more easely.

/!\ Early development

Example

import { Database } from './types.ts'; // the Database interface
import { Pool } from 'pg';
import { Kysely, PostgresDialect } from 'kysely';
import { stf, setDefaultOptions } from 'kysely-postgis';

const dialect = new PostgresDialect({
  pool: new Pool(/* Pool configuration*/),
});
const db = new Kysely()<Database>({ dialect });

// By default, WKT and GeoJSON are validated
// Disable GeoJSON and WKT validation globally
// setDefaultOptions({
//   validate: false,
// });

// Returns a GeoJSON from geometry column
const query1 = db
  .selectFrom('table')
  .select((eb) => stf(eb).asGeoJSON('column').as('geojson'))
  .compile();
console.log(query1.sql);
//select ST_AsGeoJSON("column") as "geojson" from "table"

// Transform a GeoJSON Object to geometry
const query2 = db
  .selectFrom('table')
  .select((eb) =>
    stf(eb)
      .geomFromGeoJSON({
        type: 'Point',
        coordinates: [125.6, 10.1],
      })
      .as('geojson'),
  )
  .compile();
console.log(query2.sql, query2.parameters);
//select ST_GeomFromGeoJSON($1) as "geojson" from "table" [ '{"type":"Point","coordinates":[125.6,10.1]}' ]

// Transform a GeoJSON string to geometry
const query3 = db
  .selectFrom('table')
  .select((eb) =>
    stf(eb)
      .geomFromGeoJSON(eb.val(`{"type": "Point","coordinates": [125.6, 10.1]}`))
      .as('geojson'),
  )
  .compile();
console.log(query3.sql, query3.parameters);
//select ST_GeomFromGeoJSON($1) as "geojson" from "table" [ '{"type": "Point","coordinates": [125.6, 10.1]}' ]

// Transform a WKT string to geometry
const query4 = db
  .selectFrom('table')
  .select((eb) =>
    stf(eb)
      .geomFromText('POINT(1 2)'))
      .as('geom'),
  )
  .compile();
console.log(query4.sql, query4.parameters);
//select ST_GeomFromText($1) as "geom" from "table" [ 'POINT(1, 2)' ]

// You need to use eb.val() to pass a GeoJSON string or the string will be considerate like a column
const query5 = db
  .selectFrom('table')
  .select((eb) =>
    stf(eb)
      .area(eb.val(`{"type": "Polygon","coordinates": [
        [[100.0, 0.0],[101.0, 0.0],[101.0, 1.0],[100.0, 1.0],[100.0, 0.0]]
      ]}`))
      .as('geom'),
  )
  .compile();
console.log(query5.sql, query5.parameters);
//select ST_Area($1) as "geom" from "table" [ '{"type": "Polygon","coordinates": [[[100.0, 0.0],[101.0, 0.0],[101.0, 1.0],[100.0, 1.0][100.0, 0.0]]]}' ]

// You can pass any additional parameters to a ST function
// Be careful, theses parameters will simply be add at the end of the parameters
const query6 = db
  .selectFrom('table')
  .select((eb) =>
    stf(eb)
      .buffer('geom', 1, {
          additionalParameters: [eb.val('endcap=square join=round')],
        })
      .as('geom'),
  )
  .compile();
console.log(query6.sql, query6.parameters);
//select ST_Buffer("geom", $1, $2) as "geojson" from "etablissementIsochrone" [ 1, 'endcap=square join=round' ]

Currently supported functions

0.5.1

5 months ago

0.5.0

10 months ago

0.4.0

10 months ago

0.3.3

10 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.1

10 months ago