Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ST_Union #39

Open
zackteo opened this issue Apr 5, 2020 · 2 comments
Open

ST_Union #39

zackteo opened this issue Apr 5, 2020 · 2 comments

Comments

@zackteo
Copy link

zackteo commented Apr 5, 2020

I saw the reply to #38 .

Not really sure how define extra function works though.

Also, was looking through the library to see if it would be difficult to add new functions. Perhaps not now (as I'm rushing my project) but think I could help add more functions in the future!

@jfgodoy
Copy link
Owner

jfgodoy commented Apr 7, 2020

hi @zackteo,
yeah it's true, adding new functions using postgisDefineExtras is a bit tricky. It requires understanding how the current supported functions are implemented.

All the functions definitions are in https://github.com/jfgodoy/knex-postgis/blob/master/lib/functions.js. The functions must return a knex.raw instance, but most of them use a util function called postgisFn to do that.

For example, if you want to implement st_Union and you see its documentation, one possible form is:

geometry ST_Union(geometry g1, geometry g2);

to implement that you can do the following,

   union: function(g1, g2) {
      return postgisFn('union', wrapWKT(g1), wrapWKT(g2));
    },

which is the same as

   union: function(g1, g2) {
      return knex.raw('union(?, ?)',  formatter.wrapWKT(g1), formatter.wrapWKT(g2));
    },

knowing this, you can add it with postgisDefineExtras as

st.postgisDefineExtras((knex, formatter) => ({
   union: function(g1, g2) {
      return knex.raw('union(?, ?)',  formatter.wrapWKT(g1), formatter.wrapWKT(g2));
    },
}));

the formatter.wrapWKT function allows to pass the argument as the result of another function, as a WKT string or a column name.

A complete implementation of st_union requires to manage the form when it's called with a single argument.

¿Do you want to try implementing st_union on your own?

@zackteo
Copy link
Author

zackteo commented Apr 7, 2020

hi @jfgodoy

Thanks for your response! And for the clear examples!

And yes, I think I want to try implementing st_union on my own 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants