Reputation: 505
I am new to node and are building a simple API to handle geographic data. For this I am trying to implement knex-postgis
I have a connection file that I require in my queries.js like so:
const knex = require('./knex');
and use it
update(id, poi) {
return knex('poi').where('id', id).update(poi, '*');
The doc say to implement the extension like this:
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;
*/
Can someone please explain where in my structure I implement the code, this is the first time I am using an extension. Do i put it in my knex.js file?
My knex.js look like this:
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;
EDIT: I tried putting this in my queries.js file
const knex = require('./knex');
const knexPostgis = require('knex-postgis');
const st = knexPostgis(knex);
const db = knex({
dialect: 'postgres'
});
My create function:
create() {
const sql = knex.insert({
geom: st.geomFromText('Point(-71.064544 44.28787)', 4326)
}).into('poi').toString();
console.log(sql);
return sql
It console.log
a valid sql that works in pgadmin but in postman I get
"message": "queries.create(...).then is not a function"
,
And finally my route
router.post('/', (req, res, next) => {
queries.create(req.body).then(poi => {
res.json(poi[0]);
});
});
Upvotes: 1
Views: 1548
Reputation: 2392
You are returning string from your create
method, but expect a promise interface in your route handler.
And you are using knex
instead of db
for query building.
Try this
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;
Upvotes: 1