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.
Installation
$ npm install --save egg-toshihiko$ npm install --save mysql2
Usage & Configuration
config/config.default.js
exportstoshihiko = database: '' host: 'localhost' port: 3306 username: 'root' password: '' connections: default: database: 'egg-toshihiko' host: 'localhost' port: 3306 username: 'root' password: '' noBase: database: 'mysql' ;
exports.toshihiko
may contain the default configuration of Toshihiko. (refs: http://docs.toshihikojs.com/en/latest/docs/getting-started/#setting-up-a-connection)exports.toshihiko.connections
is an object that contains one or several connection configurations. The configuration will combine with default configuration.
config/plugin.js
exportstoshihiko = 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 = apptoshi;const User = conn;
And you can also define a model via default connection by calling
app.toshi.define()
. e.g.
const User = apptoshi;
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.
apptoshiString;apptoshiJson;apptoshiInteger;...
Example
Define a model first:
// app/model/user.js module { const User = apptoshi; User { return 'hello'; }; return User;};
Now you can use it in your controller:
// app/controller/users.js module { return Controller async { const user = await thisctxmodelUser; thisctxbody = user; } async { thisctxbody = await appmodelUser; } ;};
Questions & Suggestions
Please open an issue here.