Updated Repository
Repositories and controllers are merged, since the functions would just be really redundant thanks to TypeORM
This commit is contained in:
@@ -44,8 +44,7 @@ export class AuracleApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Links the API with a Sequelize Database Object
|
* Links the API with a TypeORM connection
|
||||||
* @param db_driver A Sequelize instance
|
|
||||||
*/
|
*/
|
||||||
private db_connect = async (db_driver: Promise<Connection>) => {
|
private db_connect = async (db_driver: Promise<Connection>) => {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { AuracleApiRepository } from "./AuracleApiRepository";
|
import { getRepository, Repository } from "typeorm";
|
||||||
|
|
||||||
export class AuracleApiController {
|
export class AuracleApiController {
|
||||||
public repository: AuracleApiRepository
|
public repository: Repository<any>
|
||||||
|
|
||||||
constructor(repository: AuracleApiRepository) {
|
constructor() {
|
||||||
this.repository = repository
|
// Todo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
import { Model } from "sequelize/types";
|
|
||||||
|
|
||||||
export class AuracleApiRepository {
|
|
||||||
public model: Model<any, any>
|
|
||||||
|
|
||||||
constructor(model: Model<any, any>) {
|
|
||||||
this.model = model
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +1,19 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
|
import { getRepository, Repository } from 'typeorm';
|
||||||
import { AuracleApiController } from "../common/classes/AuracleApiController";
|
import { AuracleApiController } from "../common/classes/AuracleApiController";
|
||||||
import { AuracleApiResponse } from '../common/classes/AuracleApiResponse';
|
import { AuracleApiResponse } from '../common/classes/AuracleApiResponse';
|
||||||
import { Spell, SpellCreationAttributes } from '../database/models/spells/Spell';
|
import { Spell } from '../database/models/spells/Spell';
|
||||||
import { SpellRepository } from "../repositories/SpellRepository";
|
|
||||||
|
|
||||||
export class SpellController extends AuracleApiController {
|
export class SpellController extends AuracleApiController {
|
||||||
public repository: SpellRepository
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
super(new SpellRepository)
|
super()
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getAll(req: express.Request, res: express.Response) {
|
public async getAll(req: express.Request, res: express.Response) {
|
||||||
let spells: Spell[]
|
let spells: Spell[]
|
||||||
|
|
||||||
try {
|
try {
|
||||||
spells = await this.repository.fetchAll()
|
spells = await getRepository(Spell).find()
|
||||||
res = new AuracleApiResponse(200, spells).send(res)
|
res = new AuracleApiResponse(200, spells).send(res)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res = new AuracleApiResponse(400).send(res)
|
res = new AuracleApiResponse(400).send(res)
|
||||||
@@ -27,7 +25,7 @@ export class SpellController extends AuracleApiController {
|
|||||||
let spell: Spell
|
let spell: Spell
|
||||||
|
|
||||||
try {
|
try {
|
||||||
spell = await this.repository.fetchOne(uuid)
|
spell = await getRepository(Spell).findOne(uuid)
|
||||||
res = new AuracleApiResponse(200, spell).send(res)
|
res = new AuracleApiResponse(200, spell).send(res)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
res = new AuracleApiResponse(400).send(res)
|
res = new AuracleApiResponse(400).send(res)
|
||||||
@@ -42,10 +40,10 @@ export class SpellController extends AuracleApiController {
|
|||||||
charge: req.body.charge,
|
charge: req.body.charge,
|
||||||
cost: req.body.cost,
|
cost: req.body.cost,
|
||||||
isRitual: req.body.isRitual
|
isRitual: req.body.isRitual
|
||||||
} as SpellCreationAttributes
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const newSpell = await this.repository.createOne(spell)
|
const newSpell = getRepository(Spell).insert(spell)
|
||||||
res = new AuracleApiResponse(201, newSpell).send(res)
|
res = new AuracleApiResponse(201, newSpell).send(res)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
|
|||||||
@@ -1,38 +1,8 @@
|
|||||||
import * as fs from 'fs'
|
|
||||||
import { createConnection } from 'typeorm';
|
import { createConnection } from 'typeorm';
|
||||||
import { dbConfig } from './config'
|
import { dbConfig } from './config'
|
||||||
|
|
||||||
export const AuracleDatabaseDriver = createConnection(dbConfig);
|
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
|
* If the environement is production, sync the DB
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
import { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions"
|
import { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions"
|
||||||
|
|
||||||
|
import * as fs from 'fs'
|
||||||
|
|
||||||
import { Spell } from "../models/spells/Spell"
|
import { Spell } from "../models/spells/Spell"
|
||||||
import { School } from "../models/spells/School"
|
import { School } from "../models/spells/School"
|
||||||
import { Variable } from "../models/spells/Variable"
|
import { Variable } from "../models/spells/Variable"
|
||||||
@@ -11,6 +13,35 @@ import { Role } from "../models/users/Role"
|
|||||||
|
|
||||||
const tablePrefix = 'au_'
|
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
|
* Registers the options to create the sequelize instance
|
||||||
*/
|
*/
|
||||||
@@ -26,16 +57,5 @@ export const dbConfig: MysqlConnectionOptions = {
|
|||||||
logging: false,
|
logging: false,
|
||||||
|
|
||||||
entityPrefix: tablePrefix,
|
entityPrefix: tablePrefix,
|
||||||
entities: [
|
entities: models
|
||||||
User,
|
|
||||||
Permission,
|
|
||||||
Role,
|
|
||||||
|
|
||||||
Spell,
|
|
||||||
School,
|
|
||||||
MetaSchool,
|
|
||||||
|
|
||||||
Variable,
|
|
||||||
Ingredient,
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
|||||||
23
src/main.ts
23
src/main.ts
@@ -24,31 +24,18 @@ const apiPort = process.env.API_PORT
|
|||||||
|
|
||||||
const app = new AuracleApi({
|
const app = new AuracleApi({
|
||||||
port: apiPort,
|
port: apiPort,
|
||||||
|
database: AuracleDatabaseDriver,
|
||||||
|
|
||||||
middlewares: [],
|
middlewares: [],
|
||||||
routers: [],
|
routers: [
|
||||||
|
new SpellRouter
|
||||||
|
],
|
||||||
modules: [
|
modules: [
|
||||||
express.json({ limit: '10kb' }),
|
express.json({ limit: '10kb' }),
|
||||||
morgan('dev'),
|
morgan('dev'),
|
||||||
helmet()
|
helmet()
|
||||||
],
|
],
|
||||||
database: AuracleDatabaseDriver
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Listens on apiPort...
|
// Listens on apiPort...
|
||||||
app.listen()
|
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);
|
|
||||||
|
|||||||
@@ -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<Spell[]> {
|
|
||||||
return Spell.findAll()
|
|
||||||
}
|
|
||||||
|
|
||||||
public async fetchOne(uuid: string): Promise<Spell> {
|
|
||||||
return Spell.findByPk(uuid)
|
|
||||||
}
|
|
||||||
|
|
||||||
public async createOne(spell: SpellCreationAttributes): Promise<Spell> {
|
|
||||||
return Spell.create(spell)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user