> Discover all available pages from the documentation index: https://mastra.zisheng.pro/ja/llms.txt # MSSQL ストレージ MSSQL ストレージ実装は、Microsoft SQL Server データベースを使用した本番環境対応のストレージソリューションを提供します。 ## インストール **npm**: ```bash npm install @mastra/mssql@latest ``` **pnpm**: ```bash pnpm add @mastra/mssql@latest ``` **Yarn**: ```bash yarn add @mastra/mssql@latest ``` **Bun**: ```bash bun add @mastra/mssql@latest ``` ## 使用方法 ```typescript import { MSSQLStore } from '@mastra/mssql' const storage = new MSSQLStore({ id: 'mssql-storage', connectionString: process.env.DATABASE_URL, }) ``` ## パラメーター **connectionString** (`string`): MSSQL 接続文字列(例:Server=localhost,1433;Database=mydb;User Id=sa;Password=password;Encrypt=true;TrustServerCertificate=true) **schemaName** (`string`): ストレージで使用するスキーマ名。指定しない場合はデフォルトのスキーマを使用します。 ## コンストラクターの例 `MSSQLStore` は次の方法でインスタンス化できます。 ```ts import { MSSQLStore } from '@mastra/mssql' // Using a connection string only const store1 = new MSSQLStore({ id: 'mssql-storage-1', connectionString: 'Server=localhost,1433;Database=mydb;User Id=sa;Password=password;Encrypt=true;TrustServerCertificate=true', }) // Using a connection string with a custom schema name const store2 = new MSSQLStore({ id: 'mssql-storage-2', connectionString: 'Server=localhost,1433;Database=mydb;User Id=sa;Password=password;Encrypt=true;TrustServerCertificate=true', schemaName: 'custom_schema', // optional }) // Using individual connection parameters const store4 = new MSSQLStore({ id: 'mssql-storage-3', server: 'localhost', port: 1433, database: 'mydb', user: 'user', password: 'password', }) // Individual parameters with schemaName const store5 = new MSSQLStore({ id: 'mssql-storage-4', server: 'localhost', port: 1433, database: 'mydb', user: 'user', password: 'password', schemaName: 'custom_schema', // optional }) ``` ## 補足 ### スキーマ管理 ストレージ実装は、スキーマの作成と更新を自動的に処理します。次のテーブルが作成されます。 - `mastra_workflow_snapshot`:Workflow の状態と実行データを保存します - `mastra_evals`:評価結果とメタデータを保存します - `mastra_threads`:会話スレッドを保存します - `mastra_messages`:個々のメッセージを保存します - `mastra_traces`:テレメトリとトレースデータを保存します - `mastra_scorers`:スコアリングと評価データを保存します - `mastra_resources`:リソースのワーキングメモリデータを保存します ### 初期化 ストレージを Mastra クラスに渡すと、ストレージ操作の前に `init()` が自動的に呼び出されます。 ```typescript import { Mastra } from '@mastra/core' import { MSSQLStore } from '@mastra/mssql' const storage = new MSSQLStore({ connectionString: process.env.DATABASE_URL, }) const mastra = new Mastra({ storage, // init() is called automatically }) ``` Mastra を介さずにストレージを直接使用する場合は、テーブルを作成するために `init()` を明示的に呼び出す必要があります。 ```typescript import { MSSQLStore } from '@mastra/mssql' const storage = new MSSQLStore({ id: 'mssql-storage', connectionString: process.env.DATABASE_URL, }) // Required when using storage directly await storage.init() // Access domain-specific stores via getStore() const memoryStore = await storage.getStore('memory') const thread = await memoryStore?.getThreadById({ threadId: '...' }) ``` > **警告:** `init()` を呼び出さないとテーブルが作成されず、ストレージ操作が通知なく失敗するか、エラーがスローされます。 ### データベースとプールへの直接アクセス `MSSQLStore` は、mssql 接続プールを公開フィールドとして提供します。 ```typescript store.pool // mssql connection pool instance ``` 直接クエリとカスタムトランザクション管理に対応しています。これらのフィールドを使用する場合は、次の点に注意してください。 - 接続とトランザクションを適切に処理する責任があります。 - ストアを閉じると(`store.close()`)、関連する接続プールが破棄されます。 - 直接アクセスでは、MSSQLStore のメソッドが提供する追加ロジックや検証を迂回します。 この方法は、低レベルアクセスが必要な高度なユースケースを対象としています。