Skip to content

Commit

Permalink
Merge 4e18c0e into 0619dec
Browse files Browse the repository at this point in the history
  • Loading branch information
Sefriol committed Apr 5, 2023
2 parents 0619dec + 4e18c0e commit 254c08a
Show file tree
Hide file tree
Showing 15 changed files with 8,000 additions and 2,641 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
"node": true,
"mocha": true
},

"rules": {
"indent": [
"error",
2,
{
"SwitchCase": 1
}
],
"max-len": [
"warn",
{
Expand Down
9 changes: 9 additions & 0 deletions .github/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM mongo:5.0.15

VOLUME /data
EXPOSE 27001 27002 27003

COPY docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh

ENTRYPOINT ["docker-entrypoint.sh"]
60 changes: 60 additions & 0 deletions .github/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash
set -e

REPLICA_SET_NAME=${REPLICA_SET_NAME:=rs0}
USERNAME=${USERNAME:=dev}
PASSWORD=${PASSWORD:=dev}


function waitForMongo {
port=$1
n=0
until [ $n -ge 20 ]
do
mongo admin --quiet --port $port --eval "db" && break
n=$[$n+1]
sleep 2
done
}

if [ ! "$(ls -A /data/db1)" ]; then
mkdir /data/db1
mkdir /data/db2
mkdir /data/db3
fi

echo "STARTING CLUSTER"

mongod --port 27003 --dbpath /data/db3 --replSet $REPLICA_SET_NAME --bind_ip=::,0.0.0.0 &
DB3_PID=$!
mongod --port 27002 --dbpath /data/db2 --replSet $REPLICA_SET_NAME --bind_ip=::,0.0.0.0 &
DB2_PID=$!
mongod --port 27001 --dbpath /data/db1 --replSet $REPLICA_SET_NAME --bind_ip=::,0.0.0.0 &
DB1_PID=$!

waitForMongo 27001
waitForMongo 27002
waitForMongo 27003

echo "CONFIGURING REPLICA SET"
CONFIG="{ _id: '$REPLICA_SET_NAME', members: [{_id: 0, host: 'localhost:27001', priority: 2 }, { _id: 1, host: 'localhost:27002' }, { _id: 2, host: 'localhost:27003' } ]}"
mongo admin --port 27001 --eval "db.runCommand({ replSetInitiate: $CONFIG })"

waitForMongo 27002
waitForMongo 27003

mongo admin --port 27001 --eval "db.runCommand({ setParameter: 1, quiet: 1 })"
mongo admin --port 27002 --eval "db.runCommand({ setParameter: 1, quiet: 1 })"
mongo admin --port 27003 --eval "db.runCommand({ setParameter: 1, quiet: 1 })"

mongo admin --port 27001 --eval "db.adminCommand({ setParameter: 1, maxTransactionLockRequestTimeoutMillis: 5000 })"
mongo admin --port 27002 --eval "db.adminCommand({ setParameter: 1, maxTransactionLockRequestTimeoutMillis: 5000 })"
mongo admin --port 27003 --eval "db.adminCommand({ setParameter: 1, maxTransactionLockRequestTimeoutMillis: 5000 })"

echo "REPLICA SET ONLINE"

trap 'echo "KILLING"; kill $DB1_PID $DB2_PID $DB3_PID; wait $DB1_PID; wait $DB2_PID; wait $DB3_PID' SIGINT SIGTERM EXIT

wait $DB1_PID
wait $DB2_PID
wait $DB3_PID
3 changes: 3 additions & 0 deletions .github/mongo_setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
docker build -t mongodb-rs:latest ./.github/

docker run -d -p 27001:27001 -p 27002:27002 -p 27003:27003 --name mongo -e "REPLICA_SET_NAME=rs0" mongodb-rs
2 changes: 2 additions & 0 deletions .github/semantic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Always validate the PR title AND all the commits
titleAndCommits: true
86 changes: 86 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
name: main
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [^16, ^18]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup MongoDB
run: bash ./.github/mongo_setup.sh

- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependency
run: npm install --ignore-engines

# - name: Build library
# run: npm run build

- name: Run unit tests
run: npm run test

coverall:
needs: [test]
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version: [ ^16, ^18 ]
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup MongoDB
run: bash ./.github/mongo_setup.sh

- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependency
run: npm install --ignore-engines

# - name: Build library
# run: npm run build

- name: Run code coverage
run: npm run coverage

- name: Coveralls Parallel
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

release:
needs: [test,coverall]
runs-on: ubuntu-latest
steps:
- name: Coveralls Finished
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true

- name: Checkout
uses: actions/checkout@v2

- name: Install Dependency
run: npm install --ignore-engines

# - name: Build library
# run: npm run build

- name: Release
if: github.event_name == 'push' && github.repository == 'sefriol/resourcejs' && github.branch == 'master'
run: npx -p semantic-release -p @semantic-release/git -p @semantic-release/changelog -p @semantic-release/commit-analyzer -p @semantic-release/release-notes-generator -p @semantic-release/release-notes-generator -p @semantic-release/changelog -p @semantic-release/git -p @semantic-release/github semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
21 changes: 21 additions & 0 deletions .github/workflows/release-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: release-package
on:
release:
types: [created]

jobs:
publish-gpr:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 18
registry-url: https://npm.pkg.github.com/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
5 changes: 3 additions & 2 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

module.exports = {
diff: true,
recursive: true,
extension: ['js'],
opts: false,
package: './package.json',
exit: true,
reporter: 'spec',
slow: 75,
timeout: 2000,
ui: 'bdd',
'watch-files': ['test/**/testKoa.js']
spec: 'test/**/*.js'
};
22 changes: 22 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"debug": true,
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/npm",
[
"@semantic-release/changelog",
{
"changelogFile": "Changelog.md"
}
],
[
"@semantic-release/git",
{
"assets": ["package.json", "Changelog.md"],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
],
"@semantic-release/github"
]
}
24 changes: 19 additions & 5 deletions KoaResource.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ class Resource {
}
}

static ObjectId(id) {
try {
return (new mongodb.ObjectId(id));
}
catch (e) {
return id;
}
}

/**
* Returns the method options for a specific method to be executed.
* @param method
Expand Down Expand Up @@ -375,13 +384,13 @@ class Resource {
options.convertIds &&
name.match(options.convertIds) &&
(typeof value === 'string') &&
(mongodb.ObjectID.isValid(value))
(mongodb.ObjectId.isValid(value))
) {
try {
value = new mongodb.ObjectID(value);
value = Resource.ObjectId(value);
}
catch (err) {
console.warn(`Invalid ObjectID: ${value}`);
console.warn(`Invalid ObjectId: ${value}`);
}
}

Expand Down Expand Up @@ -543,9 +552,14 @@ class Resource {
ctx.state.findQuery = this.getFindQuery(ctx);

// Get the query object.
const countQuery = ctx.state.countQuery || ctx.state.modelQuery || ctx.state.model || this.model;
let countQuery = ctx.state.countQuery || ctx.state.modelQuery || ctx.state.model || this.model;
ctx.state.query = ctx.state.modelQuery || ctx.state.model || this.model;

// Make sure to clone the count query if it is available.
if (typeof countQuery.clone === 'function') {
countQuery = countQuery.clone();
}

// First get the total count.
let count;
try {
Expand Down Expand Up @@ -870,7 +884,7 @@ class Resource {
const { __v, ...update } = ctx.request.body;
ctx.state.query = ctx.state.modelQuery || ctx.state.model || this.model;
try {
ctx.state.item = await ctx.state.query.findOne({ _id: ctx.params[`${this.name}Id`] }).exec();
ctx.state.item = await ctx.state.query.findOne({ _id: Resource.ObjectId(ctx.params[`${this.name}Id`]) }).exec();
}
catch (err) {
debug.put(err);
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ Resource.js - A simple Express library to reflect Mongoose models to a REST inte
==============================================================
[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[![Build Status](https://travis-ci.org/travist/resourcejs.svg?branch=master)](https://travis-ci.org/travist/resourcejs)
![Build Status: master](https://github.com/Sefriol/resourcejs/actions/workflows/main.yml/badge.svg?branch=master)
![Build Status: dev](https://github.com/Sefriol/resourcejs/actions/workflows/main.yml/badge.svg?branch=dev)
[![Coverage Status](https://coveralls.io/repos/github/Sefriol/resourcejs/badge.svg?branch=master)](https://coveralls.io/github/Sefriol/resourcejs?branch=master)

[npm-image]: https://img.shields.io/npm/v/resourcejs.svg?style=flat-square
Expand Down

0 comments on commit 254c08a

Please sign in to comment.