@palbase/backend 25.0.2 → 25.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -492,6 +492,20 @@ function makeTableProxy(ops, prefix) {
492
492
  findById: (id) => ops().findById(name, id),
493
493
  findMany: (query, opts) => ops().findMany(name, query, opts),
494
494
  upsert: (data, opts) => ops().upsert(name, data, opts),
495
+ // THREE VERBS THE TYPE PROMISED AND THIS PROXY DID NOT EMIT.
496
+ //
497
+ // `EnvTypedTableBase` declares `updateMany`, `deleteMany` and `count`
498
+ // (typed-db.ts) and the ops layer implements all three — only this
499
+ // proxy, which is what a handler actually touches, left them out. So
500
+ // the type said the verb exists, autocomplete offered it, and the call
501
+ // answered `undefined is not a function`.
502
+ //
503
+ // Older than this run, but the run rewrote this proxy for
504
+ // `Database.schema(name).tables.*` and would have carried the gap onto
505
+ // the new surface too.
506
+ updateMany: (where, set) => ops().updateMany(name, where, set),
507
+ deleteMany: (where) => ops().deleteMany(name, where),
508
+ count: (where) => ops().count(name, where),
495
509
  search: (params) => ops().search(name, params),
496
510
  similar: (id, params) => ops().similar(name, id, params),
497
511
  recommend: (params) => ops().recommend(name, params),
@@ -1093,6 +1107,11 @@ function assertUsableWriteValues(caller, table, cols, data) {
1093
1107
  }
1094
1108
  }
1095
1109
 
1110
+ // src/db/schema-json.ts
1111
+ function qualifiedTableKey(schemaName, tableName) {
1112
+ return schemaName === "" || schemaName === "public" ? tableName : `${schemaName}.${tableName}`;
1113
+ }
1114
+
1096
1115
  // src/engine/db.ts
1097
1116
  function quoteIdent(name) {
1098
1117
  return `"${name.replace(/"/g, '""')}"`;
@@ -1858,7 +1877,7 @@ function createOps(tx) {
1858
1877
  let exactOrderC = "";
1859
1878
  if (userWhereC !== "") {
1860
1879
  const probeRows = await liveC.unsafe(
1861
- `SELECT count(*)::int AS n FROM (SELECT 1 FROM ${quoteIdent(ck.table)} c${parentJoinC}c.embedding IS NOT NULL LIMIT ${SELECTIVITY_EXACT_THRESHOLD + 1}) s`,
1880
+ `SELECT count(*)::int AS n FROM (SELECT 1 FROM ${quoteTable(ck.table)} c${parentJoinC}c.embedding IS NOT NULL LIMIT ${SELECTIVITY_EXACT_THRESHOLD + 1}) s`,
1862
1881
  bindC.slice()
1863
1882
  );
1864
1883
  const n = probeRows?.[0]?.n;
@@ -1867,7 +1886,7 @@ function createOps(tx) {
1867
1886
  }
1868
1887
  }
1869
1888
  const vp = addC(toVectorLiteral(qvC));
1870
- semC = `SELECT c.${pidQ} AS pid, c.chunk_seq, ROW_NUMBER() OVER (ORDER BY (c.embedding OPERATOR(${quoteIdent(sch)}.${op}) ${vp}::${quoteIdent(sch)}.vector)${exactOrderC}) AS r FROM ${quoteIdent(ck.table)} c${parentJoinC}c.embedding IS NOT NULL ORDER BY r LIMIT ${cpool}`;
1889
+ semC = `SELECT c.${pidQ} AS pid, c.chunk_seq, ROW_NUMBER() OVER (ORDER BY (c.embedding OPERATOR(${quoteIdent(sch)}.${op}) ${vp}::${quoteIdent(sch)}.vector)${exactOrderC}) AS r FROM ${quoteTable(ck.table)} c${parentJoinC}c.embedding IS NOT NULL ORDER BY r LIMIT ${cpool}`;
1871
1890
  }
1872
1891
  let qpC = "";
1873
1892
  let qpCIdx = -1;
@@ -1875,7 +1894,7 @@ function createOps(tx) {
1875
1894
  const qTextC = cfg.synonyms !== void 0 ? expandSynonyms(params.query, cfg.synonyms) : params.query;
1876
1895
  qpC = addC(qTextC);
1877
1896
  qpCIdx = bindC.length - 1;
1878
- kwC = `SELECT c.${pidQ} AS pid, c.chunk_seq, ROW_NUMBER() OVER (ORDER BY ts_rank_cd(c.palbase_fts, websearch_to_tsquery('simple', ${qpC})) DESC) AS r FROM ${quoteIdent(ck.table)} c${parentJoinC}c.palbase_fts @@ websearch_to_tsquery('simple', ${qpC}) ORDER BY r LIMIT ${cpool}`;
1897
+ kwC = `SELECT c.${pidQ} AS pid, c.chunk_seq, ROW_NUMBER() OVER (ORDER BY ts_rank_cd(c.palbase_fts, websearch_to_tsquery('simple', ${qpC})) DESC) AS r FROM ${quoteTable(ck.table)} c${parentJoinC}c.palbase_fts @@ websearch_to_tsquery('simple', ${qpC}) ORDER BY r LIMIT ${cpool}`;
1879
1898
  }
1880
1899
  const msC = params.minScore === void 0 ? "" : addC(params.minScore);
1881
1900
  const colListC = cfg.cols.map((c) => `t.${quoteIdent(c)}`).join(", ");
@@ -1890,13 +1909,13 @@ function createOps(tx) {
1890
1909
  } else {
1891
1910
  fusedSrc = `WITH kw AS (${kwIn}), fused AS (SELECT kw.pid, kw.chunk_seq, (1.0/(${K2} + kw.r))::float8 AS cscore FROM kw)`;
1892
1911
  }
1893
- const inner = `${fusedSrc}, ranked AS (SELECT f.pid, f.chunk_seq, f.cscore, ROW_NUMBER() OVER (PARTITION BY f.pid ORDER BY f.cscore DESC, f.chunk_seq) AS rn FROM fused f), grouped AS (SELECT r.pid, MAX(r.cscore) AS _score, json_agg(json_build_object('content', c.content, 'score', r.cscore, 'chunkSeq', r.chunk_seq, 'charStart', c.char_start) ORDER BY r.cscore DESC, r.chunk_seq) FILTER (WHERE r.rn <= ${bpr}) AS blocks FROM ranked r JOIN ${quoteIdent(ck.table)} c ON c.${pidQ} = r.pid AND c.chunk_seq = r.chunk_seq GROUP BY r.pid) SELECT ${colListC}, ${scoreC}::float8 AS _score, g.blocks AS blocks${kwnCol} FROM grouped g JOIN ${quoteTable(table)} t ON t.${quoteIdent(cfg.pk)} = g.pid`;
1912
+ const inner = `${fusedSrc}, ranked AS (SELECT f.pid, f.chunk_seq, f.cscore, ROW_NUMBER() OVER (PARTITION BY f.pid ORDER BY f.cscore DESC, f.chunk_seq) AS rn FROM fused f), grouped AS (SELECT r.pid, MAX(r.cscore) AS _score, json_agg(json_build_object('content', c.content, 'score', r.cscore, 'chunkSeq', r.chunk_seq, 'charStart', c.char_start) ORDER BY r.cscore DESC, r.chunk_seq) FILTER (WHERE r.rn <= ${bpr}) AS blocks FROM ranked r JOIN ${quoteTable(ck.table)} c ON c.${pidQ} = r.pid AND c.chunk_seq = r.chunk_seq GROUP BY r.pid) SELECT ${colListC}, ${scoreC}::float8 AS _score, g.blocks AS blocks${kwnCol} FROM grouped g JOIN ${quoteTable(table)} t ON t.${quoteIdent(cfg.pk)} = g.pid`;
1894
1913
  return msC === "" ? `SELECT * FROM (${inner}) q ORDER BY q._score DESC, q.${quoteIdent(cfg.pk)} LIMIT ${limit}` : `SELECT * FROM (${inner}) q WHERE q._score >= ${msC} ORDER BY q._score DESC, q.${quoteIdent(cfg.pk)} LIMIT ${limit}`;
1895
1914
  };
1896
1915
  let rowsC = await liveC.unsafe(assembleC(kwC, kwC !== ""), bindC);
1897
1916
  if (wantTextC && kwC !== "" && (rowsC.length === 0 || rowsC[0]._kwn === 0)) {
1898
1917
  const tsch = await trgmSchemaOf(liveC);
1899
- const trgmKwC = `SELECT c.${pidQ} AS pid, c.chunk_seq, ROW_NUMBER() OVER (ORDER BY ${quoteIdent(tsch)}.word_similarity(${qpC}, coalesce(c.content,'')) DESC) AS r FROM ${quoteIdent(ck.table)} c${parentJoinC}${quoteIdent(tsch)}.word_similarity(${qpC}, coalesce(c.content,'')) > 0.3 ORDER BY r LIMIT ${cpool}`;
1918
+ const trgmKwC = `SELECT c.${pidQ} AS pid, c.chunk_seq, ROW_NUMBER() OVER (ORDER BY ${quoteIdent(tsch)}.word_similarity(${qpC}, coalesce(c.content,'')) DESC) AS r FROM ${quoteTable(ck.table)} c${parentJoinC}${quoteIdent(tsch)}.word_similarity(${qpC}, coalesce(c.content,'')) > 0.3 ORDER BY r LIMIT ${cpool}`;
1900
1919
  const retryBindC = bindC.slice();
1901
1920
  if (qpCIdx >= 0) retryBindC[qpCIdx] = params.query;
1902
1921
  rowsC = await liveC.unsafe(assembleC(trgmKwC, false), retryBindC);
@@ -2044,7 +2063,7 @@ function createOps(tx) {
2044
2063
  if (cfg.chunk !== void 0) {
2045
2064
  const sch = await vectorSchemaWithGuc(live);
2046
2065
  const rows = await live.unsafe(
2047
- `SELECT ${quoteIdent(sch)}.avg(c.embedding) AS v FROM ${quoteIdent(cfg.chunk.table)} c WHERE c.${quoteIdent(`parent_${cfg.pk}`)} = $1 AND c.embedding IS NOT NULL`,
2066
+ `SELECT ${quoteIdent(sch)}.avg(c.embedding) AS v FROM ${quoteTable(cfg.chunk.table)} c WHERE c.${quoteIdent(`parent_${cfg.pk}`)} = $1 AND c.embedding IS NOT NULL`,
2048
2067
  [id]
2049
2068
  );
2050
2069
  v = rows?.[0]?.v;
@@ -2111,7 +2130,7 @@ function createOps(tx) {
2111
2130
  };
2112
2131
  const avgSel = (ids) => {
2113
2132
  const inList = ids.map((x) => add(x)).join(", ");
2114
- return cfg.chunk !== void 0 ? `SELECT ${quoteIdent(sch)}.avg(c.embedding) AS v FROM ${quoteIdent(cfg.chunk.table)} c WHERE c.${quoteIdent(`parent_${cfg.pk}`)} IN (${inList}) AND c.embedding IS NOT NULL` : `SELECT ${quoteIdent(sch)}.avg(t.${quoteIdent(leg.column)}) AS v FROM ${quoteTable(table)} t WHERE t.${quoteIdent(cfg.pk)} IN (${inList}) AND t.${quoteIdent(leg.column)} IS NOT NULL`;
2133
+ return cfg.chunk !== void 0 ? `SELECT ${quoteIdent(sch)}.avg(c.embedding) AS v FROM ${quoteTable(cfg.chunk.table)} c WHERE c.${quoteIdent(`parent_${cfg.pk}`)} IN (${inList}) AND c.embedding IS NOT NULL` : `SELECT ${quoteIdent(sch)}.avg(t.${quoteIdent(leg.column)}) AS v FROM ${quoteTable(table)} t WHERE t.${quoteIdent(cfg.pk)} IN (${inList}) AND t.${quoteIdent(leg.column)} IS NOT NULL`;
2115
2134
  };
2116
2135
  const targetSql = negative.length === 0 ? `WITH pos AS (${avgSel(positive)}) SELECT pos.v AS v, (pos.v IS NULL) AS pos_missing FROM pos` : `WITH pos AS (${avgSel(positive)}), neg AS (${avgSel(negative)}) SELECT (pos.v OPERATOR(${quoteIdent(sch)}.+) (pos.v OPERATOR(${quoteIdent(sch)}.-) neg.v)) AS v, (pos.v IS NULL) AS pos_missing, (neg.v IS NULL) AS neg_missing FROM pos, neg`;
2117
2136
  const rows = await live.unsafe(targetSql, bind);
@@ -2255,9 +2274,6 @@ function setSchema(schemas) {
2255
2274
  }
2256
2275
  currentSchema = { tables };
2257
2276
  }
2258
- function qualifiedTableKey(schema, table) {
2259
- return schema === "" || schema === "public" ? table : `${schema}.${table}`;
2260
- }
2261
2277
  function withTables(ops, schema = currentSchema) {
2262
2278
  const bind = (key) => ({
2263
2279
  insert: (data) => ops.insert(key, data),