feat: add User data manager & User Database
This commit is contained in:
34
server/data/DataBaseManager.ts
Normal file
34
server/data/DataBaseManager.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// @ts-types="npm:sequelize"
|
||||||
|
import { Sequelize, Op, Model, DataTypes } from 'sequelize'
|
||||||
|
import Config from "../config.ts"
|
||||||
|
import User from "./User.ts";
|
||||||
|
|
||||||
|
abstract class BaseDataManager {
|
||||||
|
declare sequelize: Sequelize
|
||||||
|
declare name: string
|
||||||
|
constructor(name: string) {
|
||||||
|
this.init(name)
|
||||||
|
this.onInit()
|
||||||
|
}
|
||||||
|
private init(name: string) {
|
||||||
|
this.name = name
|
||||||
|
this.sequelize = new Sequelize({
|
||||||
|
dialect: 'sqlite',
|
||||||
|
storage: Config.dirs.DATABASES_DIR + '/' + name + '.db'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
abstract onInit(): void
|
||||||
|
}
|
||||||
|
|
||||||
|
class UserDataManager extends BaseDataManager {
|
||||||
|
static SINGLE_INSTANCE = new UserDataManager('users')
|
||||||
|
override onInit(): void {
|
||||||
|
User.initTable(this.sequelize, this.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class DataBaseManager {
|
||||||
|
static getUserDataManager() {
|
||||||
|
return UserDataManager.SINGLE_INSTANCE
|
||||||
|
}
|
||||||
|
}
|
||||||
19
server/data/User.ts
Normal file
19
server/data/User.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// @ts-types="npm:sequelize"
|
||||||
|
import { Sequelize, Op, Model, DataTypes } from 'sequelize'
|
||||||
|
|
||||||
|
export default class User extends Model {
|
||||||
|
declare created_id: number
|
||||||
|
static async initTable(sequelize: Sequelize, name: string) {
|
||||||
|
this.init({
|
||||||
|
created_id: {
|
||||||
|
type: DataTypes.INTEGER.UNSIGNED,
|
||||||
|
autoIncrement: true,
|
||||||
|
primaryKey: true,
|
||||||
|
}
|
||||||
|
}, {
|
||||||
|
sequelize: sequelize,
|
||||||
|
tableName: name,
|
||||||
|
})
|
||||||
|
await this.sync({ alter: true })
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user