Giter Site home page Giter Site logo

pika-pikachu's Introduction

快速搭建一个基于Koa2的应用

Example

// controller/user.c.js
const { Get, Post, Put, Delete BaseController } = require('pika-pikachu')

class UserController extends BaseController {

  @Get('/:id')
  async getById (ctx, next, { params: { id } }) {
    return await this.app.service.user.getById(id)
  }

  @Post('/')
  async insert (ctx, next, { body: { name, phone } }) {
    return await this.app.service.user.insert({ name, phone })
  }

  @Put('/:id')
  async updateById (ctx, next, { params: { id }, body: { name, phone } }) {
    return await this.app.service.user.updateById(id, { name, phone })
  }

  @Delete('/:id')
  async deleteById (ctx, next, { params: { id } }) {
    return await this.app.service.user.deleteById(id)
  }

}

module.exports = StarCoinController
// service/user.s.js
const { BaseService } = require('pika-pikachu')

class UserService extends BaseService {

  async getById (id) {
    return await DB.User.findById(id)
  }

  async insert (user) {
    return await DB.User.create(user)
  }

  async updateById (id, user) {
    return await DB.User.update(user, {
      where: { id }
    })
  }

  async deleteById (id) {
    return await DB.User.destroy({ where: { id } })
  }

}
// models/user.js
class User {

  static options () {
    return {
      paranoid: true
    }
  }

  static fields (DataTypes) {
    return {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV1,
        primaryKey: true
      },
      name: {
        type: DataTypes.STRING(50)
      },
      phone: {
        type: DataTypes.STRING(11)
      }
    }
  }

}

module.exports = User
// bootstrap.js
const { Pikachu, PikaCore, PikaRouter, SequelizeProvider } = require('pika-pikachu')
const dbConfig = require('config').get('Database')

async function bootstrap () {
  const core = new PikaCore()
  const router = new PikaRouter(8012, '/api')
  const pikachu = await Pikachu(core, router)
  global.DB = await pikachu.connectDatabase(new SequelizeProvider(dbConfig, 'models'))
  return pikachu
}

module.exports = bootstrap
// index.js
const bootstrap = require('./bootstrap')
bootstrap().catch(e => console.error(e))

pika-pikachu's People

Contributors

reidchan avatar

Stargazers

John Smith avatar Bruce Zhang avatar

Watchers

Bruce Zhang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.