简体   繁体   English

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

[英]Can knex-postgis be used with bookshelf

Has anyone used knex-postgis https://github.com/jfgodoy/knex-postgis with bookshelf? 有没有人将knex-postgis https://github.com/jfgodoy/knex-postgis与书架一起使用? The docs show how it bolts onto knex, which is great. 文档显示了它如何固定在knex上,这很棒。 I use knex through bookshelf and cannot get my head around how to get it working. 我在书架上使用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;

This is how I have knex and bookshelf set up but I am not sure how this... 这是我设置书架和书架的方式,但是我不确定如何...

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

ties in when making bookshelf queries later on. 稍后进行书架查询时,请联系。

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): 使用geo列创建一个Bookshelf模型(插入数据库)(在本例中,我的Bookshelf模型是一个名为Event的模型,其中geo列代表一个PostGIS点):

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: 现在,再次查询时,可以使用knex-postgis库的转换功能来转换点:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM