@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.
package/dist/db/index.js CHANGED
@@ -22,14 +22,14 @@ import {
22
22
  userRef,
23
23
  uuid,
24
24
  vector
25
- } from "../chunk-7JSEN7UR.js";
25
+ } from "../chunk-G4R6BTLV.js";
26
26
  import {
27
27
  TxPlanError,
28
28
  TxRefError,
29
29
  dec,
30
30
  inc,
31
31
  now
32
- } from "../chunk-P2Q27SGP.js";
32
+ } from "../chunk-CJSKYY76.js";
33
33
  import "../chunk-7D4SUZUM.js";
34
34
  export {
35
35
  EXTENSION_DEPENDENCIES,
@@ -526,6 +526,20 @@ function makeTableProxy(ops, prefix) {
526
526
  findById: (id) => ops().findById(name, id),
527
527
  findMany: (query, opts) => ops().findMany(name, query, opts),
528
528
  upsert: (data, opts) => ops().upsert(name, data, opts),
529
+ // THREE VERBS THE TYPE PROMISED AND THIS PROXY DID NOT EMIT.
530
+ //
531
+ // `EnvTypedTableBase` declares `updateMany`, `deleteMany` and `count`
532
+ // (typed-db.ts) and the ops layer implements all three — only this
533
+ // proxy, which is what a handler actually touches, left them out. So
534
+ // the type said the verb exists, autocomplete offered it, and the call
535
+ // answered `undefined is not a function`.
536
+ //
537
+ // Older than this run, but the run rewrote this proxy for
538
+ // `Database.schema(name).tables.*` and would have carried the gap onto
539
+ // the new surface too.
540
+ updateMany: (where, set) => ops().updateMany(name, where, set),
541
+ deleteMany: (where) => ops().deleteMany(name, where),
542
+ count: (where) => ops().count(name, where),
529
543
  search: (params) => ops().search(name, params),
530
544
  similar: (id, params) => ops().similar(name, id, params),
531
545
  recommend: (params) => ops().recommend(name, params),
@@ -1127,6 +1141,11 @@ function assertUsableWriteValues(caller, table, cols, data) {
1127
1141
  }
1128
1142
  }
1129
1143
 
1144
+ // src/db/schema-json.ts
1145
+ function qualifiedTableKey(schemaName, tableName) {
1146
+ return schemaName === "" || schemaName === "public" ? tableName : `${schemaName}.${tableName}`;
1147
+ }
1148
+
1130
1149
  // src/engine/db.ts
1131
1150
  function quoteIdent(name) {
1132
1151
  return `"${name.replace(/"/g, '""')}"`;
@@ -1892,7 +1911,7 @@ function createOps(tx) {
1892
1911
  let exactOrderC = "";
1893
1912
  if (userWhereC !== "") {
1894
1913
  const probeRows = await liveC.unsafe(
1895
- `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`,
1914
+ `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`,
1896
1915
  bindC.slice()
1897
1916
  );
1898
1917
  const n = probeRows?.[0]?.n;
@@ -1901,7 +1920,7 @@ function createOps(tx) {
1901
1920
  }
1902
1921
  }
1903
1922
  const vp = addC(toVectorLiteral(qvC));
1904
- 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}`;
1923
+ 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}`;
1905
1924
  }
1906
1925
  let qpC = "";
1907
1926
  let qpCIdx = -1;
@@ -1909,7 +1928,7 @@ function createOps(tx) {
1909
1928
  const qTextC = cfg.synonyms !== void 0 ? expandSynonyms(params.query, cfg.synonyms) : params.query;
1910
1929
  qpC = addC(qTextC);
1911
1930
  qpCIdx = bindC.length - 1;
1912
- 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}`;
1931
+ 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}`;
1913
1932
  }
1914
1933
  const msC = params.minScore === void 0 ? "" : addC(params.minScore);
1915
1934
  const colListC = cfg.cols.map((c) => `t.${quoteIdent(c)}`).join(", ");
@@ -1924,13 +1943,13 @@ function createOps(tx) {
1924
1943
  } else {
1925
1944
  fusedSrc = `WITH kw AS (${kwIn}), fused AS (SELECT kw.pid, kw.chunk_seq, (1.0/(${K2} + kw.r))::float8 AS cscore FROM kw)`;
1926
1945
  }
1927
- 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`;
1946
+ 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`;
1928
1947
  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}`;
1929
1948
  };
1930
1949
  let rowsC = await liveC.unsafe(assembleC(kwC, kwC !== ""), bindC);
1931
1950
  if (wantTextC && kwC !== "" && (rowsC.length === 0 || rowsC[0]._kwn === 0)) {
1932
1951
  const tsch = await trgmSchemaOf(liveC);
1933
- 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}`;
1952
+ 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}`;
1934
1953
  const retryBindC = bindC.slice();
1935
1954
  if (qpCIdx >= 0) retryBindC[qpCIdx] = params.query;
1936
1955
  rowsC = await liveC.unsafe(assembleC(trgmKwC, false), retryBindC);
@@ -2078,7 +2097,7 @@ function createOps(tx) {
2078
2097
  if (cfg.chunk !== void 0) {
2079
2098
  const sch = await vectorSchemaWithGuc(live);
2080
2099
  const rows = await live.unsafe(
2081
- `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`,
2100
+ `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`,
2082
2101
  [id]
2083
2102
  );
2084
2103
  v = rows?.[0]?.v;
@@ -2145,7 +2164,7 @@ function createOps(tx) {
2145
2164
  };
2146
2165
  const avgSel = (ids) => {
2147
2166
  const inList = ids.map((x) => add(x)).join(", ");
2148
- 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`;
2167
+ 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`;
2149
2168
  };
2150
2169
  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`;
2151
2170
  const rows = await live.unsafe(targetSql, bind);
@@ -2289,9 +2308,6 @@ function setSchema(schemas) {
2289
2308
  }
2290
2309
  currentSchema = { tables };
2291
2310
  }
2292
- function qualifiedTableKey(schema, table) {
2293
- return schema === "" || schema === "public" ? table : `${schema}.${table}`;
2294
- }
2295
2311
  function withTables(ops, schema = currentSchema) {
2296
2312
  const bind = (key) => ({
2297
2313
  insert: (data) => ops.insert(key, data),