database models
This commit is contained in:
@@ -10,8 +10,8 @@ services:
|
|||||||
image: adminer
|
image: adminer
|
||||||
restart: always
|
restart: always
|
||||||
environment:
|
environment:
|
||||||
ADMINER_DEFAULT_SERVER: postgres # Automatically connect to the "postgres" service
|
ADMINER_DEFAULT_SERVER: postgres
|
||||||
ports:
|
ports:
|
||||||
- "8080:8080"
|
- "8080:8080"
|
||||||
depends_on:
|
depends_on:
|
||||||
- postgres # Ensure PostgreSQL starts first
|
- postgres
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import express, { Express, Request, Response } from "express";
|
import express, { Express, Request, Response } from "express";
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import transactionsRouter from './routes/transactions';
|
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();
|
const app: Express = express();
|
||||||
app.use(cors());
|
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';
|
import { Table, Column, Model, CreatedAt, DataType} from 'sequelize-typescript';
|
||||||
|
|
||||||
@Table
|
@Table
|
||||||
export class User extends Model{
|
export default class User extends Model{
|
||||||
|
|
||||||
@Column({primaryKey: true, unique: true, allowNull: false})
|
@Column({primaryKey: true, unique: true, allowNull: false})
|
||||||
userID: string;
|
userID: string;
|
||||||
@@ -9,9 +9,12 @@ export class User extends Model{
|
|||||||
@Column
|
@Column
|
||||||
displayName: string;
|
displayName: string;
|
||||||
|
|
||||||
@Column(dataType: DataType.DECIMAL(20,2))
|
@Column(DataType.DECIMAL(20,2))
|
||||||
balance: number;
|
balance: number;
|
||||||
|
|
||||||
|
@Column
|
||||||
|
password: string;
|
||||||
|
|
||||||
@CreatedAt
|
@CreatedAt
|
||||||
creationDate: Date;
|
creationDate: Date;
|
||||||
|
|
||||||
@@ -1,21 +1,7 @@
|
|||||||
import { Sequelize } from 'sequelize';
|
import { Sequelize } from 'sequelize-typescript';
|
||||||
import winston, { format } from "winston";
|
import { logger } from './logging';
|
||||||
import dotenv from 'dotenv';
|
import User from '../model/user';
|
||||||
|
import Transaction from '../model/transaction';
|
||||||
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}`),
|
|
||||||
)
|
|
||||||
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize Sequelize
|
// Initialize Sequelize
|
||||||
const db = new Sequelize({
|
const db = new Sequelize({
|
||||||
@@ -28,6 +14,8 @@ const db = new Sequelize({
|
|||||||
logging: logger.debug.bind(logger),
|
logging: logger.debug.bind(logger),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
db.addModels([User, Transaction ])
|
||||||
|
|
||||||
// Test the connection
|
// Test the connection
|
||||||
async function testConnection() {
|
async function testConnection() {
|
||||||
try {
|
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