Skip to content

Commit

Permalink
delete pings and add event route for importDb script
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitSerrano committed Oct 20, 2024
1 parent 4717f00 commit 3e5ea82
Show file tree
Hide file tree
Showing 15 changed files with 75 additions and 143 deletions.
3 changes: 1 addition & 2 deletions src/dataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { DataSource } from 'typeorm';
import { config } from './config';

import { Client } from './modules/client';
import { Ping } from './modules/ping';
import { Event } from './modules/event';

const dataSource = new DataSource({
Expand All @@ -14,7 +13,7 @@ const dataSource = new DataSource({
database: config.DATABASE_NAME,
logging: ['warn', 'error'],
connectTimeoutMS: 20000,
entities: [Client, Ping, Event],
entities: [Client, Event],
subscribers: [],
migrations: ['**/migrations/*.js'],
});
Expand Down
10 changes: 5 additions & 5 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Client } from '../modules/client';
import { Ping } from '../modules/ping';
import { Event } from '../modules/event';

const api = {
fetchAllClients,
fetchAllPings,
fetchAllEvents,
};
const BASE_URL = 'https://ping-storage.osc-fr1.scalingo.io';

async function fetchAllClients(): Promise<Client[]> {
const URL = `${BASE_URL}/api/clients`;
const URL = `${BASE_URL}/api/all-clients`;

const response = await fetch(URL);
const parsedData = await response.json();
return parsedData;
}

async function fetchAllPings(): Promise<Ping[]> {
const URL = `${BASE_URL}/api/pings`;
async function fetchAllEvents(): Promise<Event[]> {
const URL = `${BASE_URL}/api/all-events`;

const response = await fetch(URL);
const parsedData = await response.json();
Expand Down
19 changes: 19 additions & 0 deletions src/migrations/1729425458873-delete-pings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class DeletePings1729425458873 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "ping" DROP CONSTRAINT "FK_eaee4e2004faa8d8af53024ed8f"`,
);
await queryRunner.query(`DROP TABLE "ping"`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "ping" ("id" SERIAL NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "clientId" uuid NOT NULL, CONSTRAINT "PK_b01cab9d614b77bac5973937663" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "ping" ADD CONSTRAINT "FK_eaee4e2004faa8d8af53024ed8f" FOREIGN KEY ("clientId") REFERENCES "client"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}
}
2 changes: 0 additions & 2 deletions src/modules/client/client.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { dataSource } from '../../dataSource';
import { buildEventService } from '../event';
import { buildPingService } from '../ping';
import { Client } from './Client.entity';

export { buildClientService };

function buildClientService() {
const clientRepository = dataSource.getRepository(Client);
const pingService = buildPingService();
const eventService = buildEventService();
const clientService = {
createClient,
Expand Down
16 changes: 16 additions & 0 deletions src/modules/event/event.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { buildEventService } from './event.service';

export { buildEventController };

function buildEventController() {
const eventService = buildEventService();
const eventController = {
getAllEvents,
};

return eventController;

async function getAllEvents() {
return eventService.getAllEvents();
}
}
7 changes: 7 additions & 0 deletions src/modules/event/event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ function buildEventService() {
createEvent,
getLastEvent,
getEvents,
getAllEvents,
};

return eventService;
Expand All @@ -31,6 +32,12 @@ function buildEventService() {
});
}

async function getAllEvents() {
return eventRepository.find({
relations: ['client'],
});
}

async function createEvent(
clientId: Client['id'],
params: { title: Event['title']; kind: Event['kind'] },
Expand Down
4 changes: 2 additions & 2 deletions src/modules/event/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Event } from './Event.entity';
import { buildEventService } from './event.service';

export { Event, buildEventService };
import { buildEventController } from './event.controller';
export { Event, buildEventService, buildEventController };
14 changes: 0 additions & 14 deletions src/modules/ping/Ping.entity.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/modules/ping/index.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/modules/ping/ping.controller.ts

This file was deleted.

63 changes: 0 additions & 63 deletions src/modules/ping/ping.service.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/router/eventRoutes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Joi from 'joi';
import { buildEventController } from '../modules/event';
import { routeType } from './types';

const eventController = buildEventController();

const eventRoutes: Array<routeType<any, any, any>> = [
{
method: 'GET',
path: '/all-events',
controller: eventController.getAllEvents,
},
];

export { eventRoutes };
19 changes: 0 additions & 19 deletions src/router/pingRoutes.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/router/routes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { clientRoutes } from './clientRoutes';
import { pingRoutes } from './pingRoutes';
import { eventRoutes } from './eventRoutes';
import { routeType } from './types';

const routes = buildRoutes();

function buildRoutes() {
const routes: routeType<any, any, any>[] = [];
routes.push(...clientRoutes);
routes.push(...pingRoutes);
routes.push(...eventRoutes);
return routes;
}

Expand Down
16 changes: 8 additions & 8 deletions src/scripts/importDb.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { dataSource } from '../dataSource';
import { api } from '../lib/api';
import { Client } from '../modules/client';
import { Ping } from '../modules/ping';
import { Event } from '../modules/event';

async function importDb() {
console.log('Initializing database...');
await dataSource.initialize();
console.log('Database initialized!');
const clientRepository = dataSource.getRepository(Client);
const pingRepository = dataSource.getRepository(Ping);
const eventRepository = dataSource.getRepository(Event);

console.log('Erasing local database...');

await clientRepository.delete({});
await pingRepository.delete({});
await eventRepository.delete({});

console.log('Fetching clients...');
const allClients = await api.fetchAllClients();
console.log(`${allClients.length} clients fetched! Inserting them in database...`);

await clientRepository.insert(allClients);
console.log('Clients inserted! Now fetching pings...');
console.log('Clients inserted! Now fetching events...');

const allPings = await api.fetchAllPings();
const allEvents = await api.fetchAllEvents();

console.log(`${allPings.length} pings fetched! Inserting them in database...`);
console.log(`${allEvents.length} events fetched! Inserting them in database...`);

await pingRepository.insert(allPings);
console.log(`${allPings.length} pings inserted!`);
await eventRepository.insert(allEvents);
console.log(`${allEvents.length} events inserted!`);

console.log('Done!');
}
Expand Down

0 comments on commit 3e5ea82

Please sign in to comment.