removing prisma ORM
This commit is contained in:
parent
e68a054712
commit
4ae6bb45d3
7 changed files with 80 additions and 30 deletions
13
package-lock.json
generated
13
package-lock.json
generated
|
@ -10,6 +10,7 @@
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "^4.15.0",
|
"@prisma/client": "^4.15.0",
|
||||||
|
"bcryptjs": "^2.4.3",
|
||||||
"dotenv": "^16.1.4",
|
"dotenv": "^16.1.4",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"jsonwebtoken": "^9.0.0",
|
"jsonwebtoken": "^9.0.0",
|
||||||
|
@ -17,6 +18,7 @@
|
||||||
"prisma": "^4.15.0"
|
"prisma": "^4.15.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/bcryptjs": "^2.4.2",
|
||||||
"@types/express": "^4.17.17"
|
"@types/express": "^4.17.17"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -51,6 +53,12 @@
|
||||||
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944.tgz",
|
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944.tgz",
|
||||||
"integrity": "sha512-sVOig4tjGxxlYaFcXgE71f/rtFhzyYrfyfNFUsxCIEJyVKU9rdOWIlIwQ2NQ7PntvGnn+x0XuFo4OC1jvPJKzg=="
|
"integrity": "sha512-sVOig4tjGxxlYaFcXgE71f/rtFhzyYrfyfNFUsxCIEJyVKU9rdOWIlIwQ2NQ7PntvGnn+x0XuFo4OC1jvPJKzg=="
|
||||||
},
|
},
|
||||||
|
"node_modules/@types/bcryptjs": {
|
||||||
|
"version": "2.4.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.2.tgz",
|
||||||
|
"integrity": "sha512-LiMQ6EOPob/4yUL66SZzu6Yh77cbzJFYll+ZfaPiPPFswtIlA/Fs1MzdKYA7JApHU49zQTbJGX3PDmCpIdDBRQ==",
|
||||||
|
"dev": true
|
||||||
|
},
|
||||||
"node_modules/@types/body-parser": {
|
"node_modules/@types/body-parser": {
|
||||||
"version": "1.19.2",
|
"version": "1.19.2",
|
||||||
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz",
|
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.2.tgz",
|
||||||
|
@ -168,6 +176,11 @@
|
||||||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||||
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
|
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg=="
|
||||||
},
|
},
|
||||||
|
"node_modules/bcryptjs": {
|
||||||
|
"version": "2.4.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
|
||||||
|
"integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ=="
|
||||||
|
},
|
||||||
"node_modules/body-parser": {
|
"node_modules/body-parser": {
|
||||||
"version": "1.20.1",
|
"version": "1.20.1",
|
||||||
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
|
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz",
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "^4.15.0",
|
"@prisma/client": "^4.15.0",
|
||||||
|
"bcryptjs": "^2.4.3",
|
||||||
"dotenv": "^16.1.4",
|
"dotenv": "^16.1.4",
|
||||||
"express": "^4.18.2",
|
"express": "^4.18.2",
|
||||||
"jsonwebtoken": "^9.0.0",
|
"jsonwebtoken": "^9.0.0",
|
||||||
|
@ -17,6 +18,7 @@
|
||||||
"prisma": "^4.15.0"
|
"prisma": "^4.15.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/bcryptjs": "^2.4.2",
|
||||||
"@types/express": "^4.17.17"
|
"@types/express": "^4.17.17"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
34
src/controllers/UserController.ts
Normal file
34
src/controllers/UserController.ts
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
import { Request, Response } from 'express';
|
||||||
|
import bcrypt from 'bcryptjs';
|
||||||
|
import { User } from '@prisma/client';
|
||||||
|
import { createUser } from '../models/User';
|
||||||
|
|
||||||
|
async function createUserController(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const { firstName, lastName, email, password, address } = req.body;
|
||||||
|
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10);
|
||||||
|
|
||||||
|
const user: User = await createUser({
|
||||||
|
firstName,
|
||||||
|
lastName,
|
||||||
|
email,
|
||||||
|
password: hashedPassword,
|
||||||
|
address,
|
||||||
|
id: '',
|
||||||
|
createdAt: undefined,
|
||||||
|
updatedAt: undefined
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json(hashedPassword);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error creating user:', error);
|
||||||
|
res.status(500).json({ error: 'An error occurred while creating the user.' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const UserController = {
|
||||||
|
createUser: createUserController,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UserController;
|
|
@ -1,11 +1,14 @@
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import router from './routes';
|
import userRoutes from './routes/user';
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
const PORT = 3000;
|
||||||
|
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
app.use(router)
|
|
||||||
|
|
||||||
app.listen(3000, () => {
|
app.use('/users', userRoutes);
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
console.log('Server started on port 3000!');
|
console.log('Server started on port 3000!');
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
17
src/models/User.ts
Normal file
17
src/models/User.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
import { PrismaClient, User } from '@prisma/client';
|
||||||
|
|
||||||
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
|
export async function createUser(user: User): Promise<User> {
|
||||||
|
const createdUser = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
firstName: user.firstName,
|
||||||
|
lastName: user.lastName,
|
||||||
|
email: user.email,
|
||||||
|
password: user.password,
|
||||||
|
address: user.address,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return createdUser;
|
||||||
|
}
|
|
@ -1,27 +0,0 @@
|
||||||
import { User } from '@prisma/client';
|
|
||||||
import Router from 'express';
|
|
||||||
|
|
||||||
const router = Router()
|
|
||||||
|
|
||||||
router.get('/', (req, res) => {
|
|
||||||
res.send('Hello World!')
|
|
||||||
})
|
|
||||||
|
|
||||||
router.post('/create-user', (req, res) => {
|
|
||||||
const { firstName, lastName, email, password, address } = req.body
|
|
||||||
|
|
||||||
const newUser: User = {
|
|
||||||
firstName,
|
|
||||||
lastName,
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
address,
|
|
||||||
id: '',
|
|
||||||
createdAt: undefined,
|
|
||||||
updatedAt: undefined
|
|
||||||
}
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
export default router;
|
|
8
src/routes/user.ts
Normal file
8
src/routes/user.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import express from 'express';
|
||||||
|
import UserController from '../controllers/UserController';
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.post('/', UserController.createUser);
|
||||||
|
|
||||||
|
export default router;
|
Loading…
Reference in a new issue