diff --git a/src/common/classes/AuracleApi.ts b/src/common/classes/AuracleApi.ts index d031d9d..7faa7a4 100644 --- a/src/common/classes/AuracleApi.ts +++ b/src/common/classes/AuracleApi.ts @@ -1,7 +1,6 @@ import express, { Application } from 'express' -import { Sequelize } from 'sequelize' +import { Connection } from 'typeorm' import { AuracleApiRouter } from './AuracleApiRouter' - export class AuracleApi { public app: Application public port: number @@ -11,7 +10,7 @@ export class AuracleApi { constructor( init: { port: any - database: Sequelize + database: Promise routers?: Array modules?: Array middlewares?: Array @@ -48,11 +47,15 @@ export class AuracleApi { * Links the API with a Sequelize Database Object * @param db_driver A Sequelize instance */ - private db_connect = async (db_driver: Sequelize) => { + private db_connect = async (db_driver: Promise) => { try { - const connection = await db_driver.authenticate(); + const connection = await db_driver console.info('Connection has been established successfully.') + // Builds the data schema from registered entities + await connection.synchronize() + console.info('Database schema has been generated successfully.') + return connection } catch (err) { console.error('Unable to connect to the database.') diff --git a/src/database/AuracleDatabaseDriver.ts b/src/database/AuracleDatabaseDriver.ts index 534d3fc..f7e2d65 100644 --- a/src/database/AuracleDatabaseDriver.ts +++ b/src/database/AuracleDatabaseDriver.ts @@ -1,11 +1,8 @@ -import { Sequelize } from 'sequelize' import * as fs from 'fs' +import { createConnection } from 'typeorm'; import { dbConfig } from './config' -export let AuracleDatabaseDriver: Sequelize; - -// Creates the sequelize instance -AuracleDatabaseDriver = new Sequelize(dbConfig) +export const AuracleDatabaseDriver = createConnection(dbConfig); /** * Fetches all Typescript files from a given directory @@ -36,16 +33,9 @@ export const fetchAllTypescriptFiles = (dir: string, acc: string[] = []): string const modelDir = __dirname + '/models' const models = fetchAllTypescriptFiles(modelDir) -models.forEach(modelPath => { - const registerModel = require(modelPath).default as Function - registerModel() -}) - /** * If the environement is production, sync the DB */ if (process.env.NODE_ENV != 'production') { - AuracleDatabaseDriver.sync({ - force: true, - }) + // Todo } diff --git a/src/database/config/index.ts b/src/database/config/index.ts index 83aa66e..00efb97 100644 --- a/src/database/config/index.ts +++ b/src/database/config/index.ts @@ -1,24 +1,41 @@ -import { Model, ModelOptions, Options } from "sequelize/types" +import { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions" + +import { Spell } from "../models/spells/Spell" +import { School } from "../models/spells/School" +import { Variable } from "../models/spells/Variable" +import { Ingredient } from "../models/spells/Ingredient" +import { MetaSchool } from "../models/spells/MetaSchool" +import { User } from "../models/users/User" +import { Permission } from "../models/users/Permission" +import { Role } from "../models/users/Role" const tablePrefix = 'au_' /** * Registers the options to create the sequelize instance */ -export const dbConfig: Options = { +export const dbConfig: MysqlConnectionOptions = { /** * Access to database */ + type: 'mariadb', database: process.env.DB_NAME, username: process.env.DB_USER, password: process.env.DB_PASSWORD, host: process.env.DB_HOST, - dialect: 'mariadb', logging: false, - /** - * Sequelize Hooks - * Docs : https://sequelize.org/master/manual/hooks.html - */ - hooks: {} + entityPrefix: tablePrefix, + entities: [ + User, + Permission, + Role, + + Spell, + School, + MetaSchool, + + Variable, + Ingredient, + ] } diff --git a/src/database/models/spells/Ingredient.ts b/src/database/models/spells/Ingredient.ts index 91ebffa..fa1f297 100644 --- a/src/database/models/spells/Ingredient.ts +++ b/src/database/models/spells/Ingredient.ts @@ -1,56 +1,15 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface IngredientAttributes { - uuid: string - name: string - description: string - - published: boolean -} - -interface IngredientCreationAttributes extends Optional { } - -export class Ingredient extends Model implements IngredientAttributes { +@Entity() +export class Ingredient { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public name!: string + @Column() public description!: string + @Column() public published: boolean - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - Ingredient.init( - { - uuid: { - type: DataTypes.UUID, - unique: true, - allowNull: false, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - name: { - type: DataTypes.STRING, - allowNull: false, - unique: true, - }, - description: { - type: DataTypes.STRING, - allowNull: false, - }, - - published: { - type: DataTypes.BOOLEAN, - defaultValue: false - } - }, - { - tableName: 'au_ingredients', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/spells/MetaSchool.ts b/src/database/models/spells/MetaSchool.ts index 3d28014..3984f00 100644 --- a/src/database/models/spells/MetaSchool.ts +++ b/src/database/models/spells/MetaSchool.ts @@ -1,56 +1,15 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm" -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface MetaSchoolAttributes { - uuid: string - name: string - description: string - - published: boolean -} - -interface MetaSchoolCreationAttributes extends Optional { } - -export class MetaSchool extends Model implements MetaSchoolAttributes { +@Entity() +export class MetaSchool { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public name!: string + @Column() public description!: string + @Column() public published: boolean - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - MetaSchool.init( - { - uuid: { - type: DataTypes.UUID, - unique: true, - allowNull: false, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - name: { - type: DataTypes.STRING, - allowNull: false, - unique: true, - }, - description: { - type: DataTypes.STRING, - allowNull: false, - }, - - published: { - type: DataTypes.BOOLEAN, - defaultValue: false - } - }, - { - tableName: 'au_meta_schools', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/spells/School.ts b/src/database/models/spells/School.ts index 2757988..26448df 100644 --- a/src/database/models/spells/School.ts +++ b/src/database/models/spells/School.ts @@ -1,56 +1,15 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface SchoolAttributes { - uuid: string - name: string - description: string - - published: boolean -} - -interface SchoolCreationAttributes extends Optional { } - -export class School extends Model implements SchoolAttributes { +@Entity() +export class School { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public name!: string + @Column() public description!: string + @Column() public published: boolean - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - School.init( - { - uuid: { - type: DataTypes.UUID, - unique: true, - allowNull: false, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - name: { - type: DataTypes.STRING, - allowNull: false, - unique: true, - }, - description: { - type: DataTypes.STRING, - allowNull: false, - }, - - published: { - type: DataTypes.BOOLEAN, - defaultValue: false - } - }, - { - tableName: 'au_schools', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/spells/Spell.ts b/src/database/models/spells/Spell.ts index a1fd441..91813a3 100644 --- a/src/database/models/spells/Spell.ts +++ b/src/database/models/spells/Spell.ts @@ -1,89 +1,27 @@ -import { DataTypes, Model, Optional, TableHints } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm" -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface SpellAttributes { - uuid: string - name: string - description: string - - level?: number - charge?: number - cost?: string - isRitual?: boolean - - published?: boolean - public?: boolean -} - -export interface SpellCreationAttributes extends Optional { } - -export class Spell extends Model implements SpellAttributes { +@Entity() +export class Spell { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string - public name!: string - public description!: string + @Column() + public name!: string + + @Column() + public description!: string + @Column() public level?: number + + @Column() public charge?: number + @Column() public cost?: string + @Column() public isRitual?: boolean + @Column() public published?: boolean + @Column() public public?: boolean - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default async () => { - Spell.init( - { - uuid: { - type: DataTypes.UUID, - unique: true, - allowNull: false, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - name: { - type: DataTypes.STRING, - allowNull: false, - unique: true, - }, - description: { - type: DataTypes.STRING, - allowNull: false, - }, - - level: { - type: DataTypes.INTEGER, - defaultValue: 0 - }, - charge: { - type: DataTypes.INTEGER, - defaultValue: 0 - }, - cost: { - type: DataTypes.STRING, - defaultValue: '0' - }, - isRitual: { - type: DataTypes.BOOLEAN, - defaultValue: false - }, - - published: { - type: DataTypes.BOOLEAN, - defaultValue: false - }, - public: { - type: DataTypes.BOOLEAN, - defaultValue: false - }, - }, - { - tableName: 'au_spells', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/spells/Variable.ts b/src/database/models/spells/Variable.ts index f2ade71..ee665c7 100644 --- a/src/database/models/spells/Variable.ts +++ b/src/database/models/spells/Variable.ts @@ -1,49 +1,13 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface VariableAttributes { - uuid: string - description: string - - published: boolean -} - -interface VariableCreationAttributes extends Optional { } - -export class Variable extends Model implements VariableAttributes { +@Entity() +export class Variable { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public description!: string + @Column() public published: boolean - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - Variable.init( - { - uuid: { - type: DataTypes.UUID, - unique: true, - allowNull: false, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - description: { - type: DataTypes.STRING, - allowNull: false, - }, - - published: { - type: DataTypes.BOOLEAN, - defaultValue: false - } - }, - { - tableName: 'au_variables', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/users/Permission.ts b/src/database/models/users/Permission.ts index f19f949..68c6d65 100644 --- a/src/database/models/users/Permission.ts +++ b/src/database/models/users/Permission.ts @@ -1,41 +1,10 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface PermissionAttributes { - uuid: string - slug: string -} - -interface PermissionCreationAttributes extends Optional { } - -export class Permission extends Model implements PermissionAttributes { +@Entity() +export class Permission { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public slug!: string - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - Permission.init( - { - uuid: { - type: DataTypes.UUID, - unique: true, - allowNull: false, - defaultValue: DataTypes.UUIDV4, - primaryKey: true - }, - slug: { - type: DataTypes.STRING, - allowNull: false, - unique: true - }, - }, - { - tableName: 'au_permissions', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/users/Role.ts b/src/database/models/users/Role.ts index 6200cb7..c5cfd33 100644 --- a/src/database/models/users/Role.ts +++ b/src/database/models/users/Role.ts @@ -1,54 +1,12 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface RoleAttributes { - id: number - uuid: string - name: string - description: string -} - -interface RoleCreationAttributes extends Optional { } - -export class Role extends Model implements RoleAttributes { - public readonly id!: number +@Entity() +export class Role { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public name!: string + @Column() public description!: string - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - Role.init( - { - id: { - type: DataTypes.INTEGER.UNSIGNED, - autoIncrement: true, - primaryKey: true - }, - uuid: { - type: DataTypes.UUID, - allowNull: false, - unique: true, - defaultValue: DataTypes.UUIDV4 - }, - name: { - type: DataTypes.STRING, - allowNull: false, - unique: true - }, - description: { - type: DataTypes.STRING, - allowNull: false, - unique: true - }, - }, - { - tableName: 'au_roles', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/database/models/users/User.ts b/src/database/models/users/User.ts index ccae0bb..8d9bb49 100644 --- a/src/database/models/users/User.ts +++ b/src/database/models/users/User.ts @@ -1,77 +1,22 @@ -import { DataTypes, Model, Optional } from "sequelize"; +import { Column, Entity, PrimaryGeneratedColumn } from "typeorm" -import { AuracleDatabaseDriver } from "../../AuracleDatabaseDriver"; - -interface UserAttributes { - id: number - uuid: string - mail: string - password: string - - name: string - avatar: string - gender: string - verified: boolean -} - -interface UserCreationAttributes extends Optional { } - -export class User extends Model implements UserAttributes { - public readonly id!: number +@Entity() +export class User { + @PrimaryGeneratedColumn('uuid') public readonly uuid!: string + + @Column() public readonly mail!: string + @Column() public readonly password!: string + @Column() public name!: string + @Column() public avatar: string + @Column() public gender: string + + @Column() public verified: boolean - - public readonly createdAt!: Date; - public readonly updatedAt!: Date; -} - -export default () => { - User.init( - { - id: { - type: DataTypes.INTEGER.UNSIGNED, - autoIncrement: true, - primaryKey: true - }, - uuid: { - type: DataTypes.UUID, - allowNull: false, - unique: true, - defaultValue: DataTypes.UUIDV4 - }, - mail: { - type: DataTypes.STRING, - allowNull: false, - unique: true, - }, - password: { - type: DataTypes.STRING, - allowNull: false, - }, - - name: { - type: DataTypes.STRING, - allowNull: false, - }, - avatar: { - type: DataTypes.STRING, - }, - gender: { - type: DataTypes.STRING, - }, - verified: { - type: DataTypes.BOOLEAN, - }, - }, - { - tableName: 'au_users', - sequelize: AuracleDatabaseDriver, - } - ) } diff --git a/src/main.ts b/src/main.ts index fab3ab1..e1bb6fc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,6 +7,10 @@ import * as dotenv from 'dotenv' dotenv.config(); // Packages + +// TypeORM +import "reflect-metadata"; + import morgan from 'morgan' import helmet from 'helmet' import express from 'express' @@ -21,9 +25,7 @@ const apiPort = process.env.API_PORT const app = new AuracleApi({ port: apiPort, middlewares: [], - routers: [ - new SpellRouter() - ], + routers: [], modules: [ express.json({ limit: '10kb' }), morgan('dev'),