bookshelf-geojson

1.0.0 • Public • Published

bookshelf-geojson

This Bookshelf.js plugin automatically parses and formats PostGIS geometry columns as GeoJSON in your models.

npm version Dependency Status Build Status Coverage Status License

Developed at the Media Engineering Institute (HEIG-VD).

Usage

const bookshelf = require('bookshelf');
const bookshelfGeojson = require('bookshelf-geojson');
const knex = require('knex');
 
const db = knex({
  client: 'postgresql',
  connection: 'postgres://localhost/bookshelf-geojson'
});
 
const Bookshelf = bookshelf(knex);
Bookshelf.plugin(bookshelfGeojson(db));
 
const Address = Bookshelf.Model.extend({
  tableName: 'addresses',
 
  // Indicate the column containing a PostGIS geometry type.
  geojson: 'location'
});
 
const geojson = {
  type: 'Point',
  coordinates: [ 24, 42 ]
};
 
// The GeoJSON is automatically serialized for PostGIS.
const address = new Address();
address.set('location', geojson);
address.save();
 
// The GeoJSON is automatically parsed with PostGIS's ST_AsGeoJSON function.
const anotherAddress = new Address({ id: 1 }).fetch();
console.log(anotherAddress.get('location'));
// { id: 1, location: { type: 'Point', coordinates: [ 33, 66 ] } }

Package Sidebar

Install

npm i bookshelf-geojson

Weekly Downloads

4

Version

1.0.0

License

MIT

Last publish

Collaborators

  • alphahydrae