News for hackers. Stuff that matters.
Re:Trailbase: Fast, single-file, open-source app server buil... by uwemaurer on Friday December 06, 2024 @05:51AM (#42338529)
it looks like that the query function has a second parameter with a "params" array.

probably something like this would work then too (didn't test):

  return await query(
      `SELECT Owner, Aroma, Flavor, Acidity, Sweetness
         FROM coffee
         ORDER BY vec_distance_L2(
           embedding, '[?,?,?,?]')
         LIMIT 100`,
      [+aroma, +flavor, +acid, +sweet],
    );

it is nice because the query string is constant then and a prepared query could be cached..
Re:Trailbase: Fast, single-file, open-source app server buil... by chrismorgan on Friday December 06, 2024 @06:21AM (#42338732)
So we’re clear, the tagged template string approach gives you a string that uses parameters properly (see the example in the README of that project that does it); it’s perfectly compatible with prepared-statement caching.

And actually on second consideration, you probably can’t use binding parameters here: you’re trying to slot numbers in as JSON values inside a string! Maybe you’d have to write vec_distance_L2(embedding, ?), with parameter JSON.stringify([+aroma, +flavor, +acid, +sweet]).

Re:Trailbase: Fast, single-file, open-source app server buil... by uwemaurer on Friday December 06, 2024 @06:54AM (#42338945)
you are right, great comment!
Re:Trailbase: Fast, single-file, open-source app server buil... by trailbase-alt on Friday December 06, 2024 @07:44AM (#42339288)
:hide: thanks for pointing out this embarrassing plunder. No excuse, just feeling silly especially after being so careful throughout the rust base. It's fixed now. Please keep yelling if you find more issues
Re:Trailbase: Fast, single-file, open-source app server buil... by chrismorgan on Saturday December 07, 2024 @07:03AM (#42349200)
Well… a couple of things that could be done better in that example:

—⁂—

  const [data, setData] = useState<Array<Array<object>> | undefined>();
That type stinks. Kill the undefined bit by giving it an initial value, then you can skip the `?? []` later too; and replace Array<object> with the actual row type, probably naming it, maybe like this:

  type Record = [string, number, number, number, number];
  const [data, setData] = useState<Record[]>([]);
—⁂—

  const params = Object.entries({ aroma, flavor, acidity, sweetness })
    .map(([k, v]) => `${k}=${v}`)
    .join("&");
No need to construct the query string manually:

  const params = new URLSearchParams({ aroma, flavor, acidity, sweetness });
I confess I’m puzzled about this one too, as trailbase.js suggests you know about URLSearchParams, which is what you should use for all typical query string manipulation, parsing and generation.
Re:Trailbase: Fast, single-file, open-source app server buil... by trailbase on Saturday December 07, 2024 @06:53PM (#42353829)
Done. Thanks