Ff00ff/mammoth

Mixing `raw` with `Expression` values.

Opened this issue · 0 comments

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',
    );
};