Commit 06361949 authored by 尚斌杰's avatar 尚斌杰

增加model

parent 0745644c
'use strict';
const path = require('path');
module.exports = {
config: path.join(__dirname, 'database/config.json'),
'migrations-path': path.join(__dirname, 'database/migrations'),
'seeders-path': path.join(__dirname, 'database/seeders'),
'models-path': path.join(__dirname, 'app/model'),
};
\ No newline at end of file
'use strict';
const Controller = require('egg').Controller;
function toInt(str) {
if (typeof str === 'number') return str;
if (!str) return str;
return parseInt(str, 10) || 0;
}
class UserController extends Controller {
async index() {
const ctx = this.ctx;
const query = { limit: toInt(ctx.query.limit), offset: toInt(ctx.query.offset) };
ctx.body = await ctx.model.User.findAll(query);
}
async show() {
const ctx = this.ctx;
ctx.body = await ctx.model.User.findById(toInt(ctx.params.id));
}
async create() {
const ctx = this.ctx;
const { name, age } = ctx.request.body;
const user = await ctx.model.User.create({ name, age });
ctx.status = 201;
ctx.body = user;
}
async update() {
const ctx = this.ctx;
const id = toInt(ctx.params.id);
const user = await ctx.model.User.findById(id);
if (!user) {
ctx.status = 404;
return;
}
const { name, age } = ctx.request.body;
await user.update({ name, age });
ctx.body = user;
}
async destroy() {
const ctx = this.ctx;
const id = toInt(ctx.params.id);
const user = await ctx.model.User.findById(id);
if (!user) {
ctx.status = 404;
return;
}
await user.destroy();
ctx.status = 200;
}
}
module.exports = UserController;
'use strict';
module.exports = app => {
const { STRING, INTEGER, DATE } = app.Sequelize;
const User = app.model.define('user', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: STRING(30),
age: INTEGER,
created_at: DATE,
updated_at: DATE,
});
return User;
};
...@@ -11,4 +11,5 @@ module.exports = app => { ...@@ -11,4 +11,5 @@ module.exports = app => {
router.get('/newscontent', controller.news.content); router.get('/newscontent', controller.news.content);
router.get('/newslist/:id', controller.news.newslist); router.get('/newslist/:id', controller.news.newslist);
router.get('/admin', controller.admin.index); router.get('/admin', controller.admin.index);
router.resources('users', '/users', controller.users);
}; };
...@@ -7,7 +7,7 @@ exports.ejs = { ...@@ -7,7 +7,7 @@ exports.ejs = {
package: 'egg-view-ejs', package: 'egg-view-ejs',
}; };
// exports.sequelize = { exports.sequelize = {
// enable: true, enable: true,
// package: 'egg-sequelize', package: 'egg-sequelize',
// }; };
{
"development": {
"username": "root",
"password": "shang4148",
"database": "egg-sequelize-example-dev",
"host": "127.0.0.1",
"dialect": "mysql"
},
"test": {
"username": "root",
"password": "shang4148",
"database": "egg-sequelize-example-dev",
"host": "127.0.0.1",
"dialect": "mysql"
},
"production": {
"username": "root",
"password": "shang4148",
"database": "egg-sequelize-example-dev",
"host": "127.0.0.1",
"dialect": "mysql"
}
}
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
const { INTEGER, DATE, STRING } = Sequelize;
await queryInterface.createTable('users', {
id: { type: INTEGER, primaryKey: true, autoIncrement: true },
name: STRING(30),
age: INTEGER,
created_at: DATE,
updated_at: DATE,
});
},
down: async queryInterface => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
await queryInterface.dropTable('users');
},
};
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
"egg-mock": "^3.14.0", "egg-mock": "^3.14.0",
"eslint": "^4.11.0", "eslint": "^4.11.0",
"eslint-config-egg": "^6.0.0", "eslint-config-egg": "^6.0.0",
"sequelize-cli": "4.1.1",
"webstorm-disable-index": "^1.2.0" "webstorm-disable-index": "^1.2.0"
}, },
"engines": { "engines": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment