Using multiple databases in Vapor 4

Hello everyone,

Does anyone have any ideas for using multiple databases in Vapor 4 and Fluent?

My project must be able to address at least 2 MySQL databases and if possible 2 PostgreSQL databases!

Thanks in advance !
Good day,
Eric

You should be able to do

extension DatabaseID {
    static let first = DatabaseID("first")
    static let second = DatabaseID("second")
    ....
}

and then just

app.databases.use(..., as: .first)
app.databases.use(..., as: .second)
...

and finally, to use them

Model.query(on: req.db(.first))
2 Likes

Thank you very much for this response. It's perfect