@mastra/dsql 1.1.1 → 1.1.2

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
@@ -3564,6 +3564,17 @@ function transformScoreRow(row) {
3564
3564
  }
3565
3565
  });
3566
3566
  }
3567
+ function applyTenancyFilters(conditions, queryParams, paramIndex, filters) {
3568
+ if (filters?.organizationId !== void 0) {
3569
+ conditions.push(`"organizationId" = $${paramIndex++}`);
3570
+ queryParams.push(filters.organizationId);
3571
+ }
3572
+ if (filters?.projectId !== void 0) {
3573
+ conditions.push(`"projectId" = $${paramIndex++}`);
3574
+ queryParams.push(filters.projectId);
3575
+ }
3576
+ return paramIndex;
3577
+ }
3567
3578
  var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3568
3579
  #db;
3569
3580
  #schema;
@@ -3581,6 +3592,11 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3581
3592
  }
3582
3593
  async init() {
3583
3594
  await this.#db.createTable({ tableName: TABLE_SCORERS, schema: TABLE_SCHEMAS[TABLE_SCORERS] });
3595
+ await this.#db.alterTable({
3596
+ tableName: TABLE_SCORERS,
3597
+ schema: TABLE_SCHEMAS[TABLE_SCORERS],
3598
+ ifNotExists: ["organizationId", "projectId", "batchId", "datasetId", "datasetItemId"]
3599
+ });
3584
3600
  await this.createDefaultIndexes();
3585
3601
  await this.createCustomIndexes();
3586
3602
  }
@@ -3654,7 +3670,8 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3654
3670
  pagination,
3655
3671
  entityId,
3656
3672
  entityType,
3657
- source
3673
+ source,
3674
+ filters
3658
3675
  }) {
3659
3676
  try {
3660
3677
  const conditions = [`"scorerId" = $1`];
@@ -3672,6 +3689,7 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3672
3689
  conditions.push(`"source" = $${paramIndex++}`);
3673
3690
  queryParams.push(source);
3674
3691
  }
3692
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3675
3693
  const whereClause = conditions.join(" AND ");
3676
3694
  const total = await this.#db.client.oneOrNone(
3677
3695
  `SELECT COUNT(*) FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause}`,
@@ -3785,12 +3803,18 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3785
3803
  }
3786
3804
  async listScoresByRunId({
3787
3805
  runId,
3788
- pagination
3806
+ pagination,
3807
+ filters
3789
3808
  }) {
3790
3809
  try {
3810
+ const conditions = [`"runId" = $1`];
3811
+ const queryParams = [runId];
3812
+ let paramIndex = 2;
3813
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3814
+ const whereClause = conditions.join(" AND ");
3791
3815
  const total = await this.#db.client.oneOrNone(
3792
- `SELECT COUNT(*) FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "runId" = $1`,
3793
- [runId]
3816
+ `SELECT COUNT(*) FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause}`,
3817
+ queryParams
3794
3818
  );
3795
3819
  const { page, perPage: perPageInput } = pagination;
3796
3820
  const perPage = normalizePerPage(perPageInput, 100);
@@ -3809,8 +3833,8 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3809
3833
  const limitValue = perPageInput === false ? Number(total?.count) : perPage;
3810
3834
  const end = perPageInput === false ? Number(total?.count) : start + perPage;
3811
3835
  const result = await this.#db.client.manyOrNone(
3812
- `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "runId" = $1 ORDER BY "createdAt" DESC LIMIT $2 OFFSET $3`,
3813
- [runId, limitValue, start]
3836
+ `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
3837
+ [...queryParams, limitValue, start]
3814
3838
  );
3815
3839
  return {
3816
3840
  pagination: {
@@ -3835,12 +3859,18 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3835
3859
  async listScoresByEntityId({
3836
3860
  entityId,
3837
3861
  entityType,
3838
- pagination
3862
+ pagination,
3863
+ filters
3839
3864
  }) {
3840
3865
  try {
3866
+ const conditions = [`"entityId" = $1`, `"entityType" = $2`];
3867
+ const queryParams = [entityId, entityType];
3868
+ let paramIndex = 3;
3869
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3870
+ const whereClause = conditions.join(" AND ");
3841
3871
  const total = await this.#db.client.oneOrNone(
3842
- `SELECT COUNT(*) FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2`,
3843
- [entityId, entityType]
3872
+ `SELECT COUNT(*) FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause}`,
3873
+ queryParams
3844
3874
  );
3845
3875
  const { page, perPage: perPageInput } = pagination;
3846
3876
  const perPage = normalizePerPage(perPageInput, 100);
@@ -3859,8 +3889,8 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3859
3889
  const limitValue = perPageInput === false ? Number(total?.count) : perPage;
3860
3890
  const end = perPageInput === false ? Number(total?.count) : start + perPage;
3861
3891
  const result = await this.#db.client.manyOrNone(
3862
- `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE "entityId" = $1 AND "entityType" = $2 ORDER BY "createdAt" DESC LIMIT $3 OFFSET $4`,
3863
- [entityId, entityType, limitValue, start]
3892
+ `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) })} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
3893
+ [...queryParams, limitValue, start]
3864
3894
  );
3865
3895
  return {
3866
3896
  pagination: {
@@ -3885,13 +3915,19 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3885
3915
  async listScoresBySpan({
3886
3916
  traceId,
3887
3917
  spanId,
3888
- pagination
3918
+ pagination,
3919
+ filters
3889
3920
  }) {
3890
3921
  try {
3891
3922
  const tableName = getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.#schema) });
3923
+ const conditions = [`"traceId" = $1`, `"spanId" = $2`];
3924
+ const queryParams = [traceId, spanId];
3925
+ let paramIndex = 3;
3926
+ paramIndex = applyTenancyFilters(conditions, queryParams, paramIndex, filters);
3927
+ const whereClause = conditions.join(" AND ");
3892
3928
  const countSQLResult = await this.#db.client.oneOrNone(
3893
- `SELECT COUNT(*) as count FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2`,
3894
- [traceId, spanId]
3929
+ `SELECT COUNT(*) as count FROM ${tableName} WHERE ${whereClause}`,
3930
+ queryParams
3895
3931
  );
3896
3932
  const total = Number(countSQLResult?.count ?? 0);
3897
3933
  const { page, perPage: perPageInput } = pagination;
@@ -3900,8 +3936,8 @@ var ScoresDSQL = class _ScoresDSQL extends ScoresStorage {
3900
3936
  const limitValue = perPageInput === false ? total : perPage;
3901
3937
  const end = perPageInput === false ? total : start + perPage;
3902
3938
  const result = await this.#db.client.manyOrNone(
3903
- `SELECT * FROM ${tableName} WHERE "traceId" = $1 AND "spanId" = $2 ORDER BY "createdAt" DESC LIMIT $3 OFFSET $4`,
3904
- [traceId, spanId, limitValue, start]
3939
+ `SELECT * FROM ${tableName} WHERE ${whereClause} ORDER BY "createdAt" DESC LIMIT $${paramIndex++} OFFSET $${paramIndex++}`,
3940
+ [...queryParams, limitValue, start]
3905
3941
  );
3906
3942
  const hasMore = end < total;
3907
3943
  const scores = result.map((row) => transformScoreRow(row)) ?? [];