added some more database interaction
This commit is contained in:
2106
package-lock.json
generated
2106
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,9 @@ async function init() {
|
||||
await Database.testConnection();
|
||||
await Server.start();
|
||||
await Router.initEndpoints();
|
||||
|
||||
|
||||
|
||||
// Logger.database('Database Log');
|
||||
// Logger.middleware('GET request to /');
|
||||
// Logger.debug('Debug mode enabled');
|
||||
|
||||
@@ -5,12 +5,15 @@ import {Config} from '../../config/config';
|
||||
|
||||
let connection;
|
||||
|
||||
let user;
|
||||
let auth;
|
||||
let session;
|
||||
let User;
|
||||
let Auth;
|
||||
let Session;
|
||||
|
||||
export class BaseDatabase {
|
||||
static get Connection() {return connection;}
|
||||
static get User() {return User}
|
||||
static get Auth() {return Auth}
|
||||
static get Session() {return Session}
|
||||
|
||||
static async init() {
|
||||
Logger.info('Connecting to SQLite Database');
|
||||
@@ -23,7 +26,7 @@ export class BaseDatabase {
|
||||
storage: 'src/models/database/sqlite/database.sqlite',
|
||||
});
|
||||
|
||||
user = connection.define('user', {
|
||||
User = connection.define('user', {
|
||||
id: {
|
||||
type: Sequelize.BIGINT,
|
||||
primaryKey: true,
|
||||
@@ -35,11 +38,13 @@ export class BaseDatabase {
|
||||
ip: Sequelize.TEXT,
|
||||
lastupdated: Sequelize.TEXT,
|
||||
verified: Sequelize.BOOLEAN,
|
||||
authcode: Sequelize.STRING,
|
||||
authcode: Sequelize.TEXT,
|
||||
timeauthed: Sequelize.TEXT
|
||||
}, {
|
||||
tableName: `user`
|
||||
});
|
||||
|
||||
auth = connection.define('auth', {
|
||||
Auth = connection.define('auth', {
|
||||
id: {
|
||||
type: Sequelize.BIGINT,
|
||||
primaryKey: true,
|
||||
@@ -49,9 +54,11 @@ export class BaseDatabase {
|
||||
validator: Sequelize.TEXT,
|
||||
uid: Sequelize.BIGINT,
|
||||
expires: Sequelize.TEXT
|
||||
}, {
|
||||
tableName: `auth`
|
||||
});
|
||||
|
||||
session = connection.define('session', {
|
||||
Session = connection.define('session', {
|
||||
sessionid: {
|
||||
type: Sequelize.BIGINT,
|
||||
primaryKey: true,
|
||||
@@ -60,9 +67,12 @@ export class BaseDatabase {
|
||||
sessiondata: Sequelize.TEXT,
|
||||
timecreated: Sequelize.TEXT,
|
||||
timeupdated: Sequelize.TEXT
|
||||
}, {
|
||||
tableName: `session`
|
||||
});
|
||||
|
||||
try {
|
||||
await connection.sync();
|
||||
await connection.sync({force: false});
|
||||
} catch (e) {
|
||||
Logger.panic('Failed to connect to SQLite Database, error:', e)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ export class Database extends BaseDatabase {
|
||||
connection
|
||||
.query(query)
|
||||
.then(result => {
|
||||
Logger.database(JSON.stringify(res, null, 4));
|
||||
res = result[0][0].result;
|
||||
resolve();
|
||||
})
|
||||
|
||||
Binary file not shown.
@@ -5,7 +5,28 @@ import {Logger} from '../logger';
|
||||
import {Config} from '../../config/config';
|
||||
|
||||
export class UserTools extends BaseDatabase {
|
||||
static async testing() {
|
||||
Logger.debug('Hello, world!');
|
||||
static async listAll() {
|
||||
let User = BaseDatabase.User;
|
||||
return User.findAll();
|
||||
}
|
||||
|
||||
static async newUser(id, username, password, email, ip, authcode) {
|
||||
let User = BaseDatabase.User;
|
||||
try {
|
||||
let user = await User.create({
|
||||
id: id,
|
||||
username: username,
|
||||
password: password,
|
||||
email: email,
|
||||
ip: ip,
|
||||
lastupdated: new Date().getTime().toString(),
|
||||
verified: false,
|
||||
authcode: authcode,
|
||||
timeauthed: '-1'
|
||||
});
|
||||
} catch (e) {
|
||||
Logger.error(`An error occured while inserting user ${username}, id ${id} into users table: ${JSON.stringify(e.errors)}`);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user