Coming from the world of PHP (Laravel/Eloquent, Symfony/Doctrine) I am used to ORMs not dictating schema but making accessible schema attributes, meaning I've never used a "Model" file to maintain schema, rather schema was maintained by migrations.
In trying to hone my NodeJS/TypeScript skills I am attempting to use TypeORM in the same way, table schemas are to be handled by migrations whereas Model files allow access to table attributes via code. After some reading and trial programming I am realizing that TypeORM doesn't seem to support this type of separation, it seems they force us to use 'Entities' which require Entity metadata which dictate schema.
My question is, is it possible to configure TypeORM so table schema is handled only by migrations, with 0 affect by Model files?
You can't really avoid entities altogether in TypeORM. But there are a few schools of thought here:
You can have entities that not only define column names and types, but the entire schema (like keys, relationships, indexes, constraints, etc). But this doesn't look like what you want. So alternatively:
You can define entities that are very "plain" and just act like simple objects to access the data. Then:
You can (and probably should, use entity schemas - which are kind of designed for your use case) - you can find more information here: https://orkhan.gitbook.io/typeorm/docs/separating-entity-definition
Or:
Example of your simple POTO: https://www.tutorialspoint.com/typeorm/typeorm_migrations.htm
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Book {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@Column()
text: string;
}
Example of the equivalent migrations file:
// XXXXXXXX0001-CreateBookTable.ts
import { MigrationInterface, QueryRunner, Table } from 'typeorm';
export class CreateBookTable implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(new Table({
name: 'book',
columns: [
{ name: 'id', type: 'int', isPrimary: true },
{ name: 'title', type: 'varchar' },
{ name: 'text', type: 'varchar' }
],
}), true);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropTable('book');
}
}
I hope this helps.
However, why not embrace the most commonly used approach of TypeORM to learn something new, that would probably be best and you'll get more widespread support and help.