Skip to content

Commit 940daf5

Browse files
committed
Initial commit
1 parent 2a830df commit 940daf5

25 files changed

+523
-0
lines changed

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PORT=8081

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
11
# React-Scheduler-CRUD-Application-with-Node-Js-and-PostgreSQL
22
Syncfusion React Scheduler CRUD Application with Node Js and PostgreSQL
3+
4+
## Step by Step Tutorial
5+
[Syncfusion React Rest-api CRUD App Tutorial]()
6+
7+
### PostgreSql Configuration
8+
9+
- Navigate to the `backend/config/db.config.js` file
10+
- In the `db.config.js` file change the USER, PASSWORD, DB as your database configuration.
11+
12+
### Start Backend Server
13+
14+
- Get inside the `backend` folder
15+
- Open terminal run `npm install` to install required packages
16+
- Run `node server.js`
17+
- This will run our backend server.
18+
19+
Open API URL on [http://localhost:8080](http://localhost:8080/) to view it in the browser.
20+
21+
## Start Syncfusion React Scheduler
22+
23+
- In the project directory, Open new terminal run `npm install` to install required packages.
24+
- Run `npm start`
25+
- This will run our frontend application.
26+
27+
Open [http://localhost:8081/](http://localhost:8081/) to view it in the browser.
28+
29+
You can perform CRUD operation on the scheduler.<br>
30+
The CURD action that you are performing will be reflected in the postgreSQL database table.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const db = require("../models");
2+
const SchedulerEvents = db.scheduler;
3+
4+
exports.crudActions = (req, res) => {
5+
6+
if (req.body.added !== null && req.body.added.length > 0) {
7+
for (var i = 0; i < req.body.added.length; i++) {
8+
var insertData = req.body.added[i];
9+
SchedulerEvents.create(insertData)
10+
.then(data => {
11+
res.send(data);
12+
})
13+
.catch(err => {
14+
res.status(500).send({
15+
message:
16+
err.message || "Some error occurred while inserting the events."
17+
});
18+
});
19+
}
20+
}
21+
22+
if (req.body.changed !== null && req.body.changed.length > 0) {
23+
for (var i = 0; i < req.body.changed.length; i++) {
24+
var updateData = req.body.changed[i];
25+
SchedulerEvents.update(updateData, { where: { id: updateData.id } })
26+
.then(num => {
27+
if (num == 1) {
28+
res.send(updateData);
29+
} else {
30+
res.send({
31+
message: `Cannot update Event with id=${id}. Maybe Event was not found or req.body is empty!`
32+
});
33+
}
34+
})
35+
.catch(err => {
36+
res.status(500).send({
37+
message: "Error updating Event with id=" + id
38+
});
39+
});
40+
}
41+
}
42+
43+
if (req.body.deleted !== null && req.body.deleted.length > 0) {
44+
for (var i = 0; i < req.body.deleted.length; i++) {
45+
var deleteData = req.body.deleted[i];
46+
SchedulerEvents.destroy({ where: { id: deleteData.id } })
47+
.then(num => {
48+
if (num == 1) {
49+
res.send(deleteData);
50+
} else {
51+
res.send({
52+
message: `Cannot delete Event with id=${id}. Maybe Event was not found!`
53+
});
54+
}
55+
})
56+
.catch(err => {
57+
res.status(500).send({
58+
message: "Could not delete Event with id=" + id
59+
});
60+
});
61+
}
62+
}
63+
};
64+
65+
exports.getData = (req, res) => {
66+
SchedulerEvents.findAll()
67+
.then(data => {
68+
res.send(data);
69+
})
70+
.catch(err => {
71+
res.status(500).send({
72+
message:
73+
err.message || "Some error occurred while retrieving Events."
74+
});
75+
});
76+
};

backend/app/models/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const dbConfig = require("../../config/db.config.js");
2+
const Sequelize = require("sequelize");
3+
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
4+
host: dbConfig.HOST,
5+
dialect: dbConfig.dialect,
6+
operatorsAliases: false,
7+
pool: {
8+
max: dbConfig.pool.max,
9+
min: dbConfig.pool.min,
10+
acquire: dbConfig.pool.acquire,
11+
idle: dbConfig.pool.idle
12+
}
13+
});
14+
const db = {};
15+
db.Sequelize = Sequelize;
16+
db.sequelize = sequelize;
17+
db.scheduler = require("./scheduler.model.js")(sequelize, Sequelize);
18+
module.exports = db;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module.exports = (sequelize, Sequelize) => {
2+
const SchedulerEvents = sequelize.define("scheduleevents", {
3+
id: {
4+
type: Sequelize.INTEGER,
5+
primaryKey: true,
6+
autoIncrement: true,
7+
},
8+
starttime: {
9+
type: Sequelize.DATE,
10+
allowNull: false
11+
},
12+
endtime: {
13+
type: Sequelize.DATE,
14+
allowNull: false
15+
},
16+
subject: {
17+
type: Sequelize.STRING
18+
},
19+
location: {
20+
type: Sequelize.STRING
21+
},
22+
description: {
23+
type: Sequelize.STRING
24+
},
25+
isallday: {
26+
type: Sequelize.BOOLEAN
27+
},
28+
starttimezone: {
29+
type: Sequelize.STRING
30+
},
31+
endtimezone: {
32+
type: Sequelize.STRING
33+
},
34+
recurrencerule: {
35+
type: Sequelize.STRING
36+
},
37+
recurrenceid: {
38+
type: Sequelize.INTEGER
39+
},
40+
recurrenceexception: {
41+
type: Sequelize.STRING
42+
},
43+
followingid: {
44+
type: Sequelize.INTEGER
45+
},
46+
createdAt: {
47+
type: Sequelize.DATE,
48+
field: 'created_at'
49+
},
50+
51+
updatedAt: {
52+
type: Sequelize.DATE,
53+
field: 'updated_at'
54+
}
55+
});
56+
return SchedulerEvents;
57+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = app => {
2+
const scheduleService = require("../controllers/scheduler.controller.js");
3+
var router = require("express").Router();
4+
router.post("/getData", scheduleService.getData);
5+
router.post("/crudActions", scheduleService.crudActions);
6+
app.use('/api/scheduleevents', router);
7+
};

backend/config/db.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
HOST: "localhost",
3+
USER: "postgres",
4+
PASSWORD: "admin",
5+
DB: "testdb",
6+
dialect: "postgres",
7+
pool: {
8+
max: 5,
9+
min: 0,
10+
acquire: 30000,
11+
idle: 10000
12+
}
13+
};

backend/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "nodejs-express-sequelize-postgresql",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"dependencies": {
7+
"body-parser": "^1.20.0",
8+
"cors": "^2.8.5",
9+
"express": "^4.18.1",
10+
"pg": "^8.7.3",
11+
"sequelize": "^6.19.0",
12+
"pg-hstore": "^2.3.4"
13+
},
14+
"devDependencies": {},
15+
"scripts": {
16+
"test": "echo \"Error: no test specified\" && exit 1",
17+
"start": "node server.js"
18+
},
19+
"author": "",
20+
"license": "ISC"
21+
}

backend/server.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const express = require("express");
2+
const bodyParser = require("body-parser");
3+
const cors = require("cors");
4+
const app = express();
5+
var corsOptions = {
6+
origin: "http://localhost:8081"
7+
};
8+
app.use(cors(corsOptions));
9+
// parse requests of content-type - application/json
10+
app.use(bodyParser.json());
11+
// parse requests of content-type - application/x-www-form-urlencoded
12+
app.use(bodyParser.urlencoded({ extended: true }));
13+
app.get("/", (req, res) => {
14+
res.json({ message: "Welcome to Scheduler backend service." });
15+
});
16+
require("./app/routes/scheduler.routes")(app);
17+
// set port, listen for requests
18+
const PORT = process.env.PORT || 8080;
19+
app.listen(PORT, () => {
20+
console.log(`Server is running on port ${PORT}.`);
21+
});
22+
23+
const db = require("./app/models");
24+
db.sequelize.sync({ force: false }).then(() => {
25+
console.log("Drop and re-sync db.");
26+
});

0 commit comments

Comments
 (0)