@protobase/query 0.108.0 → 0.109.0

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.
@@ -5,15 +5,16 @@ import { CheckedFilter, FieldModel, ResourceModel, SortSpec } from "@protobase/s
5
5
  /**
6
6
  * The kind of index a column needs for how it is read: `btree` for comparisons, lists of values, null checks and sorts;
7
7
  * `trigram` (pg_trgm, GIN or GiST) for patterns, regex, `similar` and text search; `digits` a trigram index on the
8
- * column's digits (`regexp_replace(column, '[^0-9]', '', 'g')`) for a `digitsEnd` search field; `gin` for JSON.
8
+ * column's digits (`regexp_replace(column, '[^0-9]', '', 'g')`) for a `digitsEnd` search field; `gin` for JSON; `file`
9
+ * a btree index on a file column's file without its query (`split_part(column, '?', 1)`).
9
10
  */
10
- export type IndexNeed = 'btree' | 'trigram' | 'digits' | 'gin';
11
+ export type IndexNeed = 'btree' | 'trigram' | 'digits' | 'gin' | 'file';
11
12
  /**
12
13
  * Where a column is read: the caller's filter, the access rule's row filter, the relation to a parent the caller must
13
- * read (`.parent(fk, 'read')`), a `related()` term, the sort, a facet's grouping, or the field a read keeps the first
14
- * rows per value of (`limit_per`).
14
+ * read (`.parent(fk, 'read')`), a `related()` term, the sort, a facet's grouping, the field a read keeps the first rows
15
+ * per value of (`limit_per`), or the file cleanup's check that no row holds a file it is about to delete.
15
16
  */
16
- export type ColumnUse = 'filter' | 'access' | 'parent' | 'related' | 'sort' | 'facet' | 'per';
17
+ export type ColumnUse = 'filter' | 'access' | 'parent' | 'related' | 'sort' | 'facet' | 'per' | 'cleanup';
17
18
  /** A column read without an index that serves it, and a statement that adds one. */
18
19
  export type UncoveredColumn = {
19
20
  field: string;
@@ -35,6 +36,12 @@ export type CoverageQuery = {
35
36
  };
36
37
  /** Whether the index serves reading the column with the given need, while the query pins the `pinned` columns. */
37
38
  export declare const indexServes: (index: TableIndex, column: string, need: IndexNeed, pinned: ReadonlySet<string>) => boolean;
39
+ /**
40
+ * The file columns of `model` no index looks their files up by, as the file cleanup does before it deletes one: every
41
+ * row, soft-deleted ones too, so a partial index serves none. A plain view or a foreign table can have no index and
42
+ * is not judged.
43
+ */
44
+ export declare const uncoveredFileColumns: (model: ResourceModel, table: TableIndexes) => UncoveredColumn[];
38
45
  /**
39
46
  * The columns a read of the resource would read without an index serving them; empty when every one is served. The
40
47
  * filter's and the access row filter's columns each need an index of the kind their operator uses, the sort's first
@@ -13,6 +13,7 @@ const suggestion = (model, column, need, under) => {
13
13
  if (need === "trigram") return `create index on ${table} using gin (${quoted(column)} gin_trgm_ops)`;
14
14
  if (need === "digits") return `create index on ${table} using gin ((regexp_replace(${quoted(column)}, '[^0-9]', '', 'g')) gin_trgm_ops)`;
15
15
  if (need === "gin") return `create index on ${table} using gin (${quoted(column)})`;
16
+ if (need === "file") return `create index on ${table} ((split_part(${quoted(column)}, '?', 1)))`;
16
17
  return `create index on ${table} (${quoted(column)})`;
17
18
  };
18
19
  /** The columns a filter pins to one value or a list of values in every row it keeps: `=`, `in` and `is null` under AND. */
@@ -32,6 +33,10 @@ const mentions = (definition, column) => {
32
33
  const name = column.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
34
  return new RegExp(`(^|[^\\w"])("${name}"|${name})([^\\w"]|$)`).test(definition);
34
35
  };
36
+ const fileKey = (column) => {
37
+ const name = column.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
38
+ return new RegExp(`^split_part\\((${name}|"${name}")(::text)?, '\\?'(::text)?, 1\\)$`);
39
+ };
35
40
  /** Whether the index serves reading the column with the given need, while the query pins the `pinned` columns. */
36
41
  const indexServes = (index, column, need, pinned) => {
37
42
  if (index.partial) return false;
@@ -40,8 +45,24 @@ const indexServes = (index, column, need, pinned) => {
40
45
  case "trigram": return index.keys.some((key) => key.column === column && trigramClasses.includes(key.opclass));
41
46
  case "digits": return index.keys.some((key) => key.column === null && trigramClasses.includes(key.opclass) && key.definition.includes("regexp_replace") && mentions(key.definition, column));
42
47
  case "gin": return index.method === "gin" && index.keys.some((key) => key.column === column);
48
+ case "file": {
49
+ const first = index.keys[0];
50
+ return index.method === "btree" && first !== void 0 && first.column === null && fileKey(column).test(first.definition);
51
+ }
43
52
  }
44
53
  };
54
+ /**
55
+ * The file columns of `model` no index looks their files up by, as the file cleanup does before it deletes one: every
56
+ * row, soft-deleted ones too, so a partial index serves none. A plain view or a foreign table can have no index and
57
+ * is not judged.
58
+ */
59
+ const uncoveredFileColumns = (model, table) => table.kind !== "table" ? [] : Object.values(model.fields).filter((field) => field.type === "file" && !table.indexes.some((index) => indexServes(index, field.column, "file", /* @__PURE__ */ new Set()))).map((field) => ({
60
+ field: field.name,
61
+ column: field.column,
62
+ use: "cleanup",
63
+ need: "file",
64
+ suggestion: suggestion(model, field.column, "file")
65
+ }));
45
66
  const need = (ctx, field, kind, pinned, also) => ctx.indexes.some((index) => indexServes(index, field.column, kind, pinned) || also !== void 0 && indexServes(index, field.column, also, pinned)) ? [] : [{
46
67
  field: field.name,
47
68
  column: field.column,
@@ -133,4 +154,4 @@ const uncoveredColumns = (model, table, query, scope) => {
133
154
  ]);
134
155
  };
135
156
  //#endregion
136
- export { indexServes, uncoveredColumns };
157
+ export { indexServes, uncoveredColumns, uncoveredFileColumns };
package/dist/index.d.ts CHANGED
@@ -18,10 +18,10 @@ import { MapBounds, MapBucket, MapBuckets, MapPosition, cellsAcross, checkMapPos
18
18
  import { withoutField } from "./prune-filter.js";
19
19
  import { ScrollMode, scrollMode } from "./scroll-mode.js";
20
20
  import { IndexKey, TableIndex, TableIndexes, tableIndexes } from "./table-indexes.js";
21
- import { ColumnUse, CoverageQuery, IndexNeed, UncoveredColumn, indexServes, uncoveredColumns } from "./index-coverage.js";
21
+ import { ColumnUse, CoverageQuery, IndexNeed, UncoveredColumn, indexServes, uncoveredColumns, uncoveredFileColumns } from "./index-coverage.js";
22
22
  import { QueryError, QueryErrorCode } from "./fields/query-error.js";
23
23
  import { RelatedPageRequest, relatedPage } from "./related-page.js";
24
24
  import { parentCondition } from "./read-conditions.js";
25
25
  import { readsParent } from "./reads-parent.js";
26
26
  import { SeekRequest, seekToPosition } from "./seek-to-position.js";
27
- export { type ColumnUse, type CompileOptions, type CoverageQuery, type Db, type ExtraColumn, type FacetCount, type FacetOptions, type Granularity, type GroupTotal, type GroupTotalsOptions, type Histogram, type HistogramBucket, type IndexKey, type IndexNeed, type KeyItemType, type LimitPer, type ListOptions, type ListPlan, type ListResult, type MapBounds, type MapBucket, type MapBuckets, type MapPosition, type Measure, type ParentRead, type PerValue, QueryError, type QueryErrorCode, type RelatedPage, type RelatedPageRequest, type RelatedRowsQuery, type Scope, type ScrollMode, type SeekRequest, type SeriesOptions, type SeriesPoint, type SplitPoint, type SplitSeriesOptions, type TableIndex, type TableIndexes, type UncoveredColumn, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, withoutField };
27
+ export { type ColumnUse, type CompileOptions, type CoverageQuery, type Db, type ExtraColumn, type FacetCount, type FacetOptions, type Granularity, type GroupTotal, type GroupTotalsOptions, type Histogram, type HistogramBucket, type IndexKey, type IndexNeed, type KeyItemType, type LimitPer, type ListOptions, type ListPlan, type ListResult, type MapBounds, type MapBucket, type MapBuckets, type MapPosition, type Measure, type ParentRead, type PerValue, QueryError, type QueryErrorCode, type RelatedPage, type RelatedPageRequest, type RelatedRowsQuery, type Scope, type ScrollMode, type SeekRequest, type SeriesOptions, type SeriesPoint, type SplitPoint, type SplitSeriesOptions, type TableIndex, type TableIndexes, type UncoveredColumn, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, withoutField };
package/dist/index.js CHANGED
@@ -17,8 +17,8 @@ import { histogram } from "./histogram.js";
17
17
  import { cellsAcross, checkMapPosition, mapBuckets, mapCellPixels, maxMapBuckets, maxMapZoom } from "./map-buckets.js";
18
18
  import { scrollMode } from "./scroll-mode.js";
19
19
  import { tableIndexes } from "./table-indexes.js";
20
- import { indexServes, uncoveredColumns } from "./index-coverage.js";
20
+ import { indexServes, uncoveredColumns, uncoveredFileColumns } from "./index-coverage.js";
21
21
  import { relatedPage } from "./related-page.js";
22
22
  import { readsParent } from "./reads-parent.js";
23
23
  import { seekToPosition } from "./seek-to-position.js";
24
- export { QueryError, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, withoutField };
24
+ export { QueryError, buildListQuery, buildRelatedRows, cellsAcross, checkMapPosition, compileFilter, countUpTo, estimateCount, exactCount, facetCounts, groupTotals, histogram, indexServes, listPerValue, listQuery, mapBuckets, mapCellPixels, maxLimit, maxMapBuckets, maxMapZoom, parentCondition, readsParent, relatedPage, relatedRows, scrollMode, seekToPosition, series, splitSeries, tableIndexes, tenantCondition, uncoveredColumns, uncoveredFileColumns, withoutField };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/query",
3
- "version": "0.108.0",
3
+ "version": "0.109.0",
4
4
  "description": "Kysely list, filter, count and aggregate queries over Protobase resources.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -29,7 +29,7 @@
29
29
  "access": "public"
30
30
  },
31
31
  "dependencies": {
32
- "@protobase/schema": "0.108.0",
32
+ "@protobase/schema": "0.109.0",
33
33
  "kysely": "^0.29.6"
34
34
  }
35
35
  }