Skip to content

Commit

Permalink
feat: sequelize integration part 7 - fixed tests and added express bo…
Browse files Browse the repository at this point in the history
…ilerplate

TODO:
  - test with graphql
  • Loading branch information
AnthonyLzq committed Sep 5, 2022
1 parent f37356f commit fae427f
Show file tree
Hide file tree
Showing 118 changed files with 12,621 additions and 4,575 deletions.
16 changes: 2 additions & 14 deletions .github/workflows/test.yml → .github/workflows/test-mongo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push]

jobs:
test:
environment: Test
environment: Test-mongo
name: Tests for Simba APIs
runs-on: ubuntu-latest

Expand All @@ -28,17 +28,5 @@ jobs:
- name: Test APIs
run: npm run test:ci
env:
MONGO_URI: ${{ secrets.MONGO_URI }}
DB_URI: ${{ secrets.DB_URI }}
NODE_ENV: ci

# - name: Build examples
# run: npm run build:ci
# env:
# MONGO_URI: ${{ secrets.MONGO_URI }}
# NODE_ENV: ci

# - name: Test Fastify rest example
# run: npm run test:fastify:ci
# env:
# MONGO_URI: ${{ secrets.MONGO_URI }}
# NODE_ENV: ci
32 changes: 32 additions & 0 deletions .github/workflows/test-postgresql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests for Simba APIs

on: [push]

jobs:
test:
environment: Test-postgresql
name: Tests for Simba APIs
runs-on: ubuntu-latest

steps:
- name: Check out Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16.x

- name: Install Node.js dependencies
run: npm ci

- name: Revert changes into the package-lock.json file
run: git checkout -- package-lock.json

- name: Test APIs
run: npm run test:ci
env:
DB_URI: ${{ secrets.DB_URI }}
NODE_ENV: ci
3 changes: 2 additions & 1 deletion example/express-graphql/.github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on: [push]

jobs:
test:
environment: Test
name: Testing Simba.js API
runs-on: ubuntu-latest

Expand All @@ -27,5 +28,5 @@ jobs:
- name: Run test
run: npm run test:ci
env:
MONGO_URI: ${{ secrets.MONGO_URI }}
DB_URI: ${{ secrets.DB_URI }}
NODE_ENV: ci
33 changes: 33 additions & 0 deletions example/express-graphql/index.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
### Testing store a user
POST http://localhost:1996/api/users
Content-Type: application/json

{
"args": {
"lastName": "Lzq",
"name": "Anthony"
}
}

### Testing getAll users
GET http://localhost:1996/api/users

### Testing deleteAll users
DELETE http://localhost:1996/api/users

### Testing getOne user
GET http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b

### Testing update user
PATCH http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
Content-Type: application/json

{
"args": {
"name": "Anthony",
"lastName": "Luzquiños"
}
}

### Testing delete user
DELETE http://localhost:1996/api/user/60e7e3b93b01c1a7aa74cd6b
46 changes: 23 additions & 23 deletions example/express-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,55 +16,55 @@
"author": "AnthonyLzq <sluzquinosa@uni.pe>",
"license": "MIT",
"dependencies": {
"@graphql-tools/schema": "^8.5.0",
"@sinclair/typebox": "^0.24.19",
"@graphql-tools/schema": "^9.0.2",
"@sinclair/typebox": "^0.24.35",
"ajv": "^8.11.0",
"ajv-formats": "^2.1.1",
"apollo-server-core": "^3.10.0",
"apollo-server-express": "^3.10.0",
"apollo-server-core": "^3.10.2",
"apollo-server-express": "^3.10.2",
"cors": "^2.8.5",
"express": "^4.18.1",
"express-pino-logger": "^7.0.0",
"graphql": "^16.5.0",
"graphql": "^16.6.0",
"http-errors": "^2.0.0",
"mongoose": "^6.4.4",
"pino-pretty": "^8.1.0",
"mongoose": "^6.5.4",
"pino-pretty": "^9.1.0",
"swagger-ui-express": "^4.5.0"
},
"devDependencies": {
"@jest/types": "^28.1.3",
"@jest/types": "^29.0.2",
"@types/cors": "^2.8.12",
"@types/express": "^4.17.13",
"@types/express-pino-logger": "^4.0.3",
"@types/http-errors": "^1.8.2",
"@types/jest": "^28.1.5",
"@types/node": "^18.0.4",
"@types/jest": "^29.0.0",
"@types/node": "^18.7.14",
"@types/swagger-ui-express": "^4.1.3",
"@typescript-eslint/eslint-plugin": "^5.30.6",
"@typescript-eslint/parser": "^5.30.6",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
"axios": "^0.27.2",
"dotenv": "^16.0.1",
"eslint": "^8.19.0",
"dotenv": "^16.0.2",
"eslint": "^8.23.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-standard": "^17.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.6.0",
"eslint-plugin-n": "^15.2.4",
"eslint-plugin-jest": "^27.0.1",
"eslint-plugin-n": "^15.2.5",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.0.0",
"jest": "^28.1.3",
"eslint-plugin-promise": "^6.0.1",
"jest": "^29.0.2",
"jest-unit": "^0.0.1",
"nodemon": "^2.0.19",
"prettier": "^2.7.1",
"standard-version": "^9.5.0",
"ts-jest": "^28.0.6",
"ts-jest": "^28.0.8",
"ts-loader": "^9.3.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.0.0",
"tsconfig-paths-webpack-plugin": "^3.5.2",
"typescript": "^4.7.4",
"webpack": "^5.73.0",
"tsconfig-paths": "^4.1.0",
"tsconfig-paths-webpack-plugin": "^4.0.0",
"typescript": "^4.8.2",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-node-externals": "^3.0.0"
}
Expand Down
8 changes: 4 additions & 4 deletions example/express-graphql/src/database/mongo/connection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { connect, connection } from 'mongoose'
import { HttpLogger } from 'express-pino-logger'
import { FastifyLoggerInstance } from 'fastify'

const ENVIRONMENTS_WITHOUT_RECONNECTION = ['ci', 'local']
const dbConnection = async (
logger: HttpLogger['logger']
logger: FastifyLoggerInstance
): Promise<{
connect: () => Promise<typeof import('mongoose')>
disconnect: () => Promise<void>
Expand All @@ -30,7 +30,7 @@ const dbConnection = async (
'Mongo connection disconnected. Trying to reconnected to Mongo...'
)
setTimeout(() => {
connect(process.env.MONGO_URI as string, {
connect(process.env.DB_URI as string, {
...connection,
connectTimeoutMS: 3000,
socketTimeoutMS: 3000
Expand All @@ -47,7 +47,7 @@ const dbConnection = async (
})

return {
connect: () => connect(process.env.MONGO_URI as string, connectionConfig),
connect: () => connect(process.env.DB_URI as string, connectionConfig),
disconnect: () => connection.close()
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/express-graphql/src/network/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import swaggerUi from 'swagger-ui-express'
import httpErrors from 'http-errors'

import { response } from './response'
import { Home } from './routes'
import { Home, User } from './routes'
import { docs } from 'utils'

const routers: Router[] = []
const routers = [User]
const applyRoutes = (app: Application): void => {
app.use('/', Home)
app.use('/api/docs', swaggerUi.serve, swaggerUi.setup(docs))
Expand Down
2 changes: 1 addition & 1 deletion example/express-graphql/src/network/routes/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Home = Router()
Home.route('').get((req: Request, res: Response) => {
response({
error: false,
message: 'Welcome to your GraphQL Express Backend!',
message: 'Welcome to your Express Backend!',
res,
status: 200
})
Expand Down
1 change: 1 addition & 0 deletions example/express-graphql/src/network/routes/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './home'
export * from './user'
132 changes: 132 additions & 0 deletions example/express-graphql/src/network/routes/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { NextFunction, Router } from 'express'

import { response } from 'network/response'
import { UserService } from 'services'
import { idSchema, storeUserDto, UserWithId } from 'schemas'
import { validatorCompiler } from './utils'

const User = Router()

User.route('/users')
.post(
validatorCompiler(storeUserDto, 'body'),
async (
req: CustomRequest,
res: CustomResponse,
next: NextFunction
): Promise<void> => {
try {
const {
body: { args: user }
} = req
const us = new UserService({ user })
const result = await us.process({ type: 'store' })

response({ error: false, message: result, res, status: 201 })
} catch (error) {
next(error)
}
}
)
.get(
async (
req: CustomRequest,
res: CustomResponse,
next: NextFunction
): Promise<void> => {
try {
const us = new UserService()
const result = await us.process({ type: 'getAll' })

response({ error: false, message: result, res, status: 200 })
} catch (error) {
next(error)
}
}
)
.delete(
async (
req: CustomRequest,
res: CustomResponse,
next: NextFunction
): Promise<void> => {
try {
const us = new UserService()
const result = await us.process({ type: 'deleteAll' })

response({ error: false, message: result, res, status: 200 })
} catch (error) {
next(error)
}
}
)

User.route('/user/:id')
.get(
validatorCompiler(idSchema, 'params'),
async (
req: CustomRequest,
res: CustomResponse,
next: NextFunction
): Promise<void> => {
try {
const {
params: { id }
} = req
const us = new UserService({ id })
const result = await us.process({ type: 'getOne' })

response({ error: false, message: result, res, status: 200 })
} catch (error) {
next(error)
}
}
)
.patch(
validatorCompiler(idSchema, 'params'),
validatorCompiler(storeUserDto, 'body'),
async (
req: CustomRequest,
res: CustomResponse,
next: NextFunction
): Promise<void> => {
try {
const {
body: { args },
params: { id }
} = req
const userWithId = {
id,
...args
} as UserWithId
const us = new UserService({ userWithId })
const result = await us.process({ type: 'update' })

response({ error: false, message: result, res, status: 200 })
} catch (error) {
next(error)
}
}
)
.delete(
validatorCompiler(idSchema, 'params'),
async (
req: CustomRequest,
res: CustomResponse,
next: NextFunction
): Promise<void> => {
try {
const {
params: { id }
} = req
const us = new UserService({ id })
const result = await us.process({ type: 'delete' })

response({ error: false, message: result, res, status: 200 })
} catch (error) {
next(error)
}
}
)

export { User }
Loading

0 comments on commit fae427f

Please sign in to comment.