We will start by creating our model. The code is as follows:
import Vapor
import Fluent
final class Todo: Model {
var id: Node?
var todoId: Int
var name: String
var description: String
var notes: String
var completed: Bool
var synced: Bool
var exists: Bool = false
init(node: Node, in context: Context) throws {
id = try node.extract("id")
todoId = try node.extract("todoId")
name = try node.extract("name")
description = try node.extract("description")
notes = try node.extract("notes")
completed = try node.extract("completed")
synced = try node.extract("synced")
}
init(todoId: Int, name: String, description: String, notes: String,
completed: Bool, synced: Bool) {
self.todoId = todoId
self.name = name
self.description = description
self.notes = notes
self.completed = completed
self.synced = synced
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"todoId": todoId,
"name": "(name)",
"description": "(description)",
"notes": "(notes)",
"completed": completed,
"synced": synced
])
}
}
This class imports Vapor and Fluent and includes some Todo-related properties as well as an init method.
To be able to pass this model into JSON arrays and dictionaries, we provided a makeNode(context: Context) throws -> Node implementation.