@onesy/mongo
Version:
Utils for easier using of mongodb library.
144 lines (103 loc) • 2.26 kB
Markdown
</br>
</br>
<p align='center'>
<a target='_blank' rel='noopener noreferrer' href='#'>
<img src='utils/images/logo.svg' alt='onesy logo' />
</a>
</p>
<h1 align='center'>onesy Mongo</h1>
<p align='center'>
Mongo
</p>
<br />
<h3 align='center'>
<sub>MIT license </sub>
<sub>Production ready </sub>
<sub>100% test cov </sub>
<sub>Nodejs</sub>
</h3>
<p align='center'>
<sub>Very simple code </sub>
<sub>Modern code </sub>
<sub>Junior friendly </sub>
<sub>Typescript </sub>
<sub>Made with :yellow_heart:</sub>
</p>
<br />
## Getting started
### Add
```sh
yarn add @onesy/mongo
```
Add `mongodb` as a peer dependency
```sh
yarn add mongodb
```
### Use
```javascript
import { Mongo, BaseCollection } from '@onesy/mongo';
// Make if you wanna a config file and
// inside of it add all the process.env related props
import Config from './config';
// Make a new mongo instance
const mongo = new Mongo({
uri: Config.db.mongo.uri,
name: Config.db.mongo.name,
});
await mongo.connection;
// Make a collection class
class TodoCollection extends BaseCollection {
public constructor() {
super('todos', mongo);
}
}
const todoCollection = new TodoCollection();
// Add
const todoCreated = await todoCollection.addOne({
name: 'todo',
description: 'description'
});
// Find one
const todo = await aCollection.findOne({
_id: todoCreated._id
});
todo;
// {
// _id: ObjectId('407f191e810c19729de860ea'),
// name: 'todo',
// description: 'description',
// added_at: 1777044477
// }
// etc.
```
### Dev
Install
```sh
yarn
```
Test
```sh
yarn test
```
#### One time local setup
Install docker and docker-compose
- https://docs.docker.com/get-docker
- https://docs.docker.com/compose/install
Make docker containers
```sh
yarn docker
```
(mac) Add lines below to */private/etc/hosts*
```
127.0.0.1 mongo1
127.0.0.1 mongo2
127.0.0.1 mongo3
127.0.0.1 mongo1-test
127.0.0.1 mongo2-test
127.0.0.1 mongo3-test
```
### Prod
Build
```sh
yarn build
```