Let’s create your first model. The CLI provides a useful command to generate a new file with an empty model.
FoalTS uses TypeORM, a complete Object-Relational Mapper, to communicate with the database(s). In TypeORM, simple models are called entities and are classes decorated with the
Every new project in FoalTS uses an
SQLitedatabase as it does not require any additional installation. But TypeORM supports many other databases. We will keep this one in this tutorial for simplicity.
Open the file
todo.entity.ts in the
src/app/entities directory and add a
This class is the representation of the SQL table
todo. Currently, this table does not exist in the database. You will have to create it.
You can do this manually, using a database software for example, or use migrations, a programmatic way to update a database schema. The advantage of using migrations is that you can create, update and delete your tables directly from the definition of your entities. They also ensure that all your environments (prod, dev) and co-developers have the same table definitions.
Let’s see how to use them.
First compile the application.
Then run the following command to generate your migration file. TypeORM will compare your current database schema with the definition of your entities and generate the appropriate SQL queries.
A new file appears in the
src/migrations/ directory. Open it.
up method contains all the SQL queries that will be executed during the migration.
Let's compile and run the migration.
TypeORM examines all the migrations that have been run previously (none in this case) and executes the new ones.
Your database (
db.sqlite3) now contains a new table named
Alternatively, you can also use the
synchronizeoption in your configuration file
config/default.json. When set to
true, the database schema is auto created from the entities definition on every application launch. You do not need migrations in this case. However, although this behavior may be useful during debug and development, it is not suitable for a production environment (you could lose production data).