d-band / koa-orm

koa orm using sequelize & sk2 (fork from knex)
61 stars 10 forks source link

Postgresql #28

Closed ghost closed 7 years ago

ghost commented 7 years ago

Can this ORM be used to talk to Postgresql using async/await?

helloyou2012 commented 7 years ago

Yes, simple like this:

$ npm install --save pg pg-hstore
$ npm install --save koa-orm
const join = require('path').join;
const config = {
  modelPath: join(__dirname, 'models'),
  database: 'orm_test',
  username: 'root',
  password: 'pass',
  dialect: 'postgres',
  host: '127.0.0.1',
  port: 5432,
  pool: {
    maxConnections: 10,
    minConnections: 0,
    maxIdleTime: 30000
  }
};

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

app.use(orm.middleware);

app.use(async function (ctx) {
  const { User } = ctx.orm();
  const { userId } = ctx.params;

  ctx.body = await User.findById(userId);
});
ghost commented 7 years ago

Great thanks!

alexdrans commented 7 years ago

@helloyou2012 What is pg-hstore used for?

helloyou2012 commented 7 years ago

@alexdrans sequelize use it for serializing and deserializing JSON data: http://docs.sequelizejs.com/en/v3/docs/getting-started/