@mastra/mssql 1.4.0 → 1.4.1-alpha.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.
package/dist/index.js CHANGED
@@ -4060,6 +4060,19 @@ function transformScoreRow(row) {
4060
4060
  convertTimestamps: true
4061
4061
  });
4062
4062
  }
4063
+ function buildTenancyConditions(filters) {
4064
+ const conditions = [];
4065
+ const params = {};
4066
+ if (filters?.organizationId !== void 0) {
4067
+ conditions.push("[organizationId] = @org");
4068
+ params.org = filters.organizationId;
4069
+ }
4070
+ if (filters?.projectId !== void 0) {
4071
+ conditions.push("[projectId] = @proj");
4072
+ params.proj = filters.projectId;
4073
+ }
4074
+ return { conditions, params };
4075
+ }
4063
4076
  var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4064
4077
  pool;
4065
4078
  db;
@@ -4229,7 +4242,8 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4229
4242
  pagination,
4230
4243
  entityId,
4231
4244
  entityType,
4232
- source
4245
+ source,
4246
+ filters
4233
4247
  }) {
4234
4248
  try {
4235
4249
  const conditions = ["[scorerId] = @p1"];
@@ -4250,6 +4264,9 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4250
4264
  params[`p${paramIndex}`] = source;
4251
4265
  paramIndex++;
4252
4266
  }
4267
+ const tenancy = buildTenancyConditions(filters);
4268
+ conditions.push(...tenancy.conditions);
4269
+ Object.assign(params, tenancy.params);
4253
4270
  const whereClause = conditions.join(" AND ");
4254
4271
  const tableName = getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) });
4255
4272
  const countRequest = this.pool.request();
@@ -4280,7 +4297,7 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4280
4297
  });
4281
4298
  dataRequest.input("perPage", limitValue);
4282
4299
  dataRequest.input("offset", start);
4283
- const dataQuery = `SELECT * FROM ${tableName} WHERE ${whereClause} ORDER BY [createdAt] DESC OFFSET @offset ROWS FETCH NEXT @perPage ROWS ONLY`;
4300
+ const dataQuery = `SELECT * FROM ${tableName} WHERE ${whereClause} ORDER BY [seq_id] DESC OFFSET @offset ROWS FETCH NEXT @perPage ROWS ONLY`;
4284
4301
  const result = await dataRequest.query(dataQuery);
4285
4302
  return {
4286
4303
  pagination: {
@@ -4305,13 +4322,17 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4305
4322
  }
4306
4323
  async listScoresByRunId({
4307
4324
  runId,
4308
- pagination
4325
+ pagination,
4326
+ filters
4309
4327
  }) {
4310
4328
  try {
4329
+ const tenancy = buildTenancyConditions(filters);
4330
+ const tenancySql = tenancy.conditions.length > 0 ? ` AND ${tenancy.conditions.join(" AND ")}` : "";
4311
4331
  const request = this.pool.request();
4312
4332
  request.input("p1", runId);
4333
+ Object.entries(tenancy.params).forEach(([key, value]) => request.input(key, value));
4313
4334
  const totalResult = await request.query(
4314
- `SELECT COUNT(*) as count FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [runId] = @p1`
4335
+ `SELECT COUNT(*) as count FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [runId] = @p1${tenancySql}`
4315
4336
  );
4316
4337
  const total = totalResult.recordset[0]?.count || 0;
4317
4338
  const { page, perPage: perPageInput } = pagination;
@@ -4334,8 +4355,9 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4334
4355
  dataRequest.input("p1", runId);
4335
4356
  dataRequest.input("p2", limitValue);
4336
4357
  dataRequest.input("p3", start);
4358
+ Object.entries(tenancy.params).forEach(([key, value]) => dataRequest.input(key, value));
4337
4359
  const result = await dataRequest.query(
4338
- `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [runId] = @p1 ORDER BY [createdAt] DESC OFFSET @p3 ROWS FETCH NEXT @p2 ROWS ONLY`
4360
+ `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [runId] = @p1${tenancySql} ORDER BY [seq_id] DESC OFFSET @p3 ROWS FETCH NEXT @p2 ROWS ONLY`
4339
4361
  );
4340
4362
  return {
4341
4363
  pagination: {
@@ -4361,14 +4383,18 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4361
4383
  async listScoresByEntityId({
4362
4384
  entityId,
4363
4385
  entityType,
4364
- pagination
4386
+ pagination,
4387
+ filters
4365
4388
  }) {
4366
4389
  try {
4390
+ const tenancy = buildTenancyConditions(filters);
4391
+ const tenancySql = tenancy.conditions.length > 0 ? ` AND ${tenancy.conditions.join(" AND ")}` : "";
4367
4392
  const request = this.pool.request();
4368
4393
  request.input("p1", entityId);
4369
4394
  request.input("p2", entityType);
4395
+ Object.entries(tenancy.params).forEach(([key, value]) => request.input(key, value));
4370
4396
  const totalResult = await request.query(
4371
- `SELECT COUNT(*) as count FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [entityId] = @p1 AND [entityType] = @p2`
4397
+ `SELECT COUNT(*) as count FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [entityId] = @p1 AND [entityType] = @p2${tenancySql}`
4372
4398
  );
4373
4399
  const total = totalResult.recordset[0]?.count || 0;
4374
4400
  const { page, perPage: perPageInput } = pagination;
@@ -4392,8 +4418,9 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4392
4418
  dataRequest.input("p2", entityType);
4393
4419
  dataRequest.input("p3", limitValue);
4394
4420
  dataRequest.input("p4", start);
4421
+ Object.entries(tenancy.params).forEach(([key, value]) => dataRequest.input(key, value));
4395
4422
  const result = await dataRequest.query(
4396
- `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [entityId] = @p1 AND [entityType] = @p2 ORDER BY [createdAt] DESC OFFSET @p4 ROWS FETCH NEXT @p3 ROWS ONLY`
4423
+ `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [entityId] = @p1 AND [entityType] = @p2${tenancySql} ORDER BY [seq_id] DESC OFFSET @p4 ROWS FETCH NEXT @p3 ROWS ONLY`
4397
4424
  );
4398
4425
  return {
4399
4426
  pagination: {
@@ -4419,14 +4446,18 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4419
4446
  async listScoresBySpan({
4420
4447
  traceId,
4421
4448
  spanId,
4422
- pagination
4449
+ pagination,
4450
+ filters
4423
4451
  }) {
4424
4452
  try {
4453
+ const tenancy = buildTenancyConditions(filters);
4454
+ const tenancySql = tenancy.conditions.length > 0 ? ` AND ${tenancy.conditions.join(" AND ")}` : "";
4425
4455
  const request = this.pool.request();
4426
4456
  request.input("p1", traceId);
4427
4457
  request.input("p2", spanId);
4458
+ Object.entries(tenancy.params).forEach(([key, value]) => request.input(key, value));
4428
4459
  const totalResult = await request.query(
4429
- `SELECT COUNT(*) as count FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [traceId] = @p1 AND [spanId] = @p2`
4460
+ `SELECT COUNT(*) as count FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [traceId] = @p1 AND [spanId] = @p2${tenancySql}`
4430
4461
  );
4431
4462
  const total = totalResult.recordset[0]?.count || 0;
4432
4463
  const { page, perPage: perPageInput } = pagination;
@@ -4450,8 +4481,9 @@ var ScoresMSSQL = class _ScoresMSSQL extends ScoresStorage {
4450
4481
  dataRequest.input("p2", spanId);
4451
4482
  dataRequest.input("p3", limitValue);
4452
4483
  dataRequest.input("p4", start);
4484
+ Object.entries(tenancy.params).forEach(([key, value]) => dataRequest.input(key, value));
4453
4485
  const result = await dataRequest.query(
4454
- `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [traceId] = @p1 AND [spanId] = @p2 ORDER BY [createdAt] DESC OFFSET @p4 ROWS FETCH NEXT @p3 ROWS ONLY`
4486
+ `SELECT * FROM ${getTableName2({ indexName: TABLE_SCORERS, schemaName: getSchemaName2(this.schema) })} WHERE [traceId] = @p1 AND [spanId] = @p2${tenancySql} ORDER BY [seq_id] DESC OFFSET @p4 ROWS FETCH NEXT @p3 ROWS ONLY`
4455
4487
  );
4456
4488
  return {
4457
4489
  pagination: {