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!