@mastra/mssql 0.3.5 → 0.3.6-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.
@@ -1,494 +0,0 @@
1
- import type { MastraMessageContentV2, MastraMessageV2 } from '@mastra/core/agent';
2
- export type MastraMessageV2WithTypedContent = Omit<MastraMessageV2, 'content'> & { content: MastraMessageContentV2 };
3
- import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
4
- import type { MastraMessageV1, StorageThreadType } from '@mastra/core/memory';
5
- import type { ScoreRowData } from '@mastra/core/scores';
6
- import { MastraStorage } from '@mastra/core/storage';
7
- import type {
8
- EvalRow,
9
- PaginationInfo,
10
- StorageColumn,
11
- StorageGetMessagesArg,
12
- StorageResourceType,
13
- TABLE_NAMES,
14
- WorkflowRun,
15
- WorkflowRuns,
16
- PaginationArgs,
17
- StoragePagination,
18
- ThreadSortOptions,
19
- StorageDomains,
20
- StorageGetTracesArg,
21
- StorageGetTracesPaginatedArg,
22
- } from '@mastra/core/storage';
23
- import type { Trace } from '@mastra/core/telemetry';
24
- import type { StepResult, WorkflowRunState } from '@mastra/core/workflows';
25
- import sql from 'mssql';
26
- import { LegacyEvalsMSSQL } from './domains/legacy-evals';
27
- import { MemoryMSSQL } from './domains/memory';
28
- import { StoreOperationsMSSQL } from './domains/operations';
29
- import { ScoresMSSQL } from './domains/scores';
30
- import { TracesMSSQL } from './domains/traces';
31
- import { WorkflowsMSSQL } from './domains/workflows';
32
-
33
- export type MSSQLConfigType = {
34
- schemaName?: string;
35
- } & (
36
- | {
37
- server: string;
38
- port: number;
39
- database: string;
40
- user: string;
41
- password: string;
42
- options?: sql.IOptions;
43
- }
44
- | {
45
- connectionString: string;
46
- }
47
- );
48
-
49
- export type MSSQLConfig = MSSQLConfigType;
50
-
51
- export class MSSQLStore extends MastraStorage {
52
- public pool: sql.ConnectionPool;
53
- private schema?: string;
54
- private isConnected: Promise<boolean> | null = null;
55
- stores: StorageDomains;
56
-
57
- constructor(config: MSSQLConfigType) {
58
- super({ name: 'MSSQLStore' });
59
- try {
60
- if ('connectionString' in config) {
61
- if (
62
- !config.connectionString ||
63
- typeof config.connectionString !== 'string' ||
64
- config.connectionString.trim() === ''
65
- ) {
66
- throw new Error('MSSQLStore: connectionString must be provided and cannot be empty.');
67
- }
68
- } else {
69
- const required = ['server', 'database', 'user', 'password'];
70
- for (const key of required) {
71
- if (!(key in config) || typeof (config as any)[key] !== 'string' || (config as any)[key].trim() === '') {
72
- throw new Error(`MSSQLStore: ${key} must be provided and cannot be empty.`);
73
- }
74
- }
75
- }
76
-
77
- this.schema = config.schemaName || 'dbo';
78
- this.pool =
79
- 'connectionString' in config
80
- ? new sql.ConnectionPool(config.connectionString)
81
- : new sql.ConnectionPool({
82
- server: config.server,
83
- database: config.database,
84
- user: config.user,
85
- password: config.password,
86
- port: config.port,
87
- options: config.options || { encrypt: true, trustServerCertificate: true },
88
- });
89
-
90
- const legacyEvals = new LegacyEvalsMSSQL({ pool: this.pool, schema: this.schema });
91
- const operations = new StoreOperationsMSSQL({ pool: this.pool, schemaName: this.schema });
92
- const scores = new ScoresMSSQL({ pool: this.pool, operations, schema: this.schema });
93
- const traces = new TracesMSSQL({ pool: this.pool, operations, schema: this.schema });
94
- const workflows = new WorkflowsMSSQL({ pool: this.pool, operations, schema: this.schema });
95
- const memory = new MemoryMSSQL({ pool: this.pool, schema: this.schema, operations });
96
-
97
- this.stores = {
98
- operations,
99
- scores,
100
- traces,
101
- workflows,
102
- legacyEvals,
103
- memory,
104
- };
105
- } catch (e) {
106
- throw new MastraError(
107
- {
108
- id: 'MASTRA_STORAGE_MSSQL_STORE_INITIALIZATION_FAILED',
109
- domain: ErrorDomain.STORAGE,
110
- category: ErrorCategory.USER,
111
- },
112
- e,
113
- );
114
- }
115
- }
116
-
117
- async init(): Promise<void> {
118
- if (this.isConnected === null) {
119
- this.isConnected = this._performInitializationAndStore();
120
- }
121
- try {
122
- await this.isConnected;
123
- await super.init();
124
- } catch (error) {
125
- this.isConnected = null;
126
- throw new MastraError(
127
- {
128
- id: 'MASTRA_STORAGE_MSSQL_STORE_INIT_FAILED',
129
- domain: ErrorDomain.STORAGE,
130
- category: ErrorCategory.THIRD_PARTY,
131
- },
132
- error,
133
- );
134
- }
135
- }
136
-
137
- private async _performInitializationAndStore(): Promise<boolean> {
138
- try {
139
- await this.pool.connect();
140
- return true;
141
- } catch (err) {
142
- throw err;
143
- }
144
- }
145
-
146
- public get supports(): {
147
- selectByIncludeResourceScope: boolean;
148
- resourceWorkingMemory: boolean;
149
- hasColumn: boolean;
150
- createTable: boolean;
151
- deleteMessages: boolean;
152
- } {
153
- return {
154
- selectByIncludeResourceScope: true,
155
- resourceWorkingMemory: true,
156
- hasColumn: true,
157
- createTable: true,
158
- deleteMessages: true,
159
- };
160
- }
161
-
162
- /** @deprecated use getEvals instead */
163
- async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
164
- return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
165
- }
166
-
167
- async getEvals(
168
- options: {
169
- agentName?: string;
170
- type?: 'test' | 'live';
171
- } & PaginationArgs = {},
172
- ): Promise<PaginationInfo & { evals: EvalRow[] }> {
173
- return this.stores.legacyEvals.getEvals(options);
174
- }
175
-
176
- /**
177
- * @deprecated use getTracesPaginated instead
178
- */
179
- public async getTraces(args: StorageGetTracesArg): Promise<Trace[]> {
180
- return this.stores.traces.getTraces(args);
181
- }
182
-
183
- public async getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & { traces: Trace[] }> {
184
- return this.stores.traces.getTracesPaginated(args);
185
- }
186
-
187
- async batchTraceInsert({ records }: { records: Record<string, any>[] }): Promise<void> {
188
- return this.stores.traces.batchTraceInsert({ records });
189
- }
190
-
191
- async createTable({
192
- tableName,
193
- schema,
194
- }: {
195
- tableName: TABLE_NAMES;
196
- schema: Record<string, StorageColumn>;
197
- }): Promise<void> {
198
- return this.stores.operations.createTable({ tableName, schema });
199
- }
200
-
201
- async alterTable({
202
- tableName,
203
- schema,
204
- ifNotExists,
205
- }: {
206
- tableName: TABLE_NAMES;
207
- schema: Record<string, StorageColumn>;
208
- ifNotExists: string[];
209
- }): Promise<void> {
210
- return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
211
- }
212
-
213
- async clearTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
214
- return this.stores.operations.clearTable({ tableName });
215
- }
216
-
217
- async dropTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
218
- return this.stores.operations.dropTable({ tableName });
219
- }
220
-
221
- async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, any> }): Promise<void> {
222
- return this.stores.operations.insert({ tableName, record });
223
- }
224
-
225
- async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
226
- return this.stores.operations.batchInsert({ tableName, records });
227
- }
228
-
229
- async load<R>({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, string> }): Promise<R | null> {
230
- return this.stores.operations.load({ tableName, keys });
231
- }
232
-
233
- /**
234
- * Memory
235
- */
236
-
237
- async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
238
- return this.stores.memory.getThreadById({ threadId });
239
- }
240
-
241
- /**
242
- * @deprecated use getThreadsByResourceIdPaginated instead
243
- */
244
- public async getThreadsByResourceId(args: { resourceId: string } & ThreadSortOptions): Promise<StorageThreadType[]> {
245
- return this.stores.memory.getThreadsByResourceId(args);
246
- }
247
-
248
- public async getThreadsByResourceIdPaginated(
249
- args: {
250
- resourceId: string;
251
- page: number;
252
- perPage: number;
253
- } & ThreadSortOptions,
254
- ): Promise<PaginationInfo & { threads: StorageThreadType[] }> {
255
- return this.stores.memory.getThreadsByResourceIdPaginated(args);
256
- }
257
-
258
- async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
259
- return this.stores.memory.saveThread({ thread });
260
- }
261
-
262
- async updateThread({
263
- id,
264
- title,
265
- metadata,
266
- }: {
267
- id: string;
268
- title: string;
269
- metadata: Record<string, unknown>;
270
- }): Promise<StorageThreadType> {
271
- return this.stores.memory.updateThread({ id, title, metadata });
272
- }
273
-
274
- async deleteThread({ threadId }: { threadId: string }): Promise<void> {
275
- return this.stores.memory.deleteThread({ threadId });
276
- }
277
-
278
- /**
279
- * @deprecated use getMessagesPaginated instead
280
- */
281
- public async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
282
- public async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
283
- public async getMessages(
284
- args: StorageGetMessagesArg & {
285
- format?: 'v1' | 'v2';
286
- },
287
- ): Promise<MastraMessageV1[] | MastraMessageV2[]> {
288
- return this.stores.memory.getMessages(args);
289
- }
290
-
291
- async getMessagesById({ messageIds, format }: { messageIds: string[]; format: 'v1' }): Promise<MastraMessageV1[]>;
292
- async getMessagesById({ messageIds, format }: { messageIds: string[]; format?: 'v2' }): Promise<MastraMessageV2[]>;
293
- async getMessagesById({
294
- messageIds,
295
- format,
296
- }: {
297
- messageIds: string[];
298
- format?: 'v1' | 'v2';
299
- }): Promise<MastraMessageV1[] | MastraMessageV2[]> {
300
- return this.stores.memory.getMessagesById({ messageIds, format });
301
- }
302
-
303
- public async getMessagesPaginated(
304
- args: StorageGetMessagesArg & {
305
- format?: 'v1' | 'v2';
306
- },
307
- ): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
308
- return this.stores.memory.getMessagesPaginated(args);
309
- }
310
-
311
- async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
312
- async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
313
- async saveMessages(
314
- args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraMessageV2[]; format: 'v2' },
315
- ): Promise<MastraMessageV2[] | MastraMessageV1[]> {
316
- return this.stores.memory.saveMessages(args);
317
- }
318
-
319
- async updateMessages({
320
- messages,
321
- }: {
322
- messages: (Partial<Omit<MastraMessageV2, 'createdAt'>> & {
323
- id: string;
324
- content?: {
325
- metadata?: MastraMessageContentV2['metadata'];
326
- content?: MastraMessageContentV2['content'];
327
- };
328
- })[];
329
- }): Promise<MastraMessageV2[]> {
330
- return this.stores.memory.updateMessages({ messages });
331
- }
332
-
333
- async deleteMessages(messageIds: string[]): Promise<void> {
334
- return this.stores.memory.deleteMessages(messageIds);
335
- }
336
-
337
- async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
338
- return this.stores.memory.getResourceById({ resourceId });
339
- }
340
-
341
- async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {
342
- return this.stores.memory.saveResource({ resource });
343
- }
344
-
345
- async updateResource({
346
- resourceId,
347
- workingMemory,
348
- metadata,
349
- }: {
350
- resourceId: string;
351
- workingMemory?: string;
352
- metadata?: Record<string, unknown>;
353
- }): Promise<StorageResourceType> {
354
- return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
355
- }
356
-
357
- /**
358
- * Workflows
359
- */
360
- async updateWorkflowResults({
361
- workflowName,
362
- runId,
363
- stepId,
364
- result,
365
- runtimeContext,
366
- }: {
367
- workflowName: string;
368
- runId: string;
369
- stepId: string;
370
- result: StepResult<any, any, any, any>;
371
- runtimeContext: Record<string, any>;
372
- }): Promise<Record<string, StepResult<any, any, any, any>>> {
373
- return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, runtimeContext });
374
- }
375
-
376
- async updateWorkflowState({
377
- workflowName,
378
- runId,
379
- opts,
380
- }: {
381
- workflowName: string;
382
- runId: string;
383
- opts: {
384
- status: string;
385
- result?: StepResult<any, any, any, any>;
386
- error?: string;
387
- suspendedPaths?: Record<string, number[]>;
388
- waitingPaths?: Record<string, number[]>;
389
- };
390
- }): Promise<WorkflowRunState | undefined> {
391
- return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
392
- }
393
-
394
- async persistWorkflowSnapshot({
395
- workflowName,
396
- runId,
397
- snapshot,
398
- }: {
399
- workflowName: string;
400
- runId: string;
401
- snapshot: WorkflowRunState;
402
- }): Promise<void> {
403
- return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
404
- }
405
-
406
- async loadWorkflowSnapshot({
407
- workflowName,
408
- runId,
409
- }: {
410
- workflowName: string;
411
- runId: string;
412
- }): Promise<WorkflowRunState | null> {
413
- return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
414
- }
415
-
416
- async getWorkflowRuns({
417
- workflowName,
418
- fromDate,
419
- toDate,
420
- limit,
421
- offset,
422
- resourceId,
423
- }: {
424
- workflowName?: string;
425
- fromDate?: Date;
426
- toDate?: Date;
427
- limit?: number;
428
- offset?: number;
429
- resourceId?: string;
430
- } = {}): Promise<WorkflowRuns> {
431
- return this.stores.workflows.getWorkflowRuns({ workflowName, fromDate, toDate, limit, offset, resourceId });
432
- }
433
-
434
- async getWorkflowRunById({
435
- runId,
436
- workflowName,
437
- }: {
438
- runId: string;
439
- workflowName?: string;
440
- }): Promise<WorkflowRun | null> {
441
- return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
442
- }
443
-
444
- async close(): Promise<void> {
445
- await this.pool.close();
446
- }
447
-
448
- /**
449
- * Scorers
450
- */
451
- async getScoreById({ id: _id }: { id: string }): Promise<ScoreRowData | null> {
452
- return this.stores.scores.getScoreById({ id: _id });
453
- }
454
-
455
- async getScoresByScorerId({
456
- scorerId: _scorerId,
457
- pagination: _pagination,
458
- }: {
459
- scorerId: string;
460
- pagination: StoragePagination;
461
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
462
- return this.stores.scores.getScoresByScorerId({ scorerId: _scorerId, pagination: _pagination });
463
- }
464
-
465
- async saveScore(_score: ScoreRowData): Promise<{ score: ScoreRowData }> {
466
- return this.stores.scores.saveScore(_score);
467
- }
468
-
469
- async getScoresByRunId({
470
- runId: _runId,
471
- pagination: _pagination,
472
- }: {
473
- runId: string;
474
- pagination: StoragePagination;
475
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
476
- return this.stores.scores.getScoresByRunId({ runId: _runId, pagination: _pagination });
477
- }
478
-
479
- async getScoresByEntityId({
480
- entityId: _entityId,
481
- entityType: _entityType,
482
- pagination: _pagination,
483
- }: {
484
- pagination: StoragePagination;
485
- entityId: string;
486
- entityType: string;
487
- }): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
488
- return this.stores.scores.getScoresByEntityId({
489
- entityId: _entityId,
490
- entityType: _entityType,
491
- pagination: _pagination,
492
- });
493
- }
494
- }
@@ -1,9 +0,0 @@
1
- {
2
- "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "include": ["src/**/*"],
8
- "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
- }
package/tsconfig.json DELETED
@@ -1,5 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*", "tsup.config.ts"],
4
- "exclude": ["node_modules", "**/*.test.ts"]
5
- }
package/tsup.config.ts DELETED
@@ -1,17 +0,0 @@
1
- import { generateTypes } from '@internal/types-builder';
2
- import { defineConfig } from 'tsup';
3
-
4
- export default defineConfig({
5
- entry: ['src/index.ts'],
6
- format: ['esm', 'cjs'],
7
- clean: true,
8
- dts: false,
9
- splitting: true,
10
- treeshake: {
11
- preset: 'smallest',
12
- },
13
- sourcemap: true,
14
- onSuccess: async () => {
15
- await generateTypes(process.cwd());
16
- },
17
- });
package/vitest.config.ts DELETED
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'vitest/config';
2
-
3
- export default defineConfig({
4
- test: {
5
- environment: 'node',
6
- include: ['src/**/*.test.ts'],
7
- exclude: ['src/**/*.performance.test.ts'],
8
- coverage: {
9
- reporter: ['text', 'json', 'html'],
10
- },
11
- },
12
- });