@atscript/db-sqlite 0.1.182 → 0.1.183

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/index.cjs CHANGED
@@ -420,19 +420,6 @@ function buildAggregateCount(table, where, controls) {
420
420
  return (0, _atscript_db_sql_tools.buildAggregateCount)(sqliteDialect, table, where, controls);
421
421
  }
422
422
  /**
423
- * Converts an Atlas-style similarity score (1 = exact, 0 = orthogonal) to a
424
- * vec0 distance threshold.
425
- *
426
- * - cosine / dotProduct (mapped to cosine in DDL): vec0 distance = 1 - cos_sim,
427
- * normalized score = (1 + cos_sim) / 2, so distance = 2 * (1 - score).
428
- * - euclidean (l2): vec0 distance is unbounded; the value is treated as a max
429
- * distance directly (same convention as pgvector).
430
- */
431
- function thresholdToVecDistance(threshold, similarity) {
432
- if (similarity === "euclidean") return threshold;
433
- return 2 * (1 - threshold);
434
- }
435
- /**
436
423
  * Maps an Atscript `@db.search.vector` similarity value to a sqlite-vec `vec0` metric.
437
424
  * `dotProduct` falls back to `cosine` because vec0 has no dedicated dot-product metric.
438
425
  */
@@ -2190,7 +2177,7 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
2190
2177
  _runVectorSearch(base) {
2191
2178
  const { sql, params } = (0, _atscript_db_sql_tools.buildVectorSearchSelect)(sqliteDialect, base.source, base);
2192
2179
  this._log(sql, params);
2193
- return this.driver.all(sql, params);
2180
+ return this.driver.all(sql, params).map((row) => (0, _atscript_db_sql_tools.addVectorScore)(row, base.metric));
2194
2181
  }
2195
2182
  /** Resolves a vector index (by name or first available) and its partition fields. */
2196
2183
  _resolveVectorIndex(indexName) {
@@ -2272,6 +2259,7 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
2272
2259
  innerWhereParts.push(`v."${esc(p.name)}" = ?`);
2273
2260
  params.push(p.value);
2274
2261
  }
2262
+ const metric = similarityToVecMetric(vec.similarity) === "l2" ? "euclidean" : "cosine";
2275
2263
  return {
2276
2264
  source: {
2277
2265
  sql: `SELECT t.*, v.distance AS _distance FROM "${esc(vecTable)}" v JOIN "${esc(tableName)}" t ON t.rowid = v.rowid WHERE ${innerWhereParts.join(" AND ")}`,
@@ -2279,9 +2267,10 @@ var SqliteAdapter = class SqliteAdapter extends _atscript_db.BaseDbAdapter {
2279
2267
  },
2280
2268
  limit,
2281
2269
  skip,
2282
- maxDistance: hasThreshold ? thresholdToVecDistance(threshold, vec.similarity) : void 0,
2270
+ maxDistance: hasThreshold ? (0, _atscript_db_sql_tools.vectorMaxDistance)(metric, threshold) : void 0,
2283
2271
  residual,
2284
- select: query.controls?.$select
2272
+ select: query.controls?.$select,
2273
+ metric
2285
2274
  };
2286
2275
  }
2287
2276
  /**
package/dist/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
  import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_ISOLATION_LEVELS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DB_FULLTEXT_LANGUAGES, DbError, DbSpace, constraintViolation, createChangeRecorder, defaultFulltextIndex, describeFulltext, fkColumns, searchIndexNotFoundMessage, searchTermInteger, splitFulltextFields, vectorIndexNotFoundMessage } from "@atscript/db";
3
3
  import { resolveAggregateSearch } from "@atscript/db/agg";
4
- import { EMPTY_AND, InsertSqlCache, SEARCH_SOURCE_ALIAS, buildAggregateCount, buildAggregateSelect, buildCreateView, buildDelete, buildGeoSearchCount, buildGeoSearchSelect, buildJsonColumnCopy, buildJsonifyText, buildKeyViolationCount, buildPartitionedSelect, buildSelect, buildUpdate, buildVectorSearchCount, buildVectorSearchSelect, buildWhere as buildWhere$1, defaultValueForType, defaultValueToSqlLiteral, derivedColumnExpr, fillReplacePayload, fromSourceSql, geoWindowFromControls, jsonDollarPath, orFragment, orderByList, parseRegexString, refActionToSql, renameGeoDistance, replaceColumnsFor, searchRankOrder, sqlStringLiteral, sqlTimeZoneLiteral, stripPartitionRowNumber, toSqlValue, unquoteSqlStringLiteral, viewReadSource, writeLimitWhere } from "@atscript/db-sql-tools";
4
+ import { EMPTY_AND, InsertSqlCache, SEARCH_SOURCE_ALIAS, addVectorScore, buildAggregateCount, buildAggregateSelect, buildCreateView, buildDelete, buildGeoSearchCount, buildGeoSearchSelect, buildJsonColumnCopy, buildJsonifyText, buildKeyViolationCount, buildPartitionedSelect, buildSelect, buildUpdate, buildVectorSearchCount, buildVectorSearchSelect, buildWhere as buildWhere$1, defaultValueForType, defaultValueToSqlLiteral, derivedColumnExpr, fillReplacePayload, fromSourceSql, geoWindowFromControls, jsonDollarPath, orFragment, orderByList, parseRegexString, refActionToSql, renameGeoDistance, replaceColumnsFor, searchRankOrder, sqlStringLiteral, sqlTimeZoneLiteral, stripPartitionRowNumber, toSqlValue, unquoteSqlStringLiteral, vectorMaxDistance, viewReadSource, writeLimitWhere } from "@atscript/db-sql-tools";
5
5
  import { bucketer } from "@uniqu/core";
6
6
  import { AsyncLocalStorage } from "node:async_hooks";
7
7
  //#region src/better-sqlite3-driver.ts
@@ -419,19 +419,6 @@ function buildAggregateCount$1(table, where, controls) {
419
419
  return buildAggregateCount(sqliteDialect, table, where, controls);
420
420
  }
421
421
  /**
422
- * Converts an Atlas-style similarity score (1 = exact, 0 = orthogonal) to a
423
- * vec0 distance threshold.
424
- *
425
- * - cosine / dotProduct (mapped to cosine in DDL): vec0 distance = 1 - cos_sim,
426
- * normalized score = (1 + cos_sim) / 2, so distance = 2 * (1 - score).
427
- * - euclidean (l2): vec0 distance is unbounded; the value is treated as a max
428
- * distance directly (same convention as pgvector).
429
- */
430
- function thresholdToVecDistance(threshold, similarity) {
431
- if (similarity === "euclidean") return threshold;
432
- return 2 * (1 - threshold);
433
- }
434
- /**
435
422
  * Maps an Atscript `@db.search.vector` similarity value to a sqlite-vec `vec0` metric.
436
423
  * `dotProduct` falls back to `cosine` because vec0 has no dedicated dot-product metric.
437
424
  */
@@ -2189,7 +2176,7 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
2189
2176
  _runVectorSearch(base) {
2190
2177
  const { sql, params } = buildVectorSearchSelect(sqliteDialect, base.source, base);
2191
2178
  this._log(sql, params);
2192
- return this.driver.all(sql, params);
2179
+ return this.driver.all(sql, params).map((row) => addVectorScore(row, base.metric));
2193
2180
  }
2194
2181
  /** Resolves a vector index (by name or first available) and its partition fields. */
2195
2182
  _resolveVectorIndex(indexName) {
@@ -2271,6 +2258,7 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
2271
2258
  innerWhereParts.push(`v."${esc(p.name)}" = ?`);
2272
2259
  params.push(p.value);
2273
2260
  }
2261
+ const metric = similarityToVecMetric(vec.similarity) === "l2" ? "euclidean" : "cosine";
2274
2262
  return {
2275
2263
  source: {
2276
2264
  sql: `SELECT t.*, v.distance AS _distance FROM "${esc(vecTable)}" v JOIN "${esc(tableName)}" t ON t.rowid = v.rowid WHERE ${innerWhereParts.join(" AND ")}`,
@@ -2278,9 +2266,10 @@ var SqliteAdapter = class SqliteAdapter extends BaseDbAdapter {
2278
2266
  },
2279
2267
  limit,
2280
2268
  skip,
2281
- maxDistance: hasThreshold ? thresholdToVecDistance(threshold, vec.similarity) : void 0,
2269
+ maxDistance: hasThreshold ? vectorMaxDistance(metric, threshold) : void 0,
2282
2270
  residual,
2283
- select: query.controls?.$select
2271
+ select: query.controls?.$select,
2272
+ metric
2284
2273
  };
2285
2274
  }
2286
2275
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-sqlite",
3
- "version": "0.1.182",
3
+ "version": "0.1.183",
4
4
  "description": "SQLite adapter for @atscript/db with swappable driver support.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -36,7 +36,7 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "@atscript/db-sql-tools": "^0.1.182"
39
+ "@atscript/db-sql-tools": "^0.1.183"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@atscript/core": "^0.2.13",
@@ -52,7 +52,7 @@
52
52
  "@atscript/typescript": "^0.2.13",
53
53
  "@uniqu/core": "^0.2.2",
54
54
  "better-sqlite3": ">=11.0.0",
55
- "@atscript/db": "^0.1.182"
55
+ "@atscript/db": "^0.1.183"
56
56
  },
57
57
  "peerDependenciesMeta": {
58
58
  "better-sqlite3": {