Marcel Wolf
Marcel Wolf

Reputation: 326

Use Mongo DB with Vapor and Fluent

I made a model for Users looking like this:

import Vapor
import Fluent
import Foundation

final class User: Model {

var id: Node?
var username: String
var name : String
var surename : String
var password : String
var credit : String
var isBlocked : String
var isAdmin : String

init(username: String, name: String, surename: String, password: String, credit: String, isBlocked: String, isAdmin: String) {
    self.id = UUID().uuidString.makeNode()
    self.username = username
    self.name = name
    self.surename = surename
    self.password = password
    self.credit = credit
    self.isBlocked = isBlocked
    self.isAdmin = isAdmin
}

init(node: Node, in context: Context) throws {
    id = try node.extract("_id")
    username = try node.extract("username")
    name = try node.extract("name")
    surename = try node.extract("surename")
    password = try node.extract("password")
    credit = try node.extract("credit")
    isBlocked = try node.extract("isBlocked")
    isAdmin = try node.extract("isAdmin")

}

func makeNode(context: Context) throws -> Node {
    return try Node(node: [
        "_id": id,
        "username": username,
        "name": name,
        "surename": surename,
        "password" : password,
        "credit" : credit,
        "isBlocked" : isBlocked,
        "isAdmin" : isAdmin
    ])
}
}

//extension User {
//    /**
//        This will automatically fetch from database, using example here to   load
//        automatically for example. Remove on real models.
//    */
//    public convenience init?(from string: String) throws {
//        self.init(content: string)
//    }
//}

extension User: Preparation {
static func prepare(_ database: Database) throws {
    try database.create("users") { users in
        users.id()
        users.string("username")
        users.string("name")
        users.string("surename")
        users.string("password")
        users.string("credit")
        users.string("isBlocked")
        users.string("isAdmin")
    }
}

static func revert(_ database: Database) throws {
    //
}
}

I try to create a new User with this:

    var user = User(username: "Test", name: "Name", surename: "zuname", password: "1234", credit: "0.00", isBlocked: "false", isAdmin: "true")

    try user.save()
    print(user.id) // prints the new id

i can build and run vapor. when i enter the route the could should be execute i got this error:

Uncaught Error: EntityError.noDatabase. Use middleware to catch this error and provide a better response. Otherwise, a 500 error page will be returned in the production environment.

Can anybody help me?

Where can i found a complete documentation for Fluent with mongo and Vapor?

Thanks a lot!

Upvotes: 3

Views: 1689

Answers (1)

tanner0101
tanner0101

Reputation: 4065

The EntityError.noDatabase happens when the model doesn't know which database to use.

You have two ways to fix this.

Explicitly set the database for the User type.

User.database = myDatabase

Or, pass the User type as a preparation for the Droplet.

let drop = Droplet(preparations: [User.self])

Upvotes: 9

Related Questions