使用Express和TypeORM连接到MariaDB

我尝试执行了下面的示例代码。
typeorm / typescript-express-example

准备 MariaDB

用户:scott
密码:tiger123
数据库:my_database

程序的克隆

git clone https://github.com/typeorm/typescript-express-example

准备程序

cd typescript-express-example
npm install

设置数据库连接信息

{
  "type": "mysql",
  "host": "localhost",
  "port": 3306,
  "username": "scott",
  "password": "tiger123",
  "database": "my_database",
  "synchronize": true,
  "entities": [
    "src/entity/*.js"
  ],
  "subscribers": [
    "src/subscriber/*.js"
  ],
  "migrations": [
    "src/migration/*.js"
  ],
  "cli": {
    "entitiesDir": "src/entity",
    "migrationsDir": "src/migration",
    "subscribersDir": "src/subscriber"
  }
}

服务器的启动

npm start

在客户端上进行访问

$ curl http://localhost:3000/posts
[]

将数据存入MySQL。

insert into post set title='test_aa', text='Hello';
insert into post set title='test_cc', text='Good Morning';
insert into post set title='test_dd', text='Good Afternoon';
quit

执行SQL

mysql -uscott -ptiger123 my_database < insert.sql

从客户端访问

$ curl http://localhost:3000/posts | jq .
[
  {
    "id": 9,
    "title": "test_aa",
    "text": "Hello"
  },
  {
    "id": 10,
    "title": "test_cc",
    "text": "Good Morning"
  },
  {
    "id": 11,
    "title": "test_dd",
    "text": "Good Afternoon"
  }
]

请通过指定ID进行访问。

$ curl http://localhost:3000/posts/10 | jq .
{
  "id": 10,
  "title": "test_cc",
  "text": "Good Morning"
}

插入数据

curl -X POST -H "Content-Type: application/json" \
    -d '{"title": "test_ee","text": "Good Evening"}' \
    http://localhost:3000/posts

相关页面
使用Nest.js和TypeORM来连接MariaDB

广告
将在 10 秒后关闭
bannerAds