Skip to content

Commit

Permalink
Node v16 Update!
Browse files Browse the repository at this point in the history
Notable Changes
- Mongoose v6! [breaking changes](https://mongoosejs.com/docs/migrating_to_6.html)
- MongoDB driver update!
- Updated all dependencies to latest
- No longer supports Node v10

- .github/*: Added github CI actions
- test.*: Updated for compatibility and coverage
- Removed mocha.opts, .eslintignore, .eslintrc.json .travis.yml
- CrudService.js:
  - Refactored query builder, using rest spread instead of manual deletes
  - Removed `exec` option from _count, as this now causes queries to get fired immediately (!)
- MongoService.js: Updated for compatibility
- package.json:
  - Bumped to v3.0.0
  - Updated all dependencies to latest
  - Migrated mocha/eslint configs
- README.md: Updated mongo container version
  • Loading branch information
kfitzgerald committed Mar 19, 2022
1 parent b27649e commit 4204234
Show file tree
Hide file tree
Showing 14 changed files with 4,428 additions and 1,486 deletions.
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

29 changes: 0 additions & 29 deletions .eslintrc.json

This file was deleted.

22 changes: 22 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Publish Docs

on:
release:
types: [published]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Configure AWS
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.DOCS_KEY }}
aws-secret-access-key: ${{ secrets.DOCS_SECRET }}
aws-region: us-east-1

- name: Copy Docs
run: aws s3 cp ./README.md s3://okanjo-docs/okanjo-app-elastic/ --acl public-read --content-type "text/plain; charset=utf-8" --content-encoding text
50 changes: 50 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:

build:
runs-on: ubuntu-latest

services:
mongodb:
image: mongo:4.0
options: >-
--health-cmd "mongo --eval 'db.runCommand(\"ping\").ok' --quiet"
--health-interval 10s
--health-timeout 5s
--health-retries 10
ports:
# <port on host>:<port on container>
- 27017:27017

strategy:
fail-fast: false
matrix:
node-version: [12.x, 14.x, 16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'

- run: npm ci
- run: npm run report

- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ coverage
.idea
docs
.eslint*
.travis.yml
.travis.yml
.github
34 changes: 0 additions & 34 deletions .travis.yml

This file was deleted.

25 changes: 9 additions & 16 deletions CrudService.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class CrudService {
* Constructor
* @param app
* @param model
* @param [dbService]
*/
constructor(app, model, dbService) {
// Make sure we're not going to have problems
Expand Down Expand Up @@ -187,14 +188,9 @@ class CrudService {
* @private
*/
_buildQuery(criteria, options) {

// Strip options out so we can stick them into the query builder
options = options || {};
let skip, limit, fields, sort, query, conceal = true;
if (options.skip !== undefined) { skip = options.skip; delete options.skip; }
if (options.take !== undefined) { limit = options.take; delete options.take; }
if (options.fields !== undefined) { fields = options.fields; delete options.fields; }
if (options.sort !== undefined) { sort = options.sort; delete options.sort; }
if (options.conceal !== undefined) { conceal = options.conceal; delete options.conceal; }
const { skip, take, fields, sort, conceal = true, ...opts } = (options || {});

// Actively prevent dead resources from returning, even if a status was given
if (this._concealDeadResources && conceal) {
Expand Down Expand Up @@ -241,14 +237,14 @@ class CrudService {
}

// Build the query
query = this.model.find(criteria);
const query = this.model.find(criteria);

// Add query options to the builder if present
if (skip !== undefined) { query = query.skip(skip); }
if (limit !== undefined) { query = query.limit(limit); }
if (fields !== undefined) { query = query.select(fields); }
if (sort !== undefined) { query = query.sort(sort); }
if (Object.keys(options).length > 0) { query = query.setOptions(options); }
if (skip !== undefined) { query.skip(skip); }
if (take !== undefined) { query.limit(take); }
if (fields !== undefined) { query.select(fields); }
if (sort !== undefined) { query.sort(sort); }
if (Object.keys(opts).length > 0) { query.setOptions(opts); }

return query;
}
Expand Down Expand Up @@ -311,9 +307,6 @@ class CrudService {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {

// Don't execute, we want the query so we can fudge it
options.exec = false;

// Exec the count query
const query = this._buildQuery(criteria, options);

Expand Down
23 changes: 8 additions & 15 deletions MongoService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ const Mongoose = require('mongoose');
const BaseId = require('base-id');
const ObjectId = Mongoose.Types.ObjectId;

// Hey mongoose, shut the hell up, ok?
// http://mongoosejs.com/docs/promises.html
Mongoose.Promise = global.Promise;

Mongoose.set('useCreateIndex', true);


/**
* Multi-database manager for Mongoose/MongoDB
* Keeps track of multiple database connection states and reports health on all.
Expand All @@ -38,25 +31,27 @@ class MongoService extends EventEmitter {
this._dbConnections = {};

// Register the connection with the app
app._serviceConnectors.push((cb) => {
app._serviceConnectors.push(new Promise((resolve) => {

// Do the connection
let resolved = false;
this.connect();
this.once('health_change', (state) => {
/* istanbul ignore else: too hard to edge case this with unit tests and docker */
// If the callback has not been fired, then we're ready now!
if (state && cb) {
cb();
if (state && !resolved) {
resolved = true;
resolve();
}
});

/* istanbul ignore if: too hard to edge case this with unit tests and docker */
// If the connection is already established, the health_change might not flip, so callback now if we're already good
if (this.getHealthStatus()) {
cb();
cb = null;
resolved = true;
resolve();
}
});
}));
}

/**
Expand Down Expand Up @@ -327,8 +322,6 @@ class MongoService extends EventEmitter {

// Create the connection
connection = this._dbConnections[schemaName] = Mongoose.createConnection(uri, {
useUnifiedTopology: true,
useNewUrlParser: true,
keepAlive: true
}); // this will automatically open the connection

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ Before you can run the tests, you'll need a working mongodb server. We suggest u
For example:

```bash
docker pull mongo:3.5
docker run -d -p 27017:27017 mongo:3.5
docker pull mongo:4.0
docker run -d -p 27017:27017 mongo:4.0
```

To run unit tests and code coverage:
Expand Down
Loading

0 comments on commit 4204234

Please sign in to comment.