You can define an entity and its columns right in the model, using decorators. But some people prefer to define an entity and its columns inside separate files which are called "entity schemas" in TypeORM.
When using the Decorator approach it is easy to extend basic columns to an abstract class and simply extend this. For example, your id, createdAt and updatedAt columns may be defined in such a BaseEntity. For more details, see the documentation on concrete table inheritance.
When using the EntitySchema approach, this is not possible. However, you can use the Spread Operator (...) to your advantage.
Reconsider the Category example from above. You may want to extract basic column descriptions and reuse it across your other schemas. This may be done in the following way:
import {EntitySchemaColumnOptions} from "typeorm";
export const BaseColumnSchemaPart = {
id: {
type: Number,
primary: true,
generated: true,
} as EntitySchemaColumnOptions,
createdAt: {
name: 'created_at',
type: 'timestamp with time zone',
createDate: true,
} as EntitySchemaColumnOptions,
updatedAt: {
name: 'updated_at',
type: 'timestamp with time zone',
updateDate: true,
} as EntitySchemaColumnOptions,
};
Now you can use the BaseColumnSchemaPart in your other schema models, like this:
export const CategoryEntity = new EntitySchema<Category>({
name: "category",
columns: {
...BaseColumnSchemaPart,
// the CategoryEntity now has the defined id, createdAt, updatedAt columns!
// in addition, the following NEW fields are defined
name: {
type: String
}
}
});
Be sure to add the extended columns also to the Category interface (e.g., via export interface Category extend BaseEntity).
Using Schemas to Query / Insert Data
Of course, you can use the defined schemas in your repositories or entity manager as you would use the decorators. Consider the previously defined Category example (with its Interface and CategoryEntity schema in order to get some data or manipulate the database.
// request data
const categoryRepository = getRepository<Category>(CategoryEntity);
const category = await categoryRepository.findOne(1); // category is properly typed!
// insert a new category into the database
const categoryDTO = {
// note that the ID is autogenerated; see the schema above
name: 'new category',
};
const newCategory = await categoryRepository.save(categoryDTO);