PostgreSQL查询助手

一个基于Express.js和PostgreSQL的REST API服务,提供数据库查询和数据访问功能。

  • by Harshaan-Chugh
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 Harshaan-Chugh/cds-mcpserver

使用说明

CDS MCP服务器

使用PostgreSQL后端和React前端的CDS入职项目。

团队成员

  • 韦丹塔·贾恩(vj88)
  • 哈沙安·楚(hsc53)
  • 江乃杰(nj277)

项目结构

cds-mcpserver/
├── server/                           # Backend server
│   ├── index.js                     # Express server entry point
│   ├── database/                    # Database configuration and scripts
│   │   ├── db.js                   # PostgreSQL connection pool
│   │   ├── schema.sql              # Database schema
│   │   ├── setup.js                # Schema setup script
│   │   └── seed.js                 # Data seeding script
│   └── routes/
│       └── api.js                  # API endpoints
├── client/                          # Frontend React component
│   ├── src/
│   │   └── components/
│   │       └── PostgreSQLQueryAssistant.jsx
│   └── README.md
├── test.html                        # Simple API test interface
├── package.json                     # Dependencies and scripts
├── .env                            # Environment variables (not committed)
├── .env.example                    # Environment variables template
├── SETUP.md                        # Detailed setup instructions
└── README.md                       # This file

快速开始

  1. 安装PostgreSQL (如果尚未安装)

    • 下载自https://www.postgresql.org/download/
    • 记住您的postgres用户密码
  2. 创建数据库

    psql -U postgres
    CREATE DATABASE cds_db;
    \q
    
  3. 配置环境

    cp .env.example .env
    # Edit .env with your database credentials
    
  4. 再进行

    npm install
    
  5. 设置数据库

    npm run setup
    npm run seed
    
  6. 启动服务器

    npm start
    # Or for development with auto-reload:
    npm run dev
    
  7. 测试API

    • 访问http://localhost:3001/health
    • 访问http://localhost:3001/api/users
    • 访问http://localhost:3001/api/products

特性

后端

  • ✅ Express.js REST API
  • ✅ 带连接池的PostgreSQL数据库
  • ✅ 用样本数据(用户、产品、订单)播种
  • ✅ 用于查询数据的多个API端点
  • ✅ 健康检查端点
  • ✅ 自定义SQL查询执行(仅为安全起见选择)

数据库模式

  • 用户 -具有身份验证信息的客户帐户
  • 分类 -产品类别
  • 产品 -产品目录,包括定价和库存
  • 订单 -具有状态跟踪功能的客户订单
  • 项目 -每个订单的行项目

样品数据

  • 10名用户拥有真实数据
  • 6个产品类别(电子、书籍、服装等)
  • 所有类别中有22种产品
  • 不同州的14个订单(已完成、已发货、正在处理等)
  • 28个订单项目

API终点

数据库探索

  • GET /health -检查服务器和数据库状态
  • GET /api/tables -列出所有数据库表
  • GET /api/tables/:tableName/schema -获取特定表的架构
  • POST /api/query -执行自定义SQL SELECT查询

数据访问

  • GET /api/users -获取所有用户
  • GET /api/products -获取所有分类产品
  • GET /api/categories -获取所有包含产品数量的类别
  • GET /api/orders -获取所有包含用户信息的订单
  • GET /api/orders/:orderId -获取商品的详细订单
  • GET /api/stats/users -获取用户统计信息
  • GET /api/stats/sales -获取销售统计数据

查询示例

在前端或通过curl尝试这些查询:

# Get all users
curl http://localhost:3001/api/users

# Get products
curl http://localhost:3001/api/products

# Get sales statistics
curl http://localhost:3001/api/stats/sales

# Execute custom query
curl -X POST http://localhost:3001/api/query \
  -H "Content-Type: application/json" \
  -d '{"sql": "SELECT * FROM users WHERE is_active = true LIMIT 5"}'

发展

看 设置.md 有关详细的设置说明和故障排除。

重置数据库

psql -U postgres -d postgres -c "DROP DATABASE IF EXISTS cds_db;"
psql -U postgres -d postgres -c "CREATE DATABASE cds_db;"
npm run setup
npm run seed

使用的技术

  • 后端:Node.js、Express.js
  • 数据库:PostgreSQL
  • 前端:React(见main.js)
  • 图书馆:pg(节点postgres)、cors、dotenv

许可证

国际协调委员会