#How do I generate the initial migration for all entities with TypeORM?

3 messages · Page 1 of 1 (latest)

violet beacon
#

I created entities, and now I wish to generate the migration that will create the table for these. How do I do this?

I only see documentation mentioning running migrations for changes; but what about the initial setup of the database?

"Once you get into production you'll need to synchronize model changes into the database. Typically, it is unsafe to use synchronize: true for schema synchronisation on production once you get data in your database. Here is where migrations come to help."

Source: https://orkhan.gitbook.io/typeorm/docs/migrations

In other words: How do I generate the initial schema (prior to any migrations/changes) that I need to run in production to create the initial database with tables?

I bought and followed the nest.js courses, but they do not mention these either. I really miss a comment section on the learn.nestjs.com site.

meager needle
keen hound
# violet beacon I created entities, and now I wish to generate the migration that will create th...

If you want to generate all migrations based on your current entities, use the following command:

npx typeorm migration:generate src/migrations/SchemaSync -d dist/typeorm-cli.config

This will create a migration script that corresponds to your current entities under src/migrations/SchemaSync however -d option should point at a proper config file that exports a datasource object which typeorm will use to determine the changes between your entity schema and actual database. If your database is empty with no tables, then migration script will contain all the schema from scratch