@mastra/mssql 1.5.1-alpha.0 → 1.6.0-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
@@ -1,5 +1,5 @@
1
1
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
2
- import { AGENTS_SCHEMA, AGENT_VERSIONS_SCHEMA, AgentsStorage, BackgroundTasksStorage, MastraCompositeStore, MemoryStorage, ObservabilityStorage, SPAN_SCHEMA, ScoresStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, TraceStatus, WorkflowsStorage, calculatePagination, createStorageErrorId, getDefaultValue, listTracesArgsSchema, normalizePerPage, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
2
+ import { AGENTS_SCHEMA, AGENT_VERSIONS_SCHEMA, AgentsStorage, BackgroundTasksStorage, MastraCompositeStore, MemoryStorage, ObservabilityStorage, SPAN_SCHEMA, ScoresStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_SNAPSHOT, TraceStatus, WORKFLOW_DEFINITIONS_SCHEMA, WorkflowDefinitionsStorage, WorkflowsStorage, calculatePagination, createStorageErrorId, getDefaultValue, listTracesArgsSchema, normalizePerPage, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
3
3
  import sql from "mssql";
4
4
  import { randomUUID } from "crypto";
5
5
  import { parseSqlIdentifier } from "@mastra/core/utils";
@@ -4087,6 +4087,197 @@ var ScoresMSSQL = class ScoresMSSQL extends ScoresStorage {
4087
4087
  }
4088
4088
  };
4089
4089
  //#endregion
4090
+ //#region src/storage/domains/workflow-definitions/index.ts
4091
+ function parseJson(value, column, rowId) {
4092
+ if (value == null) return null;
4093
+ if (typeof value === "string") try {
4094
+ return JSON.parse(value);
4095
+ } catch {
4096
+ throw new Error(`Workflow definition row "${String(rowId)}" has malformed JSON in column "${column}".`);
4097
+ }
4098
+ return value;
4099
+ }
4100
+ function rowToDefinition(row) {
4101
+ const inputSchema = parseJson(row.inputSchema, "inputSchema", row.id);
4102
+ const outputSchema = parseJson(row.outputSchema, "outputSchema", row.id);
4103
+ const graph = parseJson(row.graph, "graph", row.id);
4104
+ if (inputSchema == null || outputSchema == null || graph == null) throw new Error(`Workflow definition row "${String(row.id)}" is missing required JSON columns.`);
4105
+ const def = {
4106
+ id: String(row.id),
4107
+ inputSchema,
4108
+ outputSchema,
4109
+ graph,
4110
+ status: String(row.status),
4111
+ source: String(row.source),
4112
+ createdAt: row.createdAt instanceof Date ? row.createdAt : new Date(row.createdAt),
4113
+ updatedAt: row.updatedAt instanceof Date ? row.updatedAt : new Date(row.updatedAt)
4114
+ };
4115
+ if (row.description != null) def.description = String(row.description);
4116
+ const metadata = parseJson(row.metadata, "metadata", row.id);
4117
+ if (metadata != null) def.metadata = metadata;
4118
+ const stateSchema = parseJson(row.stateSchema, "stateSchema", row.id);
4119
+ if (stateSchema != null) def.stateSchema = stateSchema;
4120
+ const requestContextSchema = parseJson(row.requestContextSchema, "requestContextSchema", row.id);
4121
+ if (requestContextSchema != null) def.requestContextSchema = requestContextSchema;
4122
+ if (row.authorId != null) def.authorId = String(row.authorId);
4123
+ return def;
4124
+ }
4125
+ var WorkflowDefinitionsMSSQL = class WorkflowDefinitionsMSSQL extends WorkflowDefinitionsStorage {
4126
+ pool;
4127
+ schema;
4128
+ db;
4129
+ needsConnect;
4130
+ skipDefaultIndexes;
4131
+ indexes;
4132
+ static MANAGED_TABLES = [TABLE_WORKFLOW_DEFINITIONS];
4133
+ constructor(config) {
4134
+ super();
4135
+ const { pool, schemaName, skipDefaultIndexes, indexes, needsConnect } = resolveMssqlConfig(config);
4136
+ this.pool = pool;
4137
+ this.schema = schemaName;
4138
+ this.db = new MssqlDB({
4139
+ pool,
4140
+ schemaName,
4141
+ skipDefaultIndexes
4142
+ });
4143
+ this.needsConnect = needsConnect;
4144
+ this.skipDefaultIndexes = skipDefaultIndexes;
4145
+ this.indexes = indexes?.filter((idx) => WorkflowDefinitionsMSSQL.MANAGED_TABLES.includes(idx.table));
4146
+ }
4147
+ async init() {
4148
+ if (this.needsConnect) {
4149
+ await this.pool.connect();
4150
+ this.needsConnect = false;
4151
+ }
4152
+ await this.db.createTable({
4153
+ tableName: TABLE_WORKFLOW_DEFINITIONS,
4154
+ schema: WORKFLOW_DEFINITIONS_SCHEMA
4155
+ });
4156
+ await this.createDefaultIndexes();
4157
+ await this.createCustomIndexes();
4158
+ }
4159
+ getDefaultIndexDefinitions() {
4160
+ return [{
4161
+ name: `${this.schema && this.schema !== "dbo" ? `${this.schema}_` : ""}mastra_workflow_definitions_status_idx`,
4162
+ table: TABLE_WORKFLOW_DEFINITIONS,
4163
+ columns: ["status"]
4164
+ }];
4165
+ }
4166
+ async createDefaultIndexes() {
4167
+ if (this.skipDefaultIndexes) return;
4168
+ for (const indexDef of this.getDefaultIndexDefinitions()) try {
4169
+ await this.db.createIndex(indexDef);
4170
+ } catch (error) {
4171
+ this.logger?.warn?.(`Failed to create index ${indexDef.name}:`, error);
4172
+ }
4173
+ }
4174
+ async createCustomIndexes() {
4175
+ if (!this.indexes || this.indexes.length === 0) return;
4176
+ for (const indexDef of this.indexes) try {
4177
+ await this.db.createIndex(indexDef);
4178
+ } catch (error) {
4179
+ this.logger?.warn?.(`Failed to create custom index ${indexDef.name}:`, error);
4180
+ }
4181
+ }
4182
+ async dangerouslyClearAll() {
4183
+ await this.db.clearTable({ tableName: TABLE_WORKFLOW_DEFINITIONS });
4184
+ }
4185
+ async upsert(input) {
4186
+ const now = /* @__PURE__ */ new Date();
4187
+ if (!await this.get(input.id)) {
4188
+ if (!("inputSchema" in input) || input.inputSchema === void 0) throw new Error(`Cannot create workflow definition "${input.id}": inputSchema is required.`);
4189
+ if (!("outputSchema" in input) || input.outputSchema === void 0) throw new Error(`Cannot create workflow definition "${input.id}": outputSchema is required.`);
4190
+ if (!("graph" in input) || input.graph === void 0) throw new Error(`Cannot create workflow definition "${input.id}": graph is required.`);
4191
+ const record = {
4192
+ id: input.id,
4193
+ description: input.description ?? null,
4194
+ metadata: input.metadata ?? null,
4195
+ inputSchema: input.inputSchema,
4196
+ outputSchema: input.outputSchema,
4197
+ stateSchema: input.stateSchema ?? null,
4198
+ requestContextSchema: input.requestContextSchema ?? null,
4199
+ graph: input.graph,
4200
+ status: "active",
4201
+ source: "storage",
4202
+ authorId: "authorId" in input ? input.authorId ?? null : null,
4203
+ createdAt: now,
4204
+ updatedAt: now
4205
+ };
4206
+ try {
4207
+ await this.db.insert({
4208
+ tableName: TABLE_WORKFLOW_DEFINITIONS,
4209
+ record
4210
+ });
4211
+ } catch (error) {
4212
+ if (!await this.get(input.id)) throw error;
4213
+ return this.applyUpdate(input, now);
4214
+ }
4215
+ const created = await this.get(input.id);
4216
+ if (!created) throw new Error(`Failed to persist workflow definition "${input.id}".`);
4217
+ return created;
4218
+ }
4219
+ return this.applyUpdate(input, now);
4220
+ }
4221
+ async applyUpdate(input, now) {
4222
+ const data = { updatedAt: now };
4223
+ if ("description" in input && input.description !== void 0) data.description = input.description;
4224
+ if ("metadata" in input && input.metadata !== void 0) data.metadata = input.metadata;
4225
+ if ("inputSchema" in input && input.inputSchema !== void 0) data.inputSchema = input.inputSchema;
4226
+ if ("outputSchema" in input && input.outputSchema !== void 0) data.outputSchema = input.outputSchema;
4227
+ if ("stateSchema" in input && input.stateSchema !== void 0) data.stateSchema = input.stateSchema;
4228
+ if ("requestContextSchema" in input && input.requestContextSchema !== void 0) data.requestContextSchema = input.requestContextSchema;
4229
+ if ("graph" in input && input.graph !== void 0) data.graph = input.graph;
4230
+ if ("status" in input && input.status !== void 0) data.status = input.status;
4231
+ if ("authorId" in input && input.authorId !== void 0) data.authorId = input.authorId;
4232
+ await this.db.update({
4233
+ tableName: TABLE_WORKFLOW_DEFINITIONS,
4234
+ keys: { id: input.id },
4235
+ data
4236
+ });
4237
+ const updated = await this.get(input.id);
4238
+ if (!updated) throw new Error(`Failed to update workflow definition "${input.id}".`);
4239
+ return updated;
4240
+ }
4241
+ async get(id) {
4242
+ const row = await this.db.load({
4243
+ tableName: TABLE_WORKFLOW_DEFINITIONS,
4244
+ keys: { id }
4245
+ });
4246
+ return row ? rowToDefinition(row) : null;
4247
+ }
4248
+ async list(args) {
4249
+ const tableName = getTableName({
4250
+ indexName: TABLE_WORKFLOW_DEFINITIONS,
4251
+ schemaName: getSchemaName(this.schema)
4252
+ });
4253
+ const request = this.pool.request();
4254
+ const conditions = [];
4255
+ if (args?.status) {
4256
+ request.input("status", args.status);
4257
+ conditions.push(`[status] = @status`);
4258
+ }
4259
+ if (args?.authorId !== void 0) {
4260
+ request.input("authorId", args.authorId);
4261
+ conditions.push(`[authorId] = @authorId`);
4262
+ }
4263
+ const where = conditions.length ? `WHERE ${conditions.join(" AND ")}` : "";
4264
+ const definitions = (await request.query(`SELECT * FROM ${tableName} ${where} ORDER BY [updatedAt] DESC`)).recordset.map(rowToDefinition);
4265
+ return {
4266
+ definitions,
4267
+ total: definitions.length
4268
+ };
4269
+ }
4270
+ async delete(id) {
4271
+ const tableName = getTableName({
4272
+ indexName: TABLE_WORKFLOW_DEFINITIONS,
4273
+ schemaName: getSchemaName(this.schema)
4274
+ });
4275
+ const request = this.pool.request();
4276
+ request.input("id", id);
4277
+ await request.query(`DELETE FROM ${tableName} WHERE [id] = @id`);
4278
+ }
4279
+ };
4280
+ //#endregion
4090
4281
  //#region src/storage/domains/workflows/index.ts
4091
4282
  var WorkflowsMSSQL = class WorkflowsMSSQL extends WorkflowsStorage {
4092
4283
  pool;
@@ -4558,13 +4749,15 @@ var MSSQLStore = class extends MastraCompositeStore {
4558
4749
  const observability = new ObservabilityMSSQL(domainConfig);
4559
4750
  const backgroundTasks = new BackgroundTasksMSSQL(domainConfig);
4560
4751
  const agents = new AgentsMSSQL(domainConfig);
4752
+ const workflowDefinitions = new WorkflowDefinitionsMSSQL(domainConfig);
4561
4753
  this.stores = {
4562
4754
  scores,
4563
4755
  workflows,
4564
4756
  memory,
4565
4757
  observability,
4566
4758
  backgroundTasks,
4567
- agents
4759
+ agents,
4760
+ workflowDefinitions
4568
4761
  };
4569
4762
  } catch (e) {
4570
4763
  throw new MastraError({
@@ -4607,6 +4800,6 @@ var MSSQLStore = class extends MastraCompositeStore {
4607
4800
  }
4608
4801
  };
4609
4802
  //#endregion
4610
- export { AgentsMSSQL, BackgroundTasksMSSQL, MSSQLStore, MemoryMSSQL, ObservabilityMSSQL, ScoresMSSQL, WorkflowsMSSQL };
4803
+ export { AgentsMSSQL, BackgroundTasksMSSQL, MSSQLStore, MemoryMSSQL, ObservabilityMSSQL, ScoresMSSQL, WorkflowDefinitionsMSSQL, WorkflowsMSSQL };
4611
4804
 
4612
4805
  //# sourceMappingURL=index.js.map