diff --git a/src/common/classes/AuracleApi.ts b/src/common/classes/AuracleApi.ts index add3e7e..389cecd 100644 --- a/src/common/classes/AuracleApi.ts +++ b/src/common/classes/AuracleApi.ts @@ -44,8 +44,7 @@ export class AuracleApi { } /** - * Links the API with a Sequelize Database Object - * @param db_driver A Sequelize instance + * Links the API with a TypeORM connection */ private db_connect = async (db_driver: Promise) => { try { diff --git a/src/common/classes/AuracleApiController.ts b/src/common/classes/AuracleApiController.ts index 8105064..7ee505f 100644 --- a/src/common/classes/AuracleApiController.ts +++ b/src/common/classes/AuracleApiController.ts @@ -1,9 +1,9 @@ -import { AuracleApiRepository } from "./AuracleApiRepository"; +import { getRepository, Repository } from "typeorm"; export class AuracleApiController { - public repository: AuracleApiRepository + public repository: Repository - constructor(repository: AuracleApiRepository) { - this.repository = repository + constructor() { + // Todo } } diff --git a/src/common/classes/AuracleApiRepository.ts b/src/common/classes/AuracleApiRepository.ts deleted file mode 100644 index 480dca7..0000000 --- a/src/common/classes/AuracleApiRepository.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Model } from "sequelize/types"; - -export class AuracleApiRepository { - public model: Model - - constructor(model: Model) { - this.model = model - } -} diff --git a/src/controllers/SpellController.ts b/src/controllers/SpellController.ts index 4ad206a..be70436 100644 --- a/src/controllers/SpellController.ts +++ b/src/controllers/SpellController.ts @@ -1,21 +1,19 @@ import express from 'express'; +import { getRepository, Repository } from 'typeorm'; import { AuracleApiController } from "../common/classes/AuracleApiController"; import { AuracleApiResponse } from '../common/classes/AuracleApiResponse'; -import { Spell, SpellCreationAttributes } from '../database/models/spells/Spell'; -import { SpellRepository } from "../repositories/SpellRepository"; +import { Spell } from '../database/models/spells/Spell'; export class SpellController extends AuracleApiController { - public repository: SpellRepository - constructor() { - super(new SpellRepository) + super() } public async getAll(req: express.Request, res: express.Response) { let spells: Spell[] try { - spells = await this.repository.fetchAll() + spells = await getRepository(Spell).find() res = new AuracleApiResponse(200, spells).send(res) } catch (err) { res = new AuracleApiResponse(400).send(res) @@ -27,7 +25,7 @@ export class SpellController extends AuracleApiController { let spell: Spell try { - spell = await this.repository.fetchOne(uuid) + spell = await getRepository(Spell).findOne(uuid) res = new AuracleApiResponse(200, spell).send(res) } catch (err) { res = new AuracleApiResponse(400).send(res) @@ -42,10 +40,10 @@ export class SpellController extends AuracleApiController { charge: req.body.charge, cost: req.body.cost, isRitual: req.body.isRitual - } as SpellCreationAttributes + } try { - const newSpell = await this.repository.createOne(spell) + const newSpell = getRepository(Spell).insert(spell) res = new AuracleApiResponse(201, newSpell).send(res) } catch (err) { console.log(err) diff --git a/src/database/AuracleDatabaseDriver.ts b/src/database/AuracleDatabaseDriver.ts index f7e2d65..d321cf6 100644 --- a/src/database/AuracleDatabaseDriver.ts +++ b/src/database/AuracleDatabaseDriver.ts @@ -1,38 +1,8 @@ -import * as fs from 'fs' import { createConnection } from 'typeorm'; import { dbConfig } from './config' export const AuracleDatabaseDriver = createConnection(dbConfig); -/** - * Fetches all Typescript files from a given directory - * - * @param dir The folder to scan where the models are located - * @param acc (Optional) The array to start - * @returns All .ts files within the dir - */ -export const fetchAllTypescriptFiles = (dir: string, acc: string[] = []): string[] => { - fs.readdirSync(dir) - .forEach(file => { - - const fileIsTypescript = (file.split('.').pop() === 'ts') - const fileIsFolder = (file.indexOf('.') === -1) - - if (fileIsFolder) { - fetchAllTypescriptFiles(`${dir}/${file}`, acc) - } - - if (fileIsTypescript) { - acc.push(`${dir}/${file}`) - } - }) - - return acc -} - -const modelDir = __dirname + '/models' -const models = fetchAllTypescriptFiles(modelDir) - /** * If the environement is production, sync the DB */ diff --git a/src/database/config/index.ts b/src/database/config/index.ts index 00efb97..81153e7 100644 --- a/src/database/config/index.ts +++ b/src/database/config/index.ts @@ -1,5 +1,7 @@ import { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions" +import * as fs from 'fs' + import { Spell } from "../models/spells/Spell" import { School } from "../models/spells/School" import { Variable } from "../models/spells/Variable" @@ -11,6 +13,35 @@ import { Role } from "../models/users/Role" const tablePrefix = 'au_' +/** + * Fetches all Typescript files from a given directory + * + * @param dir The folder to scan where the models are located + * @param acc (Optional) The array to start + * @returns All .ts files within the dir + */ +export const fetchAllTypescriptFiles = (dir: string, acc: string[] = []): string[] => { + fs.readdirSync(dir) + .forEach(file => { + + const fileIsTypescript = (file.split('.').pop() === 'ts') + const fileIsFolder = (file.indexOf('.') === -1) + + if (fileIsFolder) { + fetchAllTypescriptFiles(`${dir}/${file}`, acc) + } + + if (fileIsTypescript) { + acc.push(`${dir}/${file}`) + } + }) + + return acc +} + +const modelDir = './src/database/models/' +const models = fetchAllTypescriptFiles(modelDir) + /** * Registers the options to create the sequelize instance */ @@ -26,16 +57,5 @@ export const dbConfig: MysqlConnectionOptions = { logging: false, entityPrefix: tablePrefix, - entities: [ - User, - Permission, - Role, - - Spell, - School, - MetaSchool, - - Variable, - Ingredient, - ] + entities: models } diff --git a/src/main.ts b/src/main.ts index e1bb6fc..61ba26f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -24,31 +24,18 @@ const apiPort = process.env.API_PORT const app = new AuracleApi({ port: apiPort, + database: AuracleDatabaseDriver, + middlewares: [], - routers: [], + routers: [ + new SpellRouter + ], modules: [ express.json({ limit: '10kb' }), morgan('dev'), helmet() ], - database: AuracleDatabaseDriver }) // Listens on apiPort... app.listen() - -// const routes = require('./routes'); - -// // Server -// app.listen(port, () => console.log(`App listening on port ${port}`)); - -// // Entry route -// app.use('/api/v1/', routes.auth); - -// // Routing -// app.use('/api/v1/spells', routes.spells); -// app.use('/api/v1/schools', routes.schools); -// app.use('/api/v1/meta_schools', routes.meta_schools); -// app.use('/api/v1/variables', routes.variables); -// app.use('/api/v1/ingredients', routes.ingredients); -// app.use('/api/v1/users', routes.users); diff --git a/src/repositories/SpellRepository.ts b/src/repositories/SpellRepository.ts deleted file mode 100644 index 8220736..0000000 --- a/src/repositories/SpellRepository.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { AuracleApiRepository } from "../common/classes/AuracleApiRepository" -import { Spell, SpellCreationAttributes } from "../database/models/spells/Spell" - -export class SpellRepository extends AuracleApiRepository { - constructor() { - super(new Spell) - } - - public async fetchAll(): Promise { - return Spell.findAll() - } - - public async fetchOne(uuid: string): Promise { - return Spell.findByPk(uuid) - } - - public async createOne(spell: SpellCreationAttributes): Promise { - return Spell.create(spell) - } -}