Giter VIP home page Giter VIP logo

koa-mongo's Introduction

koa-mongo

koa-mongo is a mongodb middleware for koa@2, support connection pool.

Install

npm i koa-mongo --save

Usage

app.use(mongo({
  host: 'localhost',
  port: 27017,
  user: 'admin',
  pass: '123456',
  db: 'test',
  authSource: 'admin',
  max: 100,
  min: 1,
  ...
}));

or

app.use(mongo({
  uri: 'mongodb://admin:123456@localhost:27017/test?authSource=admin', //or url
  max: 100,
  min: 1
  ...
}));

or

app.use(mongo({
  uri: 'mongodb://admin:123456@localhost:27017/test?authSource=admin', //or url
  max: 100,
  min: 1
  ...
}, {
  useUnifiedTopology: true
  ...
}
));

defaultOptions:

{
  host: 'localhost',
  port: 27017,
  db: 'test',
  authSource: 'admin',
  max: 100,
  min: 1,
  acquireTimeoutMillis: 100
}

More options see generic-pool.

ctx.mongo & ctx.db

ctx.mongo is an instance of MongoClient.

ctx.db === ctx.mongo.db(dbName)

Example

const Koa = require('koa')
const mongo = require('koa-mongo')

const app = new Koa()

app.use(mongo())
app.use(async (ctx, next) => {
  // ctx.db === ctx.mongo.db('test')
  const result = await ctx.db.collection('users').insert({ name: 'haha' })
  const userId = result.ops[0]._id.toString()
  ctx.body = await ctx.db.collection('users').find().toArray()
  ctx.db.collection('users').remove({
    _id: mongo.ObjectId(userId)
  })
})
app.listen(3000, () => {
  console.log('listening on port 3000')
})

Mongolass is a elegant MongoDB driver for Node.js.

License

MIT

koa-mongo's People

Contributors

bermanboris avatar coderoshi avatar double-m avatar lucaskatayama avatar natedean avatar nswbmw avatar south-paw avatar vanodevium avatar

Watchers

 avatar

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.