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

Mixing raw with Expression values. #422

Open
cakoose opened this issue Jan 20, 2022 · 0 comments
Open

Mixing raw with Expression values. #422

cakoose opened this issue Jan 20, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@cakoose
Copy link
Collaborator

cakoose commented Jan 20, 2022

Most of the times I want to use raw, I plug in a Mammoth Expression, e.g.

raw`ST_Intersects(ST_GeogFromText(POINT(${latitude} ${longitude})), db.regions.shape)`;

(latitude and longitude are JS number values.)

Is there a way to do that with something convenient like raw?

Right now I'm having to dig into private attributes to accomplish this:

export const postgisIntersects = (
    {latitude, longitude}: {latitude: number; longitude: number},
    expression: Expression<string, true, string>,
): Expression<boolean, true, 'st_intersects'> => {
    const pointText = `POINT(${longitude}  ${latitude})`; // In Postgres, longitude comes first
    // Have to do `expression as any` because Mammoth doesn't expose `.tokens`.
    return new Expression(
        [new StringToken(`ST_Intersects`),
            new GroupToken([new SeparatorToken(',', [
                new GroupToken([
                    new StringToken(`ST_GeogFromText`),
                        new GroupToken([new ParameterToken(pointText)])]),
                new GroupToken((expression as any).tokens)])])],
        'st_intersects',
    );
};
@cakoose cakoose added the enhancement New feature or request label Jan 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant