typeorm / ionic-example

Example project to demonstrate TypeORM in an Ionic app
67 stars 56 forks source link

Migrations generation #13

Closed elluminatte closed 6 years ago

elluminatte commented 6 years ago

Hello!

Is there any ability to auto-generate migrations for app entities?

For example, i have src/entities/category.ts:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity('category')
export class Category {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    name: string;
}

Then i've added this code to src/app/app.component.ts:

if(platform.is('cordova')) {
            // Running on device or emulator
            await createConnection({
                type: 'cordova',
                database: 'test',
                location: 'default',
                logging: ['error', 'query', 'schema'],
                synchronize: false,
                entities: [
                    Category
                ]
            });
        } else {
            // Running app in browser
            await createConnection({
                type: 'sqljs',
                autoSave: true,
                location: 'browser',
                logging: ['error', 'query', 'schema'],
                synchronize: false,
                entities: [
                    Category
                ]
            });
}

What should i do to generate migration for Category entity creation? According docs in this repo i understood that i should write migration code myself and then add this to migrations property of createConnection(). May be there is any hint here?

Regards, Nikolay

daniel-lang commented 6 years ago

The Node.js version of TypeORM supports the automatic creation of migrations. But that feature to work in the browser and with ionic Node.js needs a connection to the database. That is because it needs to check the state of your entities in the database to detected changed you made. So for now, as we haven't found a way to give node the access to the ionic sqlite database, there is no support for automatic migration generation in the browser. I guess the only option, for now, would be to create a sqlite database on your computer using the same entities and a Node.js project and create migrations that way.

kunalgrover05 commented 6 years ago

How would the migrations be applied in the app? Is there an automated way to apply migrations?

daniel-lang commented 6 years ago

@kunalgrover05 you have to list every migration in your connection options under migrations: [] (just like your entities) and set migrationsRun to true. This will automatically run your migrations when the connection is established.

kunalgrover05 commented 6 years ago

Thanks @daniel-lang for providing the solution. However, if I am right, TypeORM still doesn't generate migrations for sqlite right? So there's actually no way to generate migrations automatically for a Cordova / Ionic setup.

daniel-lang commented 6 years ago

With typeorm@next the generation of migrations should work for all node.js based drivers (this includes sqlite). But the browser setup doesn't support this feature.

I guess the only option, for now, would be to create a sqlite database on your computer using the same entities and a Node.js project and create migrations that way.