Converted models to TypeORM models
This commit is contained in:
@@ -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<Connection>
|
||||
routers?: Array<AuracleApiRouter>
|
||||
modules?: Array<any>
|
||||
middlewares?: Array<any>
|
||||
@@ -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<Connection>) => {
|
||||
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.')
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
]
|
||||
}
|
||||
|
||||
@@ -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<IngredientAttributes, 'uuid'> { }
|
||||
|
||||
export class Ingredient extends Model<IngredientAttributes, IngredientCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<MetaSchoolAttributes, 'uuid'> { }
|
||||
|
||||
export class MetaSchool extends Model<MetaSchoolAttributes, MetaSchoolCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<SchoolAttributes, 'uuid'> { }
|
||||
|
||||
export class School extends Model<SchoolAttributes, SchoolCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<SpellAttributes, 'uuid'> { }
|
||||
|
||||
export class Spell extends Model<SpellAttributes, SpellCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<VariableAttributes, 'uuid'> { }
|
||||
|
||||
export class Variable extends Model<VariableAttributes, VariableCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<PermissionAttributes, 'uuid'> { }
|
||||
|
||||
export class Permission extends Model<PermissionAttributes, PermissionCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<RoleAttributes, 'id'> { }
|
||||
|
||||
export class Role extends Model<RoleAttributes, RoleCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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<UserAttributes, 'id'> { }
|
||||
|
||||
export class User extends Model<UserAttributes, UserCreationAttributes> 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,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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'),
|
||||
|
||||
Reference in New Issue
Block a user