Name: egg-toshihiko
Owner: egg
Description: Yet another ORM called Toshihiko plugin for egg.
Created: 2017-11-15 08:56:22.0
Updated: 2018-05-14 06:09:37.0
Pushed: 2017-12-18 06:17:57.0
Homepage: null
Size: 18
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
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.
m install --save egg-toshihiko
m install --save mysql2
rts.toshihiko = {
tabase: '',
st: 'localhost',
rt: 3306,
ername: 'root',
ssword: '',
nnections: {
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.rts.toshihiko = {
able: true,
ckage: 'egg-toshihiko'
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 |
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.
t conn = app.toshi.get('conn');
t User = conn.define('users', [
.
And you can also define a model via default connection by calling
app.toshi.define()
. e.g.
t User = app.toshi.define('users', [
.
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.
toshi.String;
toshi.Json;
toshi.Integer;
Define a model first:
pp/model/user.js
le.exports = app => {
nst User = app.toshi.define('users', [
{ name: 'id', type: app.toshi.Integer, primaryKey: true },
{ name: 'username', type: app.toshi.String },
;
er.test = function() {
return 'hello';
turn User;
Now you can use it in your controller:
pp/controller/users.js
le.exports = app => {
turn 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();
}
Please open an issue here.