You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

41 lines
1.3 KiB

const path = require('path');
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync(path.join(__dirname, './db.json'));
const db = low(adapter);
const queryGraph = (req: any, res: any) => {
const { projectId = 'default' } = req.body;
const data = db.get(projectId).value() || [];
res.send({ status: 200, code: 0, success: true, data: { cells: data } });
};
const modifyGraph = (req: any, res: any) => {
const { projectId = 'default', actions = [] } = req.body;
const projectData = db.get(projectId).value() || [];
actions.forEach((action: any) => {
const { data, actionType } = action;
if (actionType === 'create') {
projectData.push(data);
} else if (actionType === 'update') {
const foundIdx = projectData.findIndex((item: any) => item.id === data.id);
if (foundIdx > -1) {
projectData[foundIdx] = data;
}
} else if (actionType === 'remove') {
const foundIdx = projectData.findIndex((item: any) => item.id === data.id);
if (foundIdx > -1) {
projectData.splice(foundIdx, 1);
}
}
});
db.set(projectId, projectData).write();
res.send({ status: 200, code: 0, success: true, data: [] });
};
export default {
'POST /api/queryGraph': queryGraph,
'POST /api/modifyGraph': modifyGraph,
};