Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benoitclaveau/qwebs-mongo
Mongo client for Qwebs server
https://github.com/benoitclaveau/qwebs-mongo
Last synced: 19 days ago
JSON representation
Mongo client for Qwebs server
- Host: GitHub
- URL: https://github.com/benoitclaveau/qwebs-mongo
- Owner: BenoitClaveau
- License: mit
- Created: 2015-10-02T13:00:35.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-11T15:06:36.000Z (over 6 years ago)
- Last Synced: 2024-12-08T10:36:48.753Z (20 days ago)
- Language: JavaScript
- Size: 11.9 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# qwebs-mongo
[Mongo](https://www.npmjs.com/package/mongodb) service for [Qwebs server](https://www.npmjs.com/package/qwebs).[![NPM][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]## Features
* [Qwebs](https://www.npmjs.com/package/qwebs)
* [Mongo API](http://mongodb.github.io/node-mongodb-native/2.2/api/)
* Singleton```js
return $mongo.db.then(db => {
//db is a singleton Mongo Db instance
});
```### Add the mongo connection string in config.json
```json
{
"mongo": {
"connectionString": "mongodb://localhost:27017/database"
},
}
```### Declare and inject $mongo service
#### Via route.json
```routes.json
{
"services": [
{ "name": "$mongo", "location": "qwebs-mongo" }
]
}
```#### Or in javascript
```js
const Qwebs = require("qwebs");
const qwebs = new Qwebs();
qwebs.inject("$mongo" ,"qwebs-mongo");
```### Use $mongo service
#### Hight level api
```js
const { CRUD } = require("qwebs-mongo");class Api extends CRUD {
constructor($mongo) {
super("collectionName", $mongo);
};/* manage skip and limit as querystring */
httpStream(request, response) {
request.mongo = { //define mongo query, options,...
options: {
limit: parseInt(request.query.limit),
skip: parseInt(request.query.skip)
}
}
return super.httpStream(request, response);
}
```#### Low level api
```js
/* no extend -> custom implementation */
class Api {
constructor($mongo) {
this.$mongo = $mongo;
};httpStream(request, response) {
return this.$mongo.db.then(db => {
const limit = parseInt(request.query.limit);
const skip = parseInt(request.query.skip);
const stream = db.collection("collectionName").find({}).limit(limit).skip(skip).stream();
return response.send({ request: request, stream: stream });
});
);
};
```## Installation
```bash
$ npm install qwebs-mongo
```## Test
To run our tests, clone the qwebs-mongo repo and install the dependencies.
```bash
$ git clone https://github.com/BenoitClaveau/qwebs-mongo --depth 1
$ cd qwebs-mongo
$ npm install
$ mongod --dbpath ./data/db
$ node.exe "../node_modules/mocha/bin/mocha" tests
```[npm-image]: https://img.shields.io/npm/v/qwebs-mongo.svg
[npm-url]: https://npmjs.org/package/qwebs-mongo
[travis-image]: https://travis-ci.org/BenoitClaveau/qwebs-mongo.svg?branch=master
[travis-url]: https://travis-ci.org/BenoitClaveau/qwebs-mongo
[coveralls-image]: https://coveralls.io/repos/BenoitClaveau/qwebs-mongo/badge.svg?branch=master&service=github
[coveralls-url]: https://coveralls.io/github/BenoitClaveau/qwebs-mongo?branch=master