-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: check user roles * refactor: nit * test: ensure and admin can assign and unassign a task * fix: authorization plugin has no dependency * fix: update migrations dir path * fix: eslint * refactor: nit * Update .env.example Signed-off-by: Jean <[email protected]> * refactor: use knex * refactor: migrations * fix: remove useless c8 ignore comments * docs: update path * refactor: change JWT auth for cookie session auth * chore: ci - env must have required property 'COOKIE_NAME' * fix: uncomment unauthenticated test * refactor: leverage fastify sensible decorators * chore: use tsx * feat: add pagination to tasks * refactor: use COUNT(*) OVER() AS rowNum for tasks pagination * refactor: decorate request for authorization * fix: use transaction for login controller * refactor: register cookie plugin in session plugin * test: mock app.compare implementation instead of reassignation * test: spy logger to ensure 500 error is due to Transaction failure * feat: allow to upload task image * refactor: improve scripts typing * docs: static and multipart plugin * chore: dangerous DB operations should be explicitly authorized * refactor: use node test runner utitities * refactor: check file size before mime-type * fix: identifier typo * feat: do not use rm -rf * fix: storage path disclosure * fix: nit --------- Signed-off-by: Jean <[email protected]>
- Loading branch information
1 parent
8ae2437
commit dfb66bb
Showing
47 changed files
with
1,209 additions
and
519 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
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 |
---|---|---|
|
@@ -137,3 +137,6 @@ bun.lockb | |
package-lock.json | ||
pnpm-lock.yaml | ||
yarn.lock | ||
|
||
# uploaded files | ||
uploads/tasks/* |
This file was deleted.
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
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,4 @@ | ||
CREATE TABLE roles ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL | ||
); |
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 @@ | ||
DROP TABLE IF EXISTS roles; |
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,7 @@ | ||
CREATE TABLE user_roles ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
user_id INT NOT NULL, | ||
role_id INT NOT NULL, | ||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, | ||
FOREIGN KEY (role_id) REFERENCES roles(id) ON DELETE 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 @@ | ||
DROP TABLE IF EXISTS user_roles; |
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 |
---|---|---|
|
@@ -12,26 +12,30 @@ | |
"build": "tsc", | ||
"watch": "tsc -w", | ||
"dev": "npm run build && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"npm:watch\" \"npm:dev:start\"", | ||
"dev:start": "fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js", | ||
"dev:start": "npm run build && fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js", | ||
"test": "npm run db:seed && tap --jobs=1 test/**/*", | ||
"standalone": "node --env-file=.env dist/server.js", | ||
"standalone": "npm run build && node --env-file=.env dist/server.js", | ||
"lint": "eslint --ignore-pattern=dist", | ||
"lint:fix": "npm run lint -- --fix", | ||
"db:migrate": "node --env-file=.env scripts/migrate.js", | ||
"db:seed": "node --env-file=.env scripts/seed-database.js" | ||
"db:create": "tsx --env-file=.env ./scripts/create-database.ts", | ||
"db:drop": "tsx --env-file=.env ./scripts/drop-database.ts", | ||
"db:migrate": "tsx --env-file=.env ./scripts/migrate.ts", | ||
"db:seed": "tsx --env-file=.env ./scripts/seed-database.ts" | ||
}, | ||
"keywords": [], | ||
"author": "Michelet Jean <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@fastify/autoload": "^6.0.0", | ||
"@fastify/cookie": "^11.0.1", | ||
"@fastify/cors": "^10.0.0", | ||
"@fastify/env": "^5.0.1", | ||
"@fastify/helmet": "^12.0.0", | ||
"@fastify/jwt": "^9.0.0", | ||
"@fastify/mysql": "^5.0.1", | ||
"@fastify/multipart": "^9.0.1", | ||
"@fastify/rate-limit": "^10.0.1", | ||
"@fastify/sensible": "^6.0.1", | ||
"@fastify/session": "^11.0.1", | ||
"@fastify/static": "^8.0.2", | ||
"@fastify/swagger": "^9.0.0", | ||
"@fastify/swagger-ui": "^5.0.1", | ||
"@fastify/type-provider-typebox": "^5.0.0", | ||
|
@@ -41,15 +45,18 @@ | |
"fastify": "^5.0.0", | ||
"fastify-cli": "^7.0.0", | ||
"fastify-plugin": "^5.0.1", | ||
"form-data": "^4.0.1", | ||
"knex": "^3.1.0", | ||
"mysql2": "^3.11.3", | ||
"postgrator": "^7.3.0" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^22.5.5", | ||
"eslint": "^9.11.0", | ||
"fastify-tsconfig": "^2.0.0", | ||
"mysql2": "^3.11.3", | ||
"neostandard": "^0.11.5", | ||
"tap": "^21.0.1", | ||
"tsx": "^4.19.1", | ||
"typescript": "~5.6.2" | ||
} | ||
} |
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,30 @@ | ||
import { createConnection, Connection } from 'mysql2/promise' | ||
|
||
if (Number(process.env.CAN_CREATE_DATABASE) !== 1) { | ||
throw new Error("You can't create the database. Set `CAN_CREATE_DATABASE=1` environment variable to allow this operation.") | ||
} | ||
|
||
async function createDatabase () { | ||
const connection = await createConnection({ | ||
host: process.env.MYSQL_HOST, | ||
port: Number(process.env.MYSQL_PORT), | ||
user: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD | ||
}) | ||
|
||
try { | ||
await createDB(connection) | ||
console.log(`Database ${process.env.MYSQL_DATABASE} has been created successfully.`) | ||
} catch (error) { | ||
console.error('Error creating database:', error) | ||
} finally { | ||
await connection.end() | ||
} | ||
} | ||
|
||
async function createDB (connection: Connection) { | ||
await connection.query(`CREATE DATABASE IF NOT EXISTS \`${process.env.MYSQL_DATABASE}\``) | ||
console.log(`Database ${process.env.MYSQL_DATABASE} created or already exists.`) | ||
} | ||
|
||
createDatabase() |
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,30 @@ | ||
import { createConnection, Connection } from 'mysql2/promise' | ||
|
||
if (Number(process.env.CAN_DROP_DATABASE) !== 1) { | ||
throw new Error("You can't drop the database. Set `CAN_DROP_DATABASE=1` environment variable to allow this operation.") | ||
} | ||
|
||
async function dropDatabase () { | ||
const connection = await createConnection({ | ||
host: process.env.MYSQL_HOST, | ||
port: Number(process.env.MYSQL_PORT), | ||
user: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD | ||
}) | ||
|
||
try { | ||
await dropDB(connection) | ||
console.log(`Database ${process.env.MYSQL_DATABASE} has been dropped successfully.`) | ||
} catch (error) { | ||
console.error('Error dropping database:', error) | ||
} finally { | ||
await connection.end() | ||
} | ||
} | ||
|
||
async function dropDB (connection: Connection) { | ||
await connection.query(`DROP DATABASE IF EXISTS \`${process.env.MYSQL_DATABASE}\``) | ||
console.log(`Database ${process.env.MYSQL_DATABASE} dropped.`) | ||
} | ||
|
||
dropDatabase() |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import mysql, { FieldPacket } from 'mysql2/promise' | ||
import path from 'node:path' | ||
import fs from 'node:fs' | ||
import Postgrator from 'postgrator' | ||
|
||
interface PostgratorResult { | ||
rows: any; | ||
fields: FieldPacket[]; | ||
} | ||
|
||
async function doMigration (): Promise<void> { | ||
const connection = await mysql.createConnection({ | ||
multipleStatements: true, | ||
host: process.env.MYSQL_HOST, | ||
port: Number(process.env.MYSQL_PORT), | ||
database: process.env.MYSQL_DATABASE, | ||
user: process.env.MYSQL_USER, | ||
password: process.env.MYSQL_PASSWORD | ||
}) | ||
|
||
try { | ||
const migrationDir = path.join(import.meta.dirname, '../migrations') | ||
|
||
if (!fs.existsSync(migrationDir)) { | ||
throw new Error( | ||
`Migration directory "${migrationDir}" does not exist. Skipping migrations.` | ||
) | ||
} | ||
|
||
const postgrator = new Postgrator({ | ||
migrationPattern: path.join(migrationDir, '*'), | ||
driver: 'mysql', | ||
database: process.env.MYSQL_DATABASE, | ||
execQuery: async (query: string): Promise<PostgratorResult> => { | ||
const [rows, fields] = await connection.query(query) | ||
return { rows, fields } | ||
}, | ||
schemaTable: 'schemaversion' | ||
}) | ||
|
||
await postgrator.migrate() | ||
|
||
console.log('Migration completed!') | ||
} catch (err) { | ||
console.error(err) | ||
} finally { | ||
await connection.end().catch(err => console.error(err)) | ||
} | ||
} | ||
|
||
doMigration() |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.