Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/prisma #4

Merged
merged 5 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ The next commits will be more focused on the following tasks, but of course keep
- [x] create the base
- [x] create a working version
- [x] configure the project (settings files)
- [ ] create connection with the database
- [x] create connection with the database
- [ ] create tests
- [ ] make a good file architecture
- [ ] create authentication system
Expand Down
2 changes: 1 addition & 1 deletion locart.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
},
database: {
// The URL to connect to MongoDB
mongodb_url: "mongodb://localhost",
database_url: "",
},
// storage settings
storage: {
Expand Down
2,958 changes: 443 additions & 2,515 deletions package-lock.json

Large diffs are not rendered by default.

15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "locart",
"version": "1.0.0",
"description": "A server for hosting images",
"main": "src/main.js",
"main": "src/server.js",
"type": "module",
"scripts": {
"start": "node src/main.js",
"dev": "nodemon src/main.js",
"start": "node src/server.js",
"dev": "nodemon src/server.js",
"lint": "eslint . --ext .js --cache --cache-strategy content",
"format": "prettier . --write --ignore-unknown --cache --cache-strategy content",
"prepare": "husky"
Expand All @@ -21,11 +21,13 @@
}
},
"dependencies": {
"@prisma/client": "^5.13.0",
"bson": "^6.7.0",
"cors": "^2.8.5",
"express": "^4.19.2",
"helmet": "^7.1.0",
"multer": "^1.4.5-lts.1",
"simpl.db": "^2.13.0"
"mongoose": "^8.3.4",
"multer": "^1.4.5-lts.1"
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
Expand All @@ -36,7 +38,8 @@
"husky": "^9.0.11",
"lint-staged": "^15.2.2",
"nodemon": "^3.1.0",
"prettier": "^3.2.5"
"prettier": "^3.2.5",
"prisma": "^5.13.0"
},
"license": "Apache-2.0",
"author": "HarukaYamamoto0 <harukayamamotodev@gmail.com>",
Expand Down
16 changes: 16 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model Image {
id String @id @map("_id")
path String @unique
createdAt DateTime @default(now())

@@map("image")
}
15 changes: 15 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from "express";
import cors from "cors";
import helmet from "helmet";

import imageController from "./controllers/image.controller.js";

const app = express();
app.use(cors());
app.use(helmet());
app.use(express.json());

// Controllers
app.use("/images", imageController);

export { app };
27 changes: 27 additions & 0 deletions src/controllers/image.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Router } from "express";
import multer from "multer";
import multerConfig from "../core/multer.js";
import prismaClient from "../database/prismaClient.js";
import { resolve } from "node:path";
import { FileOperations } from "../utils/FileOperations.js";

const router = Router();

router.post("/", multer(multerConfig).single("file"), async (req, res) => {
const path = resolve(req.file.destination, req.file.filename);

try {
const image = await prismaClient.image.create({
data: {
id: req.file.filename.split(".")[0],
path,
},
});
res.send(image);
} catch (error) {
console.error(`[ImageController] - Error when trying to create the image: ` + error);
FileOperations.rm(path);
}
});

export default router;
7 changes: 5 additions & 2 deletions src/core/multer.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import multer from "multer";
import configs from "../../locart.config.js";
import { randomUUID } from "node:crypto";
import { ObjectId } from "bson";
import { FileOperations } from "../utils/FileOperations.js";

const { path: storagePath, sizeLimit, allowedMimes } = configs.storage;

FileOperations.createStoragePath(storagePath);

const storageConfig = {
dest: storagePath,
storage: multer.diskStorage({
destination: storagePath,
filename: (req, file, cb) => {
const fileExtension = file.mimetype.split("/")[1];
cb(null, randomUUID() + "." + fileExtension);
cb(null, new ObjectId() + "." + fileExtension);
},
}),
limits: {
Expand Down
13 changes: 13 additions & 0 deletions src/database/checkDatabaseConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import prismaClient from "./prismaClient.js";

async function checkDatabaseConnection() {
try {
await prismaClient.image.findMany(); // irei trocar isso para usuarios
console.log("[Prisma] - Successful database connection");
} catch (error) {
console.error("[Prisma] - Failed when trying to connect to the database" + error);
process.exit(1);
}
}

export { checkDatabaseConnection };
8 changes: 8 additions & 0 deletions src/database/prismaClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PrismaClient } from "@prisma/client";
import configs from "../../locart.config.js";

const prismaClient = new PrismaClient({
datasourceUrl: configs.database.database_url,
});

export default prismaClient;
20 changes: 0 additions & 20 deletions src/main.js

This file was deleted.

11 changes: 0 additions & 11 deletions src/routes/images.router.js

This file was deleted.

11 changes: 11 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { app } from "./app.js";
import configs from "../locart.config.js";
import { checkDatabaseConnection } from "./database/checkDatabaseConnection.js";

const port = configs.server.port || process.env.PORT || 3333;

await checkDatabaseConnection();

app.listen(port, () => {
console.log(`[Server] - Server started`);
});
25 changes: 25 additions & 0 deletions src/utils/FileOperations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { rm as rmFile, mkdir } from "node:fs";

class FileOperations {
static rm(path) {
rmFile(path, { recursive: true }, (error) => {
if (error) {
console.error(
"[FileOperations] - Error when trying to delete the file: " + path + "\n" + error,
);
}
});
}

static createStoragePath(path) {
mkdir(path, { recursive: true }, (error) => {
if (error) {
console.error(
"[FileOperations] - Error when trying to create storage: " + path + "\n" + error,
);
}
});
}
}

export { FileOperations };
Loading