Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brickyang/egg-mongo-native
MongoDB egg.js plugin using native driver.
https://github.com/brickyang/egg-mongo-native
egg-plugin eggjs mongodb mongodb-driver
Last synced: 9 days ago
JSON representation
MongoDB egg.js plugin using native driver.
- Host: GitHub
- URL: https://github.com/brickyang/egg-mongo-native
- Owner: brickyang
- License: mit
- Created: 2017-07-02T01:11:05.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-21T06:52:01.000Z (about 5 years ago)
- Last Synced: 2024-04-26T18:21:10.146Z (7 months ago)
- Topics: egg-plugin, eggjs, mongodb, mongodb-driver
- Language: JavaScript
- Homepage:
- Size: 156 KB
- Stars: 75
- Watchers: 3
- Forks: 11
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-egg - egg-mongo-native - The native MongoDB driver for Egg.js ![](https://img.shields.io/github/stars/brickyang/egg-mongo-native.svg?style=social&label=Star) ![](https://img.shields.io/npm/dm/egg-mongo-native.svg?style=flat-square) (仓库 / 插件)
- awesome-egg - egg-mongo-native - The native MongoDB driver for Egg.js (Plugins)
README
[![NPM version][npm-image]][npm-url]
[![NPM quality][quality-image]][quality-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url][npm-image]: https://img.shields.io/npm/v/egg-mongo-native.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-mongo-native
[quality-image]: http://npm.packagequality.com/shield/egg-mongo-native.svg?style=flat-square
[quality-url]: http://packagequality.com/#?package=egg-mongo-native
[travis-image]: https://img.shields.io/travis/brickyang/egg-mongo-native.svg?branch=master&style=flat-square
[travis-url]: https://travis-ci.org/brickyang/egg-mongo-native
[codecov-image]: https://img.shields.io/codecov/c/github/brickyang/egg-mongo-native.svg?style=flat-square
[codecov-url]: https://codecov.io/github/brickyang/egg-mongo-native?branch=master
[david-image]: https://img.shields.io/david/brickyang/egg-mongo-native.svg?branch=master&style=flat-square
[david-url]: https://david-dm.org/brickyang/egg-mongo-native?branch=master
[snyk-image]: https://snyk.io/test/npm/egg-mongo-native/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-mongo-native
[download-image]: https://img.shields.io/npm/dm/egg-mongo-native.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-mongo-native[**中文版**](https://github.com/brickyang/egg-mongo/blob/master/README.zh_CN.md)
Users who don't use Egg.js could use [easy-mongodb](https://github.com/brickyang/easy-mongodb).
This plugin base on
[node-mongodb-native](https://github.com/mongodb/node-mongodb-native), provides
the official MongoDB native driver and APIs.It wraps some frequently-used API to make it easy to use but keep all properties
as it is. For example, to find a document you need this with official API```js
db.collection('name')
.find(query, options)
.skip(skip)
.limit(limit)
.project(project)
.sort(sort)
.toArray();
```and with this plugin
```js
app.mongo.find('name', { query, skip, limit, project, sort, options });
```## Install
```bash
$ npm i egg-mongo-native --save
```## Enable Plugin
```js
// {app_root}/config/plugin.js
exports.mongo = {
enable: true,
package: 'egg-mongo-native',
};
```## Configuration
### Single Instance
```js
// {app_root}/config/config.default.js
exports.mongo = {
client: {
host: 'host',
port: 'port',
name: 'test',
user: 'user',
password: 'password',
options: {},
},
};
```### Replica Set (v2.1.0 or higher)
```js
// mongodb://host1:port1,host2:port2/name?replicaSet=test
exports.mongo = {
client: {
host: 'host1,host2',
port: 'port1,port2',
name: 'name',
options: {
replicaSet: 'test',
},
},
};// mongodb://host:port1,host:port2/name?replicaSet=test
exports.mongo = {
client: {
host: 'host', // or ['host']
port: 'port1,port2', // or ['port1', 'port2']
name: 'name',
options: {
replicaSet: 'test',
},
},
};
```### Multiple Instances
> **Can not set `client` and `clients` both.**
```js
// {app_root}/config/config.default.js
exports.mongo = {
clients: {
db1: {
host: 'host',
port: 'port',
name: 'db1',
user: 'user',
password: 'password',
options: {},
},
db2: {
host: 'host',
port: 'port',
name: 'db2',
user: 'user',
password: 'password',
options: {},
},
},
};
```see [config/config.default.js](config/config.default.js) for more detail.
## Example
The APIs provided by plugin usually need two arguments. The first is commonly
the collection name, and the second is an object keeps the arguments of official
API. For example, to insert one document using official API```js
db.collection('name').insertOne(doc, options);
```and using plugin API
```js
const args = { doc, options };
app.mongo.insertOne('name', args);
```**For Multiple Instances**
```js
const args = { doc, options };
app.mongo.get('db1').insertOne('name', args);
```The `args` is an object provides the arguments to official API.
Please read [easy-mongodb](https://github.com/brickyang/easy-mongodb) for all APIs(tansaction is now supported) and more examples.
## License
[MIT](LICENSE)