From 21bc140715beb045ec7bc792137e39f59c990f38 Mon Sep 17 00:00:00 2001 From: Cyril Date: Sat, 19 Nov 2022 22:49:44 +0000 Subject: [PATCH 1/3] add message for persistent storage --- bin/json-graphql-server.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bin/json-graphql-server.js b/bin/json-graphql-server.js index a487812..d05fb75 100755 --- a/bin/json-graphql-server.js +++ b/bin/json-graphql-server.js @@ -21,7 +21,15 @@ process.argv.forEach((arg, index) => { HOST = process.argv[index + 1]; } }); - +app.use(function(req, res, next) { + console.log("before", data); + + res.on("finish", function() { + console.log("after", data); + }); + next(); + }); + app.use(cors()); app.use('/', JsonGraphqlServer(data)); app.listen(PORT, HOST); @@ -32,3 +40,4 @@ process.on('unhandledRejection', (reason, p) => { // eslint-disable-next-line no-console console.log('Unhandled Rejection at: Promise', p, 'reason:', reason); }); + From c2045b8219fa10342d4d235aa10cb71fdac637b4 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 20 Nov 2022 00:47:50 +0000 Subject: [PATCH 2/3] persistent storage --- bin/json-graphql-server.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/bin/json-graphql-server.js b/bin/json-graphql-server.js index d05fb75..573f9f3 100755 --- a/bin/json-graphql-server.js +++ b/bin/json-graphql-server.js @@ -4,14 +4,20 @@ var path = require('path'); var express = require('express'); var cors = require('cors'); var JsonGraphqlServer = require('../lib/json-graphql-server.node.min').default; +var fs = require('fs'); var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json'; var data = require(path.join(process.cwd(), dataFilePath)); var PORT = process.env.NODE_PORT || 3000; var HOST = process.env.NODE_HOST || 'localhost'; +var PERSISTENT = process.env.NODE_PERSISTENT || false; + var app = express(); process.argv.forEach((arg, index) => { + console.log("arg", arg); + console.log("argv",process.argv[index + 1]); + console.log("index",index); // allow a custom port via CLI if (arg === '--p' && process.argv.length > index + 1) { PORT = process.argv[index + 1]; @@ -20,19 +26,39 @@ process.argv.forEach((arg, index) => { if (arg === '--h' && process.argv.length > index + 1) { HOST = process.argv[index + 1]; } + + if (arg === '--persistent') { + PERSISTENT = true; + } + }); app.use(function(req, res, next) { - console.log("before", data); - - res.on("finish", function() { - console.log("after", data); - }); + console.log("before", data); + res.on("finish", function() { + console.log("after", data); + if (PERSISTENT == true){ + var json = JSON.stringify(data); + console.log("content file will be :", json); + fs.writeFile(dataFilePath, json, 'utf8', + function(err) { + if (err) { + console.error(err); + return; + }; + console.log("File has been updated"); + } + ); + } + }); next(); }); app.use(cors()); app.use('/', JsonGraphqlServer(data)); app.listen(PORT, HOST); +if (PERSISTENT==true){ + console.log("Your data will be saved in $dataFilePath") +} var msg = `GraphQL server running with your data at http://${HOST}:${PORT}/`; console.log(msg); // eslint-disable-line no-console From 5731f3b8168c916af999a128f6d558cbfa616c60 Mon Sep 17 00:00:00 2001 From: Cyril Date: Mon, 21 Nov 2022 21:08:41 +0000 Subject: [PATCH 3/3] add persitent storage --- bin/json-graphql-server.js | 74 +++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/bin/json-graphql-server.js b/bin/json-graphql-server.js index 573f9f3..3543beb 100755 --- a/bin/json-graphql-server.js +++ b/bin/json-graphql-server.js @@ -5,19 +5,20 @@ var express = require('express'); var cors = require('cors'); var JsonGraphqlServer = require('../lib/json-graphql-server.node.min').default; var fs = require('fs'); - +var path = require('path'); + var dataFilePath = process.argv.length > 2 ? process.argv[2] : './data.json'; var data = require(path.join(process.cwd(), dataFilePath)); var PORT = process.env.NODE_PORT || 3000; var HOST = process.env.NODE_HOST || 'localhost'; var PERSISTENT = process.env.NODE_PERSISTENT || false; +var PERSITENT_DIR = process.env.NODE_PERSITENT_DIR || "."; +var KEEP_CHANGE_HISTORY = process.env.NODE_KEEP_CHANGE_HISTORY || false; +var VERBOSE = process.env.NODE_VERBOSE || false; var app = express(); process.argv.forEach((arg, index) => { - console.log("arg", arg); - console.log("argv",process.argv[index + 1]); - console.log("index",index); // allow a custom port via CLI if (arg === '--p' && process.argv.length > index + 1) { PORT = process.argv[index + 1]; @@ -26,30 +27,66 @@ process.argv.forEach((arg, index) => { if (arg === '--h' && process.argv.length > index + 1) { HOST = process.argv[index + 1]; } - + if (arg === '--persistent-dir' && process.argv.length > index + 1) { + PERSITENT_DIR = process.argv[index + 1]; + } if (arg === '--persistent') { PERSISTENT = true; } + if (arg === '--keep-change-history') { + KEEP_CHANGE_HISTORY = true; + } + if (arg === '--verbose') { + VERBOSE = true; + } }); app.use(function(req, res, next) { - console.log("before", data); - res.on("finish", function() { - console.log("after", data); - if (PERSISTENT == true){ - var json = JSON.stringify(data); - console.log("content file will be :", json); - fs.writeFile(dataFilePath, json, 'utf8', - function(err) { + if (VERBOSE == true){ + console.log("before", data); + } + fs.readdir(".", (err, files) => { + files.forEach(file => { + console.log(file); + }); + }); + res.on("finish", function() { + if (VERBOSE == true){ + console.log("after", data); + } + if (PERSISTENT == true){ + var json = JSON.stringify(data); + if (VERBOSE == true){ + console.log("File content will be :", json); + } + + if(KEEP_CHANGE_HISTORY == true){ + var destFile = dataFilePath.replace(/^.*[\\\/]/, '') + var timestamp = Date.now() + var destFilename = destFile.concat("-",timestamp) + var backupDirFilename = path.join(PERSITENT_DIR, destFilename); + console.log(backupDirFilename) + fs.copyFile(dataFilePath, backupDirFilename, (err) => { + if (err) { + console.error(err); + return; + }; + console.log('File was copied to ' + backupDirFilename ); + + }); + } + + fs.writeFile(dataFilePath, json, 'utf8', + function(err) { if (err) { console.error(err); return; }; - console.log("File has been updated"); + console.log("File " + dataFilePath + " has been updated"); } - ); + ); } - }); + }); next(); }); @@ -57,7 +94,10 @@ app.use(cors()); app.use('/', JsonGraphqlServer(data)); app.listen(PORT, HOST); if (PERSISTENT==true){ - console.log("Your data will be saved in $dataFilePath") + console.log("Your data will be saved in " + dataFilePath) +} +if (KEEP_CHANGE_HISTORY==true){ + console.log("Your data changes will be saved in " + PERSITENT_DIR) } var msg = `GraphQL server running with your data at http://${HOST}:${PORT}/`; console.log(msg); // eslint-disable-line no-console