npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-toshihiko

v1.0.1

Published

Yet another ORM called Toshihiko plugin for egg.

Downloads

5

Readme

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',
    },
  },
};
  • 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

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