forked from ritza-co/bryntum-grid-lazyload-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
164 lines (134 loc) · 4.59 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import bodyParser from "body-parser";
import express from "express";
import sessions from "express-session";
import { readFileSync } from "fs";
import path from "path";
global.__dirname = path.resolve();
const port = process.env.PORT || 1337;
const app = express();
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static(path.join(__dirname, "/node_modules/@bryntum/grid")));
const fakeDelay = 250;
app.use(bodyParser.json());
app.use(
sessions({
secret: "bryntum",
saveUninitialized: true,
resave: false,
cookie: {
maxAge: 7200000, // 2 hours
sameSite: "strict", // Can be 'strict', 'lax', or 'none'
secure: false, // Set to true in production with HTTPS
httpOnly: true,
},
})
);
// Saving each user session data in a shared variable.
// Much more performant than saving directly in the session.
const sessionData = {},
// Used to filter the in-memory "database" by different "operators"
filterEvaluators = {
"*": (a, b) => filterEvaluators["="](a, b) || a?.toString().includes(b),
"=": (a, b) => a === b,
">": (a, b) => a > b,
"<": (a, b) => a < b,
};
// Use to get data, either saved in the current session or from the data file
const getData = (req) => {
// Demo data is saved with the session
if (!sessionData[req.sessionID]) {
sessionData[req.sessionID] = {};
checkSessions(req);
}
if (!sessionData[req.sessionID].data) {
sessionData[req.sessionID].data = JSON.parse(
readFileSync("./data/data.json")
);
}
return sessionData[req.sessionID].data;
};
// Used to clear the "memory" of old sessions
const checkSessions = (req) => {
for (const [sessionID, session] of Object.entries(sessionData)) {
// Extend life of current session
if (sessionID === req.sessionID) {
session.expires = new Date().getTime() + 1000 * 60 * 60 * 2;
}
// Non-current session has expired, clear the saved data
else if (session.expires < new Date().getTime()) {
sessionData[sessionID] = undefined;
}
}
};
// Use to sort the in-memory "database" on different fields.
// The default sorting is ascending sortIndex
function sortData(req, data, field = "sortIndex", ascending = true) {
data.sort((a, b) =>
a[field] > b[field] ? (ascending ? 1 : -1) : ascending ? -1 : 1
);
sessionData[req.sessionID].dataIsSorted = arguments.length > 1;
}
app.listen(port, () => {
console.log("Server is running on port " + port + "...");
});
async function serverConfig() {
app.get("/read", async (req, res) => {
let data = getData(req);
await new Promise((resolve) => setTimeout(resolve, fakeDelay));
// Return the expected JSON response
res.json({
success: true,
data: data,
});
});
app.post("/create", async (req, res) => {
const data = getData(req),
records = req.body.data; // We get the added records as an array of objects
let maxId = data.reduce((acc, r) => (r.id > acc ? r.id : acc), 0);
// Create unique ids for all added records
records.forEach((r) => (r.id = maxId += 1));
// Add the records to the session "database"
data.push(...records);
sortData(req, data);
await new Promise((resolve) => setTimeout(resolve, fakeDelay));
// Return the expected JSON response
res.json({
success: true,
data: records,
});
});
app.post("/delete", async (req, res) => {
const { ids } = req.body, // We get the ids to delete as an array
data = getData(req);
// Remove records from the session "database"
for (const id of ids) {
data.splice(
data.findIndex((r) => r.id === id),
1
);
}
await new Promise((resolve) => setTimeout(resolve, fakeDelay));
// Return the expected JSON response
res.json({
success: true,
});
});
app.post("/update", async (req, res) => {
const records = req.body.data, // We get the modified records as an array of objects
updatedRecords = [],
data = getData(req);
while (records.length) {
const record = data.find((r) => r.id === records[0].id);
Object.assign(record, records.shift());
// Keep track of the modified records, they need to be a part of the response sent to the client
updatedRecords.push(record);
}
await new Promise((resolve) => setTimeout(resolve, fakeDelay));
// Return the expected JSON response
res.json({
success: true,
data: updatedRecords,
});
});
}
serverConfig();