From 0dabdaa136f4e34a04f3c946210a7b99272ec437 Mon Sep 17 00:00:00 2001 From: eneller Date: Fri, 6 Mar 2026 11:16:22 +0100 Subject: [PATCH] database models --- server/compose.yml | 4 ++-- server/src/index.ts | 3 ++- server/src/model/transaction.ts | 25 +++++++++++++++++++++++++ {shared => server}/src/model/user.ts | 11 +++++++---- server/src/{util.ts => util/db.ts} | 24 ++++++------------------ server/src/util/logging.ts | 20 ++++++++++++++++++++ 6 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 server/src/model/transaction.ts rename {shared => server}/src/model/user.ts (70%) rename server/src/{util.ts => util/db.ts} (59%) create mode 100644 server/src/util/logging.ts diff --git a/server/compose.yml b/server/compose.yml index bda9fa5..5ecb1e8 100644 --- a/server/compose.yml +++ b/server/compose.yml @@ -10,8 +10,8 @@ services: image: adminer restart: always environment: - ADMINER_DEFAULT_SERVER: postgres # Automatically connect to the "postgres" service + ADMINER_DEFAULT_SERVER: postgres ports: - "8080:8080" depends_on: - - postgres # Ensure PostgreSQL starts first + - postgres diff --git a/server/src/index.ts b/server/src/index.ts index f1a7a0d..bd396a8 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -1,7 +1,8 @@ import express, { Express, Request, Response } from "express"; import cors from "cors"; import transactionsRouter from './routes/transactions'; -import { db, testConnection, logger } from "./util"; +import { db, testConnection } from "./util/db"; +import { logger } from "./util/logging"; const app: Express = express(); app.use(cors()); diff --git a/server/src/model/transaction.ts b/server/src/model/transaction.ts new file mode 100644 index 0000000..65866e0 --- /dev/null +++ b/server/src/model/transaction.ts @@ -0,0 +1,25 @@ +import { Table, Column, Model, CreatedAt, ForeignKey, BelongsTo} from 'sequelize-typescript'; +import User from './user'; + +@Table +export default class Transaction extends Model{ + @Column + amount: number; + + @Column + @ForeignKey(()=> User) + senderID: string; + + @BelongsTo(() => User, 'senderID') + sender: User; + + @Column + @ForeignKey(()=> User) + receiverID: string; + + @BelongsTo(() => User, 'receiverID') + receiver: User; + + @CreatedAt + creationDate: Date; +} \ No newline at end of file diff --git a/shared/src/model/user.ts b/server/src/model/user.ts similarity index 70% rename from shared/src/model/user.ts rename to server/src/model/user.ts index b7c471a..9922009 100644 --- a/shared/src/model/user.ts +++ b/server/src/model/user.ts @@ -1,17 +1,20 @@ import { Table, Column, Model, CreatedAt, DataType} from 'sequelize-typescript'; @Table -export class User extends Model{ +export default class User extends Model{ @Column({primaryKey: true, unique: true, allowNull: false}) userID: string; - + @Column displayName: string; - - @Column(dataType: DataType.DECIMAL(20,2)) + + @Column(DataType.DECIMAL(20,2)) balance: number; + @Column + password: string; + @CreatedAt creationDate: Date; diff --git a/server/src/util.ts b/server/src/util/db.ts similarity index 59% rename from server/src/util.ts rename to server/src/util/db.ts index 4382109..d92d16c 100644 --- a/server/src/util.ts +++ b/server/src/util/db.ts @@ -1,21 +1,7 @@ -import { Sequelize } from 'sequelize'; -import winston, { format } from "winston"; -import dotenv from 'dotenv'; - -dotenv.config(); -const logger = winston.createLogger({ - level:'info', -}) -if (process.env.NODE_ENV !== 'production') { - logger.add(new winston.transports.Console({ - format: format.combine( - format.colorize({all: true}), - format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), - format.printf((info) => `${info.timestamp} [${info.level}]: ${info.message}`), - ) - -})); -} +import { Sequelize } from 'sequelize-typescript'; +import { logger } from './logging'; +import User from '../model/user'; +import Transaction from '../model/transaction'; // Initialize Sequelize const db = new Sequelize({ @@ -28,6 +14,8 @@ const db = new Sequelize({ logging: logger.debug.bind(logger), }); +db.addModels([User, Transaction ]) + // Test the connection async function testConnection() { try { diff --git a/server/src/util/logging.ts b/server/src/util/logging.ts new file mode 100644 index 0000000..4d2835a --- /dev/null +++ b/server/src/util/logging.ts @@ -0,0 +1,20 @@ +import winston, { format } from "winston"; +import dotenv from 'dotenv'; + +dotenv.config(); +const logger = winston.createLogger({ + level:'info', +}) +if (process.env.NODE_ENV !== 'production') { + logger.add(new winston.transports.Console({ + format: format.combine( + format.colorize({all: true}), + format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }), + format.printf((info) => `${info.timestamp} [${info.level}]: ${info.message}`), + ) + +})); +} + +// Export Sequelize instance and models +export { logger };