create-jerry 1.0.0 โ 2.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/README.md +225 -55
- package/Templates/base/js/app.js +19 -0
- package/Templates/base/js/config/index.js +18 -0
- package/Templates/base/js/middlewares/error.middleware.js +8 -0
- package/Templates/base/js/routes/index.js +12 -0
- package/Templates/base/js/server.js +21 -0
- package/Templates/base/js/utils/asyncHandler.js +5 -0
- package/Templates/base/ts/app.ts +19 -0
- package/Templates/base/ts/config/index.ts +18 -0
- package/Templates/base/ts/middlewares/error.middleware.ts +15 -0
- package/Templates/base/ts/routes/index.ts +12 -0
- package/Templates/base/ts/server.ts +21 -0
- package/Templates/base/ts/tsconfig.json +18 -0
- package/Templates/base/ts/utils/asyncHandler.ts +5 -0
- package/Templates/db/mongodb/js/db.js +39 -0
- package/Templates/db/mongodb/ts/db.ts +50 -0
- package/Templates/db/mysql/js/db.js +58 -0
- package/Templates/db/mysql/ts/db.ts +72 -0
- package/Templates/modules/ai/js/ai.controller.js +30 -0
- package/Templates/modules/ai/js/ai.provider.js +32 -0
- package/Templates/modules/ai/js/ai.routes.js +8 -0
- package/Templates/modules/ai/js/ai.service.js +16 -0
- package/Templates/modules/ai/ts/ai.controller.ts +31 -0
- package/Templates/modules/ai/ts/ai.provider.ts +30 -0
- package/Templates/modules/ai/ts/ai.routes.ts +8 -0
- package/Templates/modules/ai/ts/ai.service.ts +16 -0
- package/Templates/modules/user/js/user.controller.js +46 -0
- package/Templates/modules/user/js/user.provider.js +25 -0
- package/Templates/modules/user/js/user.routes.js +12 -0
- package/Templates/modules/user/js/user.service.js +21 -0
- package/Templates/modules/user/ts/user.controller.ts +27 -0
- package/Templates/modules/user/ts/user.provider.ts +25 -0
- package/Templates/modules/user/ts/user.routes.ts +12 -0
- package/Templates/modules/user/ts/user.service.ts +21 -0
- package/fileCreator.js +140 -31
- package/folderCreator.js +15 -0
- package/index.js +85 -76
- package/installer.js +11 -13
- package/package.json +5 -3
- package/packageJsonCreator.js +45 -72
- package/plugins/ai.plugin.js +25 -0
- package/plugins/mongodb.plugin.js +15 -0
- package/plugins/mysql.plugin.js +15 -0
- package/plugins/pluginManager.js +18 -0
- package/Templates/db-mongo.js +0 -20
- package/Templates/db-sql.js +0 -35
- package/Templates/express.js +0 -35
- package/folderCreater.js +0 -34
package/README.md
CHANGED
|
@@ -1,81 +1,251 @@
|
|
|
1
|
-
#
|
|
1
|
+
# ๐ create-jerry (JerrIt CLI)
|
|
2
|
+
|
|
3
|
+
> A lightning-fast CLI tool to scaffold production-ready Express backend projects with MongoDB or MySQL, plugin-based architecture, and optional AI module support.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/create-jerry)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## โจ Overview
|
|
11
|
+
|
|
12
|
+
**create-jerry** is a backend scaffolding CLI inspired by tools like Vite and Create React App.
|
|
13
|
+
|
|
14
|
+
It generates a structured Express.js backend with:
|
|
15
|
+
|
|
16
|
+
- MongoDB OR MySQL support
|
|
17
|
+
- JavaScript OR TypeScript support
|
|
18
|
+
- Optional AI module (OpenAI integration)
|
|
19
|
+
- Plugin-based architecture (extensible design)
|
|
20
|
+
- Clean modular folder structure
|
|
21
|
+
|
|
2
22
|
---
|
|
3
23
|
|
|
4
|
-
##
|
|
5
|
-
|
|
24
|
+
## โก Features
|
|
25
|
+
|
|
26
|
+
- ๐ฏ Interactive CLI prompts (Inquirer-based)
|
|
27
|
+
- ๐ง Optional AI module (OpenAI integration)
|
|
28
|
+
- ๐๏ธ Database selection (MongoDB / MySQL)
|
|
29
|
+
- ๐งพ JavaScript & TypeScript support
|
|
30
|
+
- ๐ Plugin-based architecture system
|
|
31
|
+
- ๐ Production-ready folder structure
|
|
32
|
+
- โ๏ธ Auto dependency injection in package.json
|
|
33
|
+
- ๐ Fully working Express server setup
|
|
6
34
|
|
|
7
35
|
---
|
|
8
36
|
|
|
37
|
+
## ๐ Quick Start
|
|
9
38
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
XeoBuilds/
|
|
13
|
-
|_ Templates/
|
|
14
|
-
| |_ db-mongo.js
|
|
15
|
-
| |_ db-sql.js
|
|
16
|
-
| |_ express.js
|
|
17
|
-
|
|
|
18
|
-
|_ .gitignore
|
|
19
|
-
|_ fileCreator.js
|
|
20
|
-
|_ folderCreator.js
|
|
21
|
-
|_ installer.js
|
|
22
|
-
|_ index.js
|
|
23
|
-
|_ package.json
|
|
24
|
-
|_ package-lock.json
|
|
25
|
-
|_ README.md
|
|
39
|
+
```bash
|
|
40
|
+
npx create-jerry
|
|
26
41
|
```
|
|
42
|
+
|
|
43
|
+
Then follow prompts:
|
|
44
|
+
|
|
45
|
+
1. Project name
|
|
46
|
+
2. Database (MongoDB / MySQL)
|
|
47
|
+
3. Language (JavaScript / TypeScript)
|
|
48
|
+
4. AI module (Yes / No)
|
|
49
|
+
|
|
27
50
|
---
|
|
28
51
|
|
|
52
|
+
## ๐ฆ What Gets Generated
|
|
53
|
+
|
|
54
|
+
### Project Structure
|
|
55
|
+
|
|
56
|
+
```
|
|
57
|
+
your-project/
|
|
58
|
+
โโโ src/
|
|
59
|
+
โ โโโ config/
|
|
60
|
+
โ โ โโโ db.js / db.ts
|
|
61
|
+
โ โ โโโ index.js
|
|
62
|
+
โ โ
|
|
63
|
+
โ โโโ modules/
|
|
64
|
+
โ โ โโโ user/
|
|
65
|
+
โ โ โโโ ai/ (optional)
|
|
66
|
+
โ โ
|
|
67
|
+
โ โโโ routes/
|
|
68
|
+
โ โ โโโ index.js
|
|
69
|
+
โ โ
|
|
70
|
+
โ โโโ middlewares/
|
|
71
|
+
โ โ โโโ error.middleware.js
|
|
72
|
+
โ โ
|
|
73
|
+
โ โโโ utils/
|
|
74
|
+
โ โ โโโ asyncHandler.js
|
|
75
|
+
โ โ
|
|
76
|
+
โ โโโ app.js
|
|
77
|
+
โ โโโ server.js
|
|
78
|
+
โ
|
|
79
|
+
โโโ .env
|
|
80
|
+
โโโ package.json
|
|
81
|
+
โโโ tsconfig.json (if TypeScript)
|
|
82
|
+
```
|
|
29
83
|
|
|
30
|
-
## Libraries used to build:
|
|
31
|
-
- inquirer
|
|
32
|
-
- chalk
|
|
33
|
-
- chalkAnimation
|
|
34
|
-
- execSync
|
|
35
|
-
- nanospinner
|
|
36
|
-
- fs module
|
|
37
|
-
<!-- fs/promises module -->
|
|
38
84
|
---
|
|
39
85
|
|
|
86
|
+
## ๐ง Architecture
|
|
87
|
+
|
|
88
|
+
This CLI uses a **plugin-based generator system**:
|
|
89
|
+
|
|
90
|
+
### Supported Plugins
|
|
91
|
+
|
|
92
|
+
- ๐ข MongoDB Plugin
|
|
93
|
+
- ๐ก MySQL Plugin
|
|
94
|
+
- ๐ค AI Plugin (OpenAI)
|
|
95
|
+
|
|
96
|
+
### Each Plugin Handles:
|
|
97
|
+
|
|
98
|
+
- Module generation (if required)
|
|
99
|
+
- Dependency injection
|
|
100
|
+
- Route injection
|
|
101
|
+
- Setup logic execution
|
|
40
102
|
|
|
41
|
-
## Packages/Libraries for Backend:
|
|
42
|
-
- express
|
|
43
|
-
- cors
|
|
44
|
-
- mongoose
|
|
45
|
-
- mysql2
|
|
46
|
-
- dotenv
|
|
47
|
-
- router
|
|
48
103
|
---
|
|
49
104
|
|
|
105
|
+
## ๐ Example CLI Flow
|
|
50
106
|
|
|
51
|
-
|
|
107
|
+
```bash
|
|
108
|
+
โ Project name? my-app
|
|
109
|
+
โ Select Database MongoDB
|
|
110
|
+
โ Select Language Typescript
|
|
111
|
+
โ Add AI module? Yes
|
|
52
112
|
```
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
113
|
+
|
|
114
|
+
### Output:
|
|
115
|
+
|
|
116
|
+
- MongoDB configured project
|
|
117
|
+
- AI module added (optional)
|
|
118
|
+
- Routes auto-injected
|
|
119
|
+
- Dependencies installed dynamically
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
## ๐งฐ Available Scripts (Generated Project)
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
npm run dev # Development mode
|
|
127
|
+
npm start # Production mode
|
|
128
|
+
npm run build # TypeScript build (if TS)
|
|
68
129
|
```
|
|
130
|
+
|
|
69
131
|
---
|
|
70
132
|
|
|
133
|
+
## ๐ง AI Module (Optional)
|
|
134
|
+
|
|
135
|
+
When enabled:
|
|
136
|
+
|
|
137
|
+
- `/ai/chat` endpoint is created
|
|
138
|
+
- OpenAI SDK installed
|
|
139
|
+
- Controller โ Service โ Provider architecture
|
|
140
|
+
- GPT-powered backend endpoint ready to use
|
|
71
141
|
|
|
72
|
-
## Demo Video:
|
|
73
|
-
Video Link: https://drive.google.com/file/d/1o_9_5RMI21Inew4knCqXlCmeArhAG3xD/view?usp=sharing
|
|
74
|
-
<!-- ![]() -->
|
|
75
142
|
---
|
|
76
143
|
|
|
144
|
+
## ๐๏ธ Database Support
|
|
77
145
|
|
|
146
|
+
### MongoDB
|
|
78
147
|
|
|
79
|
-
|
|
80
|
-
|
|
148
|
+
- Mongoose integration
|
|
149
|
+
- Predefined User model + CRUD
|
|
150
|
+
- Auto connection setup
|
|
151
|
+
|
|
152
|
+
### MySQL
|
|
153
|
+
|
|
154
|
+
- mysql2 connection pool
|
|
155
|
+
- Prebuilt SQL queries
|
|
156
|
+
- Ready-to-use CRUD layer
|
|
157
|
+
|
|
158
|
+
---
|
|
159
|
+
|
|
160
|
+
## ๐ Plugin System
|
|
161
|
+
|
|
162
|
+
Plugins follow this structure:
|
|
163
|
+
|
|
164
|
+
```js
|
|
165
|
+
{
|
|
166
|
+
name: "plugin-name",
|
|
167
|
+
setup(),
|
|
168
|
+
inject,
|
|
169
|
+
dependencies
|
|
170
|
+
}
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
### Benefits:
|
|
174
|
+
|
|
175
|
+
- Extensible architecture
|
|
176
|
+
- Clean separation of concerns
|
|
177
|
+
- Easy future expansion (auth, redis, payments, etc.)
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## ๐ Tech Stack
|
|
182
|
+
|
|
183
|
+
- Node.js
|
|
184
|
+
- Express.js
|
|
185
|
+
- Inquirer.js
|
|
186
|
+
- Chalk
|
|
187
|
+
- Nanospinner
|
|
188
|
+
- OpenAI SDK (optional)
|
|
189
|
+
- Mongoose / MySQL2
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## ๐ฏ Why create-jerry?
|
|
81
194
|
|
|
195
|
+
Because backend setup is repetitive:
|
|
196
|
+
|
|
197
|
+
- Folder structure setup โ
|
|
198
|
+
- Database configuration โ
|
|
199
|
+
- Boilerplate code โ
|
|
200
|
+
|
|
201
|
+
Now:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
npx create-jerry
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Done in seconds โก
|
|
208
|
+
|
|
209
|
+
---
|
|
210
|
+
|
|
211
|
+
## ๐ ๏ธ Future Plans
|
|
212
|
+
- ๐ฆ Typescript Migration
|
|
213
|
+
- ๐ Authentication plugin (JWT / OAuth)
|
|
214
|
+
- โก Redis caching plugin
|
|
215
|
+
- ๐ฆ Docker support
|
|
216
|
+
- ๐งช Testing setup (Jest)
|
|
217
|
+
- โ๏ธ Deployment presets
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
## ๐จโ๐ป Author
|
|
222
|
+
|
|
223
|
+
**Tanmay Khanna**
|
|
224
|
+
|
|
225
|
+
- GitHub: [@TomLucasakaTGeek](https://github.com/TomLucasakaTGeek)
|
|
226
|
+
- Project: JerrIt CLI
|
|
227
|
+
|
|
228
|
+
---
|
|
229
|
+
|
|
230
|
+
## โญ Support
|
|
231
|
+
|
|
232
|
+
If this helped you:
|
|
233
|
+
|
|
234
|
+
- โญ Star the repo
|
|
235
|
+
- ๐ Share with developers
|
|
236
|
+
- ๐งฉ Contribute plugins
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## ๐ License
|
|
241
|
+
|
|
242
|
+
MIT License
|
|
243
|
+
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
**Built with โค๏ธ to make backend development faster and easier**
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
<!-- ## Low Level Daigram ## Challenges -->
|
|
251
|
+
<!-- ======= -->
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import cors from 'cors';
|
|
3
|
+
import routes from './routes/index.js';
|
|
4
|
+
import { errorHandler } from './middlewares/error.middleware.js';
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(cors());
|
|
9
|
+
app.use(express.json());
|
|
10
|
+
|
|
11
|
+
app.use('/api', routes);
|
|
12
|
+
|
|
13
|
+
app.get('/health', (_, res) => {
|
|
14
|
+
res.json({ status: 'OK' });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.use(errorHandler);
|
|
18
|
+
|
|
19
|
+
export default app;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
|
|
3
|
+
dotenv.config();
|
|
4
|
+
|
|
5
|
+
export const config = {
|
|
6
|
+
port: process.env.PORT || 3000,
|
|
7
|
+
|
|
8
|
+
// MongoDB
|
|
9
|
+
mongoUri: process.env.MONGO_URI,
|
|
10
|
+
|
|
11
|
+
// MySQL
|
|
12
|
+
mysql: {
|
|
13
|
+
host: process.env.MYSQL_HOST,
|
|
14
|
+
user: process.env.MYSQL_USER,
|
|
15
|
+
password: process.env.MYSQL_PASSWORD,
|
|
16
|
+
database: process.env.MYSQL_DATABASE
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import app from './app.js';
|
|
2
|
+
import { config } from './config/index.js';
|
|
3
|
+
import { connectDB } from './config/db.js';
|
|
4
|
+
|
|
5
|
+
const start = async () => {
|
|
6
|
+
try {
|
|
7
|
+
if (connectDB) {
|
|
8
|
+
await connectDB();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
app.listen(config.port, () => {
|
|
12
|
+
console.log(`๐ Server running on port ${config.port}`);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error('โ Failed to start server:', err);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
start();
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import express from 'express';
|
|
2
|
+
import cors from 'cors';
|
|
3
|
+
import routes from './routes/index.js';
|
|
4
|
+
import { errorHandler } from './middlewares/error.middleware.js';
|
|
5
|
+
|
|
6
|
+
const app = express();
|
|
7
|
+
|
|
8
|
+
app.use(cors());
|
|
9
|
+
app.use(express.json());
|
|
10
|
+
|
|
11
|
+
app.use('/api', routes);
|
|
12
|
+
|
|
13
|
+
app.get('/health', (_, res) => {
|
|
14
|
+
res.json({ status: 'OK' });
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
app.use(errorHandler);
|
|
18
|
+
|
|
19
|
+
export default app;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import dotenv from 'dotenv';
|
|
2
|
+
|
|
3
|
+
dotenv.config();
|
|
4
|
+
|
|
5
|
+
export const config = {
|
|
6
|
+
port: process.env.PORT || 3000,
|
|
7
|
+
|
|
8
|
+
// MongoDB
|
|
9
|
+
mongoUri: process.env.MONGO_URI,
|
|
10
|
+
|
|
11
|
+
// MySQL
|
|
12
|
+
mysql: {
|
|
13
|
+
host: process.env.MYSQL_HOST,
|
|
14
|
+
user: process.env.MYSQL_USER,
|
|
15
|
+
password: process.env.MYSQL_PASSWORD,
|
|
16
|
+
database: process.env.MYSQL_DATABASE
|
|
17
|
+
}
|
|
18
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from 'express';
|
|
2
|
+
|
|
3
|
+
export const errorHandler = (
|
|
4
|
+
err: any,
|
|
5
|
+
_req: Request,
|
|
6
|
+
res: Response,
|
|
7
|
+
_next: NextFunction
|
|
8
|
+
) => {
|
|
9
|
+
console.error(err);
|
|
10
|
+
|
|
11
|
+
res.status(err.status || 500).json({
|
|
12
|
+
success: false,
|
|
13
|
+
message: err.message || 'Internal Server Error',
|
|
14
|
+
});
|
|
15
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import app from './app.js';
|
|
2
|
+
import { config } from './config/index.js';
|
|
3
|
+
import { connectDB } from './config/db.js';
|
|
4
|
+
|
|
5
|
+
const start = async () => {
|
|
6
|
+
try {
|
|
7
|
+
if (connectDB) {
|
|
8
|
+
await connectDB();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
app.listen(config.port, () => {
|
|
12
|
+
console.log(`๐ Server running on port ${config.port}`);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error('โ Failed to start server:', err);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
start();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "NodeNext",
|
|
5
|
+
"moduleResolution": "NodeNext",
|
|
6
|
+
|
|
7
|
+
"rootDir": "src",
|
|
8
|
+
"outDir": "dist",
|
|
9
|
+
|
|
10
|
+
"strict": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"skipLibCheck": true,
|
|
13
|
+
|
|
14
|
+
"forceConsistentCasingInFileNames": true
|
|
15
|
+
},
|
|
16
|
+
"include": ["src"],
|
|
17
|
+
"exclude": ["node_modules", "dist"]
|
|
18
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
const connectDB = async () => {
|
|
7
|
+
try {
|
|
8
|
+
await mongoose.connect(process.env.MONGO_URI);
|
|
9
|
+
console.log('MongoDB connected');
|
|
10
|
+
} catch (err) {
|
|
11
|
+
console.error(err);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const userSchema = new mongoose.Schema(
|
|
17
|
+
{
|
|
18
|
+
name: String,
|
|
19
|
+
email: String,
|
|
20
|
+
password: String
|
|
21
|
+
},
|
|
22
|
+
{ timestamps: true }
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const User = mongoose.model('User', userSchema);
|
|
26
|
+
|
|
27
|
+
// CRUD
|
|
28
|
+
|
|
29
|
+
export const getAllUsers = async () => User.find();
|
|
30
|
+
|
|
31
|
+
export const getUserById = async (id) => User.findById(id);
|
|
32
|
+
|
|
33
|
+
export const createUser = async (data) => User.create(data);
|
|
34
|
+
|
|
35
|
+
export const updateUser = async (id, data) =>
|
|
36
|
+
User.findByIdAndUpdate(id, data, { new: true });
|
|
37
|
+
|
|
38
|
+
export const deleteUser = async (id) =>
|
|
39
|
+
User.findByIdAndDelete(id);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
const connectDB = async (): Promise<void> => {
|
|
7
|
+
try {
|
|
8
|
+
await mongoose.connect(process.env.MONGO_URI as string);
|
|
9
|
+
console.log('MongoDB connected');
|
|
10
|
+
} catch (err) {
|
|
11
|
+
console.error(err);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
interface IUser {
|
|
17
|
+
name: string;
|
|
18
|
+
email: string;
|
|
19
|
+
password: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const userSchema = new mongoose.Schema<IUser>(
|
|
23
|
+
{
|
|
24
|
+
name: String,
|
|
25
|
+
email: String,
|
|
26
|
+
password: String
|
|
27
|
+
},
|
|
28
|
+
{ timestamps: true }
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const User = mongoose.model<IUser>('User', userSchema);
|
|
32
|
+
|
|
33
|
+
// CRUD
|
|
34
|
+
|
|
35
|
+
export const getAllUsers = async () => User.find();
|
|
36
|
+
|
|
37
|
+
export const getUserById = async (id: string) =>
|
|
38
|
+
User.findById(id);
|
|
39
|
+
|
|
40
|
+
export const createUser = async (data: IUser) =>
|
|
41
|
+
User.create(data);
|
|
42
|
+
|
|
43
|
+
export const updateUser = async (
|
|
44
|
+
id: string,
|
|
45
|
+
data: Partial<IUser>
|
|
46
|
+
) =>
|
|
47
|
+
User.findByIdAndUpdate(id, data, { new: true });
|
|
48
|
+
|
|
49
|
+
export const deleteUser = async (id: string) =>
|
|
50
|
+
User.findByIdAndDelete(id);
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import mysql from 'mysql2/promise';
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
|
|
4
|
+
dotenv.config();
|
|
5
|
+
|
|
6
|
+
const pool = mysql.createPool({
|
|
7
|
+
host: process.env.MYSQL_HOST,
|
|
8
|
+
user: process.env.MYSQL_USER,
|
|
9
|
+
password: process.env.MYSQL_PASSWORD,
|
|
10
|
+
database: process.env.MYSQL_DATABASE
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
export const connectDB = async () => {
|
|
14
|
+
try {
|
|
15
|
+
await pool.getConnection();
|
|
16
|
+
console.log('MySQL connected');
|
|
17
|
+
} catch (err) {
|
|
18
|
+
console.error(err);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// CRUD
|
|
24
|
+
|
|
25
|
+
export const getAllUsers = async () => {
|
|
26
|
+
const [rows] = await pool.query('SELECT * FROM users');
|
|
27
|
+
return rows;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const getUserById = async (id) => {
|
|
31
|
+
const [rows] = await pool.query(
|
|
32
|
+
'SELECT * FROM users WHERE id = ?',
|
|
33
|
+
[id]
|
|
34
|
+
);
|
|
35
|
+
return rows[0];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const createUser = async ({ name, email, password }) => {
|
|
39
|
+
const [result] = await pool.query(
|
|
40
|
+
'INSERT INTO users (name, email, password) VALUES (?, ?, ?)',
|
|
41
|
+
[name, email, password]
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return getUserById(result.insertId);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const updateUser = async (id, data) => {
|
|
48
|
+
await pool.query(
|
|
49
|
+
'UPDATE users SET name=?, email=?, password=? WHERE id=?',
|
|
50
|
+
[data.name, data.email, data.password, id]
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return getUserById(id);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const deleteUser = async (id) => {
|
|
57
|
+
await pool.query('DELETE FROM users WHERE id=?', [id]);
|
|
58
|
+
};
|