mongodb

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

README

mongodb

MongoDB storage adapter for [mtgo] Telegram clients.

Install

go get github.com/mtgo-labs/storage/mongodb

Usage

ext, err := mongodb.Open(ctx, mongodb.Config{
    URI:      "mongodb://localhost:27017",
    Database: "mtgo",
})
if err != nil {
    log.Fatal(err)
}
defer ext.Close()

client, err := tg.NewClient(mustAtoi(apiID), apiHash, &tg.Config{
    BotToken:    botToken,
    SessionName: "storage_bot",
    SavePeers:   true,
    Storage:     storage.NewAdapter(ext),
})

Open connects to the cluster, verifies connectivity with Ping, and creates indexes for peer username lookups and conversation composite queries. Collections are created implicitly on first use.

Collections

sessions

Single-document collection. Fields map to [storage.Session]:

Field Type
dc_id int
api_id int
api_hash string
test_mode int
auth_key []byte
state []byte
user_id int64
is_bot int
first_name string
last_name string
username string
date int
server_address string
port int

peers

Field Type Index
_id int64
type int
access_hash int64
username string indexed
usernames string
first_name string
last_name string
phone_number string
is_bot int
photo_id int64
language string
last_updated int64

conversations

Field Type Index
chat_id int64 compound (chat_id, user_id)
user_id int64
name string
step int
data []byte
created_at int64
updated_at int64

Interfaces

var _ storage.Adapter           = (*MongoDB)(nil)
var _ storage.ConversationStore = (*MongoDB)(nil)

Documentation

Overview

Package mongodb provides a storage adapter backed by a MongoDB database.

It implements storage.Adapter and storage.ConversationStore. Collections and indexes are created implicitly on first use.

Basic usage:

store, err := mongodb.Open(ctx, mongodb.Config{
    URI:      "mongodb://localhost:27017",
    Database: "mtgo",
})
if err != nil {
    log.Fatal(err)
}
defer store.Close()

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(cfg Config) storage.Storage

Types

type Config

type Config struct {
	URI      string
	Database string
}

Config holds MongoDB connection parameters.

type MongoDB

type MongoDB struct {
	// contains filtered or unexported fields
}

MongoDB is a storage adapter backed by a MongoDB database.

func Open

func Open(ctx context.Context, cfg Config) (*MongoDB, error)

Open connects to a MongoDB cluster and verifies connectivity. Collections are created implicitly on first use.

func (*MongoDB) Close

func (m *MongoDB) Close() error

func (*MongoDB) DeleteConversation

func (m *MongoDB) DeleteConversation(chatID, userID int64) error

func (*MongoDB) DeleteDurableUpdate

func (m *MongoDB) DeleteDurableUpdate(sessionID string, id string) error

func (*MongoDB) DeletePeer

func (m *MongoDB) DeletePeer(id int64) error

func (*MongoDB) EnqueueDurableUpdate

func (m *MongoDB) EnqueueDurableUpdate(u *storage.DurableUpdate) error

func (*MongoDB) GetPeer

func (m *MongoDB) GetPeer(id int64) (*storage.Peer, error)

func (*MongoDB) GetPeerByUsername

func (m *MongoDB) GetPeerByUsername(username string) (*storage.Peer, error)

func (*MongoDB) LoadAllChannelUpdateStates

func (m *MongoDB) LoadAllChannelUpdateStates(sessionID string) ([]*storage.ChannelUpdateState, error)

func (*MongoDB) LoadChannelUpdateState

func (m *MongoDB) LoadChannelUpdateState(sessionID string, channelID int64) (*storage.ChannelUpdateState, error)

func (*MongoDB) LoadConversation

func (m *MongoDB) LoadConversation(chatID, userID int64) (*storage.Conversation, error)

func (*MongoDB) LoadDurableUpdates

func (m *MongoDB) LoadDurableUpdates(sessionID string, limit int) ([]*storage.DurableUpdate, error)

func (*MongoDB) LoadPeers

func (m *MongoDB) LoadPeers() ([]*storage.Peer, error)

func (*MongoDB) LoadSession

func (m *MongoDB) LoadSession() (*storage.Session, error)

func (*MongoDB) LoadUpdateState

func (m *MongoDB) LoadUpdateState(sessionID string) (*storage.UpdateState, error)

func (*MongoDB) MarkDurableUpdateFailed

func (m *MongoDB) MarkDurableUpdateFailed(sessionID string, id string, attempts int, lastErr string) error

func (*MongoDB) SaveChannelUpdateState

func (m *MongoDB) SaveChannelUpdateState(s *storage.ChannelUpdateState) error

func (*MongoDB) SaveConversation

func (m *MongoDB) SaveConversation(c *storage.Conversation) error

func (*MongoDB) SavePeer

func (m *MongoDB) SavePeer(p *storage.Peer) error

func (*MongoDB) SaveSession

func (m *MongoDB) SaveSession(s *storage.Session) error

func (*MongoDB) SaveUpdateDedupKey

func (m *MongoDB) SaveUpdateDedupKey(sessionID string, key string) (bool, error)

func (*MongoDB) SaveUpdateState

func (m *MongoDB) SaveUpdateState(s *storage.UpdateState) error

func (*MongoDB) UpdateDedupKeyExists

func (m *MongoDB) UpdateDedupKeyExists(sessionID string, key string) (bool, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL