-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user repor,error middleware and db connect
- Loading branch information
raulgonzalez
committed
Apr 30, 2024
1 parent
6e7afed
commit 6edb3b7
Showing
22 changed files
with
587 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
const config = { | ||
clearMocks: true, | ||
export default { | ||
collectCoverage: true, | ||
|
||
collectCoverageFrom: ['src/*/*.ts'], | ||
coverageDirectory: 'coverage', | ||
|
||
coveragePathIgnorePatterns: [ | ||
'index.ts', | ||
'type.repo.ts', | ||
'entities', | ||
'interface', | ||
'tools', | ||
'_mock', | ||
], | ||
coverageProvider: 'v8', | ||
preset: 'ts-jest', | ||
testPathIgnorePatterns: ['dist'], | ||
resolver: 'jest-ts-webcompat-resolver', | ||
}; | ||
export default config; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
-- CreateEnum | ||
CREATE TYPE "Race" AS ENUM ('men', 'elve', 'dwarf', 'urukhai', 'orc', 'hobbit'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" TEXT NOT NULL, | ||
"email" TEXT NOT NULL, | ||
"password" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Character" ( | ||
"id" TEXT NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"description" TEXT NOT NULL, | ||
"imgUrl" TEXT NOT NULL, | ||
"faction" TEXT NOT NULL, | ||
"race" "Race" NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updatedAt" TIMESTAMP(3) NOT NULL, | ||
|
||
CONSTRAINT "Character_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Character" ADD CONSTRAINT "Character_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
Warnings: | ||
- A unique constraint covering the columns `[userNamo]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
- Added the required column `userNamo` to the `User` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "userNamo" TEXT NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_userNamo_key" ON "User"("userNamo"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `userNamo` on the `User` table. All the data in the column will be lost. | ||
- A unique constraint covering the columns `[userName]` on the table `User` will be added. If there are existing duplicate values, this will fail. | ||
- Added the required column `userName` to the `User` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- DropIndex | ||
DROP INDEX "User_userNamo_key"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "User" DROP COLUMN "userNamo", | ||
ADD COLUMN "userName" TEXT NOT NULL; | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "User_userName_key" ON "User"("userName"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? | ||
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
model User { | ||
id String @id @default(cuid()) | ||
email String @unique | ||
userName String @unique | ||
password String | ||
character Character[] | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
} | ||
model Character{ | ||
id String @id @default(cuid()) | ||
user User @relation(fields:[userId], references:[id]) | ||
userId String | ||
name String | ||
description String | ||
imgUrl String | ||
faction String | ||
race Race | ||
createdAt DateTime @default(now()) | ||
updatedAt DateTime @updatedAt | ||
} | ||
|
||
|
||
enum Race { | ||
men | ||
elve | ||
dwarf | ||
urukhai | ||
orc | ||
hobbit | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import express, { type Express } from 'express'; | ||
import morgan from 'morgan'; | ||
import cors from 'cors'; | ||
import createDebug from 'debug'; | ||
import { type PrismaClient } from '@prisma/client'; | ||
|
||
const debug = createDebug('GONJI:app'); | ||
export const createApp = () => { | ||
debug('Creating app'); | ||
return express(); | ||
}; | ||
|
||
export const startApp = (app: Express, prisma: PrismaClient) => { | ||
debug('Starting app'); | ||
app.use(express.json()); | ||
app.use(morgan('dev')); | ||
app.use(cors()); | ||
app.use(express.static('public')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import joy from 'joi'; | ||
import { type CharacterCreateDto } from './character'; | ||
|
||
export const characterCreateSchema = joy.object<CharacterCreateDto>({ | ||
name: joy.string().required(), | ||
imgUrl: joy.string().uri().required(), | ||
description: joy.string().required(), | ||
faction: joy.string().required(), | ||
userId: joy.string().required(), | ||
}); | ||
export const characterUpdateSchema = joy.object<Partial<CharacterCreateDto>>({ | ||
name: joy.string(), | ||
imgUrl: joy.string().uri(), | ||
description: joy.string(), | ||
faction: joy.string(), | ||
userId: joy.string(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export type Character = { | ||
id: string; | ||
name: string; | ||
imgUrl: string; | ||
description: string; | ||
faction: string; | ||
race: 'Men' | 'Elf' | 'Elve' | 'Dwarf' | 'Uruk-hai' | 'Orc' | 'Hobbit'; | ||
userId: string; | ||
}; | ||
export type CharacterCreateDto = { | ||
name: string; | ||
imgUrl: string; | ||
description: string; | ||
faction: string; | ||
race: 'Men' | 'Elf' | 'Elve' | 'Dwarf' | 'Uruk-hai' | 'Orc' | 'Hobbit'; | ||
userId: string; | ||
}; | ||
export type CharacterUpdateDto = Partial<CharacterCreateDto>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import joy from 'joi'; | ||
import { type UserCreateDto } from './user'; | ||
|
||
export const userCreateSchema = joy.object<UserCreateDto>({ | ||
email: joy.string().email().required(), | ||
password: joy.string().min(5).required(), | ||
userName: joy.string().min(3).required(), | ||
}); | ||
export const userUpdateSchema = joy.object<Partial<UserCreateDto>>({ | ||
email: joy.string().email(), | ||
password: joy.string().min(5), | ||
userName: joy.string().min(3), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { type Character } from './character'; | ||
|
||
export type User = { | ||
id: string; | ||
email: string; | ||
password: string; | ||
userName: string; | ||
characters: Character[]; | ||
}; | ||
|
||
export type UserCreateDto = { | ||
email: string; | ||
password: string; | ||
userName: string; | ||
}; | ||
export type UserUpdateDto = Partial<UserCreateDto>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,31 @@ | ||
import { createServer } from 'http'; | ||
import createDebug from 'debug'; | ||
import 'dotenv/config'; | ||
import { createApp, startApp } from './app.js'; | ||
import { dbConnect } from './tools/db.connect.js'; | ||
|
||
const debug = createDebug('GONJI:server'); | ||
debug('Starting server'); | ||
|
||
const port = process.env.PORT ?? 3000; | ||
|
||
const app = createApp(); | ||
const server = createServer(app); | ||
|
||
dbConnect() | ||
.then((prisma) => { | ||
startApp(app, prisma); | ||
server.listen(port); | ||
}) | ||
.catch((error) => { | ||
server.emit('error', error); | ||
}); | ||
|
||
server.on('error', (error) => { | ||
debug('Error:', error); | ||
process.exit(1); | ||
}); | ||
|
||
server.on('listening', () => { | ||
console.log(`Server Express is running http://localhost:${port}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type Request, type Response } from 'express'; | ||
import { ErrorsMiddleware, HttpError } from './errors.middleware'; | ||
import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'; | ||
|
||
const req = {} as unknown as Request; | ||
const res = { | ||
json: jest.fn(), | ||
status: jest.fn(), | ||
} as unknown as Response; | ||
const next = jest.fn(); | ||
|
||
describe('Given a instance of the class ErrorsMiddleware', () => { | ||
const middleware = new ErrorsMiddleware(); | ||
test('Then it should be instance of the class', () => { | ||
expect(middleware).toBeInstanceOf(ErrorsMiddleware); | ||
}); | ||
describe('When we use the method handle with a HttpError', () => { | ||
test('Then it should call res.status 404', () => { | ||
const error = new HttpError(404, 'Not Found', 'Article not found'); | ||
middleware.handle(error, req, res, next); | ||
expect(res.status).toHaveBeenCalledWith(404); | ||
expect(res.json).toHaveBeenCalled(); | ||
}); | ||
}); | ||
describe('When we use the method handle with a PrismaClientKnownRequestError', () => { | ||
test('Then it should call res.status 404', () => { | ||
const error = new PrismaClientKnownRequestError('error', { | ||
code: 'P2025', | ||
clientVersion: '3.0.0', | ||
}); | ||
middleware.handle(error, req, res, next); | ||
expect(res.status).toHaveBeenCalledWith(403); | ||
expect(res.json).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('When we use the method handle with a Error', () => { | ||
test('Then it should call res.status with 500', () => { | ||
const error = new Error('Something went wrong'); | ||
middleware.handle(error, req, res, next); | ||
expect(res.status).toHaveBeenCalledWith(500); | ||
expect(res.json).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.