@mastra/pg 1.15.0-alpha.0 → 1.15.0-alpha.1

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.js CHANGED
@@ -3723,6 +3723,20 @@ function transformFromSqlRow({
3723
3723
  });
3724
3724
  return result;
3725
3725
  }
3726
+ function tenancyWhere(filters, startIndex) {
3727
+ const conditions = [];
3728
+ const params = [];
3729
+ let idx = startIndex;
3730
+ if (filters?.organizationId !== void 0) {
3731
+ conditions.push(`"organizationId" = $${idx++}`);
3732
+ params.push(filters.organizationId);
3733
+ }
3734
+ if (filters?.projectId !== void 0) {
3735
+ conditions.push(`"projectId" = $${idx++}`);
3736
+ params.push(filters.projectId);
3737
+ }
3738
+ return { conditions, params, nextIndex: idx };
3739
+ }
3726
3740
 
3727
3741
  // src/storage/domains/agents/index.ts
3728
3742
  var AgentsPG = class _AgentsPG extends AgentsStorage {
@@ -5578,8 +5592,8 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5578
5592
  groundTruthSchema: input.groundTruthSchema ?? null,
5579
5593
  requestContextSchema: input.requestContextSchema ?? null,
5580
5594
  targetType: input.targetType ?? null,
5581
- targetIds: input.targetIds !== void 0 ? JSON.stringify(input.targetIds) : null,
5582
- scorerIds: input.scorerIds ? JSON.stringify(input.scorerIds) : null,
5595
+ targetIds: input.targetIds ?? null,
5596
+ scorerIds: input.scorerIds ?? null,
5583
5597
  organizationId: input.organizationId ?? null,
5584
5598
  projectId: input.projectId ?? null,
5585
5599
  candidateKey: input.candidateKey ?? null,
@@ -5619,10 +5633,15 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5619
5633
  );
5620
5634
  }
5621
5635
  }
5622
- async getDatasetById({ id }) {
5636
+ async getDatasetById({
5637
+ id,
5638
+ filters
5639
+ }) {
5623
5640
  try {
5624
5641
  const tableName = getTableName2({ indexName: TABLE_DATASETS, schemaName: getSchemaName2(this.#schema) });
5625
- const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1`, [id]);
5642
+ const { conditions, params } = tenancyWhere(filters, 2);
5643
+ const whereSql = ['"id" = $1', ...conditions].join(" AND ");
5644
+ const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE ${whereSql}`, [id, ...params]);
5626
5645
  return result ? this.transformDatasetRow(result) : null;
5627
5646
  } catch (error) {
5628
5647
  throw new MastraError(
@@ -5637,7 +5656,7 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5637
5656
  }
5638
5657
  async _doUpdateDataset(args) {
5639
5658
  try {
5640
- const existing = await this.getDatasetById({ id: args.id });
5659
+ const existing = await this.getDatasetById({ id: args.id, filters: args.filters });
5641
5660
  if (!existing) {
5642
5661
  throw new MastraError({
5643
5662
  id: createStorageErrorId("PG", "UPDATE_DATASET", "NOT_FOUND"),
@@ -5726,7 +5745,7 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5726
5745
  );
5727
5746
  }
5728
5747
  }
5729
- async deleteDataset({ id }) {
5748
+ async deleteDataset({ id, filters }) {
5730
5749
  try {
5731
5750
  const datasetsTable = getTableName2({ indexName: TABLE_DATASETS, schemaName: getSchemaName2(this.#schema) });
5732
5751
  const itemsTable = getTableName2({ indexName: TABLE_DATASET_ITEMS, schemaName: getSchemaName2(this.#schema) });
@@ -5739,24 +5758,31 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5739
5758
  indexName: TABLE_EXPERIMENT_RESULTS,
5740
5759
  schemaName: getSchemaName2(this.#schema)
5741
5760
  });
5742
- try {
5743
- await this.#db.client.none(
5744
- `DELETE FROM ${experimentResultsTable} WHERE "experimentId" IN (SELECT "id" FROM ${experimentsTable} WHERE "datasetId" = $1)`,
5745
- [id]
5746
- );
5747
- } catch {
5748
- }
5749
- try {
5750
- await this.#db.client.none(
5751
- `UPDATE ${experimentsTable} SET "datasetId" = NULL, "datasetVersion" = NULL WHERE "datasetId" = $1`,
5752
- [id]
5753
- );
5754
- } catch {
5755
- }
5761
+ const { conditions, params } = tenancyWhere(filters, 2);
5762
+ const scopedWhere = ['"id" = $1', ...conditions].join(" AND ");
5756
5763
  await this.#db.client.tx(async (t) => {
5764
+ const locked = await t.oneOrNone(`SELECT "id" FROM ${datasetsTable} WHERE ${scopedWhere} FOR UPDATE`, [
5765
+ id,
5766
+ ...params
5767
+ ]);
5768
+ if (!locked) return;
5769
+ const experimentTablesExist = await t.oneOrNone(
5770
+ `SELECT to_regclass($1) IS NOT NULL AND to_regclass($2) IS NOT NULL AS exists`,
5771
+ [experimentResultsTable, experimentsTable]
5772
+ );
5773
+ if (experimentTablesExist?.exists) {
5774
+ await t.none(
5775
+ `DELETE FROM ${experimentResultsTable} WHERE "experimentId" IN (SELECT "id" FROM ${experimentsTable} WHERE "datasetId" = $1)`,
5776
+ [id]
5777
+ );
5778
+ await t.none(
5779
+ `UPDATE ${experimentsTable} SET "datasetId" = NULL, "datasetVersion" = NULL WHERE "datasetId" = $1`,
5780
+ [id]
5781
+ );
5782
+ }
5757
5783
  await t.none(`DELETE FROM ${versionsTable} WHERE "datasetId" = $1`, [id]);
5758
5784
  await t.none(`DELETE FROM ${itemsTable} WHERE "datasetId" = $1`, [id]);
5759
- await t.none(`DELETE FROM ${datasetsTable} WHERE "id" = $1`, [id]);
5785
+ await t.none(`DELETE FROM ${datasetsTable} WHERE ${scopedWhere}`, [id, ...params]);
5760
5786
  });
5761
5787
  } catch (error) {
5762
5788
  if (error instanceof MastraError) throw error;
@@ -5778,7 +5804,7 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5778
5804
  const queryParams = [];
5779
5805
  let paramIndex = 1;
5780
5806
  if (args.filters) {
5781
- const { organizationId, projectId, candidateKey, candidateId } = args.filters;
5807
+ const { organizationId, projectId, candidateKey, candidateId, targetType, targetIds, name } = args.filters;
5782
5808
  if (organizationId !== void 0) {
5783
5809
  conditions.push(`"organizationId" = $${paramIndex++}`);
5784
5810
  queryParams.push(organizationId);
@@ -5795,6 +5821,18 @@ var DatasetsPG = class _DatasetsPG extends DatasetsStorage {
5795
5821
  conditions.push(`"candidateId" = $${paramIndex++}`);
5796
5822
  queryParams.push(candidateId);
5797
5823
  }
5824
+ if (targetType !== void 0) {
5825
+ conditions.push(`"targetType" = $${paramIndex++}`);
5826
+ queryParams.push(targetType);
5827
+ }
5828
+ if (targetIds !== void 0 && targetIds.length > 0) {
5829
+ conditions.push(`"targetIds" ?| $${paramIndex++}::text[]`);
5830
+ queryParams.push(targetIds);
5831
+ }
5832
+ if (name !== void 0 && name.length > 0) {
5833
+ conditions.push(`"name" ILIKE $${paramIndex++}`);
5834
+ queryParams.push(`%${name}%`);
5835
+ }
5798
5836
  }
5799
5837
  const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
5800
5838
  const countResult = await this.#db.client.one(
@@ -6806,10 +6844,15 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6806
6844
  );
6807
6845
  }
6808
6846
  }
6809
- async getExperimentById({ id }) {
6847
+ async getExperimentById({
6848
+ id,
6849
+ filters
6850
+ }) {
6810
6851
  try {
6811
6852
  const tableName = getTableName2({ indexName: TABLE_EXPERIMENTS, schemaName: getSchemaName2(this.#schema) });
6812
- const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1`, [id]);
6853
+ const { conditions, params } = tenancyWhere(filters, 2);
6854
+ const whereSql = ['"id" = $1', ...conditions].join(" AND ");
6855
+ const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE ${whereSql}`, [id, ...params]);
6813
6856
  return result ? this.transformExperimentRow(result) : null;
6814
6857
  } catch (error) {
6815
6858
  throw new MastraError(
@@ -6896,15 +6939,21 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
6896
6939
  );
6897
6940
  }
6898
6941
  }
6899
- async deleteExperiment({ id }) {
6942
+ async deleteExperiment({ id, filters }) {
6900
6943
  try {
6901
6944
  const resultsTable = getTableName2({
6902
6945
  indexName: TABLE_EXPERIMENT_RESULTS,
6903
6946
  schemaName: getSchemaName2(this.#schema)
6904
6947
  });
6905
6948
  const experimentsTable = getTableName2({ indexName: TABLE_EXPERIMENTS, schemaName: getSchemaName2(this.#schema) });
6906
- await this.#db.client.none(`DELETE FROM ${resultsTable} WHERE "experimentId" = $1`, [id]);
6907
- await this.#db.client.none(`DELETE FROM ${experimentsTable} WHERE "id" = $1`, [id]);
6949
+ const { conditions, params } = tenancyWhere(filters, 2);
6950
+ const gateSql = `SELECT "id" FROM ${experimentsTable} WHERE ${['"id" = $1', ...conditions].join(" AND ")} FOR UPDATE`;
6951
+ await this.#db.client.tx(async (t) => {
6952
+ const parent = await t.oneOrNone(gateSql, [id, ...params]);
6953
+ if (!parent) return;
6954
+ await t.none(`DELETE FROM ${resultsTable} WHERE "experimentId" = $1`, [id]);
6955
+ await t.none(`DELETE FROM ${experimentsTable} WHERE "id" = $1`, [id]);
6956
+ });
6908
6957
  } catch (error) {
6909
6958
  throw new MastraError(
6910
6959
  {
@@ -7034,10 +7083,15 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
7034
7083
  );
7035
7084
  }
7036
7085
  }
7037
- async getExperimentResultById({ id }) {
7086
+ async getExperimentResultById({
7087
+ id,
7088
+ filters
7089
+ }) {
7038
7090
  try {
7039
7091
  const tableName = getTableName2({ indexName: TABLE_EXPERIMENT_RESULTS, schemaName: getSchemaName2(this.#schema) });
7040
- const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE "id" = $1`, [id]);
7092
+ const { conditions, params } = tenancyWhere(filters, 2);
7093
+ const whereSql = ['"id" = $1', ...conditions].join(" AND ");
7094
+ const result = await this.#db.client.oneOrNone(`SELECT * FROM ${tableName} WHERE ${whereSql}`, [id, ...params]);
7041
7095
  return result ? this.transformExperimentResultRow(result) : null;
7042
7096
  } catch (error) {
7043
7097
  throw new MastraError(
@@ -7112,9 +7166,23 @@ var ExperimentsPG = class _ExperimentsPG extends ExperimentsStorage {
7112
7166
  );
7113
7167
  }
7114
7168
  }
7115
- async deleteExperimentResults({ experimentId }) {
7169
+ async deleteExperimentResults({
7170
+ experimentId,
7171
+ filters
7172
+ }) {
7116
7173
  try {
7117
7174
  const tableName = getTableName2({ indexName: TABLE_EXPERIMENT_RESULTS, schemaName: getSchemaName2(this.#schema) });
7175
+ const experimentsTable = getTableName2({ indexName: TABLE_EXPERIMENTS, schemaName: getSchemaName2(this.#schema) });
7176
+ if (filters?.organizationId !== void 0 || filters?.projectId !== void 0) {
7177
+ const { conditions, params } = tenancyWhere(filters, 2);
7178
+ const gateSql = `SELECT "id" FROM ${experimentsTable} WHERE ${['"id" = $1', ...conditions].join(" AND ")} FOR UPDATE`;
7179
+ await this.#db.client.tx(async (t) => {
7180
+ const parent = await t.oneOrNone(gateSql, [experimentId, ...params]);
7181
+ if (!parent) return;
7182
+ await t.none(`DELETE FROM ${tableName} WHERE "experimentId" = $1`, [experimentId]);
7183
+ });
7184
+ return;
7185
+ }
7118
7186
  await this.#db.client.none(`DELETE FROM ${tableName} WHERE "experimentId" = $1`, [experimentId]);
7119
7187
  } catch (error) {
7120
7188
  throw new MastraError(
@@ -16803,6 +16871,11 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
16803
16871
  tableName: TABLE_SCORER_DEFINITIONS,
16804
16872
  schema: TABLE_SCHEMAS[TABLE_SCORER_DEFINITIONS]
16805
16873
  });
16874
+ await this.#db.alterTable({
16875
+ tableName: TABLE_SCORER_DEFINITIONS,
16876
+ schema: TABLE_SCHEMAS[TABLE_SCORER_DEFINITIONS],
16877
+ ifNotExists: ["organizationId", "projectId"]
16878
+ });
16806
16879
  await this.#db.createTable({
16807
16880
  tableName: TABLE_SCORER_DEFINITION_VERSIONS,
16808
16881
  schema: TABLE_SCHEMAS[TABLE_SCORER_DEFINITION_VERSIONS]
@@ -16858,14 +16931,16 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
16858
16931
  const nowIso = now.toISOString();
16859
16932
  await this.#db.client.none(
16860
16933
  `INSERT INTO ${tableName} (
16861
- id, status, "activeVersionId", "authorId", metadata,
16934
+ id, status, "activeVersionId", "authorId", "organizationId", "projectId", metadata,
16862
16935
  "createdAt", "createdAtZ", "updatedAt", "updatedAtZ"
16863
- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)`,
16936
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
16864
16937
  [
16865
16938
  scorerDefinition.id,
16866
16939
  "draft",
16867
16940
  null,
16868
16941
  scorerDefinition.authorId ?? null,
16942
+ scorerDefinition.organizationId ?? null,
16943
+ scorerDefinition.projectId ?? null,
16869
16944
  scorerDefinition.metadata ? JSON.stringify(scorerDefinition.metadata) : null,
16870
16945
  nowIso,
16871
16946
  nowIso,
@@ -16873,7 +16948,14 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
16873
16948
  nowIso
16874
16949
  ]
16875
16950
  );
16876
- const { id: _id, authorId: _authorId, metadata: _metadata, ...snapshotConfig } = scorerDefinition;
16951
+ const {
16952
+ id: _id,
16953
+ authorId: _authorId,
16954
+ organizationId: _organizationId,
16955
+ projectId: _projectId,
16956
+ metadata: _metadata,
16957
+ ...snapshotConfig
16958
+ } = scorerDefinition;
16877
16959
  const versionId = crypto.randomUUID();
16878
16960
  await this.createVersion({
16879
16961
  id: versionId,
@@ -16888,6 +16970,8 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
16888
16970
  status: "draft",
16889
16971
  activeVersionId: void 0,
16890
16972
  authorId: scorerDefinition.authorId,
16973
+ organizationId: scorerDefinition.organizationId,
16974
+ projectId: scorerDefinition.projectId,
16891
16975
  metadata: scorerDefinition.metadata,
16892
16976
  createdAt: now,
16893
16977
  updatedAt: now
@@ -17001,7 +17085,16 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
17001
17085
  }
17002
17086
  }
17003
17087
  async list(args) {
17004
- const { page = 0, perPage: perPageInput, orderBy, authorId, metadata, status } = args || {};
17088
+ const {
17089
+ page = 0,
17090
+ perPage: perPageInput,
17091
+ orderBy,
17092
+ authorId,
17093
+ organizationId,
17094
+ projectId,
17095
+ metadata,
17096
+ status
17097
+ } = args || {};
17005
17098
  const { field, direction } = this.parseOrderBy(orderBy);
17006
17099
  if (page < 0) {
17007
17100
  throw new MastraError(
@@ -17029,6 +17122,14 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
17029
17122
  conditions.push(`"authorId" = $${paramIdx++}`);
17030
17123
  queryParams.push(authorId);
17031
17124
  }
17125
+ if (organizationId !== void 0) {
17126
+ conditions.push(`"organizationId" = $${paramIdx++}`);
17127
+ queryParams.push(organizationId);
17128
+ }
17129
+ if (projectId !== void 0) {
17130
+ conditions.push(`"projectId" = $${paramIdx++}`);
17131
+ queryParams.push(projectId);
17132
+ }
17032
17133
  if (metadata && Object.keys(metadata).length > 0) {
17033
17134
  conditions.push(`metadata @> $${paramIdx++}::jsonb`);
17034
17135
  queryParams.push(JSON.stringify(metadata));
@@ -17355,6 +17456,8 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
17355
17456
  status: row.status,
17356
17457
  activeVersionId: row.activeVersionId,
17357
17458
  authorId: row.authorId,
17459
+ organizationId: row.organizationId,
17460
+ projectId: row.projectId,
17358
17461
  metadata: parseJsonResilient(row.metadata),
17359
17462
  createdAt: new Date(row.createdAtZ || row.createdAt),
17360
17463
  updatedAt: new Date(row.updatedAtZ || row.updatedAt)
@@ -17379,6 +17482,18 @@ var ScorerDefinitionsPG = class _ScorerDefinitionsPG extends ScorerDefinitionsSt
17379
17482
  };
17380
17483
  }
17381
17484
  };
17485
+ function applyTenancyFilters(conditions, queryParams, startIndex, filters) {
17486
+ let paramIndex = startIndex;
17487
+ if (filters?.organizationId !== void 0) {
17488
+ conditions.push(`"organizationId" = $${paramIndex++}`);
17489
+ queryParams.push(filters.organizationId);
17490
+ }
17491
+ if (filters?.projectId !== void 0) {
17492
+ conditions.push(`"projectId" = $${paramIndex++}`);
17493
+ queryParams.push(filters.projectId);
17494
+ }
17495
+ return paramIndex;
17496
+ }
17382
17497
  function getSchemaName6(schema) {
17383
17498
  return schema ? `"${schema}"` : '"public"';
17384
17499
  }
@@ -17421,7 +17536,7 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17421
17536
  await this.#db.alterTable({
17422
17537
  tableName: TABLE_SCORERS,
17423
17538
  schema: TABLE_SCHEMAS[TABLE_SCORERS],
17424
- ifNotExists: ["spanId", "requestContext"]
17539
+ ifNotExists: ["spanId", "requestContext", "organizationId", "projectId", "batchId", "datasetId", "datasetItemId"]
17425
17540
  });
17426
17541
  await this.createDefaultIndexes();
17427
17542
  await this.createCustomIndexes();
@@ -17557,7 +17672,8 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17557
17672
  pagination,
17558
17673
  entityId,
17559
17674
  entityType,
17560
- source
17675
+ source,
17676
+ filters
17561
17677
  }) {
17562
17678
  try {
17563
17679
  const conditions = [`"scorerId" = $1`];
@@ -17575,6 +17691,7 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17575
17691
  conditions.push(`"source" = $${paramIndex++}`);
17576
17692
  queryParams.push(source);
17577
17693
  }
17694
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
17578
17695
  const whereClause = conditions.join(" AND ");
17579
17696
  const total = await this.#db.client.oneOrNone(
17580
17697
  `SELECT COUNT(*) FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE ${whereClause}`,
@@ -17688,12 +17805,17 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17688
17805
  }
17689
17806
  async listScoresByRunId({
17690
17807
  runId,
17691
- pagination
17808
+ pagination,
17809
+ filters
17692
17810
  }) {
17693
17811
  try {
17812
+ const conditions = [`"runId" = $1`];
17813
+ const queryParams = [runId];
17814
+ let paramIndex = applyTenancyFilters(conditions, queryParams, 2, filters);
17815
+ const whereClause = conditions.join(" AND ");
17694
17816
  const total = await this.#db.client.oneOrNone(
17695
- `SELECT COUNT(*) FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE "runId" = $1`,
17696
- [runId]
17817
+ `SELECT COUNT(*) FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE ${whereClause}`,
17818
+ queryParams
17697
17819
  );
17698
17820
  const { page, perPage: perPageInput } = pagination;
17699
17821
  const perPage = normalizePerPage(perPageInput, 100);
@@ -17712,8 +17834,8 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17712
17834
  const limitValue = perPageInput === false ? Number(total?.count) : perPage;
17713
17835
  const end = perPageInput === false ? Number(total?.count) : start + perPage;
17714
17836
  const result = await this.#db.client.manyOrNone(
17715
- `SELECT * FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE "runId" = $1 LIMIT $2 OFFSET $3`,
17716
- [runId, limitValue, start]
17837
+ `SELECT * FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
17838
+ [...queryParams, limitValue, start]
17717
17839
  );
17718
17840
  return {
17719
17841
  pagination: {
@@ -17738,12 +17860,17 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17738
17860
  async listScoresByEntityId({
17739
17861
  entityId,
17740
17862
  entityType,
17741
- pagination
17863
+ pagination,
17864
+ filters
17742
17865
  }) {
17743
17866
  try {
17867
+ const conditions = [`"entityId" = $1`, `"entityType" = $2`];
17868
+ const queryParams = [entityId, entityType];
17869
+ let paramIndex = applyTenancyFilters(conditions, queryParams, 3, filters);
17870
+ const whereClause = conditions.join(" AND ");
17744
17871
  const total = await this.#db.client.oneOrNone(
17745
- `SELECT COUNT(*) FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2`,
17746
- [entityId, entityType]
17872
+ `SELECT COUNT(*) FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE ${whereClause}`,
17873
+ queryParams
17747
17874
  );
17748
17875
  const { page, perPage: perPageInput } = pagination;
17749
17876
  const perPage = normalizePerPage(perPageInput, 100);
@@ -17762,8 +17889,8 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17762
17889
  const limitValue = perPageInput === false ? Number(total?.count) : perPage;
17763
17890
  const end = perPageInput === false ? Number(total?.count) : start + perPage;
17764
17891
  const result = await this.#db.client.manyOrNone(
17765
- `SELECT * FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2 LIMIT $3 OFFSET $4`,
17766
- [entityId, entityType, limitValue, start]
17892
+ `SELECT * FROM ${getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
17893
+ [...queryParams, limitValue, start]
17767
17894
  );
17768
17895
  return {
17769
17896
  pagination: {
@@ -17788,13 +17915,18 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17788
17915
  async listScoresBySpan({
17789
17916
  traceId,
17790
17917
  spanId,
17791
- pagination
17918
+ pagination,
17919
+ filters
17792
17920
  }) {
17793
17921
  try {
17794
17922
  const tableName = getTableName6({ indexName: TABLE_SCORERS, schemaName: getSchemaName6(this.#schema) });
17923
+ const conditions = [`"traceId" = $1`, `"spanId" = $2`];
17924
+ const queryParams = [traceId, spanId];
17925
+ let paramIndex = applyTenancyFilters(conditions, queryParams, 3, filters);
17926
+ const whereClause = conditions.join(" AND ");
17795
17927
  const countSQLResult = await this.#db.client.oneOrNone(
17796
- `SELECT COUNT(*) as count FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2`,
17797
- [traceId, spanId]
17928
+ `SELECT COUNT(*) as count FROM ${tableName} WHERE ${whereClause}`,
17929
+ queryParams
17798
17930
  );
17799
17931
  const total = Number(countSQLResult?.count ?? 0);
17800
17932
  const { page, perPage: perPageInput } = pagination;
@@ -17803,8 +17935,8 @@ var ScoresPG = class _ScoresPG extends ScoresStorage {
17803
17935
  const limitValue = perPageInput === false ? total : perPage;
17804
17936
  const end = perPageInput === false ? total : start + perPage;
17805
17937
  const result = await this.#db.client.manyOrNone(
17806
- `SELECT * FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2 ORDER BY "createdAt" DESC LIMIT $3 OFFSET $4`,
17807
- [traceId, spanId, limitValue, start]
17938
+ `SELECT * FROM ${tableName} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
17939
+ [...queryParams, limitValue, start]
17808
17940
  );
17809
17941
  const hasMore = end < total;
17810
17942
  const scores = result.map((row) => transformScoreRow(row)) ?? [];