Skip to content

Commit

Permalink
Added support of persons.json file in root
Browse files Browse the repository at this point in the history
  • Loading branch information
Голов Никита Викторович authored and Голов Никита Викторович committed Mar 5, 2023
1 parent 9860cf5 commit 72562b2
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 104 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ uploads/*

.idea

data/persons.json*
data/persons.json*
persons.json
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\index.js"
}
]
}
182 changes: 103 additions & 79 deletions modules/postgres.js
Original file line number Diff line number Diff line change
@@ -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();
}
});
},
}
);
}
});
},
};
26 changes: 2 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72562b2

Please sign in to comment.