Modifying a query column when fetching
See original GitHub issueI’m using knex-postgis to try and convert an encoded geometry table attribute to readable text. I’m trying to modify the query on the ‘fetching’ event, but it seems the modifications are ignored.
my code:
const bookshelf = require('./.database');
const st = require('knex-postgis')(bookshelf.knex);
const Events = bookshelf.Model.extend({
initialize: function() {
this.on('fetching', this._locationToText);
},
_locationToText: function(model, columns, options) {
options.query.select('id', st.asText('location')).from(model);
console.log(options.query.toString());
}
})
This logs the expect query string to the terminal, but the fetch request still returns all attributes and the location attribute still encoded in the unreadable format. Is it possible to modify the SELECT part of the query? Am I doing something wrong?
Issue Analytics
- State:
- Created 7 years ago
- Comments:16 (1 by maintainers)
Top Results From Across the Web
SQL queries to change the column type
This article will show the way to change the data type of the columns in SQL Server 2019, MySQL Server, and PostgreSQL.
Read more >Access 2010: More Query Design Options - GCF Global
In this lesson, you'll learn how to modify and sort your queries within Query Design view, as well as how to use the...
Read more >Spring Data JPA @Modifying Annotation - Baeldung
The @Modifying annotation is used to enhance the @Query annotation so that we can execute not only SELECT queries, but also INSERT, UPDATE,...
Read more >Using Lookup and multivalued fields in queries
Add criteria to a multivalued field in a query · Open the query in Design View. · In this example, add the Issues...
Read more >Catching latest column value change in SQL - Stack Overflow
If you add another column 'changed datetime' you can fill this using an update trigger that inserts NOW(). If you query your table...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I actually just got this working in an even simpler way. Now doing:
It just checks to see if the location should be returned as defined by what’s in the
columns
parameter. If location should be returned, it just pushesst.asGeoJSON('location')
to columns. Knex takes care of modifying the query after that.Applying the initialize arguments should only be necessary if you’re using some other library that also modifies the initialize function; as you stated before.
I think the last mentioned problem can be solved with
parse
&format
. And as you do not know automatically which fields need to be parsed when fetching from and formatted when saving/updating to database, it is required to declare them in Model.For example: I’m using MySQL 5.7 which supports JSON type. But
mysql2
doesn’t support it straight forward, It means it handles it as a string. So I have to parse/stringify the field every time I perform read/write operation. For this I have made a Bookshelf plugin for myself:Then you shall declare jsonColumns on the model:
So quick plugin refactoring for PostGIS:
And use it jus by defining
GISColumns
: