async-matrixSourceAsyncMatrixBridgeDiscordDB

module DB

Database module for the Discord bridge.

Establishes a connection using the bridge config's database section, runs Sequel migrations, and wires up all model classes to the database.

Supports both SQLite (with foreign keys + WAL) and PostgreSQL.

db = Async::Matrix::Bridge::Discord::DB.connect(config)

All models are now ready:

DB::User.create(mxid: "@alice:example.com") DB::Portal.where(discord_guild_id: "123").all

Nested

Definitions

def self.connect(config)

Connect to the database, run migrations, and bind all models.

Implementation

def self.connect(config)
  db = Connection.establish(config.database)
  migrate!(db)
  bind_models(db)
  db
end

def self.migrate!(db)

Run pending migrations.

Implementation

def self.migrate!(db)
  Sequel::Migrator.run(db, MIGRATIONS_PATH)
end

def self.bind_models(db)

Bind all model classes to the given database.

Implementation

def self.bind_models(db)
  Async::Matrix::Bridge::Discord::DB::User.dataset        = db[:users]
  Async::Matrix::Bridge::Discord::DB::Guild.dataset       = db[:guilds]
  Async::Matrix::Bridge::Discord::DB::Portal.dataset      = db[:portals]
  Async::Matrix::Bridge::Discord::DB::Puppet.dataset      = db[:puppets]
  Async::Matrix::Bridge::Discord::DB::Message.dataset     = db[:messages]
  Async::Matrix::Bridge::Discord::DB::Reaction.dataset    = db[:reactions]
  Async::Matrix::Bridge::Discord::DB::CachedFile.dataset  = db[:files]
end