Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Telegrammer with Vapor and Fluent (Postgresql) #36

Closed
ts95 opened this issue Oct 6, 2019 · 1 comment
Closed

Using Telegrammer with Vapor and Fluent (Postgresql) #36

ts95 opened this issue Oct 6, 2019 · 1 comment

Comments

@ts95
Copy link

ts95 commented Oct 6, 2019

I've set up a project with Telegrammer and Vapor. I've installed the postgresql driver and registered FluentPostgreSQLProvider, but I'm not sure how to get a database connection in my bot class.

If I have a model called User and I want to query all users, I'd write the following code: User.query(on: conn).all(), where conn is an object that conforms to DatabaseConnectable. I'm not sure where/how I can access this object in my bot class.

@ts95
Copy link
Author

ts95 commented Oct 7, 2019

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.

@ts95 ts95 closed this as completed Oct 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant