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 , res ) => { 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 , res ) => { const { projectId = 'default' , actions = [ ] } = req . body ; const projectData = db . get ( projectId ) . value ( ) || [ ] ; actions . forEach ( ( action ) => { const { data , actionType } = action ; if ( actionType === 'create' ) { projectData . push ( data ) ; } else if ( actionType === 'update' ) { const foundIdx = projectData . findIndex ( ( item ) => item . id === data . id ) ; if ( foundIdx > - 1 ) { projectData [ foundIdx ] = data ; } } else if ( actionType === 'remove' ) { const foundIdx = projectData . findIndex ( ( item ) => 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 , } ;