Skip to content

Commit

Permalink
fix ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
lagden committed Jan 5, 2024
0 parents commit 0c8e35c
Show file tree
Hide file tree
Showing 16 changed files with 2,530 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
indent_style = tab
indent_size = 2
tab_width = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{md,yml,yaml,json,graphql}]
indent_style = space

[*.{md,yml,yaml}]
trim_trailing_whitespace = false
63 changes: 63 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
root: true
env:
es2022: true
node: true
parserOptions:
ecmaVersion: 13
sourceType: module
extends:
- eslint:recommended
- plugin:sonarjs/recommended
- plugin:unicorn/recommended
rules:
camelcase: off
capitalized-comments: off
indent: [error, tab]
linebreak-style: [error, unix]
no-console: off
no-debugger: off
# no-multi-assign: off
# no-multiple-empty-lines:
# - error
# -
# max: 2
# maxBOF: 2
# maxEOF: 0
# no-new-func: off
no-undef-init: off
no-unused-expressions:
- error
-
allowShortCircuit: true
allowTernary: true
allowTaggedTemplates: true
padding-line-between-statements: off
quotes: [error, single]
# require-atomic-updates: off
semi: [error, never]
semi-spacing:
- error
-
before: false
after: true
spaced-comment: off
# unicorn/consistent-destructuring: off
unicorn/filename-case: off
# unicorn/import-style:
# - error
# -
# styles:
# util: false
# path:
# named: true
# unicorn/no-abusive-eslint-disable: off
unicorn/no-array-reduce: off
# unicorn/no-negated-condition: off
# unicorn/no-null: off
unicorn/no-useless-undefined: off
unicorn/no-zero-fractions: off
# unicorn/prefer-dom-node-dataset: off
unicorn/prefer-includes: off
# unicorn/prefer-object-from-entries: off
unicorn/prefer-query-selector: off
unicorn/prevent-abbreviations: off
35 changes: 35 additions & 0 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Node.js CI

on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install pnpm
run: npm i -g pnpm

- name: Install dependencies
run: pnpm install

- name: Run test
run: npm test

- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
node_modules
test/helper/**/*
3 changes: 3 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
engine-strict=true
resolution-mode=highest
auto-install-peers=true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Thiago Lagden <lagden@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
121 changes: 121 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Mongo Singleton

[![NPM version][npm-img]][npm]
[![Node.js CI][ci-img]][ci]
[![Coverage Status][coveralls-img]][coveralls]


[npm-img]: https://img.shields.io/npm/v/@tadashi/mongo-singleton.svg
[npm]: https://www.npmjs.com/package/@tadashi/mongo-singleton
[ci-img]: https://github.com/lagden/mongo-singleton/actions/workflows/nodejs.yml/badge.svg
[ci]: https://github.com/lagden/mongo-singleton/actions/workflows/nodejs.yml
[coveralls-img]: https://coveralls.io/repos/github/lagden/mongo-singleton/badge.svg?branch=master
[coveralls]: https://coveralls.io/github/lagden/mongo-singleton?branch=master

-----

Simplifying MongoDB

## Install

```
$ npm i -S @tadashi/mongo-singleton
```


## API

### Environment variables available

- MONGO_CONN
- MONGO_DB
- MONGO_USER
- MONGO_PASS
- MONGO_AUTHSOURCE
- MONGO_POOL_SIZE = 10

### Mongo.conn( \[args\]):MongoClient

Name | Type | Default | Description
----------- | --------- | ----------------- | ------------
args | object | {} | [See bellow](#args)


#### args

Name | Type | Default | Description
----------- | --------- | ------------------------------ | ------------
url | string | MONGO_CONN | [See the manual](https://www.mongodb.com/docs/manual/reference/connection-string/)
username | string | MONGO_USER | Database user
password | string | MONGO_PASS | Database password
authSource | string | MONGO_AUTHSOURCE | Authentication Database
options | object | {maxPoolSize: MONGO_POOL_SIZE} | [See the manual](https://mongodb.github.io/node-mongodb-native/6.3/interfaces/MongoClientOptions.html)


### Mongo.collection(collectionName \[, options \]):Collection

Name | Type | Default | Description
-------------- | --------- | -------------- | ------------
collectionName | string | - | Collection name
options | object | {} | [See bellow](#options)


#### options

Name | Type | Default | Description
------------- | --------- | -------------------------------- | ------------
dbName | string | MONGO_DB | Database name
dbOptions | object | {} | [See the manual](https://mongodb.github.io/node-mongodb-native/6.3/interfaces/DbOptions.html)
collectionOptions | object | [See bellow](#collectionOptions) | [See the manual](https://mongodb.github.io/node-mongodb-native/6.3/interfaces/CollectionOptions.html)


##### collectionOptions

Name | Type | Default | Description
----------------------- | --------- | ---------- | ------------
writeConcern | object | {w: 1} | -


## Usage

**Example A:**

```js
import Mongo from '@tadashi/mongo-singleton'

const client = await Mongo.conn({
url: 'mongodb://mongodb.example.com:27017',
username: 'username',
password: 'password'
})
const db = client.db('my_DB')
await db.dropDatabase()
// more code...
```


**Example B:**

```js
// Set the environment variables before start the application
// - MONGO_CONN
// - MONGO_DB
// - MONGO_USER
// - ...
import Mongo from '@tadashi/mongo-singleton'

// will return the collection if exists or create new one
const collection = await Mongo.collection('users')
const users = await collection.find({name: 'Tadashi'}).toArray()
// more code...
```


## Donate ❤️

- BTC: bc1q7famhuj5f25n6qvlm3sssnymk2qpxrfwpyq7g4


## License

MIT © [Thiago Lagden](https://github.com/lagden)
54 changes: 54 additions & 0 deletions bin/helper/fn
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/bin/sh

abort() {
printf "\n \033[31mError: $@\033[0m\n\n" && exit 1
}

ok() {
printf "\n \033[32mOk: $@\033[0m\n\n"
}

load_env() {
_DIR="$(pwd)"
ENVFILE_LOCAL="${2:-$_DIR}/.conf/local.sh"
ENVFILE_OPT="${2:-$_DIR}/.conf/${1:-development}.sh"
USE_LOCAL_ENV=0

if test ! -e $ENVFILE_OPT; then
abort "Environment file not found"
fi

set -a
. ${ENVFILE_OPT}
set +a

if test "${1}" = "development" -o "${1}" = "test"; then
USE_LOCAL_ENV=1
fi

if test -e $ENVFILE_LOCAL -a "${USE_LOCAL_ENV:-0}" = "1"; then
set -a
. ${ENVFILE_LOCAL}
set +a
fi
}

gen_env() {
_DIR="$(cd $(dirname $0) && pwd)"
_BIN_DIR="$(cd $DIR/.. && pwd)"
GEN_ENV="${2:-$_BIN_DIR}/node/env.js"

if test -z $1; then
abort "Missing output"
fi

if test ! -f "${GEN_ENV}"; then
abort "File not found: ${GEN_ENV}"
fi

_DIR_FILE=$(dirname $1)
mkdir -p $_DIR_FILE

$GEN_ENV > $1
ok "ENVs generated... ${1}"
}
32 changes: 32 additions & 0 deletions bin/helper/wait
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/sh

: ${SLEEP_LENGTH:=2}
: ${TIMEOUT_LENGTH:=60}

DIR="$(cd $(dirname $0) && pwd)"
BIN_DIR="$(cd $DIR/.. && pwd)"

# Import functions
. $BIN_DIR/helper/fn

wait_for() {
START="$(date +%s)"
echo "Waiting for $1 to listen on $2..."
while ! nc -z $1 $2; do
if [ $(($(date +%s) - $START)) -gt $TIMEOUT_LENGTH ]; then
abort "Service $1:$2 did not start within $TIMEOUT_LENGTH seconds. Aborting..."
fi
echo "sleeping"
sleep $SLEEP_LENGTH
done
}

for var in "$@"
do
host=${var%:*}
port=${var#*:}
wait_for $host $port
ok "Listening ${host}:${port}"
done

exit 0
Loading

0 comments on commit 0c8e35c

Please sign in to comment.