💾 データベースドライバー
データベースドライバーは、NodeBlocksアプリケーションでデータ永続化のための一貫したインターフェースを提供します。データベース接続と操作を抽象化し、ビジネスロジックを変更することなく異なるデータベースを使用できるようにします。
🎯 概要
NodeBlocksのデータベースドライバーは、設定されたデータベースインスタンスを作成するファクトリ関数です。これらは以下を提供します:
- 一貫したインターフェース - 異なるデータベースタイプ間で
- シンプルな設定 - 接続文字列とデータベース名を使用
- 柔軟な実装 - カスタムデータベースアダプター用
📋 利用可能なデータベースドライバー
MongoDB Driver
MongoDBドライバーは、NodeBlocksサービスで使用するための設定されたMongoDBデータベースインスタンスを作成します。
getMongoClient
指定された接続URLとデータベース名でMongoDBデータベースクライアントを作成します。
パラメータ:
url: string- MongoDB接続文字列(例:mongodb://localhost:27017)dbName: string- MongoDBインスタンス内で接続するデータベース名
戻り値: Db - データベース操作を実行するためのMongoDBデータベースインスタンス
使用例:
import { getMongoClient } from '@nodeblocks/backend-sdk/drivers';
const db = getMongoClient('mongodb://localhost:27017', 'myapp');
// Access collections
const usersCollection = db.collection('users');
const postsCollection = db.collection('posts');
環境変数を使用した例:
import { getMongoClient } from '@nodeblocks/backend-sdk/drivers';
const db = getMongoClient(
process.env.MONGODB_URL || 'mongodb://localhost:27017',
process.env.DB_NAME || 'myapp'
);
// Use with services
const userService = services.userService({
users: db.collection('users'),
identity: db.collection('identity')
});
MongoDB Atlasを使用した例:
import { getMongoClient } from '@nodeblocks/backend-sdk/drivers';
const db = getMongoClient(
'mongodb+srv://username:password@cluster.mongodb.net/myapp',
'myapp'
);
withMongo
自動コレクションアクセス付きの認証済みMongoDB接続を作成するカリー化ユーティリティ。
目的: 認証付きでMongoDBに接続する柔軟で合成可能な方法を提供し、再利用可能な接続ファクトリの部分適用をサポートします。
パラメータ:
dbUrl: string- MongoDB接続URLdbName: string- 接続するデータベース名dbUser: string- 認証用のユーザー名dbPassword: string- 認証用のパスワードcollectionName: string- アクセスするコレクション名
戻り値: Promise<{ [collectionName]: Collection }> - 要求されたコレクションを含むオブジェクト
使用例:
import { withMongo } from '@nodeblocks/backend-sdk/drivers';
// 完全な適用 - usersコレクションを取得
const users = await withMongo(
'mongodb://localhost:27017',
'myapp',
'admin',
'password',
'users'
);
// 戻り値: { users: Collection }
// カリー化された使用法 - 再利用可能な接続ファクトリを作成
const connectToMyApp = withMongo('mongodb://localhost:27017', 'myapp');
const users = await connectToMyApp('admin', 'password', 'users');
const orders = await connectToMyApp('admin', 'password', 'orders');
// 認証付きファクトリ
const connectWithAuth = withMongo('mongodb://localhost:27017', 'myapp', 'admin', 'password');
const posts = await connectWithAuth('posts');
const comments = await connectWithAuth('comments');
// 最大の柔軟性のために完全にカリー化
const postsCollection = await withMongo('mongodb://localhost:27017')('myapp')('admin')('password')('posts');
サービスとの統合:
import { services } from '@nodeblocks/backend-sdk';
import { withMongo } from '@nodeblocks/backend-sdk/drivers';
// 接続してコレクションを取得
const { users } = await withMongo('mongodb://localhost:27017', 'myapp', 'admin', 'password', 'users');
// サービスで使用
const userService = services.userService({
users,
identity: (await withMongo('mongodb://localhost:27017', 'myapp', 'admin', 'password', 'identity')).identity
});
利点:
- 合成可能: 再利用可能な接続パターンの部分適用をサポート
- 型安全: 適切な型付けによる完全なTypeScriptサポート
- 柔軟: あらゆるMongoDBデプロイメント(ローカル、Atlasなど)で動作
- セキュア: 認証パラメータを適切に処理
- 便利: サービス対応形式でコレクションを返す
🔧 データベースドライバーの使用
サービスでの使用
データストアパラメータを通じてデータベースコレクションをサービスに渡します:
import { services } from '@nodeblocks/backend-sdk';
import { withMongo } from '@nodeblocks/backend-sdk/drivers';
const connectToDatabase = withMongo('mongodb://localhost:27017', 'dev', 'user', 'password');
express()
.use(
services.userService(
{
...(await connectToDatabase('users')),
...(await connectToDatabase('identity')),
},
{
authSecrets: {
authEncSecret: 'your-encryption-secret',
authSignSecret: 'your-signing-secret',
},
}
)
)
.listen(8089);
🔗 関連ドキュメント
- カスタムDataStoreの使用 - カスタムデータベースドライバーの実装方法