# koa2-cli-template
**Repository Path**: baimifan2020/koa2-cli-template
## Basic Information
- **Project Name**: koa2-cli-template
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-05-13
- **Last Updated**: 2022-05-30
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# sequelize_cli_template
> nodejs连接mysql (pm2 + koa2 + sequelize_cli)
> 作者: Hsuna
> 日期: 2018/08、10
### 一、项目架构
```
./workspace
├── config // 配置
├── db // 数据库
├── migrations // 迁移文件
├── models // 模型
├── router // 路由
├── utils // 工具库录
├── .babelrc // babelrc配置文件
├── .sequelizerc // sequelizerc配置文件
├── ecosystem.config.js // pm2配置文件
├── app.js // 项目入口
├── start.js // 启动文件
└── package.json // 项目配置信息
```
### 二、开始项目
#### 1.配置项目
配置config/host文件:
```
{
port: 3000 // 启动端口
}
```
配置config/db文件:
```
{
username: 'www', // 用户名
password: "www", // 用户密码
database: 'nodejs', // 数据库名
host: '127.0.0.1', // 地址
port: 3306, // 端口
dialect: 'mysql', // 方言
}
```
#### 2.安装及启动项目
1. `git clone http://git.3k.com/web/Tech/sequelize_cli_template.git`
3. 安装依赖:`npm i`
4. 开发:`npm start`
5. 测试:`npm run test`
6. 生产:`npm run prod`
### 三、sequelize命令
```
eg: sequelize [Commands]
Commands:
db:migrate Run pending migrations
db:migrate:schema:timestamps:add Update migration table to have timestamps 更新迁移表,获取时间戳。
db:migrate:status List the status of all migrations
db:migrate:undo Reverts a migration
db:migrate:undo:all Revert all migrations ran
db:seed Run specified seeder
db:seed:undo Deletes data from the database
db:seed:all Run every seeder
db:seed:undo:all Deletes data from the database
db:create Create database specified by configuration
db:drop Drop database specified by configuration
init Initializes project
init:config Initializes configuration
init:migrations Initializes migrations
init:models Initializes models
init:seeders Initializes seeders
migration:generate|create Generates a new migration file
model:generate|create Generates a model and its migration
seed:generate|create Generates a new seed file
```
#### 1.配置.sequelizerc
如果不配置.sequelizerc 的话,sequelize init 初始化的文件夹会出现在项目目录下面,如果配置了.sequelizerc 就可以指定到相应的目录
```
const path = require('path')
module.exports = {
'config': path.resolve('./config/db.js'),
'migrations-path': path.resolve('./migrations'),
'models-path': path.resolve('./models'),
'seeders-path': path.resolve('./seeders'),
}
```
执行 sequelize init 命令
```
sequelize init
```
#### 2.创建model (Todo)
```
$ sequelize model:create --name Todo --attibutes "text:string,flag:boolean,value:integer"
```
| 参数 | 说明 | 类型 |
| -- | -- | --: |
| --version | Show version number | [boolean]|
|--help | Show help | [boolean] |
|--env | The environment to run the command in | [string] [default: "development"]|
|--config | The path to the config file | [string]|
|--options-path | The path to a JSON file with additional options | [string]|
|--migrations-path | The path to the migrations folder | [string] [default: "migrations"]|
|--seeders-path | The path to the seeders folder | [string] [default: "seeders"]|
|--models-path | The path to the models folder | [string] [default: "models"]|
| --url | The database connection string to use. Alternative to using --config files | [string]|
|--debug | When available show various debug information | [boolean] [default: false]|
| --name | Defines the name of the new model | [string] [required]|
| --attributes | A list of attributes | [string] [required] |
|--underscored | Use snake case for the timestamp's attribute names | [boolean] [default: false] |
#### 3.创建migrations
```
$ sequelize db:migrate:create --name add-testcolumn-to-todo
```
执行命令后,会在migrations文件夹中生成20170726122010-add-testcolumn-to-todo.js 修改文件里的内容。
```
'use strict';
module.exports = {
up: function(queryInterface, Sequelize) {
queryInterface.addColumn('Todos', 'test', {
type: Sequelize.STRING,
allowNull: false
})
},
down: function(queryInterface, Sequelize) {
queryInterface.removeColumn('Todos', 'test')
}
};
```
#### 4.运行migrations
```
$ sequelize db:migrate
```
#### 5.回滚migrations
```
$ sequelize db:migrate:undo
$ sequelize db:migrate:undo:all
```
[Migrations](http://docs.sequelizejs.com/manual/tutorial/migrations.html)
## sequlize 快捷命令
```js
// 创建模型
npx sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string
//在数据库中实际创建该表,需要运行 db:migrate 命令。
npx sequelize db:migrate
// 更新user表的migration
npx sequelize migration:generate --name=user
// 运行种子文件
npx sequelize db:seed:all
```
## 添加本地项目到远程仓康
```js
cd existing_git_repo
git remote add origin https://gitee.com/baimifan2020/koa2-cli-template.git
git push -u origin "master"s
```
## sequelize 关联操作
``js
https://www.jianshu.com/p/13b59e11ab4c
```