Merge pull request 'New Feature: User seeding feature/19-user_seeding' (#26) from feature/19-user_seeding into dev
Reviewed-on: #26 closes #19
This commit was merged in pull request #26.
	This commit is contained in:
		
							
								
								
									
										25
									
								
								ormconfig.ts
									
									
									
									
									
								
							
							
						
						
									
										25
									
								
								ormconfig.ts
									
									
									
									
									
								
							@@ -1,12 +1,13 @@
 | 
				
			|||||||
import { config } from 'dotenv';
 | 
					import { config } from 'dotenv';
 | 
				
			||||||
config();
 | 
					config();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
	type: process.env.DB_TYPE,
 | 
						type: process.env.DB_TYPE,
 | 
				
			||||||
	host: process.env.DB_HOST,
 | 
						host: process.env.DB_HOST,
 | 
				
			||||||
	port: process.env.DB_PORT,
 | 
						port: process.env.DB_PORT,
 | 
				
			||||||
	username: process.env.DB_USER,
 | 
						username: process.env.DB_USER,
 | 
				
			||||||
	password: process.env.DB_PASSWORD,
 | 
						password: process.env.DB_PASSWORD,
 | 
				
			||||||
	database: process.env.DB_NAME,
 | 
						database: process.env.DB_NAME,
 | 
				
			||||||
	entities: ["src/models/entities/*.ts"]
 | 
						entities: ["src/models/entities/*.ts"],
 | 
				
			||||||
};
 | 
						seeds: ['src/seeds/*.ts'],
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -42,6 +42,7 @@
 | 
				
			|||||||
    "swagger-ui-express": "^4.1.5",
 | 
					    "swagger-ui-express": "^4.1.5",
 | 
				
			||||||
    "typeorm": "^0.2.29",
 | 
					    "typeorm": "^0.2.29",
 | 
				
			||||||
    "typeorm-routing-controllers-extensions": "^0.2.0",
 | 
					    "typeorm-routing-controllers-extensions": "^0.2.0",
 | 
				
			||||||
 | 
					    "typeorm-seeding": "^1.6.1",
 | 
				
			||||||
    "uuid": "^8.3.1"
 | 
					    "uuid": "^8.3.1"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
@@ -71,7 +72,8 @@
 | 
				
			|||||||
    "docs": "typedoc --out docs src",
 | 
					    "docs": "typedoc --out docs src",
 | 
				
			||||||
    "test": "jest",
 | 
					    "test": "jest",
 | 
				
			||||||
    "test:watch": "jest --watchAll",
 | 
					    "test:watch": "jest --watchAll",
 | 
				
			||||||
    "test:ci": "start-server-and-test dev http://localhost:4010/api/openapi.json test"
 | 
					    "test:ci": "start-server-and-test dev http://localhost:4010/api/openapi.json test",
 | 
				
			||||||
 | 
					    "seed": "ts-node ./node_modules/typeorm/cli.js schema:sync && ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "nodemonConfig": {
 | 
					  "nodemonConfig": {
 | 
				
			||||||
    "ignore": [
 | 
					    "ignore": [
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,10 +1,15 @@
 | 
				
			|||||||
import { createConnection } from "typeorm";
 | 
					import { createConnection } from "typeorm";
 | 
				
			||||||
 | 
					import { runSeeder } from 'typeorm-seeding';
 | 
				
			||||||
 | 
					import { User } from '../models/entities/User';
 | 
				
			||||||
 | 
					import SeedUsers from '../seeds/SeedUsers';
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Loader for the database that creates the database connection and initializes the database tabels.
 | 
					 * Loader for the database that creates the database connection and initializes the database tabels.
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export default async () => {
 | 
					export default async () => {
 | 
				
			||||||
    const connection = await createConnection();
 | 
					    const connection = await createConnection();
 | 
				
			||||||
    connection.synchronize();
 | 
					    await connection.synchronize();
 | 
				
			||||||
 | 
					    if (await connection.getRepository(User).count() === 0) {
 | 
				
			||||||
 | 
					        await runSeeder(SeedUsers);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    return connection;
 | 
					    return connection;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
							
								
								
									
										20
									
								
								src/seeds/SeedUsers.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								src/seeds/SeedUsers.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					import { Connection } from 'typeorm';
 | 
				
			||||||
 | 
					import { Factory, Seeder } from 'typeorm-seeding';
 | 
				
			||||||
 | 
					import { CreateUser } from '../models/actions/CreateUser';
 | 
				
			||||||
 | 
					import { User } from '../models/entities/User';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default class SeedUsers implements Seeder {
 | 
				
			||||||
 | 
					    public async run(factory: Factory, connection: Connection): Promise<any> {
 | 
				
			||||||
 | 
					        let initialUser = new CreateUser();
 | 
				
			||||||
 | 
					        initialUser.firstname = "demo";
 | 
				
			||||||
 | 
					        initialUser.lastname = "demo";
 | 
				
			||||||
 | 
					        initialUser.username = "demo";
 | 
				
			||||||
 | 
					        initialUser.password = "demo";
 | 
				
			||||||
 | 
					        await connection
 | 
				
			||||||
 | 
					            .createQueryBuilder()
 | 
				
			||||||
 | 
					            .insert()
 | 
				
			||||||
 | 
					            .into(User)
 | 
				
			||||||
 | 
					            .values([await initialUser.toUser()])
 | 
				
			||||||
 | 
					            .execute()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user