@alinex/datastore
Version:
Read, work and write data structures from and to differents locations and formats.
101 lines • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.modified = exports.save = exports.load = void 0;
const util_1 = require("util");
const debug_1 = require("debug");
const flat = require("flat");
const luxon_1 = require("luxon");
const async_1 = require("@alinex/async");
let pg = require('pg');
try {
require.resolve('pg-native');
pg = pg.native;
console.log('Using native pg!');
}
catch (e) { }
const debug = debug_1.default('datastore:protocol:psql');
const debugDetails = debug_1.default('datastore:details');
function cast(value) {
if (value === '')
return null;
if (value === 'false')
return false;
if (value === 'true')
return true;
const n = Number(value);
if (!isNaN(n))
return n;
if (value.match(/[-:]/)) {
const d = luxon_1.DateTime.fromISO(value);
if (d.isValid)
return d.toJSDate();
}
return value;
}
function toObject(arr, recordFormat) {
let res = recordFormat ? [] : {};
arr.forEach(row => {
if (recordFormat) {
res.push(flat.unflatten(row));
}
else {
const val = Object.values(row);
res[val[0]] = cast(val[1]);
}
});
return recordFormat ? res : flat.unflatten(res);
}
const load = function (parsedUri, options = {}) {
if (!parsedUri.pathname)
return Promise.reject(`No pathname given in ${parsedUri.href}`);
options.format = 'json';
debug(`loading ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`);
const conn = `${parsedUri.protocol}//${parsedUri.username}:${parsedUri.password}@${parsedUri.host}${parsedUri.pathname}`;
const sql = decodeURI(parsedUri.hash.substring(1));
debug(`query ${sql}`);
let client;
return (async_1.default
.retry(() => {
client = new pg.Client({
connectionString: conn
});
return client.connect().catch((e) => {
debug(`WARN: ${e.message}`);
client.end();
return Promise.reject(e);
});
})
.then(() => options.search && client.query(`SET search_path TO ${options.search}`))
.then(() => client.query(sql))
.catch((e) => {
client.end();
console.error(e);
return Promise.reject(e);
})
.then((res) => {
debugDetails(`postgres rows ${util_1.inspect(res.rowCount)}`);
debugDetails(`postgres result ${util_1.inspect(res.rows)}`);
const recordFormat = options.records || res.rows.length && Object.keys(res.rows[0]).length > 2;
return Promise.resolve(toObject(res.rows, recordFormat));
})
.then((data) => {
client.end();
return Promise.resolve([Buffer.from(JSON.stringify(data)), {}]);
}));
};
exports.load = load;
const save = (function (parsedUri, buffer, options) {
if (!parsedUri.pathname)
return Promise.reject(`No pathname given in ${parsedUri.href}`);
debug(`storing to ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`);
return Promise.reject(new Error('Save not implemented here!'));
});
exports.save = save;
const modified = async function (parsedUri, options) {
if (!parsedUri.pathname)
return Promise.reject(`No pathname given in ${parsedUri.href}`);
debug(`last modification check ${parsedUri.protocol}//${parsedUri.username}@${parsedUri.host}${parsedUri.pathname}`);
return Promise.resolve(new Date());
};
exports.modified = modified;
//# sourceMappingURL=postgres.js.map