Giter VIP home page Giter VIP logo

egg-toshihiko's Introduction

egg-toshihiko

Yet another ORM called Toshihiko plugin for egg.

NOTE: This plugin just for integrate Toshihiko into Egg.js, more documentation please visit http://github.com/XadillaX/Toshihiko.

NPM version Build Status Build status Test coverage David deps Known Vulnerabilities npm download

Installation

$ npm install --save egg-toshihiko
$ npm install --save mysql2

Usage & Configuration

config/config.default.js

exports.toshihiko = {
  database: '',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: '',

  connections: {
    default: {
      database: 'egg-toshihiko',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: '',
    },
    noBase: {
      database: 'mysql',
    },
  },
};

config/plugin.js

exports.toshihiko = {
  enable: true,
  package: 'egg-toshihiko'
};

Model Files

Please put models under app/model directory.

Model File Class Name
user.js app.model.User
person.js app.model.Person
user_group.js app.model.UserGroup

Defining a Model

When define a model, you should get a toshihiko connection first.

app.toshi or app.toshihiko equals to require('toshihiko').Toshihiko.

And an extra function app.toshi.get(CONN_NAME) returns a toshihiko connection with name CONN_NAME.

You may use a connection to define a model. e.g.

const conn = app.toshi.get('conn');
const User = conn.define('users', [
  ...
]);

And you can also define a model via default connection by calling app.toshi.define(). e.g.

const User = app.toshi.define('users', [
  ...
]);

Types

In package Toshihiko, the types that be used in defining are in require('toshihiko').Type. Here in egg-toshihiko, you may access types directly in app.toshi. e.g.

app.toshi.String;
app.toshi.Json;
app.toshi.Integer;
...

Example

Define a model first:

// app/model/user.js

module.exports = app => {
  const User = app.toshi.define('users', [
    { name: 'id', type: app.toshi.Integer, primaryKey: true },
    { name: 'username', type: app.toshi.String },
  ]);

  User.test = function() {
    return 'hello';
  };

  return User;
};

Now you can use it in your controller:

// app/controller/users.js

module.exports = app => {
  return class UsersController extends app.Controller {
    async show() {
      const user = await this.ctx.model.User.findById(this.ctx.params.id);
      this.ctx.body = user;
    }

    async create() {
      this.ctx.body = await app.model.User.build({
        username: this.ctx.request.body.username,
      }).save();
    }
  };
};

Questions & Suggestions

Please open an issue here.

License

MIT

egg-toshihiko's People

Contributors

xadillax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.