Hi Mircea,
Greetings from Syncfusion Support…!
We can bind the data to Scheduler from MongoDB and we have prepared a sample with Angular version as 9 for your reference which can be downloaded from the following location.
In the above sample, we have added CRUD actions code snippet in server.js.
var MongoClient = require('mongodb').MongoClient;
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
var url = "mongodb://localhost:27017/";
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }))
app.listen(5000, function () {
console.log('listening on 5000')
})
app.use(express.static(__dirname))
MongoClient.connect(url, function (err, db) {
if (err) throw err;
var dbo = db.db("mydb");
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post("/GetData", (req, res) => {
dbo.collection('ScheduleData').find({}).toArray((err, cus) => {
res.send(cus);
});
});
app.post("/BatchData", (req, res) => {
console.log(req.body);
var eventData = [];
if (req.body.action == "insert" || (req.body.action == "batch" && req.body.added.length > 0)) {
(req.body.action == "insert") ? eventData.push(req.body.value) : eventData = req.body.added;
for (var i = 0; i < eventData.length; i++) {
var sdate = new Date(eventData[i].StartTime);
var edate = new Date(eventData[i].EndTime);
eventData[i].StartTime = sdate;
eventData[i].EndTime = edate;
dbo.collection('ScheduleData').insertOne(eventData[i]);
}
}
if (req.body.action == "update" || (req.body.action == "batch" && req.body.changed.length > 0)) {
(req.body.action == "update") ? eventData.push(req.body.value) : eventData = req.body.changed;
for (var i = 0; i < eventData.length; i++) {
delete eventData[i]._id;
var sdate = new Date(eventData[i].StartTime);
var edate = new Date(eventData[i].EndTime);
eventData[i].StartTime = sdate;
eventData[i].EndTime = edate;
dbo.collection('ScheduleData').updateOne({ "Id": eventData[i].Id }, { $set: eventData[i] });
}
}
if (req.body.action == "remove" || (req.body.action == "batch" && req.body.deleted.length > 0)) {
(req.body.action == "remove") ? eventData.push({ Id: req.body.key }) : eventData = req.body.deleted;
for (var i = 0; i < eventData.length; i++) {
dbo.collection('ScheduleData').deleteOne({ "Id": eventData[i].Id });
}
}
res.send(req.body);
});
});
Note: We have removed the local offset time before inserting and updating the events in the database collection to render the appointments without considering system UTC in the scheduler.
In the below code we have given the GetData and BatchData url path to initial fetching and for performing CRUD actions using UrlAdaptor and assigned it to the scheduler datasource.
private dataManager: DataManager = new DataManager({
url: 'http://localhost:5000/GetData',
crudUrl: 'http://localhost:5000/BatchData',
adaptor: new UrlAdaptor,
crossDomain: true
});
ngOnInit(): void {
this.selectedDate = new Date(2018, 1, 14);
this.eventSettings = { dataSource: this.dataManager };
}
Steps to run the sample:
- Run MongoDB and create the collection named ‘ScheduleData’ in ‘mydb’ database.
- Run the below comments.
npm install
npm run server
npm start
Please try the above sample and let us know if you need further assistance.
Regards,
Hareesh