Browse Source

完成数据库的调用

程旭源 1 year ago
parent
commit
df2268c81c

+ 1 - 0
.gitignore

@@ -49,3 +49,4 @@ thumbs/
 _thumbs/
 */expressionengine/cache/*
 .idea
+

+ 9 - 10
admin_node/app/controller/api/User.ts

@@ -19,19 +19,18 @@ class UserController extends ApiController {
         const err = this.app.validator.validate(createRule, this.ctx.request.body);
 
         if (err) {
-            console.log(err);
             return this.fail('不知道怎么返回');
         }
-        console.log(this);
-        // const params = this.ctx.request.body;
-        // const user = await this.app.model.User.create({
-        //     username: params.username,
-        //     name: '',
-        //     password: '123',
-        //     salt: '123',
-        // });
 
-        return this.success([]);
+        const params = this.ctx.request.body;
+        const user = await this.ctx.model.User.create({
+            username: params.username,
+            name: '',
+            password: '123',
+            salt: '123',
+        });
+
+        return this.success(user);
     }
 }
 

+ 1 - 1
admin_node/app/model/User.ts

@@ -1,7 +1,7 @@
 module.exports = app => {
     const { STRING, INTEGER, DATE } = app.Sequelize;
 
-    const User = app.model.define('user', {
+    const User = app.model.define('users', {
         id: {
             type: INTEGER,
             primaryKey: true,

+ 20 - 4
admin_node/config/config.default.ts

@@ -32,6 +32,22 @@ export default (appInfo: EggAppInfo) => {
         origin: '*',
         allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
     };
+    config.sequelize = {
+        dialect: 'mysql',
+        host: process.env.DATABASE_HOST || '127.0.0.1',
+        port: process.env.DATABASE_PORT || 3306,
+        database: process.env.DATABASE_DATABASE || 'egg',
+        username: process.env.DATABASE_USERNAME || 'egg',
+        password: process.env.DATABASE_PWD || '1995',
+        underscored: true,
+        timezone: process.env.TZ || '+UTC',
+        pool: {
+            max: 5,
+            min: 0,
+            acquire: 30000,
+            idle: 10000,
+        },
+    };
     // the return config will combines to EggAppConfig
     return {
         ...config,
@@ -42,11 +58,11 @@ export default (appInfo: EggAppInfo) => {
 // 数据库链接
 exports.sequelize = {
     dialect: 'mysql',
-    host: process.env.DATABASE_HOST || '192.168.2.254',
+    host: process.env.DATABASE_HOST || '127.0.0.1',
     port: process.env.DATABASE_PORT || 3306,
-    database: process.env.DATABASE_DATABASE || 'test_egg',
-    username: process.env.DATABASE_USERNAME || 'test_egg',
-    password: process.env.DATABASE_PWD || '123456',
+    database: process.env.DATABASE_DATABASE || 'egg',
+    username: process.env.DATABASE_USERNAME || 'egg',
+    password: process.env.DATABASE_PWD || '1995',
     underscored: true,
     timezone: process.env.TZ || '+UTC',
     pool: {

+ 1 - 0
admin_node/database/.gitignore

@@ -0,0 +1 @@
+config.json

+ 0 - 23
admin_node/database/config.json

@@ -1,23 +0,0 @@
-{
-  "development": {
-    "username": "test_egg",
-    "password": "123456",
-    "database": "test_egg",
-    "host": "192.168.2.254",
-    "dialect": "mysql"
-  },
-  "test": {
-    "username": "test_egg",
-    "password": "123456",
-    "database": "test_egg",
-    "host": "192.168.2.254",
-    "dialect": "mysql"
-  },
-  "production": {
-    "username": "test_egg",
-    "password": "123456",
-    "database": "test_egg",
-    "host": "192.168.2.254",
-    "dialect": "mysql"
-  }
-}

+ 4 - 0
admin_node/typings/config/plugin.d.ts

@@ -24,6 +24,8 @@ import '@eggjs/tegg-aop-plugin';
 import 'egg-tracer';
 import 'egg-jwt';
 import 'egg-cors';
+import 'egg-router-plus';
+import 'egg-validate';
 import { EggPluginItem } from 'egg';
 declare module 'egg' {
   interface EggPlugin {
@@ -48,5 +50,7 @@ declare module 'egg' {
     tracer?: EggPluginItem;
     jwt?: EggPluginItem;
     cors?: EggPluginItem;
+    routerPlus?: EggPluginItem;
+    validate?: EggPluginItem;
   }
 }