mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added basic sample how to use migrations
This commit is contained in:
parent
1481b9a49f
commit
43dcd5640f
39
sample/sample32-migrations/app.ts
Normal file
39
sample/sample32-migrations/app.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import "reflect-metadata";
|
||||
import {ConnectionOptions, createConnection} from "../../src/index";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Author} from "./entity/Author";
|
||||
|
||||
const options: ConnectionOptions = {
|
||||
driver: {
|
||||
type: "mysql",
|
||||
host: "localhost",
|
||||
port: 3306,
|
||||
username: "root",
|
||||
password: "admin",
|
||||
database: "test"
|
||||
},
|
||||
autoSchemaSync: true,
|
||||
logging: {
|
||||
// logQueries: true,
|
||||
// logSchemaCreation: true,
|
||||
// logFailedQueryError: true
|
||||
},
|
||||
entities: [Post, Author],
|
||||
};
|
||||
|
||||
createConnection(options).then(async connection => {
|
||||
|
||||
let author = new Author();
|
||||
author.firstName = "Umed";
|
||||
author.lastName = "Khudoiberdiev";
|
||||
|
||||
let post = new Post();
|
||||
post.title = "hello";
|
||||
post.author = author;
|
||||
|
||||
let postRepository = connection.getRepository(Post);
|
||||
|
||||
await postRepository.persist(post);
|
||||
console.log("Post has been saved");
|
||||
|
||||
}).catch(error => console.log("Error: ", error));
|
||||
15
sample/sample32-migrations/entity/Author.ts
Normal file
15
sample/sample32-migrations/entity/Author.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import {PrimaryGeneratedColumn, Column, Table} from "../../../src/index";
|
||||
|
||||
@Table()
|
||||
export class Author {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
firstName: string;
|
||||
|
||||
@Column()
|
||||
lastName: string;
|
||||
|
||||
}
|
||||
18
sample/sample32-migrations/entity/Post.ts
Normal file
18
sample/sample32-migrations/entity/Post.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import {PrimaryGeneratedColumn, Column, Table, ManyToOne} from "../../../src/index";
|
||||
import {Author} from "./Author";
|
||||
|
||||
@Table()
|
||||
export class Post {
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@ManyToOne(type => Author, {
|
||||
cascadeInsert: true
|
||||
})
|
||||
author: Author;
|
||||
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
import {MigrationInterface} from "../../../src/migration/MigrationInterface";
|
||||
import {Connection} from "../../../src/connection/Connection";
|
||||
import {QueryRunner} from "../../../src/query-runner/QueryRunner";
|
||||
|
||||
export class FirstReleaseMigration implements MigrationInterface {
|
||||
|
||||
async up(queryRunner: QueryRunner, connection: Connection): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `post` CHANGE `title` `name` VARCHAR(255)");
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner, connection: Connection): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `post` CHANGE `name` `title` VARCHAR(255)");
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user