db-json-cli 1.0.0 β†’ 1.0.1

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/LICENCE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 hitorigotoh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -0,0 +1,108 @@
1
+ # πŸ“¦ db-json-cli
2
+
3
+ > Lightweight local JSON API server with JWT auth + private/public route control
4
+ > μ™„μ „ν•œ 둜컬 λͺ©μ„œλ²„ + 인증/κΆŒν•œ μ œμ–΄ CLI 툴
5
+
6
+ --------------
7
+
8
+ ## πŸš€ μ„€μΉ˜
9
+
10
+ npm install -g db-json-cli
11
+ λ˜λŠ” ν”„λ‘œμ νŠΈ λ‚΄λΆ€μ—μ„œ:
12
+
13
+ bash
14
+ npm install db-json-cli
15
+ βš™οΈ μ‚¬μš©λ²•
16
+ 1️⃣ μ„œλ²„ μ‹œμž‘
17
+ db-json-cli --db ./db.json --port 4000
18
+ κΈ°λ³Έ ν¬νŠΈλŠ” 4000, JSON 파일 κ²½λ‘œλŠ” ./db.json
19
+
20
+ 2️⃣ κΈ°λ³Έ JSON ꡬ쑰 μ˜ˆμ‹œ
21
+
22
+ {
23
+ "users": [],
24
+ "list": [
25
+ { "id": 1, "title": "Private item" },
26
+ { "id": 2, "title": "Visible to authenticated users only" }
27
+ ],
28
+ "public": [
29
+ { "id": 1, "title": "Publicly visible item" }
30
+ ],
31
+ "rules": {
32
+ "list": "private",
33
+ "public": "public"
34
+ }
35
+ }
36
+
37
+ rules 객체에 각 μ»¬λ ‰μ…˜μ˜ μ ‘κ·Ό κΆŒν•œ(public / private)을 μ§€μ •ν•©λ‹ˆλ‹€.
38
+
39
+ ### πŸ” μ—”λ“œν¬μΈνŠΈ
40
+
41
+ βœ… νšŒμ›κ°€μž…
42
+ POST /register
43
+
44
+ μš”μ²­ μ˜ˆμ‹œ
45
+ {
46
+ "email": "user@test.com",
47
+ "password": "1234",
48
+ "name": "tester"
49
+ }
50
+
51
+ 응닡 μ˜ˆμ‹œ
52
+ {
53
+ "accessToken": "xxxxx.yyyyy.zzzzz",
54
+ "refreshToken": "xxxxx.yyyyy.zzzzz"
55
+ }
56
+
57
+ βœ… 둜그인
58
+ POST /login
59
+
60
+ μš”μ²­ μ˜ˆμ‹œ
61
+ {
62
+ "email": "user@test.com",
63
+ "password": "1234"
64
+ }
65
+
66
+ 응닡 μ˜ˆμ‹œ
67
+ {
68
+ "accessToken": "xxxxx.yyyyy.zzzzz",
69
+ "refreshToken": "xxxxx.yyyyy.zzzzz"
70
+ }
71
+
72
+
73
+
74
+ #### πŸ”’ Private 데이터 쑰회 (인증 ν•„μš”)
75
+ GET /list
76
+ λ˜λŠ” νŠΉμ • ID만 쑰회:
77
+ GET /list/1
78
+ μš”μ²­ 헀더에 JWT 토큰 ν•„μš”:
79
+ Authorization: Bearer <accessToken>
80
+
81
+
82
+ ##### 🌐 Public 데이터 쑰회 (λˆ„κ΅¬λ‚˜ κ°€λŠ₯)
83
+
84
+ GET /public
85
+ λ˜λŠ”:
86
+
87
+ GET /public/1
88
+
89
+ πŸ“¨ 데이터 μΆ”κ°€ (POST)
90
+ POST /list
91
+ POST /public
92
+
93
+ μ˜ˆμ‹œ
94
+ { "id": 3, "title": "New item" }
95
+
96
+ 🧠 CLI μ˜΅μ…˜
97
+
98
+ --db JSON DB 파일 경둜 ./db.json
99
+ --port μ„œλ²„ 포트 번호 4000
100
+ --watch 파일 λ³€κ²½ μ‹œ μžλ™ λ¦¬λ‘œλ“œ false
101
+
102
+ πŸ“„ μ˜ˆμ‹œ μ‹€ν–‰
103
+ db-json-cli --db ./example.json --port 5000
104
+ /register, /login 인증 지원
105
+
106
+ /list, /public μ ‘κ·Ό μ œμ–΄
107
+
108
+ μ‹€μ‹œκ°„ JSON λ³€κ²½ μžλ™ 반영 (watch μ˜΅μ…˜ μ‹œ)
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "db-json-cli",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
+ "license": "MIT",
4
5
  "description": "JSON file based backend server with JWT auth and CLI",
5
6
  "bin": {
6
7
  "db-json-cli": "./bin/db-json-cli.js"
package/src/auth.js CHANGED
@@ -4,22 +4,22 @@ import bcrypt from "bcryptjs";
4
4
  const ACCESS_SECRET = "access-secret";
5
5
  const REFRESH_SECRET = "refresh-secret";
6
6
 
7
- export async function hashPassword(password) {
7
+ export const hashPassword = async (password) => {
8
8
  const salt = await bcrypt.genSalt(10);
9
9
  return bcrypt.hash(password, salt);
10
- }
10
+ };
11
11
 
12
- export async function comparePassword(password, hash) {
12
+ export const comparePassword = async (password, hash) => {
13
13
  return bcrypt.compare(password, hash);
14
- }
14
+ };
15
15
 
16
- export function generateTokens(payload) {
16
+ export const generateTokens = async (payload) => {
17
17
  const accessToken = jwt.sign(payload, ACCESS_SECRET, { expiresIn: "1h" });
18
18
  const refreshToken = jwt.sign(payload, REFRESH_SECRET, { expiresIn: "7d" });
19
19
  return { accessToken, refreshToken };
20
- }
20
+ };
21
21
 
22
- export function authMiddleware(req, res, next) {
22
+ export const authMiddleware = async (req, res, next) => {
23
23
  const auth = req.headers["authorization"];
24
24
  if (!auth) return res.status(401).json({ message: "No token" });
25
25
 
@@ -31,115 +31,4 @@ export function authMiddleware(req, res, next) {
31
31
  } catch (e) {
32
32
  return res.status(401).json({ message: "Invalid token" });
33
33
  }
34
- }
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
34
+ };
package/src/server.js CHANGED
@@ -4,19 +4,18 @@ import bodyParser from "body-parser";
4
4
  import cors from "cors";
5
5
  import { hashPassword, comparePassword, generateTokens, authMiddleware } from "./auth.js";
6
6
 
7
- export async function startServer(dbPath, port = 4000) {
7
+ export const startServer = async (dbPath, port = 4000) => {
8
8
  const app = express();
9
9
  app.use(cors());
10
10
  app.use(bodyParser.json());
11
11
 
12
- // 초기 JSON 읽기
13
- let db = { users: [], list: [] };
12
+ // JSON Load
13
+ let db = { users: [], rules: {} };
14
14
  if (fs.existsSync(dbPath)) db = await fs.readJson(dbPath);
15
15
 
16
- // JSON μ €μž₯ ν•¨μˆ˜
17
16
  const saveDB = async () => fs.writeJson(dbPath, db, { spaces: 2 });
18
17
 
19
- // ================== REGISTER ==================
18
+ // πŸ”Ή REGISTER
20
19
  app.post("/register", async (req, res) => {
21
20
  const { email, password, name } = req.body;
22
21
  if (!email || !password) return res.status(400).json({ message: "Email/password required" });
@@ -34,7 +33,7 @@ export async function startServer(dbPath, port = 4000) {
34
33
  res.json(tokens);
35
34
  });
36
35
 
37
- // ================== LOGIN ==================
36
+ // πŸ”Ή LOGIN
38
37
  app.post("/login", async (req, res) => {
39
38
  const { email, password } = req.body;
40
39
  const user = db.users.find((u) => u.email === email);
@@ -47,10 +46,53 @@ export async function startServer(dbPath, port = 4000) {
47
46
  res.json(tokens);
48
47
  });
49
48
 
50
- // ================== GET LIST (Protected) ==================
51
- app.get("/list", authMiddleware, (req, res) => {
52
- res.json(db.list);
49
+ // πŸ”Ή AUTO ROUTES
50
+ Object.keys(db).forEach((key) => {
51
+ if (["users", "rules"].includes(key)) return;
52
+ const isPrivate = db.rules?.[key] === "private";
53
+
54
+ const route = express.Router();
55
+
56
+ // βœ… GET /key
57
+ route.get("/", async (req, res) => {
58
+ const { from, to } = req.query;
59
+ let data = db[key];
60
+
61
+ if (from && to) {
62
+ const fromNum = Number(from);
63
+ const toNum = Number(to);
64
+ data = data.filter((item) => item.id >= fromNum && item.id <= toNum);
65
+ }
66
+
67
+ res.json(data);
68
+ });
69
+
70
+ // βœ… GET /key/:id
71
+ route.get("/:id", async (req, res) => {
72
+ const id = Number(req.params.id);
73
+ const item = db[key].find((i) => i.id === id);
74
+ if (!item) return res.status(404).json({ message: "Not found" });
75
+ res.json(item);
76
+ });
77
+
78
+ // βœ… POST /key
79
+ route.post("/", async (req, res) => {
80
+ const newItem = req.body;
81
+ if (!newItem || typeof newItem !== "object") return res.status(400).json({ message: "Invalid body" });
82
+
83
+ const id = db[key].length ? Math.max(...db[key].map((i) => i.id)) + 1 : 1;
84
+ const item = { id, ...newItem };
85
+ db[key].push(item);
86
+ await saveDB();
87
+ res.json(item);
88
+ });
89
+
90
+ if (isPrivate) {
91
+ app.use(`/${key}`, authMiddleware, route);
92
+ } else {
93
+ app.use(`/${key}`, route);
94
+ }
53
95
  });
54
96
 
55
- app.listen(port, () => console.log(`db-json-cli server running on port ${port}`));
56
- }
97
+ app.listen(port, () => console.log(`βœ… db-json-cli running on http://localhost:${port}`));
98
+ };