@mastra/cloudflare-d1 1.1.0 → 1.1.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
@@ -1865,6 +1865,14 @@ var MemoryStorageD1 = class extends MemoryStorage {
1865
1865
  }
1866
1866
  }
1867
1867
  };
1868
+ function applyTenancyFilters(query, filters) {
1869
+ if (filters?.organizationId !== void 0) {
1870
+ query.andWhere("organizationId = ?", filters.organizationId);
1871
+ }
1872
+ if (filters?.projectId !== void 0) {
1873
+ query.andWhere("projectId = ?", filters.projectId);
1874
+ }
1875
+ }
1868
1876
  function transformScoreRow(row) {
1869
1877
  return transformScoreRow$1(row, {
1870
1878
  preferredTimestampFields: {
@@ -1881,6 +1889,11 @@ var ScoresStorageD1 = class extends ScoresStorage {
1881
1889
  }
1882
1890
  async init() {
1883
1891
  await this.#db.createTable({ tableName: TABLE_SCORERS, schema: TABLE_SCHEMAS[TABLE_SCORERS] });
1892
+ await this.#db.alterTable({
1893
+ tableName: TABLE_SCORERS,
1894
+ schema: TABLE_SCHEMAS[TABLE_SCORERS],
1895
+ ifNotExists: ["organizationId", "projectId", "batchId", "datasetId", "datasetItemId"]
1896
+ });
1884
1897
  }
1885
1898
  async dangerouslyClearAll() {
1886
1899
  await this.#db.clearTable({ tableName: TABLE_SCORERS });
@@ -1969,7 +1982,8 @@ var ScoresStorageD1 = class extends ScoresStorage {
1969
1982
  entityId,
1970
1983
  entityType,
1971
1984
  source,
1972
- pagination
1985
+ pagination,
1986
+ filters
1973
1987
  }) {
1974
1988
  try {
1975
1989
  const { page, perPage: perPageInput } = pagination;
@@ -1986,6 +2000,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
1986
2000
  if (source) {
1987
2001
  countQuery.andWhere("source = ?", source);
1988
2002
  }
2003
+ applyTenancyFilters(countQuery, filters);
1989
2004
  const countResult = await this.#db.executeQuery(countQuery.build());
1990
2005
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
1991
2006
  if (total === 0) {
@@ -2011,7 +2026,8 @@ var ScoresStorageD1 = class extends ScoresStorage {
2011
2026
  if (source) {
2012
2027
  selectQuery.andWhere("source = ?", source);
2013
2028
  }
2014
- selectQuery.limit(limitValue).offset(start);
2029
+ applyTenancyFilters(selectQuery, filters);
2030
+ selectQuery.orderBy("createdAt", "DESC").limit(limitValue).offset(start);
2015
2031
  const { sql, params } = selectQuery.build();
2016
2032
  const results = await this.#db.executeQuery({ sql, params });
2017
2033
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
@@ -2037,7 +2053,8 @@ var ScoresStorageD1 = class extends ScoresStorage {
2037
2053
  }
2038
2054
  async listScoresByRunId({
2039
2055
  runId,
2040
- pagination
2056
+ pagination,
2057
+ filters
2041
2058
  }) {
2042
2059
  try {
2043
2060
  const { page, perPage: perPageInput } = pagination;
@@ -2045,6 +2062,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
2045
2062
  const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2046
2063
  const fullTableName = this.#db.getTableName(TABLE_SCORERS);
2047
2064
  const countQuery = createSqlBuilder().count().from(fullTableName).where("runId = ?", runId);
2065
+ applyTenancyFilters(countQuery, filters);
2048
2066
  const countResult = await this.#db.executeQuery(countQuery.build());
2049
2067
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
2050
2068
  if (total === 0) {
@@ -2060,7 +2078,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
2060
2078
  }
2061
2079
  const end = perPageInput === false ? total : start + perPage;
2062
2080
  const limitValue = perPageInput === false ? total : perPage;
2063
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId).limit(limitValue).offset(start);
2081
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("runId = ?", runId);
2082
+ applyTenancyFilters(selectQuery, filters);
2083
+ selectQuery.orderBy("createdAt", "DESC").limit(limitValue).offset(start);
2064
2084
  const { sql, params } = selectQuery.build();
2065
2085
  const results = await this.#db.executeQuery({ sql, params });
2066
2086
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
@@ -2087,7 +2107,8 @@ var ScoresStorageD1 = class extends ScoresStorage {
2087
2107
  async listScoresByEntityId({
2088
2108
  entityId,
2089
2109
  entityType,
2090
- pagination
2110
+ pagination,
2111
+ filters
2091
2112
  }) {
2092
2113
  try {
2093
2114
  const { page, perPage: perPageInput } = pagination;
@@ -2095,6 +2116,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
2095
2116
  const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2096
2117
  const fullTableName = this.#db.getTableName(TABLE_SCORERS);
2097
2118
  const countQuery = createSqlBuilder().count().from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
2119
+ applyTenancyFilters(countQuery, filters);
2098
2120
  const countResult = await this.#db.executeQuery(countQuery.build());
2099
2121
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
2100
2122
  if (total === 0) {
@@ -2110,7 +2132,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
2110
2132
  }
2111
2133
  const end = perPageInput === false ? total : start + perPage;
2112
2134
  const limitValue = perPageInput === false ? total : perPage;
2113
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType).limit(limitValue).offset(start);
2135
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("entityId = ?", entityId).andWhere("entityType = ?", entityType);
2136
+ applyTenancyFilters(selectQuery, filters);
2137
+ selectQuery.orderBy("createdAt", "DESC").limit(limitValue).offset(start);
2114
2138
  const { sql, params } = selectQuery.build();
2115
2139
  const results = await this.#db.executeQuery({ sql, params });
2116
2140
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];
@@ -2137,7 +2161,8 @@ var ScoresStorageD1 = class extends ScoresStorage {
2137
2161
  async listScoresBySpan({
2138
2162
  traceId,
2139
2163
  spanId,
2140
- pagination
2164
+ pagination,
2165
+ filters
2141
2166
  }) {
2142
2167
  try {
2143
2168
  const { page, perPage: perPageInput } = pagination;
@@ -2145,6 +2170,7 @@ var ScoresStorageD1 = class extends ScoresStorage {
2145
2170
  const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
2146
2171
  const fullTableName = this.#db.getTableName(TABLE_SCORERS);
2147
2172
  const countQuery = createSqlBuilder().count().from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
2173
+ applyTenancyFilters(countQuery, filters);
2148
2174
  const countResult = await this.#db.executeQuery(countQuery.build());
2149
2175
  const total = Array.isArray(countResult) ? Number(countResult?.[0]?.count ?? 0) : Number(countResult?.count ?? 0);
2150
2176
  if (total === 0) {
@@ -2160,7 +2186,9 @@ var ScoresStorageD1 = class extends ScoresStorage {
2160
2186
  }
2161
2187
  const end = perPageInput === false ? total : start + perPage;
2162
2188
  const limitValue = perPageInput === false ? total : perPage;
2163
- const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId).orderBy("createdAt", "DESC").limit(limitValue).offset(start);
2189
+ const selectQuery = createSqlBuilder().select("*").from(fullTableName).where("traceId = ?", traceId).andWhere("spanId = ?", spanId);
2190
+ applyTenancyFilters(selectQuery, filters);
2191
+ selectQuery.orderBy("createdAt", "DESC").limit(limitValue).offset(start);
2164
2192
  const { sql, params } = selectQuery.build();
2165
2193
  const results = await this.#db.executeQuery({ sql, params });
2166
2194
  const scores = Array.isArray(results) ? results.map(transformScoreRow) : [];