Git Product home page Git Product logo

koa-orm's Introduction

koa-orm

NPM version NPM downloads Dependency Status Build Status Coverage Status Greenkeeper badge

koa-orm using sequelize & sk2.

Installation

npm install koa-orm

Example

Single database

const join = require('path').join;
const config = {
  name: 'test',
  modelPath: join(__dirname, 'models'),
  database: 'orm_test',
  username: 'root',
  password: 'pass',
  dialect: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  pool: {
    max: 10,
    min: 0,
    idle: 30000
  }
};

const orm = require('koa-orm')(config);

app.use(orm.middleware);

app.use(async function (ctx) {
  const raws = await ctx.orm().sql.select().from('table');
  // const raws = await ctx.orm('test').sql('table').select();
  ctx.body = raws;
});

Multiple database

const join = require('path').join;
const configs = [{
  name: 'user',
  database: 'db_user',
  username: 'root',
  password: 'pass',
  dialect: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  modelPath: join(__dirname, 'models/user')
}, {
  name: 'product',
  database: 'db_product',
  username: 'root',
  password: 'pass',
  dialect: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  modelPath: join(__dirname, 'models/product')
}];

const orm = require('koa-orm')(configs);

app.use(orm.middleware);

app.use(async function (ctx) {
  const { User } = ctx.orm('user');
  const { Product } = ctx.orm('product');
  const { userId } = ctx.params;
  
  const user = await User.findByPk(userId);
  const products = await Product.findAll({
    where: { userId }
  });
  ctx.body = { user, products };
});

API

orm(configs)

  • configs: Multi database config array.

Koa 1 Support

To use koa-orm with koa@1, please use koa-orm 1.x.

npm install koa-orm@1 --save

License

MIT

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.