TypeORM’s dropSchema option allows you to remove all tables from your database, which has the overall effect of emptying your schema. It will mainly come in handy while developing or testing an application where an old state of the database no longer serves any purpose.
Once the option dropSchema is set to true, the following actions are performed when the ORM is synchronized with the database
All database tables are deleted.
Associated tables, columns, indexes, and relationships are removed.
If dropSchema is set to true and synchronize is set to false, then it will not remove any tables. However, if both options are set to true, then the existing tables will be deleted and the schema will be constructed according to the entity definitions.
Example:
import { DataSource } from 'typeorm';
import { User } from './entities/User';
const dataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'test_db',
synchronize: true, // Recreate schema after dropping
dropSchema: true, // Drop all tables
entities: [User],
});
dataSource
.initialize()
.then(() => console.log('Schema dropped and recreated'))
.catch((error) => console.log('Error: ', error));
Ready to transform your business with our technology solutions? Contact Us Now!