partial conversion to typeORM, error on init tho
This commit is contained in:
parent
c916075754
commit
d2371cbd52
11 changed files with 2211 additions and 55 deletions
3
.env
Normal file
3
.env
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
WEBHOOK_URL=https://discord.com/api/webhooks/1322002321957191710/M0OBUFRzZhnkkmOOyBw-bx07sGSUae5kvfQN4gGNrgMp_KkTF__4ZLANF5SbT0y8ASap
|
||||||
|
WEBHOOK_NAME=Auction_Notifier
|
||||||
|
WEBHOOK_PROFILE_PICTURE=https://www.speedrun.com/static/game/3dxe951y/cover.png?v=14e2c50
|
13
.gitignore
vendored
13
.gitignore
vendored
|
@ -1,7 +1,6 @@
|
||||||
node_modules
|
.idea/
|
||||||
.env
|
.vscode/
|
||||||
pnpm-lock*
|
node_modules/
|
||||||
dist
|
build/
|
||||||
*.db
|
tmp/
|
||||||
*.sqlite*
|
temp/
|
||||||
db_data
|
|
1
index.ts
1
index.ts
|
@ -6,6 +6,7 @@ import { string } from 'prismarine-nbt';
|
||||||
import { SqlSystem } from './src/sqlFunctions';
|
import { SqlSystem } from './src/sqlFunctions';
|
||||||
import { setupErrorHandlers } from './src/errorHandler';
|
import { setupErrorHandlers } from './src/errorHandler';
|
||||||
import { ItemCompactData } from './src/Types';
|
import { ItemCompactData } from './src/Types';
|
||||||
|
import "reflect-metadata"
|
||||||
|
|
||||||
setupErrorHandlers();
|
setupErrorHandlers();
|
||||||
|
|
||||||
|
|
62
package.json
62
package.json
|
@ -1,29 +1,37 @@
|
||||||
{
|
{
|
||||||
"name": "hypixel-auction-notifier",
|
"name": "hypixel-auction-notifier",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Hypixel Skyblock Auction House Flip Notifier",
|
"description": "Hypixel Skyblock Auction House Flip Notifier",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"author": "DuckySoLucky + MashClashXD + Ulysia + Sol",
|
"author": "DuckySoLucky + MashClashXD + Ulysia + Sol",
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"start": "node dist/index.js",
|
"start": "ts-node src/index.ts",
|
||||||
"dev": "ts-node index.ts"
|
"dev": "ts-node index.ts",
|
||||||
},
|
"typeorm": "typeorm-ts-node-commonjs"
|
||||||
"dependencies": {
|
},
|
||||||
"@types/node": "^22.10.2",
|
"dependencies": {
|
||||||
"@types/toastify-js": "^1.12.3",
|
"@types/toastify-js": "^1.12.3",
|
||||||
"axios": "^0.24.0",
|
"axios": "^0.24.0",
|
||||||
"copy-paste": "^1.5.3",
|
"copy-paste": "^1.5.3",
|
||||||
"discord.js": "^14.16.3",
|
"discord.js": "^14.16.3",
|
||||||
"express": "^4.21.2",
|
"express": "^4.21.2",
|
||||||
"mariadb": "^3.4.0",
|
"mysql2": "^3.12.0",
|
||||||
"node-notifier": "^10.0.1",
|
"node-notifier": "^10.0.1",
|
||||||
"open": "^8.4.2",
|
"open": "^8.4.2",
|
||||||
"prismarine-nbt": "^2.7.0",
|
"prismarine-nbt": "^2.7.0",
|
||||||
"socket.io": "^4.8.1",
|
"socket.io": "^4.8.1",
|
||||||
"toastify-js": "^1.12.0",
|
"toastify-js": "^1.12.0",
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"typescript": "^5.7.2"
|
"typeorm": "0.3.20",
|
||||||
}
|
"typescript": "^5.7.2",
|
||||||
|
"reflect-metadata": "^0.1.13",
|
||||||
|
"mysql": "^2.14.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^16.11.10",
|
||||||
|
"ts-node": "10.9.1",
|
||||||
|
"typescript": "4.5.2"
|
||||||
|
}
|
||||||
}
|
}
|
1981
pnpm-lock.yaml
Normal file
1981
pnpm-lock.yaml
Normal file
File diff suppressed because it is too large
Load diff
17
src/data-source.ts
Normal file
17
src/data-source.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import "reflect-metadata"
|
||||||
|
import { DataSource } from "typeorm"
|
||||||
|
import { User } from "./entity/User"
|
||||||
|
|
||||||
|
export const AppDataSource = new DataSource({
|
||||||
|
type: "mariadb",
|
||||||
|
host: "localhost",
|
||||||
|
port: 3306,
|
||||||
|
username: "test",
|
||||||
|
password: "test",
|
||||||
|
database: "test",
|
||||||
|
synchronize: true,
|
||||||
|
logging: false,
|
||||||
|
entities: [User],
|
||||||
|
migrations: [],
|
||||||
|
subscribers: [],
|
||||||
|
})
|
18
src/entity/User.ts
Normal file
18
src/entity/User.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class User {
|
||||||
|
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
id: number
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
firstName: string
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
lastName: string
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
age: number
|
||||||
|
|
||||||
|
}
|
20
src/index.ts
Normal file
20
src/index.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import { AppDataSource } from "./data-source"
|
||||||
|
import { User } from "./entity/User"
|
||||||
|
|
||||||
|
AppDataSource.initialize().then(async () => {
|
||||||
|
|
||||||
|
console.log("Inserting a new user into the database...")
|
||||||
|
const user = new User()
|
||||||
|
user.firstName = "Timber"
|
||||||
|
user.lastName = "Saw"
|
||||||
|
user.age = 25
|
||||||
|
await AppDataSource.manager.save(user)
|
||||||
|
console.log("Saved a new user with id: " + user.id)
|
||||||
|
|
||||||
|
console.log("Loading users from the database...")
|
||||||
|
const users = await AppDataSource.manager.find(User)
|
||||||
|
console.log("Loaded users: ", users)
|
||||||
|
|
||||||
|
console.log("Here you can setup and run express / fastify / any other framework.")
|
||||||
|
|
||||||
|
}).catch(error => console.log(error))
|
81
src/migration/CreateDBExtras.ts
Normal file
81
src/migration/CreateDBExtras.ts
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
// migrations/CreateDbExtras.ts
|
||||||
|
|
||||||
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
|
export class CreateDbExtras implements MigrationInterface {
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
// Triggers
|
||||||
|
await queryRunner.query(`
|
||||||
|
DELIMITER $$
|
||||||
|
|
||||||
|
-- Insert
|
||||||
|
CREATE TRIGGER IF NOT EXISTS insertLifetime
|
||||||
|
AFTER INSERT ON auctions
|
||||||
|
FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
INSERT INTO lifetimes (id, insertedon) VALUES (NEW.id, NOW(2));
|
||||||
|
END$$
|
||||||
|
|
||||||
|
-- Update
|
||||||
|
CREATE TRIGGER IF NOT EXISTS updateLifetime
|
||||||
|
AFTER UPDATE ON auctions
|
||||||
|
FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
UPDATE lifetimes SET insertedon = NOW(2) WHERE id = NEW.id;
|
||||||
|
END$$
|
||||||
|
|
||||||
|
-- Delete
|
||||||
|
CREATE TRIGGER IF NOT EXISTS removeLifetime
|
||||||
|
BEFORE DELETE ON auctions
|
||||||
|
FOR EACH ROW
|
||||||
|
BEGIN
|
||||||
|
DELETE FROM lifetimes WHERE id = OLD.id;
|
||||||
|
END$$
|
||||||
|
|
||||||
|
DELIMITER ;
|
||||||
|
`);
|
||||||
|
|
||||||
|
// Stored procedure + event
|
||||||
|
await queryRunner.query(`
|
||||||
|
DELIMITER $$
|
||||||
|
CREATE PROCEDURE IF NOT EXISTS CleanupOldEntries()
|
||||||
|
BEGIN
|
||||||
|
DECLARE done INT DEFAULT 0;
|
||||||
|
DECLARE temp_id INT;
|
||||||
|
|
||||||
|
DECLARE cur CURSOR FOR
|
||||||
|
SELECT id
|
||||||
|
FROM lifetimes
|
||||||
|
WHERE TIMESTAMPDIFF(HOUR, insertedon, NOW()) >= 6;
|
||||||
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
||||||
|
|
||||||
|
OPEN cur;
|
||||||
|
read_loop: LOOP
|
||||||
|
FETCH cur INTO temp_id;
|
||||||
|
IF done THEN
|
||||||
|
LEAVE read_loop;
|
||||||
|
END IF;
|
||||||
|
DELETE FROM auctions WHERE id = temp_id;
|
||||||
|
END LOOP;
|
||||||
|
CLOSE cur;
|
||||||
|
END$$
|
||||||
|
DELIMITER ;
|
||||||
|
|
||||||
|
SET GLOBAL event_scheduler = ON;
|
||||||
|
|
||||||
|
CREATE EVENT IF NOT EXISTS CleanupOldEntriesEvent
|
||||||
|
ON SCHEDULE EVERY 2 MINUTE
|
||||||
|
DO
|
||||||
|
CALL CleanupOldEntries();
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
// Reverse: drop triggers, event, procedure, etc.
|
||||||
|
await queryRunner.query(`DROP EVENT IF EXISTS CleanupOldEntriesEvent;`);
|
||||||
|
await queryRunner.query(`DROP PROCEDURE IF EXISTS CleanupOldEntries;`);
|
||||||
|
await queryRunner.query(`DROP TRIGGER IF EXISTS removeLifetime;`);
|
||||||
|
await queryRunner.query(`DROP TRIGGER IF EXISTS updateLifetime;`);
|
||||||
|
await queryRunner.query(`DROP TRIGGER IF EXISTS insertLifetime;`);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,43 @@
|
||||||
import mariadb from 'mariadb';
|
import { DataSource, Entity, PrimaryGeneratedColumn, Column, PrimaryColumn } from "typeorm";
|
||||||
import { loadConfig } from './configLoader';
|
import { loadConfig } from './configLoader';
|
||||||
|
import { CreateDbExtras } from "./migration/CreateDBExtras";
|
||||||
const config = loadConfig();
|
const config = loadConfig();
|
||||||
|
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
class Auction {
|
||||||
|
@PrimaryGeneratedColumn()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Column("varchar", { length: 255, unique: true })
|
||||||
|
auctionid: string;
|
||||||
|
|
||||||
|
@Column("decimal", { precision: 65, scale: 5 })
|
||||||
|
lbin: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class Lifetime {
|
||||||
|
@PrimaryColumn()
|
||||||
|
id: number;
|
||||||
|
|
||||||
|
@Column('datetime', { precision: 2 })
|
||||||
|
insertedon: Date;
|
||||||
|
}
|
||||||
|
|
||||||
class SqlSystem {
|
class SqlSystem {
|
||||||
|
|
||||||
private static pool: mariadb.Pool = mariadb.createPool({ database: process.env.DATABASE, host: process.env.DB_HOST, user: process.env.DB_USER, password: process.env.DB_PASSWORD, connectionLimit: config.data.worker_count ?? 10 })
|
private static DS: DataSource = new DataSource({
|
||||||
|
type: "mariadb",
|
||||||
|
host: process.env.DB_HOST,
|
||||||
|
port: 3306,
|
||||||
|
username: process.env.DB_USER,
|
||||||
|
password: process.env.DB_PASSWORD,
|
||||||
|
database: "hypixel",
|
||||||
|
synchronize: true,
|
||||||
|
entities: [Auction, Lifetime],
|
||||||
|
migrations: [CreateDbExtras],
|
||||||
|
});
|
||||||
|
|
||||||
public static async InitDB() {
|
public static async InitDB() {
|
||||||
let conn: mariadb.PoolConnection | undefined;
|
let conn: mariadb.PoolConnection | undefined;
|
||||||
|
|
|
@ -1,20 +1,15 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"module": "NodeNext",
|
"lib": [
|
||||||
"moduleResolution": "nodenext",
|
"es5",
|
||||||
"target": "ES2024",
|
"es6"
|
||||||
"jsx": "preserve",
|
],
|
||||||
"strictNullChecks": true,
|
"target": "es5",
|
||||||
"strictFunctionTypes": true,
|
"module": "commonjs",
|
||||||
"sourceMap": true,
|
"moduleResolution": "node",
|
||||||
"resolveJsonModule": true,
|
"outDir": "./build",
|
||||||
"esModuleInterop": true,
|
"emitDecoratorMetadata": true,
|
||||||
"skipLibCheck": true,
|
"experimentalDecorators": true,
|
||||||
"outDir": "./dist",
|
"sourceMap": true
|
||||||
"strict": true,
|
}
|
||||||
},
|
|
||||||
"exclude": [
|
|
||||||
"node_modules",
|
|
||||||
"**/node_modules/*"
|
|
||||||
]
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue