knex-postgis可以与书架一起使用

时间:2016-01-18 16:54:55

标签: node.js bookshelf.js knex.js

有没有人使用knex-postgis https://github.com/jfgodoy/knex-postgis 用书架?文档展示了如何将其固定在knex上,这很棒。我在书架上使用knex,无法理解如何让它工作。

'use strict';

var knex = require('knex')({
  client: 'pg',
  connection: {
    host     : 'XXXXXXX.com',
    user     : 'XXXX',
    password : 'XXXXXX',
    database : 'XXXXX',
    searchPath: 'public'
  }
});

// var st = require('knex-postgis')(knex);
var bookshelf = require('bookshelf')(knex);
bookshelf.plugin('registry');
bookshelf.plugin('virtuals')

module.exports = bookshelf;

这就是我设置knex和书架的方法,但我不知道这是怎么回事......

var st = require('knex-postgis')(knex);

在稍后进行书架查询时会有所作为。

1 个答案:

答案 0 :(得分:2)

Yes, you can.

Create a Bookshelf model (insert into the DB) with a geo column (in this case, my Bookshelf model is a model called Event with a geo column that represents a PostGIS point):

new Event({
  geo: st.geomFromText(`Point(${lat} ${lng})`, 4326)
})
.save()
// ...

Now when you query it again, you can use the transform capabilities of the knex-postgis library to transform your points:

Event.collection().query((qb) => {
  qb.select('*', st.asGeoJSON('geo'));
}).fetch().then((collection) => {
  // collection.geo will contain a GeoJSON representation of your column.
});