File tree Expand file tree Collapse file tree 9 files changed +88
-37
lines changed Expand file tree Collapse file tree 9 files changed +88
-37
lines changed Original file line number Diff line number Diff line change @@ -18,12 +18,13 @@ export class Pet {
18
18
public age : number ;
19
19
20
20
@Column ( {
21
+ name : 'user_id' ,
21
22
nullable : true ,
22
23
} )
23
24
public userId : number ;
24
25
25
26
@ManyToOne ( type => User , user => user . pets )
26
- @JoinColumn ( { name : 'userId ' } )
27
+ @JoinColumn ( { name : 'user_id ' } )
27
28
public user : User ;
28
29
29
30
public toString ( ) : string {
Original file line number Diff line number Diff line change @@ -5,12 +5,12 @@ import { Pet } from '../models/Pet';
5
5
export class PetRepository extends Repository < Pet > {
6
6
7
7
/**
8
- * Find by userId is used for our data-loader to get all needed pets in one query.
8
+ * Find by user_id is used for our data-loader to get all needed pets in one query.
9
9
*/
10
10
public findByUserIds ( ids : string [ ] ) : Promise < Pet [ ] > {
11
11
return this . createQueryBuilder ( )
12
12
. select ( )
13
- . where ( `pet.userId IN (${ ids . map ( id => `'${ id } '` ) . join ( ', ' ) } )` )
13
+ . where ( `pet.user_id IN (${ ids . map ( id => `'${ id } '` ) . join ( ', ' ) } )` )
14
14
. getMany ( ) ;
15
15
}
16
16
Original file line number Diff line number Diff line change 1
- import { MigrationInterface , QueryRunner } from 'typeorm' ;
1
+ import { MigrationInterface , QueryRunner , Table } from 'typeorm' ;
2
2
3
3
export class CreateUserTable1511105183653 implements MigrationInterface {
4
4
5
5
public async up ( queryRunner : QueryRunner ) : Promise < any > {
6
- await queryRunner . query ( `
7
- CREATE TABLE \`user\` (
8
- \`id\` varchar(255) NOT NULL PRIMARY KEY,
9
- \`first_name\` varchar(255) NOT NULL,
10
- \`last_name\` varchar(255) NOT NULL,
11
- \`email\` varchar(255) NOT NULL) ENGINE=InnoDB;`
12
- ) ;
6
+ const table = new Table ( 'user' , [
7
+ {
8
+ name : 'id' ,
9
+ type : 'varchar' ,
10
+ length : 255 ,
11
+ isPrimary : true ,
12
+ isNullable : false ,
13
+ } , {
14
+ name : 'first_name' ,
15
+ type : 'varchar' ,
16
+ length : 255 ,
17
+ isPrimary : false ,
18
+ isNullable : false ,
19
+ } , {
20
+ name : 'last_name' ,
21
+ type : 'varchar' ,
22
+ length : 255 ,
23
+ isPrimary : false ,
24
+ isNullable : false ,
25
+ } , {
26
+ name : 'email' ,
27
+ type : 'varchar' ,
28
+ length : 255 ,
29
+ isPrimary : false ,
30
+ isNullable : false ,
31
+ } ,
32
+ ] ) ;
33
+ await queryRunner . createTable ( table ) ;
13
34
}
14
35
15
36
public async down ( queryRunner : QueryRunner ) : Promise < any > {
16
- await queryRunner . query ( `DROP TABLE \` user\`;` ) ;
37
+ await queryRunner . dropTable ( ' user' ) ;
17
38
}
18
39
19
40
}
Original file line number Diff line number Diff line change 1
- import { MigrationInterface , QueryRunner } from 'typeorm' ;
1
+ import { MigrationInterface , QueryRunner , Table } from 'typeorm' ;
2
2
3
3
export class CreatePetTable1512663524808 implements MigrationInterface {
4
4
5
5
public async up ( queryRunner : QueryRunner ) : Promise < any > {
6
- await queryRunner . query ( `
7
- CREATE TABLE \`pet\` (
8
- \`id\` varchar(255) NOT NULL PRIMARY KEY,
9
- \`name\` varchar(255) NOT NULL,
10
- \`age\` int(11) NOT NULL,
11
- \`userId\` varchar(255)) ENGINE=InnoDB;`
12
- ) ;
6
+ const table = new Table ( 'pet' , [
7
+ {
8
+ name : 'id' ,
9
+ type : 'varchar' ,
10
+ length : 255 ,
11
+ isPrimary : true ,
12
+ isNullable : false ,
13
+ } , {
14
+ name : 'name' ,
15
+ type : 'varchar' ,
16
+ length : 255 ,
17
+ isPrimary : false ,
18
+ isNullable : false ,
19
+ } , {
20
+ name : 'age' ,
21
+ type : 'int' ,
22
+ length : 11 ,
23
+ isPrimary : false ,
24
+ isNullable : false ,
25
+ } , {
26
+ name : 'user_id' ,
27
+ type : 'varchar' ,
28
+ length : 255 ,
29
+ isPrimary : false ,
30
+ isNullable : true ,
31
+ } ,
32
+ ] ) ;
33
+ await queryRunner . createTable ( table ) ;
13
34
}
14
35
15
36
public async down ( queryRunner : QueryRunner ) : Promise < any > {
16
- await queryRunner . query ( `DROP TABLE \` pet\`;` ) ;
37
+ await queryRunner . dropTable ( ' pet' ) ;
17
38
}
18
39
19
40
}
Original file line number Diff line number Diff line change 1
- import { MigrationInterface , QueryRunner } from 'typeorm' ;
1
+ import { MigrationInterface , QueryRunner , TableForeignKey } from 'typeorm' ;
2
2
3
3
export class AddUserRelationToPetTable1512663990063 implements MigrationInterface {
4
4
5
+ private tableForeignKey = new TableForeignKey (
6
+ 'fk_user_pet' ,
7
+ [ 'user_id' ] ,
8
+ [ 'id' ] ,
9
+ 'user' ,
10
+ ''
11
+ ) ;
12
+
5
13
public async up ( queryRunner : QueryRunner ) : Promise < any > {
6
- await queryRunner . query ( `
7
- ALTER TABLE \`pet\`
8
- ADD CONSTRAINT \`fk_user_pet\`
9
- FOREIGN KEY (\`userId\`)
10
- REFERENCES \`user\`(\`id\`);`
11
- ) ;
14
+ await queryRunner . createForeignKey ( 'pet' , this . tableForeignKey ) ;
12
15
}
13
16
14
17
public async down ( queryRunner : QueryRunner ) : Promise < any > {
15
- await queryRunner . query ( `
16
- ALTER TABLE \`pet\`
17
- DROP FOREIGN KEY \`fk_user_pet\`;`
18
- ) ;
18
+ await queryRunner . dropForeignKey ( 'pet' , this . tableForeignKey ) ;
19
19
}
20
20
21
21
}
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ export const typeormLoader: MicroframeworkLoader = async (settings: Microframewo
15
15
synchronize : env . db . synchronize ,
16
16
logging : env . db . logging ,
17
17
entities : env . app . dirs . entities ,
18
+ migrations : env . app . dirs . migrations ,
18
19
} ) ;
19
20
20
21
if ( settings ) {
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { getFactory } from './../../../src/lib/seeds/index';
5
5
import { Factory } from './../../../src/lib/seeds/Factory' ;
6
6
import { User } from './../../../src/api/models/User' ;
7
7
import { bootstrapApp , BootstrapSettings } from '../utils/bootstrap' ;
8
- import { synchronizeDatabase , closeDatabase } from '../../utils/database' ;
8
+ import { migrateDatabase , closeDatabase } from '../../utils/database' ;
9
9
import { fakeAuthenticationForUser } from '../utils/auth' ;
10
10
11
11
@@ -20,7 +20,7 @@ describe('/api/users', () => {
20
20
let bruce : User ;
21
21
let authServer : nock . Scope ;
22
22
beforeAll ( async ( ) => settings = await bootstrapApp ( ) ) ;
23
- beforeAll ( async ( ) => synchronizeDatabase ( settings . connection ) ) ;
23
+ beforeAll ( async ( ) => migrateDatabase ( settings . connection ) ) ;
24
24
beforeAll ( async ( ) => factory = getFactory ( settings . connection ) ) ;
25
25
beforeAll ( async ( ) => bruce = await factory . runSeed < User > ( CreateBruce ) ) ;
26
26
beforeAll ( async ( ) => authServer = fakeAuthenticationForUser ( bruce , true ) ) ;
Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ import { Connection } from 'typeorm';
3
3
4
4
import { Pet } from '../../src/api/models/Pet' ;
5
5
import { PetService } from './../../src/api/services/PetService' ;
6
- import { createDatabaseConnection , synchronizeDatabase , closeDatabase } from '../utils/database' ;
6
+ import { createDatabaseConnection , migrateDatabase , closeDatabase } from '../utils/database' ;
7
7
8
8
describe ( 'PetService' , ( ) => {
9
9
@@ -13,7 +13,7 @@ describe('PetService', () => {
13
13
14
14
let connection : Connection ;
15
15
beforeAll ( async ( ) => connection = await createDatabaseConnection ( ) ) ;
16
- beforeEach ( ( ) => synchronizeDatabase ( connection ) ) ;
16
+ beforeEach ( ( ) => migrateDatabase ( connection ) ) ;
17
17
18
18
// -------------------------------------------------------------------------
19
19
// Tear down
Original file line number Diff line number Diff line change @@ -10,14 +10,21 @@ export const createDatabaseConnection = async (): Promise<Connection> => {
10
10
database : env . db . database ,
11
11
logging : env . db . logging ,
12
12
entities : env . app . dirs . entities ,
13
+ migrations : env . app . dirs . migrations ,
13
14
} ) ;
14
15
return connection ;
15
16
} ;
16
17
17
- export const synchronizeDatabase = ( connection : Connection ) => {
18
+ export const synchronizeDatabase = async ( connection : Connection ) => {
19
+ await connection . dropDatabase ( ) ;
18
20
return connection . synchronize ( true ) ;
19
21
} ;
20
22
23
+ export const migrateDatabase = async ( connection : Connection ) => {
24
+ await connection . dropDatabase ( ) ;
25
+ return connection . runMigrations ( ) ;
26
+ } ;
27
+
21
28
export const closeDatabase = ( connection : Connection ) => {
22
29
return connection . close ( ) ;
23
30
} ;
You can’t perform that action at this time.
0 commit comments