@oangia/services 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Crud.js ADDED
@@ -0,0 +1,27 @@
1
+ const express = require("express");
2
+
3
+ module.exports = function crudRouter(db, collection) {
4
+ const router = express.Router();
5
+
6
+ router.get("/", async (req, res) =>
7
+ res.json(await db.all(collection))
8
+ );
9
+
10
+ router.post("/", async (req, res) =>
11
+ res.json(await db.create(collection, req.body))
12
+ );
13
+
14
+ router.get("/:id", async (req, res) =>
15
+ res.json(await db.read(collection, req.params.id))
16
+ );
17
+
18
+ router.put("/:id", async (req, res) =>
19
+ res.json(await db.update(collection, req.params.id, req.body))
20
+ );
21
+
22
+ router.delete("/:id", async (req, res) =>
23
+ res.json(await db.delete(collection, req.params.id))
24
+ );
25
+
26
+ return router;
27
+ };
@@ -0,0 +1,26 @@
1
+ const crypto = require("crypto");
2
+ const JwtService = require("./JwtService");
3
+ class Auth {
4
+ validate_password(user_password, password) {
5
+ return user_password == this.encrypt(password)
6
+ }
7
+ encrypt(password) {
8
+ password = password + "theredhotchilipeppers";
9
+ return this.md5(password);
10
+ }
11
+
12
+ md5(text) {
13
+ return crypto.createHash("md5").update(text).digest("hex");
14
+ }
15
+
16
+ jwt_generate(data, secret, expiresIn, issuer) {
17
+ const jwtService = new JwtService({
18
+ secret: secret,
19
+ expiresIn: expiresIn,
20
+ issuer: issuer,
21
+ });
22
+ return jwtService.generate(data);
23
+ }
24
+ }
25
+
26
+ module.exports = Auth;
@@ -0,0 +1,30 @@
1
+ // services/auth/JwtService.js
2
+ const jwt = require("jsonwebtoken");
3
+
4
+ class JwtService {
5
+ constructor({ secret, expiresIn = "1h", issuer }) {
6
+ if (!secret) throw new Error("JWT secret is required");
7
+ this.secret = secret;
8
+ this.expiresIn = expiresIn;
9
+ this.issuer = issuer;
10
+ }
11
+
12
+ generate(payload) {
13
+ return jwt.sign(payload, this.secret, {
14
+ expiresIn: this.expiresIn,
15
+ issuer: this.issuer,
16
+ });
17
+ }
18
+
19
+ verify(token) {
20
+ return jwt.verify(token, this.secret, {
21
+ issuer: this.issuer,
22
+ });
23
+ }
24
+
25
+ decode(token) {
26
+ return jwt.decode(token);
27
+ }
28
+ }
29
+
30
+ module.exports = JwtService;
@@ -0,0 +1,56 @@
1
+ // db/mongo.js
2
+ const { MongoClient, ObjectId } = require("mongodb");
3
+
4
+ class MongoDBService {
5
+ constructor() {
6
+ this.client = null;
7
+ this.db = null;
8
+ }
9
+
10
+ init(uri) {
11
+ if (this.client) return this; // already initialized
12
+ this.client = new MongoClient(uri);
13
+ return this;
14
+ }
15
+
16
+ database(dbName) {
17
+ this.db = this.client.db(dbName);
18
+ }
19
+
20
+ async connect() {
21
+ if (!this.db) {
22
+ if (!this.client) throw new Error("MongoService not initialized");
23
+ await this.client.connect();
24
+ }
25
+ }
26
+
27
+ async create(col, data) {
28
+ await this.connect();
29
+ return this.db.collection(col).insertOne(data);
30
+ }
31
+
32
+ async read(col, id) {
33
+ await this.connect();
34
+ return this.db.collection(col).findOne({ _id: new ObjectId(id) });
35
+ }
36
+
37
+ async all(col, query = {}) {
38
+ await this.connect();
39
+ return this.db.collection(col).find(query).toArray();
40
+ }
41
+
42
+ async update(col, id, data) {
43
+ await this.connect();
44
+ return this.db.collection(col).updateOne(
45
+ { _id: new ObjectId(id) },
46
+ { $set: data }
47
+ );
48
+ }
49
+
50
+ async delete(col, id) {
51
+ await this.connect();
52
+ return this.db.collection(col).deleteOne({ _id: new ObjectId(id) });
53
+ }
54
+ }
55
+
56
+ module.exports = new MongoDBService();
package/package.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "name": "@oangia/services",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "author": "",
10
+ "license": "ISC",
11
+ "dependencies": {
12
+ }
13
+ }