@sixb/pg 0.2.1 → 0.2.3

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.
Files changed (114) hide show
  1. package/README.md +9 -8
  2. package/dist/agents/computers.d.ts +8 -2
  3. package/dist/agents/computers.d.ts.map +1 -1
  4. package/dist/auth-storage/auth-storage.d.ts +2 -1
  5. package/dist/auth-storage/auth-storage.d.ts.map +1 -1
  6. package/dist/auth-storage/identities.d.ts +4 -0
  7. package/dist/auth-storage/identities.d.ts.map +1 -1
  8. package/dist/auth-storage/rows.d.ts +2 -1
  9. package/dist/auth-storage/rows.d.ts.map +1 -1
  10. package/dist/auth-storage/users.d.ts.map +1 -1
  11. package/dist/connector-connection-storage.d.ts.map +1 -1
  12. package/dist/index.d.ts +6 -0
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +2377 -428
  15. package/dist/index.js.map +37 -30
  16. package/dist/migrations.d.ts.map +1 -1
  17. package/dist/objects/conditional-reader.d.ts +7 -2
  18. package/dist/objects/conditional-reader.d.ts.map +1 -1
  19. package/dist/objects/partitions.d.ts.map +1 -1
  20. package/dist/objects/prepare-queries.d.ts.map +1 -1
  21. package/dist/objects/query-compiler.d.ts +14 -1
  22. package/dist/objects/query-compiler.d.ts.map +1 -1
  23. package/dist/objects/query-indexes.d.ts +13 -1
  24. package/dist/objects/query-indexes.d.ts.map +1 -1
  25. package/dist/objects/read-scope.d.ts.map +1 -1
  26. package/dist/objects/reader.d.ts +14 -1
  27. package/dist/objects/reader.d.ts.map +1 -1
  28. package/dist/objects/storage.d.ts +11 -1
  29. package/dist/objects/storage.d.ts.map +1 -1
  30. package/dist/objects/visibility-indexes.d.ts +22 -0
  31. package/dist/objects/visibility-indexes.d.ts.map +1 -0
  32. package/dist/objects/visibility-statistics.d.ts +26 -0
  33. package/dist/objects/visibility-statistics.d.ts.map +1 -0
  34. package/dist/objects/visibility.d.ts +34 -0
  35. package/dist/objects/visibility.d.ts.map +1 -0
  36. package/dist/ontology-storage/materialization-session.d.ts +5 -0
  37. package/dist/ontology-storage/materialization-session.d.ts.map +1 -1
  38. package/dist/ontology-storage/materialization-writer.d.ts +14 -0
  39. package/dist/ontology-storage/materialization-writer.d.ts.map +1 -1
  40. package/dist/ontology-storage/materializations.d.ts.map +1 -1
  41. package/dist/pg-artifact-storage.d.ts +14 -2
  42. package/dist/pg-artifact-storage.d.ts.map +1 -1
  43. package/dist/pg-client.d.ts.map +1 -1
  44. package/dist/pg-dataset-ingest-storage.d.ts +6 -0
  45. package/dist/pg-dataset-ingest-storage.d.ts.map +1 -0
  46. package/dist/pg-lake-retention-run-storage.d.ts +11 -0
  47. package/dist/pg-lake-retention-run-storage.d.ts.map +1 -0
  48. package/dist/pg-maintenance-heartbeat-storage.d.ts +11 -0
  49. package/dist/pg-maintenance-heartbeat-storage.d.ts.map +1 -0
  50. package/dist/pg-pipeline-run-storage.d.ts +5 -2
  51. package/dist/pg-pipeline-run-storage.d.ts.map +1 -1
  52. package/dist/pg-projection-run-storage.d.ts +5 -1
  53. package/dist/pg-projection-run-storage.d.ts.map +1 -1
  54. package/dist/pg-sync-run-storage.d.ts +6 -2
  55. package/dist/pg-sync-run-storage.d.ts.map +1 -1
  56. package/dist/pg-webhook-run-storage.d.ts +2 -1
  57. package/dist/pg-webhook-run-storage.d.ts.map +1 -1
  58. package/dist/pg-workflow-run-storage.d.ts +4 -3
  59. package/dist/pg-workflow-run-storage.d.ts.map +1 -1
  60. package/dist/run-claim-sql.d.ts +37 -0
  61. package/dist/run-claim-sql.d.ts.map +1 -0
  62. package/dist/share-session-storage.d.ts +2 -1
  63. package/dist/share-session-storage.d.ts.map +1 -1
  64. package/package.json +3 -3
  65. package/src/agents/computers.ts +54 -5
  66. package/src/auth-storage/auth-storage.ts +66 -0
  67. package/src/auth-storage/identities.ts +15 -0
  68. package/src/auth-storage/rows.ts +3 -0
  69. package/src/auth-storage/users.ts +3 -0
  70. package/src/connector-connection-storage.ts +33 -16
  71. package/src/index.ts +15 -0
  72. package/src/migrations/087-agent-computer-background-commands.sql +4 -0
  73. package/src/migrations/088-web-search-model-kind.sql +12 -0
  74. package/src/migrations/089-maintenance-heartbeats.sql +10 -0
  75. package/src/migrations/090-lake-retention-runs.sql +8 -0
  76. package/src/migrations/091-dataset-ingest-inbox.sql +38 -0
  77. package/src/migrations/092-run-recovery.sql +23 -0
  78. package/src/migrations/093-webhook-and-projection-leases.sql +10 -0
  79. package/src/migrations/094-artifact-board-previews.sql +14 -0
  80. package/src/migrations/095-artifact-open-versions.sql +101 -0
  81. package/src/migrations/096-artifact-documents.sql +11 -0
  82. package/src/migrations/097-artifact-frame-files.sql +8 -0
  83. package/src/migrations/098-artifact-comments.sql +36 -0
  84. package/src/migrations/099-object-artifact-refs.sql +12 -0
  85. package/src/migrations/100-shared-link-guests.sql +17 -0
  86. package/src/migrations/101-connector-connection-owners.sql +38 -0
  87. package/src/migrations/102-user-suspension-source.sql +12 -0
  88. package/src/migrations.ts +46 -0
  89. package/src/objects/conditional-reader.ts +26 -4
  90. package/src/objects/partitions.ts +17 -4
  91. package/src/objects/prepare-queries.ts +8 -3
  92. package/src/objects/query-compiler.ts +64 -23
  93. package/src/objects/query-indexes.ts +93 -27
  94. package/src/objects/read-scope.ts +147 -77
  95. package/src/objects/reader.ts +46 -18
  96. package/src/objects/storage.ts +53 -0
  97. package/src/objects/visibility-indexes.ts +222 -0
  98. package/src/objects/visibility-statistics.ts +73 -0
  99. package/src/objects/visibility.ts +135 -0
  100. package/src/ontology-storage/materialization-session.ts +4 -0
  101. package/src/ontology-storage/materialization-writer.ts +68 -1
  102. package/src/ontology-storage/materializations.ts +6 -2
  103. package/src/pg-artifact-storage.ts +798 -137
  104. package/src/pg-client.ts +4 -0
  105. package/src/pg-dataset-ingest-storage.ts +305 -0
  106. package/src/pg-lake-retention-run-storage.ts +40 -0
  107. package/src/pg-maintenance-heartbeat-storage.ts +53 -0
  108. package/src/pg-pipeline-run-storage.ts +85 -42
  109. package/src/pg-projection-run-storage.ts +61 -2
  110. package/src/pg-sync-run-storage.ts +100 -37
  111. package/src/pg-webhook-run-storage.ts +37 -11
  112. package/src/pg-workflow-run-storage.ts +39 -100
  113. package/src/run-claim-sql.ts +173 -0
  114. package/src/share-session-storage.ts +20 -0
@@ -5,9 +5,9 @@ import type {
5
5
  import {
6
6
  type CompiledPgObjectQuery,
7
7
  compilePgObjectStatement,
8
- compilePredicate,
9
8
  type PgObjectQuerySource,
10
9
  } from "./query-compiler"
10
+ import { compilePgConditionFilter } from "./visibility"
11
11
 
12
12
  export interface PgSelectedObjectReadSource extends PgObjectQuerySource {
13
13
  readonly objectPropertyPermissionsTable: string
@@ -34,8 +34,9 @@ export function compilePgSelectedObjectReadSource(
34
34
  objects: scope.objects,
35
35
  steps: scope.steps,
36
36
  })
37
+ const relations = compileSelectedReadScopeCte(scopeJson, projectId)
37
38
  const source: PgSelectedObjectReadSource = {
38
- ...scopedQuerySource(compileSelectedReadScopeCte(scopeJson, projectId)),
39
+ ...scopedQuerySource(() => relations),
39
40
  objectPropertyPermissionsTable: "_sixb_scope_object_permissions",
40
41
  traversalProbe: { sql: "", args: [] },
41
42
  }
@@ -77,12 +78,18 @@ export function compilePgConditionalObjectReadSource(
77
78
  const filters = new Map(
78
79
  scope.conditions.map((condition) => [
79
80
  condition.objectTypeId,
80
- compilePredicate(condition.predicate),
81
+ compilePgConditionFilter(condition),
81
82
  ])
82
83
  )
83
84
 
85
+ const relations = compileConditionalReadScopeRelations(projectId, filters)
86
+
84
87
  return Object.freeze({
85
- ...scopedQuerySource(compileConditionalReadScopeCte(projectId, filters)),
88
+ ...scopedQuerySource((sql) => withReadRelations(relations, sql)),
89
+ // A link is visible while both endpoints are, so a statement that reads both through
90
+ // `_sixb_scope_objects` reads stored links. Checking each endpoint again plans every partition
91
+ // of objects: the endpoint's type comes from the link row, so PostgreSQL cannot prune them.
92
+ linksBetweenVisibleObjectsTable: "links",
86
93
  // One type's visible objects are the stored ones its own condition matches.
87
94
  objectsOfType: (objectTypeId: string) => {
88
95
  const filter = filters.get(objectTypeId)
@@ -97,33 +104,68 @@ interface CompiledReadScopeCte {
97
104
  }
98
105
 
99
106
  /** Prefix every statement of a query with the scope's relations, ahead of its own arguments. */
100
- function scopedQuerySource(compiled: CompiledReadScopeCte): PgObjectQuerySource {
101
- const wrapStatement = (sql: string, args: readonly unknown[] = []) => ({
102
- sql: `${compiled.sql}\n${sql}`,
103
- args: [...compiled.args, ...args],
104
- })
107
+ function scopedQuerySource(
108
+ withClause: (statementSql: string) => CompiledReadScopeCte
109
+ ): PgObjectQuerySource {
110
+ const wrapStatement = (sql: string, args: readonly unknown[] = []) => {
111
+ const compiled = withClause(sql)
112
+ return {
113
+ sql: `${compiled.sql}\n${sql}`,
114
+ args: [...compiled.args, ...args],
115
+ }
116
+ }
105
117
 
106
118
  return {
107
119
  objectsTable: "_sixb_scope_objects",
108
120
  linksTable: "_sixb_scope_links",
109
121
  wrapStatement,
110
- wrapQuery: (query: CompiledPgObjectQuery): CompiledPgObjectQuery => ({
111
- ...query,
112
- ...wrapStatement(query.sql, query.args),
113
- vectorProbe: query.vectorProbe
114
- ? { ...query.vectorProbe, ...wrapStatement(query.vectorProbe.sql, query.vectorProbe.args) }
115
- : undefined,
116
- totalSql: `${compiled.sql}\n${query.totalSql}`,
117
- totalArgs: [...compiled.args, ...query.totalArgs],
118
- ...(query.hasMoreProbe
119
- ? {
120
- hasMoreProbe: {
121
- ...query.hasMoreProbe,
122
- ...wrapStatement(query.hasMoreProbe.sql, query.hasMoreProbe.args),
123
- },
124
- }
125
- : {}),
126
- }),
122
+ wrapQuery: (query: CompiledPgObjectQuery): CompiledPgObjectQuery => {
123
+ const total = wrapStatement(query.totalSql, query.totalArgs)
124
+
125
+ return {
126
+ ...query,
127
+ ...wrapStatement(query.sql, query.args),
128
+ vectorProbe: query.vectorProbe
129
+ ? {
130
+ ...query.vectorProbe,
131
+ ...wrapStatement(query.vectorProbe.sql, query.vectorProbe.args),
132
+ }
133
+ : undefined,
134
+ totalSql: total.sql,
135
+ totalArgs: total.args,
136
+ ...(query.hasMoreProbe
137
+ ? {
138
+ hasMoreProbe: {
139
+ ...query.hasMoreProbe,
140
+ ...wrapStatement(query.hasMoreProbe.sql, query.hasMoreProbe.args),
141
+ },
142
+ }
143
+ : {}),
144
+ }
145
+ },
146
+ }
147
+ }
148
+
149
+ interface ReadRelation extends CompiledReadScopeCte {
150
+ readonly name: string
151
+ }
152
+
153
+ /**
154
+ * The WITH clause naming the relations a statement reads. PostgreSQL parses and analyzes every
155
+ * relation a WITH clause names, read or not, and each one repeats every condition.
156
+ */
157
+ function withReadRelations(
158
+ relations: readonly ReadRelation[],
159
+ statementSql: string
160
+ ): CompiledReadScopeCte {
161
+ const read = relations.filter((relation) =>
162
+ new RegExp(`\\b${relation.name}\\b`).test(statementSql)
163
+ )
164
+ if (read.length === 0) return { sql: "", args: [] }
165
+
166
+ return {
167
+ sql: `WITH ${read.map((relation) => relation.sql).join(",\n")}`,
168
+ args: read.flatMap((relation) => relation.args),
127
169
  }
128
170
  }
129
171
 
@@ -133,23 +175,31 @@ type ConditionFilters = ReadonlyMap<
133
175
  { readonly sql: string; readonly args: readonly unknown[] }
134
176
  >
135
177
 
136
- function compileConditionalReadScopeCte(
178
+ function compileConditionalReadScopeRelations(
137
179
  projectId: string,
138
180
  filters: ConditionFilters
139
- ): CompiledReadScopeCte {
181
+ ): readonly ReadRelation[] {
140
182
  if (filters.size === 0) {
141
- return {
142
- sql: `
143
- WITH
144
- _sixb_scope_objects AS NOT MATERIALIZED (
145
- SELECT * FROM objects WHERE project_id = ?
146
- ),
147
- _sixb_scope_links AS NOT MATERIALIZED (
148
- SELECT * FROM links WHERE project_id = ?
149
- )
150
- `,
151
- args: [projectId, projectId],
152
- }
183
+ return [
184
+ {
185
+ name: "_sixb_scope_objects",
186
+ sql: `
187
+ _sixb_scope_objects AS NOT MATERIALIZED (
188
+ SELECT * FROM objects WHERE project_id = ?
189
+ )
190
+ `,
191
+ args: [projectId],
192
+ },
193
+ {
194
+ name: "_sixb_scope_links",
195
+ sql: `
196
+ _sixb_scope_links AS NOT MATERIALIZED (
197
+ SELECT * FROM links WHERE project_id = ?
198
+ )
199
+ `,
200
+ args: [projectId],
201
+ },
202
+ ]
153
203
  }
154
204
 
155
205
  const conditionedTypeArgs = [...filters.keys()]
@@ -162,45 +212,65 @@ function compileConditionalReadScopeCte(
162
212
  matchArgs.push(objectTypeId, ...filter.args)
163
213
  }
164
214
 
165
- const visibleEndpoint = (end: "source" | "target") => `
166
- (
167
- stored.${end}_type_id NOT IN (${conditionedTypes})
168
- OR EXISTS (
169
- SELECT 1
170
- FROM _sixb_scope_objects AS endpoint
171
- WHERE endpoint.project_id = stored.project_id
172
- AND endpoint.object_type_id = stored.${end}_type_id
173
- AND endpoint.primary_id = stored.${end}_id
174
- )
175
- )
176
- `
215
+ // An endpoint of a conditioned type is a visible object of that type. Each check names its type,
216
+ // so PostgreSQL plans that type's partition alone: matched on the link row's type, one check
217
+ // would plan every partition of objects.
218
+ const visibleEndpoint = (end: "source" | "target"): CompiledReadScopeCte => {
219
+ const checks: string[] = []
220
+ const args: unknown[] = [...conditionedTypeArgs]
177
221
 
178
- return {
179
- sql: `
180
- WITH
181
- _sixb_scope_objects AS NOT MATERIALIZED (
182
- SELECT *
183
- FROM objects
184
- WHERE project_id = ?
185
- AND (object_type_id NOT IN (${conditionedTypes}) OR ${matches.join(" OR ")})
186
- ),
187
- _sixb_scope_links AS NOT MATERIALIZED (
188
- SELECT stored.*
189
- FROM links AS stored
190
- WHERE stored.project_id = ?
191
- AND ${visibleEndpoint("source")}
192
- AND ${visibleEndpoint("target")}
193
- )
194
- `,
195
- args: [
196
- projectId,
197
- ...conditionedTypeArgs,
198
- ...matchArgs,
199
- projectId,
200
- ...conditionedTypeArgs,
201
- ...conditionedTypeArgs,
202
- ],
222
+ for (const [objectTypeId, filter] of filters) {
223
+ checks.push(`
224
+ (
225
+ stored.${end}_type_id = ?::text
226
+ AND EXISTS (
227
+ SELECT 1
228
+ FROM objects AS endpoint
229
+ WHERE endpoint.project_id = stored.project_id
230
+ AND endpoint.object_type_id = ?::text
231
+ AND endpoint.primary_id = stored.${end}_id
232
+ AND (${filter.sql})
233
+ )
234
+ )
235
+ `)
236
+ args.push(objectTypeId, objectTypeId, ...filter.args)
237
+ }
238
+
239
+ return {
240
+ sql: `(stored.${end}_type_id NOT IN (${conditionedTypes}) OR ${checks.join(" OR ")})`,
241
+ args,
242
+ }
203
243
  }
244
+ const visibleSource = visibleEndpoint("source")
245
+ const visibleTarget = visibleEndpoint("target")
246
+
247
+ return [
248
+ {
249
+ name: "_sixb_scope_objects",
250
+ sql: `
251
+ _sixb_scope_objects AS NOT MATERIALIZED (
252
+ SELECT *
253
+ FROM objects
254
+ WHERE project_id = ?
255
+ AND (object_type_id NOT IN (${conditionedTypes}) OR ${matches.join(" OR ")})
256
+ )
257
+ `,
258
+ args: [projectId, ...conditionedTypeArgs, ...matchArgs],
259
+ },
260
+ {
261
+ name: "_sixb_scope_links",
262
+ sql: `
263
+ _sixb_scope_links AS NOT MATERIALIZED (
264
+ SELECT stored.*
265
+ FROM links AS stored
266
+ WHERE stored.project_id = ?
267
+ AND ${visibleSource.sql}
268
+ AND ${visibleTarget.sql}
269
+ )
270
+ `,
271
+ args: [projectId, ...visibleSource.args, ...visibleTarget.args],
272
+ },
273
+ ]
204
274
  }
205
275
 
206
276
  function compileSelectedReadScopeCte(scopeJson: string, projectId: string): CompiledReadScopeCte {
@@ -32,7 +32,7 @@ import {
32
32
  type PgObjectQuerySource,
33
33
  pgObjectsOfType,
34
34
  } from "./query-compiler"
35
- import { findPgSortIndexBound, type PgSortIndexCatalog } from "./query-indexes"
35
+ import { findPgSortIndexRead, type PgSortIndexCatalog } from "./query-indexes"
36
36
  import type { PgSelectedObjectReadSource } from "./read-scope"
37
37
  import {
38
38
  type FacetDatabaseRow,
@@ -55,7 +55,13 @@ export class PgObjectReader {
55
55
  private readonly sql: SQLClient,
56
56
  private readonly source: PgObjectQuerySource | PgSelectedObjectReadSource,
57
57
  private readonly aggregateAdmission?: PgAggregateAdmission,
58
- private readonly sortIndexes?: PgSortIndexCatalog
58
+ /**
59
+ * Ordered pages walk a prepared sort index when one fits. A selected source takes none: its
60
+ * objects are the ones its paths reach, which no index over a type's rows orders.
61
+ */
62
+ private readonly sortIndexes?: PgSortIndexCatalog,
63
+ /** Whether the reader sees so few objects of a type that its pages read them before ordering. */
64
+ private readonly readsFewObjects?: (sql: SQLClient, objectTypeId: string) => Promise<boolean>
59
65
  ) {}
60
66
 
61
67
  private async assertVectorSearch(
@@ -115,12 +121,19 @@ export class PgObjectReader {
115
121
  private async compilePageQuery(params: QueryObjectsInput): Promise<CompiledPgObjectQuery> {
116
122
  const sql = this.sql
117
123
  const source = this.source
118
- const sortIndexBound =
119
- source === DEFAULT_OBJECT_QUERY_SOURCE && this.sortIndexes
120
- ? await findPgSortIndexBound(sql, this.sortIndexes, params.projectId, params.query)
121
- : undefined
124
+ let sortIndex = this.sortIndexes
125
+ ? await findPgSortIndexRead(sql, this.sortIndexes, params.projectId, params.query)
126
+ : undefined
127
+
128
+ // Walking the index would check nearly every object to find the few this reader sees.
129
+ const unorderedStart =
130
+ sortIndex !== undefined &&
131
+ (await this.readsFewObjects?.(sql, sortIndex.objectTypeId)) === true
132
+ if (unorderedStart) sortIndex = undefined
133
+
122
134
  let compiled = compilePgObjectQuery(params.projectId, params.query, {
123
- sortIndexBound,
135
+ sortIndex,
136
+ unorderedStart,
124
137
  includeTotal: params.includeTotal,
125
138
  source,
126
139
  })
@@ -132,7 +145,8 @@ export class PgObjectReader {
132
145
  includeTotal: params.includeTotal,
133
146
  source,
134
147
  correlatedTraversal: true,
135
- sortIndexBound,
148
+ sortIndex,
149
+ unorderedStart,
136
150
  })
137
151
  }
138
152
  }
@@ -402,20 +416,34 @@ export class PgObjectReader {
402
416
  const source = this.source
403
417
  const result = new Map<ObjectBatchKey, ObjectRow>()
404
418
  if (params.items.length === 0) return result
405
- const items = params.items.map((item, batchIndex) => ({ ...item, batchIndex }))
406
- const statement = compilePgObjectStatement(
407
- `
419
+
420
+ const requestedByType = new Map<string, { primaryId: string; batchIndex: number }[]>()
421
+ for (const [batchIndex, item] of params.items.entries()) {
422
+ const requested = requestedByType.get(item.objectTypeId) ?? []
423
+ requested.push({ primaryId: item.primaryId, batchIndex })
424
+ requestedByType.set(item.objectTypeId, requested)
425
+ }
426
+
427
+ // One branch per type, each naming it, so PostgreSQL plans only the requested types'
428
+ // partitions. Matched on a requested row's type, it would plan every partition of objects.
429
+ const branches: string[] = []
430
+ const args: unknown[] = []
431
+ for (const [objectTypeId, requested] of requestedByType) {
432
+ const objects = pgObjectsOfType(source, objectTypeId)
433
+ branches.push(`
408
434
  SELECT object.*, requested."batchIndex" AS _batch_index
409
435
  FROM jsonb_to_recordset(?::text::jsonb)
410
- AS requested("objectTypeId" text, "primaryId" text, "batchIndex" integer)
411
- JOIN ${source.objectsTable} AS object
436
+ AS requested("primaryId" text, "batchIndex" integer)
437
+ JOIN ${objects.table} AS object
412
438
  ON object.project_id = ?
413
- AND object.object_type_id = requested."objectTypeId"
439
+ AND object.object_type_id = ?::text
414
440
  AND object.primary_id = requested."primaryId"
415
- `,
416
- [JSON.stringify(items), params.projectId],
417
- source
418
- )
441
+ ${objects.filter}
442
+ `)
443
+ args.push(JSON.stringify(requested), params.projectId, objectTypeId, ...objects.args)
444
+ }
445
+
446
+ const statement = compilePgObjectStatement(branches.join(" UNION ALL "), args, source)
419
447
  const rows = await sql.unsafe<ObjectBatchDatabaseRow[]>(
420
448
  statement.sql,
421
449
  statement.args as SqlParameter[]
@@ -1,5 +1,6 @@
1
1
  import { hasVectorProfile } from "@sixb/core/internal/query"
2
2
  import type {
3
+ ArtifactHolder,
3
4
  CompiledConditionalObjectReadScope,
4
5
  CompiledSelectedObjectReadScope,
5
6
  CountObjectsInput,
@@ -41,6 +42,7 @@ import {
41
42
  rowToObject,
42
43
  } from "./rows"
43
44
  import { createPgSelectedReader } from "./selected-reader"
45
+ import { PgVisibilityStatisticsCatalog } from "./visibility-statistics"
44
46
 
45
47
  const PG_OBJECT_QUERY_CAPABILITIES: ObjectQueryCapabilities = {
46
48
  queryObjects: true,
@@ -99,6 +101,7 @@ const PG_OBJECT_QUERY_CAPABILITIES: ObjectQueryCapabilities = {
99
101
  timestamp: { equality: true, ordering: true },
100
102
  // jsonb equality and containment compare both fields of `{ type, id }`, whatever key order.
101
103
  userRef: { equality: true },
104
+ artifactRef: { equality: true },
102
105
  },
103
106
  limits: {
104
107
  totalCount: true,
@@ -116,6 +119,7 @@ const PG_OBJECT_QUERY_CAPABILITIES: ObjectQueryCapabilities = {
116
119
  export class PgObjectStorage implements ObjectStorage {
117
120
  private readonly aggregateAdmission?: PgAggregateAdmission
118
121
  private readonly reader: PgObjectReader
122
+ private readonly visibilityStatistics = new PgVisibilityStatisticsCatalog()
119
123
 
120
124
  constructor(
121
125
  private readonly sql: PgStoreClient,
@@ -151,6 +155,8 @@ export class PgObjectStorage implements ObjectStorage {
151
155
  }): ObjectReadStorage {
152
156
  return createPgConditionalReader(this.sql, params, {
153
157
  aggregateAdmission: this.aggregateAdmission,
158
+ sortIndexes: this.sortIndexes,
159
+ visibilityStatistics: this.visibilityStatistics,
154
160
  queryCapabilities: () => this.queryCapabilities(),
155
161
  })
156
162
  }
@@ -303,6 +309,33 @@ export class PgObjectStorage implements ObjectStorage {
303
309
  }
304
310
  }
305
311
 
312
+ async listArtifactHolders(params: {
313
+ projectId: string
314
+ artifactId: string
315
+ after?: ArtifactHolder
316
+ limit: number
317
+ }): Promise<{ holders: readonly ArtifactHolder[]; next?: ArtifactHolder }> {
318
+ assertArtifactHolderPageLimit(params.limit)
319
+
320
+ const { after } = params
321
+ const rows = await this.sql<ArtifactHolderRow[]>`
322
+ SELECT object_type_id, primary_id, property_id FROM object_artifact_refs
323
+ WHERE project_id = ${params.projectId} AND artifact_id = ${params.artifactId}
324
+ ${
325
+ after
326
+ ? this.sql`AND (object_type_id, primary_id, property_id)
327
+ > (${after.objectTypeId}, ${after.primaryId}, ${after.propertyId})`
328
+ : this.sql``
329
+ }
330
+ ORDER BY object_type_id, primary_id, property_id
331
+ LIMIT ${params.limit + 1}
332
+ `
333
+
334
+ const holders = rows.slice(0, params.limit).map(artifactHolderFromRow)
335
+ const last = holders.at(-1)
336
+ return { holders, ...(rows.length > params.limit && last ? { next: last } : {}) }
337
+ }
338
+
306
339
  async list(params: {
307
340
  projectId: string
308
341
  objectTypeId?: string | readonly string[]
@@ -366,3 +399,23 @@ function assertReconciliationPageLimit(limit: number): void {
366
399
  throw new Error("Object reconciliation page limit must be a positive safe integer.")
367
400
  }
368
401
  }
402
+
403
+ interface ArtifactHolderRow {
404
+ readonly object_type_id: string
405
+ readonly primary_id: string
406
+ readonly property_id: string
407
+ }
408
+
409
+ function artifactHolderFromRow(row: ArtifactHolderRow): ArtifactHolder {
410
+ return {
411
+ objectTypeId: row.object_type_id,
412
+ primaryId: row.primary_id,
413
+ propertyId: row.property_id,
414
+ }
415
+ }
416
+
417
+ function assertArtifactHolderPageLimit(limit: number): void {
418
+ if (!Number.isSafeInteger(limit) || limit <= 0) {
419
+ throw new Error("Artifact holder page limit must be a positive safe integer.")
420
+ }
421
+ }