-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support of persons.json file in root
- Loading branch information
Голов Никита Викторович
authored and
Голов Никита Викторович
committed
Mar 5, 2023
1 parent
9860cf5
commit 72562b2
Showing
4 changed files
with
124 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,5 @@ uploads/* | |
|
||
.idea | ||
|
||
data/persons.json* | ||
data/persons.json* | ||
persons.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
}, | ||
} | ||
); | ||
} | ||
}); | ||
}, | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.