@mastra/mssql 0.0.0-mssql-store-20250804200341 → 0.0.0-netlify-no-bundle-20251127120354

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.
Files changed (43) hide show
  1. package/CHANGELOG.md +938 -3
  2. package/README.md +324 -37
  3. package/dist/index.cjs +1741 -688
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.ts +2 -1
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +1743 -690
  8. package/dist/index.js.map +1 -1
  9. package/dist/storage/domains/memory/index.d.ts +16 -37
  10. package/dist/storage/domains/memory/index.d.ts.map +1 -1
  11. package/dist/storage/domains/observability/index.d.ts +44 -0
  12. package/dist/storage/domains/observability/index.d.ts.map +1 -0
  13. package/dist/storage/domains/operations/index.d.ts +67 -4
  14. package/dist/storage/domains/operations/index.d.ts.map +1 -1
  15. package/dist/storage/domains/scores/index.d.ts +15 -6
  16. package/dist/storage/domains/scores/index.d.ts.map +1 -1
  17. package/dist/storage/domains/utils.d.ts +19 -0
  18. package/dist/storage/domains/utils.d.ts.map +1 -1
  19. package/dist/storage/domains/workflows/index.d.ts +25 -11
  20. package/dist/storage/domains/workflows/index.d.ts.map +1 -1
  21. package/dist/storage/index.d.ts +91 -73
  22. package/dist/storage/index.d.ts.map +1 -1
  23. package/package.json +31 -12
  24. package/dist/storage/domains/legacy-evals/index.d.ts +0 -20
  25. package/dist/storage/domains/legacy-evals/index.d.ts.map +0 -1
  26. package/dist/storage/domains/traces/index.d.ts +0 -37
  27. package/dist/storage/domains/traces/index.d.ts.map +0 -1
  28. package/docker-compose.yaml +0 -14
  29. package/eslint.config.js +0 -6
  30. package/src/index.ts +0 -2
  31. package/src/storage/domains/legacy-evals/index.ts +0 -175
  32. package/src/storage/domains/memory/index.ts +0 -1024
  33. package/src/storage/domains/operations/index.ts +0 -401
  34. package/src/storage/domains/scores/index.ts +0 -289
  35. package/src/storage/domains/traces/index.ts +0 -212
  36. package/src/storage/domains/utils.ts +0 -12
  37. package/src/storage/domains/workflows/index.ts +0 -259
  38. package/src/storage/index.test.ts +0 -2228
  39. package/src/storage/index.ts +0 -448
  40. package/tsconfig.build.json +0 -9
  41. package/tsconfig.json +0 -5
  42. package/tsup.config.ts +0 -22
  43. package/vitest.config.ts +0 -12
@@ -1,212 +0,0 @@
1
- import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
2
- import type { PaginationInfo, PaginationArgs } from '@mastra/core/storage';
3
- import { TABLE_TRACES, TracesStorage } from '@mastra/core/storage';
4
- import { parseFieldKey } from '@mastra/core/utils';
5
- import sql from 'mssql';
6
- import type { StoreOperationsMSSQL } from '../operations';
7
- import { getSchemaName, getTableName } from '../utils';
8
-
9
- export class TracesMSSQL extends TracesStorage {
10
- public pool: sql.ConnectionPool;
11
- private operations: StoreOperationsMSSQL;
12
- private schema?: string;
13
-
14
- constructor({
15
- pool,
16
- operations,
17
- schema,
18
- }: {
19
- pool: sql.ConnectionPool;
20
- operations: StoreOperationsMSSQL;
21
- schema?: string;
22
- }) {
23
- super();
24
- this.pool = pool;
25
- this.operations = operations;
26
- this.schema = schema;
27
- }
28
-
29
- /** @deprecated use getTracesPaginated instead*/
30
- public async getTraces(args: {
31
- name?: string;
32
- scope?: string;
33
- attributes?: Record<string, string>;
34
- filters?: Record<string, any>;
35
- page: number;
36
- perPage?: number;
37
- fromDate?: Date;
38
- toDate?: Date;
39
- }): Promise<any[]> {
40
- if (args.fromDate || args.toDate) {
41
- (args as any).dateRange = {
42
- start: args.fromDate,
43
- end: args.toDate,
44
- };
45
- }
46
- const result = await this.getTracesPaginated(args);
47
- return result.traces;
48
- }
49
-
50
- public async getTracesPaginated(
51
- args: {
52
- name?: string;
53
- scope?: string;
54
- attributes?: Record<string, string>;
55
- filters?: Record<string, any>;
56
- } & PaginationArgs,
57
- ): Promise<
58
- PaginationInfo & {
59
- traces: any[];
60
- }
61
- > {
62
- const { name, scope, page = 0, perPage: perPageInput, attributes, filters, dateRange } = args;
63
- const fromDate = dateRange?.start;
64
- const toDate = dateRange?.end;
65
-
66
- const perPage = perPageInput !== undefined ? perPageInput : 100;
67
- const currentOffset = page * perPage;
68
-
69
- const paramMap: Record<string, any> = {};
70
- const conditions: string[] = [];
71
- let paramIndex = 1;
72
-
73
- if (name) {
74
- const paramName = `p${paramIndex++}`;
75
- conditions.push(`[name] LIKE @${paramName}`);
76
- paramMap[paramName] = `${name}%`;
77
- }
78
- if (scope) {
79
- const paramName = `p${paramIndex++}`;
80
- conditions.push(`[scope] = @${paramName}`);
81
- paramMap[paramName] = scope;
82
- }
83
- if (attributes) {
84
- Object.entries(attributes).forEach(([key, value]) => {
85
- const parsedKey = parseFieldKey(key);
86
- const paramName = `p${paramIndex++}`;
87
- conditions.push(`JSON_VALUE([attributes], '$.${parsedKey}') = @${paramName}`);
88
- paramMap[paramName] = value;
89
- });
90
- }
91
- if (filters) {
92
- Object.entries(filters).forEach(([key, value]) => {
93
- const parsedKey = parseFieldKey(key);
94
- const paramName = `p${paramIndex++}`;
95
- conditions.push(`[${parsedKey}] = @${paramName}`);
96
- paramMap[paramName] = value;
97
- });
98
- }
99
- if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
100
- const paramName = `p${paramIndex++}`;
101
- conditions.push(`[createdAt] >= @${paramName}`);
102
- paramMap[paramName] = fromDate.toISOString();
103
- }
104
- if (toDate instanceof Date && !isNaN(toDate.getTime())) {
105
- const paramName = `p${paramIndex++}`;
106
- conditions.push(`[createdAt] <= @${paramName}`);
107
- paramMap[paramName] = toDate.toISOString();
108
- }
109
-
110
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
111
-
112
- const countQuery = `SELECT COUNT(*) as total FROM ${getTableName({ indexName: TABLE_TRACES, schemaName: getSchemaName(this.schema) })} ${whereClause}`;
113
- let total = 0;
114
- try {
115
- const countRequest = this.pool.request();
116
- Object.entries(paramMap).forEach(([key, value]) => {
117
- if (value instanceof Date) {
118
- countRequest.input(key, sql.DateTime, value);
119
- } else {
120
- countRequest.input(key, value);
121
- }
122
- });
123
- const countResult = await countRequest.query(countQuery);
124
- total = parseInt(countResult.recordset[0].total, 10);
125
- } catch (error) {
126
- throw new MastraError(
127
- {
128
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_TRACES_PAGINATED_FAILED_TO_RETRIEVE_TOTAL_COUNT',
129
- domain: ErrorDomain.STORAGE,
130
- category: ErrorCategory.THIRD_PARTY,
131
- details: {
132
- name: args.name ?? '',
133
- scope: args.scope ?? '',
134
- },
135
- },
136
- error,
137
- );
138
- }
139
-
140
- if (total === 0) {
141
- return {
142
- traces: [],
143
- total: 0,
144
- page,
145
- perPage,
146
- hasMore: false,
147
- };
148
- }
149
-
150
- const dataQuery = `SELECT * FROM ${getTableName({ indexName: TABLE_TRACES, schemaName: getSchemaName(this.schema) })} ${whereClause} ORDER BY [seq_id] DESC OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
151
- const dataRequest = this.pool.request();
152
- Object.entries(paramMap).forEach(([key, value]) => {
153
- if (value instanceof Date) {
154
- dataRequest.input(key, sql.DateTime, value);
155
- } else {
156
- dataRequest.input(key, value);
157
- }
158
- });
159
- dataRequest.input('offset', currentOffset);
160
- dataRequest.input('limit', perPage);
161
-
162
- try {
163
- const rowsResult = await dataRequest.query(dataQuery);
164
- const rows = rowsResult.recordset;
165
- const traces = rows.map(row => ({
166
- id: row.id,
167
- parentSpanId: row.parentSpanId,
168
- traceId: row.traceId,
169
- name: row.name,
170
- scope: row.scope,
171
- kind: row.kind,
172
- status: JSON.parse(row.status),
173
- events: JSON.parse(row.events),
174
- links: JSON.parse(row.links),
175
- attributes: JSON.parse(row.attributes),
176
- startTime: row.startTime,
177
- endTime: row.endTime,
178
- other: row.other,
179
- createdAt: row.createdAt,
180
- }));
181
-
182
- return {
183
- traces,
184
- total,
185
- page,
186
- perPage,
187
- hasMore: currentOffset + traces.length < total,
188
- };
189
- } catch (error) {
190
- throw new MastraError(
191
- {
192
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_TRACES_PAGINATED_FAILED_TO_RETRIEVE_TRACES',
193
- domain: ErrorDomain.STORAGE,
194
- category: ErrorCategory.THIRD_PARTY,
195
- details: {
196
- name: args.name ?? '',
197
- scope: args.scope ?? '',
198
- },
199
- },
200
- error,
201
- );
202
- }
203
- }
204
-
205
- async batchTraceInsert({ records }: { records: Record<string, any>[] }): Promise<void> {
206
- this.logger.debug('Batch inserting traces', { count: records.length });
207
- await this.operations.batchInsert({
208
- tableName: TABLE_TRACES,
209
- records,
210
- });
211
- }
212
- }
@@ -1,12 +0,0 @@
1
- import { parseSqlIdentifier } from '@mastra/core/utils';
2
-
3
- export function getSchemaName(schema?: string) {
4
- return schema ? `[${parseSqlIdentifier(schema, 'schema name')}]` : undefined;
5
- }
6
-
7
- export function getTableName({ indexName, schemaName }: { indexName: string; schemaName?: string }) {
8
- const parsedIndexName = parseSqlIdentifier(indexName, 'index name');
9
- const quotedIndexName = `[${parsedIndexName}]`;
10
- const quotedSchemaName = schemaName;
11
- return quotedSchemaName ? `${quotedSchemaName}.${quotedIndexName}` : quotedIndexName;
12
- }
@@ -1,259 +0,0 @@
1
- import type { WorkflowRun, WorkflowRuns, WorkflowRunState } from '@mastra/core';
2
- import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
3
- import { WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';
4
- import sql from 'mssql';
5
- import type { StoreOperationsMSSQL } from '../operations';
6
- import { getSchemaName, getTableName } from '../utils';
7
-
8
- function parseWorkflowRun(row: any): WorkflowRun {
9
- let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;
10
- if (typeof parsedSnapshot === 'string') {
11
- try {
12
- parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;
13
- } catch (e) {
14
- console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
15
- }
16
- }
17
- return {
18
- workflowName: row.workflow_name,
19
- runId: row.run_id,
20
- snapshot: parsedSnapshot,
21
- createdAt: row.createdAt,
22
- updatedAt: row.updatedAt,
23
- resourceId: row.resourceId,
24
- };
25
- }
26
-
27
- export class WorkflowsMSSQL extends WorkflowsStorage {
28
- public pool: sql.ConnectionPool;
29
- private operations: StoreOperationsMSSQL;
30
- private schema: string;
31
-
32
- constructor({
33
- pool,
34
- operations,
35
- schema,
36
- }: {
37
- pool: sql.ConnectionPool;
38
- operations: StoreOperationsMSSQL;
39
- schema: string;
40
- }) {
41
- super();
42
- this.pool = pool;
43
- this.operations = operations;
44
- this.schema = schema;
45
- }
46
-
47
- async persistWorkflowSnapshot({
48
- workflowName,
49
- runId,
50
- snapshot,
51
- }: {
52
- workflowName: string;
53
- runId: string;
54
- snapshot: WorkflowRunState;
55
- }): Promise<void> {
56
- const table = getTableName({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName(this.schema) });
57
- const now = new Date().toISOString();
58
- try {
59
- const request = this.pool.request();
60
- request.input('workflow_name', workflowName);
61
- request.input('run_id', runId);
62
- request.input('snapshot', JSON.stringify(snapshot));
63
- request.input('createdAt', sql.DateTime2, new Date(now));
64
- request.input('updatedAt', sql.DateTime2, new Date(now));
65
- const mergeSql = `MERGE INTO ${table} AS target
66
- USING (SELECT @workflow_name AS workflow_name, @run_id AS run_id) AS src
67
- ON target.workflow_name = src.workflow_name AND target.run_id = src.run_id
68
- WHEN MATCHED THEN UPDATE SET
69
- snapshot = @snapshot,
70
- [updatedAt] = @updatedAt
71
- WHEN NOT MATCHED THEN INSERT (workflow_name, run_id, snapshot, [createdAt], [updatedAt])
72
- VALUES (@workflow_name, @run_id, @snapshot, @createdAt, @updatedAt);`;
73
- await request.query(mergeSql);
74
- } catch (error) {
75
- throw new MastraError(
76
- {
77
- id: 'MASTRA_STORAGE_MSSQL_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED',
78
- domain: ErrorDomain.STORAGE,
79
- category: ErrorCategory.THIRD_PARTY,
80
- details: {
81
- workflowName,
82
- runId,
83
- },
84
- },
85
- error,
86
- );
87
- }
88
- }
89
-
90
- async loadWorkflowSnapshot({
91
- workflowName,
92
- runId,
93
- }: {
94
- workflowName: string;
95
- runId: string;
96
- }): Promise<WorkflowRunState | null> {
97
- try {
98
- const result = await this.operations.load({
99
- tableName: TABLE_WORKFLOW_SNAPSHOT,
100
- keys: {
101
- workflow_name: workflowName,
102
- run_id: runId,
103
- },
104
- });
105
- if (!result) {
106
- return null;
107
- }
108
- return (result as any).snapshot;
109
- } catch (error) {
110
- throw new MastraError(
111
- {
112
- id: 'MASTRA_STORAGE_MSSQL_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED',
113
- domain: ErrorDomain.STORAGE,
114
- category: ErrorCategory.THIRD_PARTY,
115
- details: {
116
- workflowName,
117
- runId,
118
- },
119
- },
120
- error,
121
- );
122
- }
123
- }
124
-
125
- async getWorkflowRunById({
126
- runId,
127
- workflowName,
128
- }: {
129
- runId: string;
130
- workflowName?: string;
131
- }): Promise<WorkflowRun | null> {
132
- try {
133
- const conditions: string[] = [];
134
- const paramMap: Record<string, any> = {};
135
-
136
- if (runId) {
137
- conditions.push(`[run_id] = @runId`);
138
- paramMap['runId'] = runId;
139
- }
140
-
141
- if (workflowName) {
142
- conditions.push(`[workflow_name] = @workflowName`);
143
- paramMap['workflowName'] = workflowName;
144
- }
145
-
146
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
147
- const tableName = getTableName({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName(this.schema) });
148
- const query = `SELECT * FROM ${tableName} ${whereClause}`;
149
- const request = this.pool.request();
150
- Object.entries(paramMap).forEach(([key, value]) => request.input(key, value));
151
- const result = await request.query(query);
152
-
153
- if (!result.recordset || result.recordset.length === 0) {
154
- return null;
155
- }
156
-
157
- return parseWorkflowRun(result.recordset[0]);
158
- } catch (error) {
159
- throw new MastraError(
160
- {
161
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED',
162
- domain: ErrorDomain.STORAGE,
163
- category: ErrorCategory.THIRD_PARTY,
164
- details: {
165
- runId,
166
- workflowName: workflowName || '',
167
- },
168
- },
169
- error,
170
- );
171
- }
172
- }
173
-
174
- async getWorkflowRuns({
175
- workflowName,
176
- fromDate,
177
- toDate,
178
- limit,
179
- offset,
180
- resourceId,
181
- }: {
182
- workflowName?: string;
183
- fromDate?: Date;
184
- toDate?: Date;
185
- limit?: number;
186
- offset?: number;
187
- resourceId?: string;
188
- } = {}): Promise<WorkflowRuns> {
189
- try {
190
- const conditions: string[] = [];
191
- const paramMap: Record<string, any> = {};
192
-
193
- if (workflowName) {
194
- conditions.push(`[workflow_name] = @workflowName`);
195
- paramMap['workflowName'] = workflowName;
196
- }
197
-
198
- if (resourceId) {
199
- const hasResourceId = await this.operations.hasColumn(TABLE_WORKFLOW_SNAPSHOT, 'resourceId');
200
- if (hasResourceId) {
201
- conditions.push(`[resourceId] = @resourceId`);
202
- paramMap['resourceId'] = resourceId;
203
- } else {
204
- console.warn(`[${TABLE_WORKFLOW_SNAPSHOT}] resourceId column not found. Skipping resourceId filter.`);
205
- }
206
- }
207
-
208
- if (fromDate instanceof Date && !isNaN(fromDate.getTime())) {
209
- conditions.push(`[createdAt] >= @fromDate`);
210
- paramMap[`fromDate`] = fromDate.toISOString();
211
- }
212
-
213
- if (toDate instanceof Date && !isNaN(toDate.getTime())) {
214
- conditions.push(`[createdAt] <= @toDate`);
215
- paramMap[`toDate`] = toDate.toISOString();
216
- }
217
-
218
- const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
219
- let total = 0;
220
- const tableName = getTableName({ indexName: TABLE_WORKFLOW_SNAPSHOT, schemaName: getSchemaName(this.schema) });
221
- const request = this.pool.request();
222
- Object.entries(paramMap).forEach(([key, value]) => {
223
- if (value instanceof Date) {
224
- request.input(key, sql.DateTime, value);
225
- } else {
226
- request.input(key, value);
227
- }
228
- });
229
-
230
- if (limit !== undefined && offset !== undefined) {
231
- const countQuery = `SELECT COUNT(*) as count FROM ${tableName} ${whereClause}`;
232
- const countResult = await request.query(countQuery);
233
- total = Number(countResult.recordset[0]?.count || 0);
234
- }
235
-
236
- let query = `SELECT * FROM ${tableName} ${whereClause} ORDER BY [seq_id] DESC`;
237
- if (limit !== undefined && offset !== undefined) {
238
- query += ` OFFSET @offset ROWS FETCH NEXT @limit ROWS ONLY`;
239
- request.input('limit', limit);
240
- request.input('offset', offset);
241
- }
242
- const result = await request.query(query);
243
- const runs = (result.recordset || []).map(row => parseWorkflowRun(row));
244
- return { runs, total: total || runs.length };
245
- } catch (error) {
246
- throw new MastraError(
247
- {
248
- id: 'MASTRA_STORAGE_MSSQL_STORE_GET_WORKFLOW_RUNS_FAILED',
249
- domain: ErrorDomain.STORAGE,
250
- category: ErrorCategory.THIRD_PARTY,
251
- details: {
252
- workflowName: workflowName || 'all',
253
- },
254
- },
255
- error,
256
- );
257
- }
258
- }
259
- }