database models
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
25
server/src/model/transaction.ts
Normal file
25
server/src/model/transaction.ts
Normal file
@@ -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;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
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;
|
||||
@@ -9,9 +9,12 @@ export class User extends Model{
|
||||
@Column
|
||||
displayName: string;
|
||||
|
||||
@Column(dataType: DataType.DECIMAL(20,2))
|
||||
@Column(DataType.DECIMAL(20,2))
|
||||
balance: number;
|
||||
|
||||
@Column
|
||||
password: string;
|
||||
|
||||
@CreatedAt
|
||||
creationDate: Date;
|
||||
|
||||
@@ -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 {
|
||||
20
server/src/util/logging.ts
Normal file
20
server/src/util/logging.ts
Normal file
@@ -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 };
|
||||
Reference in New Issue
Block a user