项目作者: EvanLiu2968

项目描述 :
A full example egg crud app framework, include mysql, redis and swagger-doc.
高级语言: JavaScript
项目地址: git://github.com/EvanLiu2968/egg-crud.git
创建时间: 2019-10-02T06:12:13Z
项目社区:https://github.com/EvanLiu2968/egg-crud

开源协议:MIT License

下载


egg-crud

A full example egg crud app framework, include mysql, redis and swagger-doc.

此项目是为了提供一个开箱即用的egg应用模版(依赖mysql, redis),按正常的前后端分离逻辑,后端服务仅需提供接口服务、接口文档及部署流程

需要加入webpack、react、vue这些前端服务的话,可参考https://github.com/EvanLiu2968/evanliu2968

主要集成服务

Swagger URL

sequelize

Node的sequelize相当于Java的mybatis, golang的gorm。在一些较为复杂的应用中,我们可能会需要一个 ORM 框架来帮助我们管理数据层的代码

sequelize-cli

  • 初始化 Migrations 配置文件和目录
    1. npx sequelize init:config
    2. npx sequelize init:migrations
  • 初始化 Migration 文件
    1. npx sequelize migration:generate --name=init-user
  • 升级数据库(初始化数据库)
    1. # 升级数据库
    2. npx sequelize db:migrate
    3. # 如果有问题需要回滚,可以通过 `db:migrate:undo` 回退一个变更
    4. # npx sequelize db:migrate:undo
    5. # 可以通过 `db:migrate:undo:all` 回退到初始状态
    6. # npx sequelize db:migrate:undo:all

migrate可以用于升级数据库和初始方法化数据库,但初始化数据库还可以使用Model.sync()

  1. // 在定义Model后加上,会同步Model至数据库表,新增的字段也会同步,migrate更适合记录数据库的升级、变更、迁移,如何选择看具体场景
  2. ModelInstance.sync({ alter: true });
  • 初始化数据
    1. # 创建seed
    2. npx sequelize seed:create --name init-user
    3. # 执行所有seed
    4. npx sequelize db:seed:all
    5. # 执行指定seed
    6. npx sequelize db:seed --seed ${timestamp}-init-user.js

Build Setup

  1. # install dependencies
  2. $ yarn
  3. # serve at localhost:7001
  4. $ yarn run dev

Deploy

  1. $ yarn run start
  2. $ yarn run stop

npm scripts

  • Use yarn run lint to check code style.
  • Use yarn run test to run unit test.
  • Use yarn run autod to auto detect dependencies upgrade, see autod for more detail.
Mac start
  1. brew install redis mongodb
  2. brew services start redis
  3. brew services start mongodb
Windows start

可使用phpstudy等类似集成环境工具快速启动本地服务

Linux(CentOS7) start
  1. # redis install [yum install redis](https://www.cnblogs.com/rslai/p/8249812.html)
  2. yum install redis
  3. # control
  4. systemctl start redis.service
  5. systemctl stop redis.service
  6. # install mysql
  7. # https://stackoverflow.com/questions/9083408/fatal-error-cant-open-and-lock-privilege-tables-table-mysql-host-doesnt-ex
  8. wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
  9. rpm -ivh mysql-community-release-el7-5.noarch.rpm
  10. yum install mysql-server
  11. # edit config
  12. vim /etc/my.cnf
  13. datadir=/data/mysql
  14. # mysql control
  15. systemctl start mysqld
  16. # login
  17. # cat /var/log/mysqld.log|grep 'A temporary password'
  18. mysql -u root -p
  19. use mysql
  20. update mysql.user set authentication_string = password("123456") where user="root";
  21. GRANT ALL ON *.* to root@'%' IDENTIFIED BY 'root'; # to allow remote connect
  22. flush privileges;
  23. quit;
  24. # download mongodb package
  25. wget http://mirrors.aliyun.com/mongodb/yum/redhat/6Server/mongodb-org/3.4/x86_64/RPMS/mongodb-org-mongos-3.4.0-1.el6.x86_64.rpm
  26. # create yum resposity
  27. rpm -ivh mongodb-org-mongos-3.4.0-1.el6.x86_64.rpm
  28. # install
  29. yum install mongodb
  30. # start service
  31. service mongod start
  32. # conf
  33. cd /etc/mongo.conf