diff --git a/.gitignore b/.gitignore index 4de337b..7497efc 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,5 @@ uploads/* .idea -data/persons.json* \ No newline at end of file +data/persons.json* +persons.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..10519eb --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}\\index.js" + } + ] +} \ No newline at end of file diff --git a/modules/postgres.js b/modules/postgres.js index 25885e8..89b517c 100644 --- a/modules/postgres.js +++ b/modules/postgres.js @@ -1,103 +1,127 @@ -var pg = require('pg'); +var pg = require("pg"); +var path = require("path"); +var fs = require("fs"); var connectionString = process.env.PRODUCTION - ? process.env.DATABASE_URL - : 'postgres://postgres:12qw34er56ty@localhost:5432/postgres'; + ? process.env.DATABASE_URL + : "postgres://postgres:12qw34er56ty@localhost:5432/postgres"; function createClient() { - return new pg.Client({ connectionString, ssl: { rejectUnauthorized: false } }); + return new pg.Client({ + connectionString, + ssl: { rejectUnauthorized: false }, + }); +} + +function readPersonsFile() { + var content; + try { + content = fs.readFileSync(path.resolve(__dirname, "..", "persons.json"), { + encoding: "utf8", + }); + } catch (e) {} + return content ? JSON.parse(content) : null; } function createTableIf(cb) { - var client = createClient(); - client.connect(); - client.query('SELECT * FROM files', function (err, result) { - if (err) { + var client = createClient(); + client.connect(); + client.query("SELECT * FROM files", function (err, result) { + if (err) { + client.query( + "CREATE TABLE files(id SERIAL PRIMARY KEY, body VARCHAR(10000000))", + function (err, result) { + if (err) { + client.end(); + cb(err); + } else { client.query( - 'CREATE TABLE files(id SERIAL PRIMARY KEY, body VARCHAR(10000000))', - function (err, result) { - if (err) { - client.end(); - cb(err); - } else { - client.query( - 'INSERT INTO files(body) VALUES($1)', - ['{}'], - function (err, result) { - if (err) { - client.end(); - cb(err); - } else { - cb(); - } - } - ); - } + "INSERT INTO files(body) VALUES($1)", + ["{}"], + function (err, result) { + if (err) { + client.end(); + cb(err); + } else { + cb(); } + } ); - } else { - client.end(); - cb(); + } } - }); + ); + } else { + client.end(); + cb(); + } + }); } function select(sql, pars, cb) { - var client = createClient(); - var fn = function (err, result) { - if (err) { - cb(err); - } else { - client.end(); - cb(null, result); - } - }; - client.connect(function (err) { - if (err) { - cb(err); - } - client.query(sql, pars ? pars : fn, pars ? fn : undefined); - }); + var client = createClient(); + var fn = function (err, result) { + if (err) { + cb(err); + } else { + client.end(); + cb(null, result); + } + }; + client.connect(function (err) { + if (err) { + cb(err); + } + client.query(sql, pars ? pars : fn, pars ? fn : undefined); + }); } function wrap(cb) { - createTableIf(function (err) { - if (err) { - cb(err); - } else { - cb(); - } - }); + createTableIf(function (err) { + if (err) { + cb(err); + } else { + cb(); + } + }); } module.exports = { - getPersonsJson: function (cb) { - wrap(function (err) { - if (err) { - cb(err); - } else { - select('SELECT * FROM files', null, function (err, result) { - if (err) { - cb(err); - } else { - cb(null, JSON.parse(result.rows[0].body)); - } - }); - } + getPersonsJson: function (cb) { + var personsFromFile = readPersonsFile(); + if (personsFromFile) { + cb(null, personsFromFile); + return; + } + wrap(function (err) { + if (err) { + cb(err); + } else { + select("SELECT * FROM files", null, function (err, result) { + if (err) { + cb(err); + } else { + cb(null, JSON.parse(result.rows[0].body)); + } }); - }, - setPersons: function (strPersons, cb) { - wrap(function (err) { + } + }); + }, + setPersons: function (strPersons, cb) { + wrap(function (err) { + if (err) { + cb(err); + } else { + select( + "UPDATE files SET body = ($1)", + [strPersons], + function (err, result) { if (err) { - cb(err); + cb(err); } else { - select('UPDATE files SET body = ($1)', [strPersons], function (err, result) { - if (err) { - cb(err); - } else { - cb(); - } - }); + cb(); } - }); - }, + } + ); + } + }); + }, }; diff --git a/package-lock.json b/package-lock.json index 4ce3df3..e30ceb9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -28,7 +28,7 @@ "winston": "1.0.x" }, "engines": { - "node": "4.2.x" + "node": "14.15.x" } }, "node_modules/@types/node": { @@ -819,8 +819,7 @@ "esprima": "^4.0.1", "estraverse": "^4.2.0", "esutils": "^2.0.2", - "optionator": "^0.8.1", - "source-map": "~0.6.1" + "optionator": "^0.8.1" }, "bin": { "escodegen": "bin/escodegen.js", @@ -1296,7 +1295,6 @@ "dom-serializer": "~0.1.0", "entities": "~1.1.1", "htmlparser2": "~3.8.1", - "jsdom": "^7.0.2", "lodash": "^4.1.0" }, "engines": { @@ -1528,9 +1526,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", - "dependencies": { - "graceful-fs": "^4.1.6" - }, "optionalDependencies": { "graceful-fs": "^4.1.6" } @@ -1569,16 +1564,6 @@ "version": "2.7.3", "resolved": "https://registry.npmjs.org/less/-/less-2.7.3.tgz", "integrity": "sha512-KPdIJKWcEAb02TuJtaLrhue0krtRLoRoo7x6BNJIBelO00t/CCdJQUnHW5V34OnHMWzIktSalJxRO+FvytQlCQ==", - "dependencies": { - "errno": "^0.1.1", - "graceful-fs": "^4.1.2", - "image-size": "~0.5.0", - "mime": "^1.2.11", - "mkdirp": "^0.5.0", - "promise": "^7.1.1", - "request": "2.81.0", - "source-map": "^0.5.3" - }, "bin": { "lessc": "bin/lessc" }, @@ -2491,20 +2476,13 @@ "node >= 0.8.0" ], "dependencies": { - "aws-sign2": "~0.5.0", "bl": "~0.9.0", "caseless": "~0.6.0", "forever-agent": "~0.5.0", - "form-data": "~0.1.0", - "hawk": "1.1.1", - "http-signature": "~0.10.0", "json-stringify-safe": "~5.0.0", "mime-types": "~1.0.1", "node-uuid": "~1.4.0", - "oauth-sign": "~0.4.0", "qs": "~1.2.0", - "stringstream": "~0.0.4", - "tough-cookie": ">=0.12.0", "tunnel-agent": "~0.4.0" }, "optionalDependencies": {