diff --git a/README.md b/README.md index c530819..fedbc7d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,24 @@ # play -བཀྲ་ཤིས་བདེ་ལེགས། ང་ནི་Dia Atef Ahmed ཡིན། ང་ནི་@EG ཡི་སློབ་མ་ཡིན། སློབ་མའི་ཨང་རྟགས་ @tanduc265ab ཡིན། ཧི། -play1 + +This is a simple example project demonstrating how to integrate +[TypeORM](https://typeorm.io) with a MySQL database and a Kafka broker. + +## Getting Started + +1. Install dependencies (requires npm with internet access): + ```bash + npm install + ``` +2. Compile TypeScript sources: + ```bash + npm run build + ``` +3. Start the application: + ```bash + npm start + ``` + +The app connects to MySQL using environment variables `DB_HOST`, `DB_PORT`, +`DB_USER`, `DB_PASSWORD` and `DB_NAME`. It also connects to Kafka using the +`KAFKA_BROKERS` variable. Defaults are provided for local development. + diff --git a/package.json b/package.json new file mode 100644 index 0000000..ec509d6 --- /dev/null +++ b/package.json @@ -0,0 +1,26 @@ +{ + "name": "play", + "version": "1.0.0", + "description": "Sample project integrating TypeORM with MySQL and Kafka", + "main": "dist/index.js", + "scripts": { + "build": "tsc", + "start": "node dist/index.js", + "dev": "ts-node src/index.ts", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "kafkajs": "^2.2.4", + "mysql": "^2.18.1", + "reflect-metadata": "^0.1.13", + "typeorm": "^0.3.20" + }, + "devDependencies": { + "ts-node": "^10.9.1", + "typescript": "^5.2.2" + } +} + diff --git a/src/data-source.ts b/src/data-source.ts new file mode 100644 index 0000000..9c087a5 --- /dev/null +++ b/src/data-source.ts @@ -0,0 +1,25 @@ +import "reflect-metadata"; +import { DataSource } from "typeorm"; +import { User } from "./entity/User"; + +export const AppDataSource = new DataSource({ + type: "mysql", + host: process.env.DB_HOST || "localhost", + port: +(process.env.DB_PORT || 3306), + username: process.env.DB_USER || "root", + password: process.env.DB_PASSWORD || "password", + database: process.env.DB_NAME || "test", + entities: [User], + synchronize: true, + logging: false, +}); + +export const initializeDatabase = async () => { + try { + await AppDataSource.initialize(); + console.log("Database connected"); + } catch (err) { + console.error("Database connection failed", err); + throw err; + } +}; diff --git a/src/entity/User.ts b/src/entity/User.ts new file mode 100644 index 0000000..63b393b --- /dev/null +++ b/src/entity/User.ts @@ -0,0 +1,10 @@ +import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"; + +@Entity() +export class User { + @PrimaryGeneratedColumn() + id: number; + + @Column() + name: string; +} diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..c665b6c --- /dev/null +++ b/src/index.ts @@ -0,0 +1,27 @@ +import { initializeDatabase } from "./data-source"; +import { connectKafka, consumer, producer } from "./kafka"; +import { User } from "./entity/User"; +import { AppDataSource } from "./data-source"; + +const start = async () => { + await initializeDatabase(); + await connectKafka(); + + consumer.run({ + eachMessage: async ({ topic, partition, message }) => { + console.log(`received message: ${message.value?.toString()}`); + }, + }); + + const userRepo = AppDataSource.getRepository(User); + const user = userRepo.create({ name: "Alice" }); + await userRepo.save(user); + await producer.send({ + topic: "users", + messages: [{ value: `User created: ${user.name}` }], + }); +}; + +start().catch((err) => { + console.error(err); +}); diff --git a/src/kafka.ts b/src/kafka.ts new file mode 100644 index 0000000..5b215ac --- /dev/null +++ b/src/kafka.ts @@ -0,0 +1,15 @@ +import { Kafka } from "kafkajs"; + +const kafka = new Kafka({ + clientId: "my-app", + brokers: (process.env.KAFKA_BROKERS || "localhost:9092").split(","), +}); + +export const producer = kafka.producer(); +export const consumer = kafka.consumer({ groupId: "test-group" }); + +export const connectKafka = async () => { + await producer.connect(); + await consumer.connect(); + console.log("Kafka connected"); +}; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..51c3a03 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,12 @@ +{ + "compilerOptions": { + "target": "es2018", + "module": "commonjs", + "outDir": "dist", + "strict": true, + "esModuleInterop": true, + "experimentalDecorators": true, + "emitDecoratorMetadata": true + }, + "include": ["src/**/*.ts"] +}