如何为 knex 设置 postgis 扩展?
How to setup postgis extension for knex?
我是节点的新手,正在构建一个简单的 API 来处理地理数据。
为此,我正在尝试实施 knex-postgis
我的 queries.js 中有一个需要的连接文件,如下所示:
const knex = require('./knex');
并使用它
update(id, poi) {
return knex('poi').where('id', id).update(poi, '*');
文档说要像这样实现扩展:
const knex = require('knex');
const knexPostgis = require('knex-postgis');
const db = knex({
dialect: 'postgres'
});
// install postgis functions in knex.postgis;
const st = knexPostgis(db);
/* or:
* knexPostgis(db);
* const st = db.postgis;
*/
谁能解释一下我在我的结构中的什么地方实现了代码,这是我第一次使用扩展。我要把它放在我的 knex.js 文件中吗?
我的 knex.js 看起来像这样:
const environment = process.env.NODE_ENV || 'development';
const config = require('../knexfile');
const environmentConfig = config[environment];
const knex = require('knex');
const connection = knex(environmentConfig);
module.exports = connection;
编辑:
我尝试将其放入我的 queries.js 文件
const knex = require('./knex');
const knexPostgis = require('knex-postgis');
const st = knexPostgis(knex);
const db = knex({
dialect: 'postgres'
});
我的创建函数:
create() {
const sql = knex.insert({
geom: st.geomFromText('Point(-71.064544 44.28787)', 4326)
}).into('poi').toString();
console.log(sql);
return sql
它 console.log
一个有效的 sql 在 pgadmin 中有效,但在邮递员中我得到
"message": "queries.create(...).then is not a function"
,
最后是我的路线
router.post('/', (req, res, next) => {
queries.create(req.body).then(poi => {
res.json(poi[0]);
});
});
您正在呼叫 knex.insert
。你应该打电话给 db.insert
.
您正在从 create
方法返回字符串,但期望路由处理程序中有一个 promise 接口。
您正在使用 knex
而不是 db
进行查询构建。
试试这个
const builder = db.insert(/*same as above*/).into('tablename');
const sql = builder.toString();
console.log(sql);
// Return builder instance itself (promise) instead of string
return builder;
我是节点的新手,正在构建一个简单的 API 来处理地理数据。 为此,我正在尝试实施 knex-postgis
我的 queries.js 中有一个需要的连接文件,如下所示:
const knex = require('./knex');
并使用它
update(id, poi) {
return knex('poi').where('id', id).update(poi, '*');
文档说要像这样实现扩展:
const knex = require('knex');
const knexPostgis = require('knex-postgis');
const db = knex({
dialect: 'postgres'
});
// install postgis functions in knex.postgis;
const st = knexPostgis(db);
/* or:
* knexPostgis(db);
* const st = db.postgis;
*/
谁能解释一下我在我的结构中的什么地方实现了代码,这是我第一次使用扩展。我要把它放在我的 knex.js 文件中吗?
我的 knex.js 看起来像这样:
const environment = process.env.NODE_ENV || 'development';
const config = require('../knexfile');
const environmentConfig = config[environment];
const knex = require('knex');
const connection = knex(environmentConfig);
module.exports = connection;
编辑: 我尝试将其放入我的 queries.js 文件
const knex = require('./knex');
const knexPostgis = require('knex-postgis');
const st = knexPostgis(knex);
const db = knex({
dialect: 'postgres'
});
我的创建函数:
create() {
const sql = knex.insert({
geom: st.geomFromText('Point(-71.064544 44.28787)', 4326)
}).into('poi').toString();
console.log(sql);
return sql
它 console.log
一个有效的 sql 在 pgadmin 中有效,但在邮递员中我得到
"message": "queries.create(...).then is not a function"
,
最后是我的路线
router.post('/', (req, res, next) => {
queries.create(req.body).then(poi => {
res.json(poi[0]);
});
});
您正在呼叫 knex.insert
。你应该打电话给 db.insert
.
您正在从 create
方法返回字符串,但期望路由处理程序中有一个 promise 接口。
您正在使用 knex
而不是 db
进行查询构建。
试试这个
const builder = db.insert(/*same as above*/).into('tablename');
const sql = builder.toString();
console.log(sql);
// Return builder instance itself (promise) instead of string
return builder;