1.0.2 • Published 6 years ago

koa-mongo-driver v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

koa-mongo-driver

Package Version Node Version Codecov Build

MongoDB middleware developed on top of official node-mongodb-native.

Installation

NPM

npm install koa-mongo-driver --save

Yarn

yarn add koa-mongo-driver

You will need a MongoDB instance running to be able to use this package.

Basic Usage

const Koa = require('koa');
const app = new Koa();

const mongo = require('koa-mongo-driver');

app.use(mongo());
app.use(async (ctx) => {
  const collection = ctx.mongo.db('test').collection('objects');

  ctx.body = await collection.insertOne({ content: 'Just another object.' });
});

app.listen(3000);

See spec/koa-example.js to check a simple Koa application.

Custom URL Configurations

The mongo() function accepts as first argument an object that is used to define the connection URL for the MongoDB instance.

The host, port, and databaseName can be passed as described in the example below:

const Koa = require('koa');
const app = new Koa();

const mongo = require('koa-mongo-driver');

const urlOptions = {
  url: 'my-mongodb-selfhosted.com',
  port: 12345,
  databaseName: 'myDatabase'
};

app.use(mongo(urlOptions));
app.use(async (ctx) => {
  const collection = ctx.mongo.db('test').collection('objects');

  ctx.body = await collection.insertOne({ content: 'Just another object.' });
});

app.listen(3000);

Custom Connection Configurations

The mongo() function accepts as second argument an object that is used to define the MongoClient options.

Any configuration accepted by the node-mongodb-native can be passed. Check the available settings on MongoClient API Docs by MongoDB.

The next example describes how to open an authenticated connection.

const Koa = require('koa');
const app = new Koa();

const mongo = require('koa-mongo-driver');

const urlOptions = {
  url: 'my-mongodb-selfhosted.com',
  port: 12345,
  databaseName: 'myDatabase'
};

const connectionOptions = {
  auth: {
    user: 'root',
    password: 'toor'
  },
  authSource: 'admin',
  authMechanism: 'PLAIN'
};

app.use(mongo(urlOptions, connectionOptions));
app.use(async (ctx) => {
  const collection = ctx.mongo.db('test').collection('objects');

  ctx.body = await collection.insertOne({ content: 'Just another object.' });
});

app.listen(3000);
1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.1.0

6 years ago