Creating an item

Now that we have all of the code written out, we can see how Fluent makes it easy to create a new item in the database using its high-level API. We will test that everything works by first creating an item and saving it to the database:

  1. We need to add the following code towards the end of main.swift:
let item = Item(name: "Apple")try item.save()
  1. Now, let's run this code using the swift run command, and you will see the database file called main.sqlite created inside of your FluentDemo folder. This is the database file where the item's tables, along with its data, is stored.
  2. We can check that it saved the data in the database by opening the sqlite file in the Terminal using the sqlite3 command as such sqlite3 main.sqlite ...

Get Hands-On Full-Stack Development with Swift now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.