Implemented models and cleaned some code
This commit is contained in:
45
src/common/database/index.ts
Normal file
45
src/common/database/index.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { Model, ModelOptions, Options, Sequelize } from 'sequelize'
|
||||
import registerModels from './models'
|
||||
|
||||
const tablePrefix = 'au_'
|
||||
|
||||
/**
|
||||
* Registers the options to create the sequelize instance
|
||||
*/
|
||||
const driverOptions: Options = {
|
||||
/**
|
||||
* Access to database
|
||||
*/
|
||||
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: {
|
||||
beforeDefine: (model: ModelOptions<Model<any, any>>) => {
|
||||
model.tableName = tablePrefix + model.name.plural
|
||||
}
|
||||
}
|
||||
}
|
||||
// Creates the sequelize instance
|
||||
export const AuracleDatabaseDriver = new Sequelize(driverOptions)
|
||||
|
||||
/**
|
||||
* If the environement is production...
|
||||
*/
|
||||
if (process.env.NODE_ENV != 'production') {
|
||||
AuracleDatabaseDriver.sync({
|
||||
force: true
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers models presents in the /models folder
|
||||
*/
|
||||
registerModels()
|
||||
23
src/common/database/models/index.ts
Normal file
23
src/common/database/models/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import registerUserModel from "./users/User"
|
||||
import registerRoleModel from "./users/Role"
|
||||
import registerPermissionModel from "./users/Permission"
|
||||
|
||||
import registerSpellModel from "./spells/Spell"
|
||||
import registerSchoolModel from "./spells/School"
|
||||
import registerMetaSchoolModel from "./spells/MetaSchool"
|
||||
|
||||
const models = [
|
||||
registerUserModel,
|
||||
registerRoleModel,
|
||||
registerPermissionModel,
|
||||
|
||||
registerSpellModel,
|
||||
registerSchoolModel,
|
||||
registerMetaSchoolModel,
|
||||
]
|
||||
|
||||
export default () => {
|
||||
models.forEach((registerModel) => {
|
||||
registerModel()
|
||||
})
|
||||
}
|
||||
54
src/common/database/models/spells/Ingredient.ts
Normal file
54
src/common/database/models/spells/Ingredient.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface IngredientAttributes {
|
||||
id: number
|
||||
name: string
|
||||
description: string
|
||||
|
||||
published: boolean
|
||||
}
|
||||
|
||||
interface IngredientCreationAttributes extends Optional<IngredientAttributes, 'id'> { }
|
||||
|
||||
class Ingredient extends Model<IngredientAttributes, IngredientCreationAttributes> implements IngredientAttributes {
|
||||
public id!: number
|
||||
public name!: string
|
||||
public description!: string
|
||||
|
||||
public published: boolean
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
Ingredient.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
primaryKey: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
|
||||
published: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
}
|
||||
},
|
||||
{
|
||||
tableName: 'ingredient',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
54
src/common/database/models/spells/MetaSchool.ts
Normal file
54
src/common/database/models/spells/MetaSchool.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface MetaSchoolAttributes {
|
||||
id: number
|
||||
name: string
|
||||
description: string
|
||||
|
||||
published: boolean
|
||||
}
|
||||
|
||||
interface MetaSchoolCreationAttributes extends Optional<MetaSchoolAttributes, 'id'> { }
|
||||
|
||||
class MetaSchool extends Model<MetaSchoolAttributes, MetaSchoolCreationAttributes> implements MetaSchoolAttributes {
|
||||
public id!: number
|
||||
public name!: string
|
||||
public description!: string
|
||||
|
||||
public published: boolean
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
MetaSchool.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
primaryKey: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
|
||||
published: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
}
|
||||
},
|
||||
{
|
||||
tableName: 'meta-school',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
54
src/common/database/models/spells/School.ts
Normal file
54
src/common/database/models/spells/School.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface SchoolAttributes {
|
||||
id: number
|
||||
name: string
|
||||
description: string
|
||||
|
||||
published: boolean
|
||||
}
|
||||
|
||||
interface SchoolCreationAttributes extends Optional<SchoolAttributes, 'id'> { }
|
||||
|
||||
class School extends Model<SchoolAttributes, SchoolCreationAttributes> implements SchoolAttributes {
|
||||
public id!: number
|
||||
public name!: string
|
||||
public description!: string
|
||||
|
||||
public published: boolean
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
School.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
primaryKey: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
},
|
||||
|
||||
published: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
}
|
||||
},
|
||||
{
|
||||
tableName: 'school',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
87
src/common/database/models/spells/Spell.ts
Normal file
87
src/common/database/models/spells/Spell.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface SpellAttributes {
|
||||
id: number
|
||||
name: string
|
||||
description: string
|
||||
|
||||
level: number
|
||||
charge: number
|
||||
cost: string
|
||||
isRitual: boolean
|
||||
|
||||
published: boolean
|
||||
public: boolean
|
||||
}
|
||||
|
||||
interface SpellCreationAttributes extends Optional<SpellAttributes, 'id'> { }
|
||||
|
||||
class Spell extends Model<SpellAttributes, SpellCreationAttributes> implements SpellAttributes {
|
||||
public id!: number
|
||||
public name!: string
|
||||
public description!: string
|
||||
|
||||
public level: number
|
||||
public charge: number
|
||||
public cost: string
|
||||
public isRitual: boolean
|
||||
|
||||
public published: boolean
|
||||
public public: boolean
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
Spell.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
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: 'spell',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
39
src/common/database/models/users/Permission.ts
Normal file
39
src/common/database/models/users/Permission.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface PermissionAttributes {
|
||||
id: number
|
||||
slug: string
|
||||
}
|
||||
|
||||
interface PermissionCreationAttributes extends Optional<PermissionAttributes, 'id'> { }
|
||||
|
||||
class Permission extends Model<PermissionAttributes, PermissionCreationAttributes> implements PermissionAttributes {
|
||||
public id!: number
|
||||
public slug!: string
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
Permission.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
primaryKey: true
|
||||
},
|
||||
slug: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
},
|
||||
{
|
||||
tableName: 'permission',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
46
src/common/database/models/users/Role.ts
Normal file
46
src/common/database/models/users/Role.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface RoleAttributes {
|
||||
id: number
|
||||
name: string
|
||||
description: string
|
||||
}
|
||||
|
||||
interface RoleCreationAttributes extends Optional<RoleAttributes, 'id'> { }
|
||||
|
||||
class Role extends Model<RoleAttributes, RoleCreationAttributes> implements RoleAttributes {
|
||||
public id!: number
|
||||
public name!: string
|
||||
public description!: string
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
Role.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
primaryKey: true
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
},
|
||||
{
|
||||
tableName: 'role',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
69
src/common/database/models/users/User.ts
Normal file
69
src/common/database/models/users/User.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { DataTypes, Model, Optional } from "sequelize";
|
||||
|
||||
import { AuracleDatabaseDriver } from "../../";
|
||||
|
||||
interface UserAttributes {
|
||||
id: number
|
||||
mail: string
|
||||
password: string
|
||||
|
||||
name: string
|
||||
avatar: string
|
||||
gender: string
|
||||
verified: boolean
|
||||
}
|
||||
|
||||
interface UserCreationAttributes extends Optional<UserAttributes, 'id'> { }
|
||||
|
||||
class User extends Model<UserAttributes, UserCreationAttributes> implements UserAttributes {
|
||||
public id!: number
|
||||
public readonly mail!: string
|
||||
public readonly password!: string
|
||||
|
||||
public name!: string
|
||||
public avatar: string
|
||||
public gender: string
|
||||
public verified: boolean
|
||||
|
||||
public readonly createdAt!: Date;
|
||||
public readonly updatedAt!: Date;
|
||||
}
|
||||
|
||||
export default () => {
|
||||
User.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.INTEGER.UNSIGNED,
|
||||
autoIncrement: true,
|
||||
primaryKey: true
|
||||
},
|
||||
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: 'user',
|
||||
sequelize: AuracleDatabaseDriver,
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
// Setting up the database connection
|
||||
const knex = require('knex')({
|
||||
client: "mysql",
|
||||
connection: {
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_DATABASE,
|
||||
charset: "utf8"
|
||||
},
|
||||
});
|
||||
const bookshelf = require('bookshelf')(knex);
|
||||
|
||||
module.exports = { bookshelf };
|
||||
@@ -4,11 +4,13 @@ dotenv.config();
|
||||
// Dependencies
|
||||
import morgan from 'morgan';
|
||||
import helmet from 'helmet';
|
||||
|
||||
import express from 'express'
|
||||
|
||||
import { AuracleApi } from './common/classes/AuracleApi'
|
||||
|
||||
// Database
|
||||
import { AuracleDatabaseDriver } from './common/database';
|
||||
|
||||
const port = Number(process.env.API_PORT);
|
||||
|
||||
const app = new AuracleApi({
|
||||
@@ -19,11 +21,10 @@ const app = new AuracleApi({
|
||||
morgan('dev'),
|
||||
helmet()
|
||||
],
|
||||
database: AuracleDatabaseDriver
|
||||
});
|
||||
|
||||
app.listen()
|
||||
|
||||
process.on('SIGINT', () => app.close());
|
||||
|
||||
// const routes = require('./routes');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user