@cbnsndwch/ghl-sdk-storage-mongodb
v0.5.1
Published
MongoDB storage adapter for GHL SDK
Readme
@cbnsndwch/ghl-sdk-storage-mongodb
MongoDB storage adapter for GHL SDK.
Installation
pnpm add @cbnsndwch/ghl-sdk-storage-mongodb mongodbUsage
import { HighLevel } from '@cbnsndwch/ghl-sdk-core';
import { MongoDBSessionStorage } from '@cbnsndwch/ghl-sdk-storage-mongodb';
// Create MongoDB storage
const storage = new MongoDBSessionStorage(
'mongodb://localhost:27017',
'mydb',
'ghl_sessions' // optional, defaults to 'ghl_sessions'
);
await storage.init();
storage.setClientId(process.env.GHL_CLIENT_ID);
// Use with HighLevel SDK
const ghl = new HighLevel({
clientId: process.env.GHL_CLIENT_ID,
clientSecret: process.env.GHL_CLIENT_SECRET,
tokenProvider: storage,
});
// Make API calls - tokens will be stored in MongoDB
const contacts = await ghl.contacts.searchContactsAdvanced({
locationId: 'location-id',
// ... search params
});
// Cleanup when done
await storage.disconnect();Features
- ✅ Persistent storage in MongoDB
- ✅ Automatic indexes for performance
- ✅ ESM-only
- ✅ Full TypeScript support
- ✅ Production-ready
API
MongoDBSessionStorage
Extends BaseSessionStorage from @cbnsndwch/ghl-sdk-storage.
Constructor
new MongoDBSessionStorage(
dbUrl: string,
dbName: string,
collectionName?: string // defaults to 'ghl_sessions'
)Methods
init()- Connect to MongoDB and create indexesdisconnect()- Close MongoDB connectionsetClientId(clientId)- Set the client IDsetSession(resourceId, data)- Store a sessiongetSession(resourceId)- Retrieve a sessiondeleteSession(resourceId)- Delete a sessionhasSession(resourceId)- Check if session existsgetToken(type, id)- Get access token (implements ITokenProvider)setToken(type, id, token, expiresAt)- Store access tokendeleteToken(type, id)- Delete access tokenisStorageActive()- Check if connected to MongoDB
MongoDB Schema
The adapter creates a collection with the following indexes:
{
resourceId: 1 // unique index
},
{
expire_at: 1 // for TTL and expiration queries
}License
MIT
