Git Product home page Git Product logo

Comments (1)

ts95 avatar ts95 commented on May 18, 2024

I figured it out.

You need to add the following to the configure function in configure.swift:

/// Registering providers
try services.register(FluentPostgreSQLProvider())

let pgsqConfig = PostgreSQLDatabaseConfig(hostname: "<hostname>", username: "<username>")

var databases = DatabasesConfig()
databases.add(database: PostgreSQLDatabase(config: pgsqConfig), as: .psql)
services.register(databases)

var migrations = MigrationConfig()
migrations.add(model: YourModel.self, database: .psql)
services.register(migrations)

Add a new conn: DatabaseConnectable field to your bot class:

final class MyBot: ServiceType {
    let bot: Bot
    let conn: DatabaseConnectable
    var updater: Updater?
    var dispatcher: Dispatcher?

    init(settings: Bot.Settings, conn: DatabaseConnectable) throws {
        self.bot = try Bot(settings: settings)
        self.conn = conn
        let dispatcher = try configureDispatcher()
        self.updater = Updater(bot: bot, dispatcher: dispatcher)
        self.dispatcher = dispatcher
    }

    // ...
}

Then in the bot class, you need to create a new database connection in the makeService function body, and pass it to the MyBot initializer:

static func makeService(for container: Container) throws -> MyBot {
    guard let token = Environment.get("TELEGRAM_BOT_TOKEN") else {
        throw CoreError(identifier: "Enviroment variables", reason: "Cannot find telegram bot token")
    }
    let settings = Bot.Settings(token: token, debugMode: debugMode)
    let conn = try container.newConnection(to: DatabaseIdentifier<PostgreSQLDatabase>.psql).wait()
    return try MyBot(settings: settings, conn: conn)
}

You now have a reference to a database connection in the bot class.

from telegrammer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.