UNPKG

ckn.data

Version:

207 lines (196 loc) 8.17 kB
import { ckn } from "ckn"; import { DataConnector } from "./connector.js"; import pg from 'pg'; class PostgreSQLConnector extends DataConnector { constructor() { super(); this.isTransaction = false; this.client = null; } begin = async () => { return await new Promise(onCompleted => { this.isTransaction = true; this.client = new pg.Client({ user: this.user, // default process.env.PGUSER || process.env.USER password: this.password, //default process.env.PGPASSWORD host: this.host, // default process.env.PGHOST database: this.database, // default process.env.PGDATABASE || process.env.USER port: this.port || 5432, // default process.env.PGPORT }); this.client.connect(); console.log("CKN.DATABASE.QUERY> BEGIN"); this.client.query("BEGIN", (err, res) => { if (err != null) console.error(err); onCompleted(res.rows); }); }); } rollback = async () => { return await new Promise(onCompleted => { this.isTransaction = true; console.log("CKN.DATABASE.QUERY> ROLLBACK"); this.client.query("ROLLBACK", (err, res) => { if (err != null) console.error(err); onCompleted(res.rows); }); }); } commit = async () => { return await new Promise(onCompleted => { this.isTransaction = true; console.log("CKN.DATABASE.QUERY> COMMIT"); this.client.query("COMMIT", (err, res) => { if (err != null) console.error(err); onCompleted(res.rows); }); }); } end = async () => { return await new Promise(onCompleted => { this.client.end(); this.isTransaction = false; onCompleted(); }); } query = async (query) => { return await new Promise(onCompleted => { if (!this.isTransaction) { this.client = new pg.Client({ user: this.user, // default process.env.PGUSER || process.env.USER password: this.password, //default process.env.PGPASSWORD host: this.host, // default process.env.PGHOST database: this.database, // default process.env.PGDATABASE || process.env.USER port: this.port || 5432, // default process.env.PGPORT }); this.client.connect(); } console.log("CKN.DATABASE.QUERY> " + query); this.client.query(query, (err, res) => { if (err != null) console.error(err); onCompleted(res.rows); if (!this.isTransaction) this.client.end(); }); }); } insert = async (tableName, objs, returnValue = null) => { return await new Promise(async (onCompleted, onError) => { if (!Array.isArray(objs)) { objs = [objs]; } let columnNames = ""; let columns = []; let maxLengthObj = objs.reduce(function(prev, current) { return (Object.keys(prev).length > Object.keys(current).length) ? prev : current }) Object.keys(maxLengthObj).forEach(key => { columnNames += ("\"" + key + "\","); columns.push(key); }); columnNames = columnNames.slice(0, -1); let sql = "INSERT INTO \"" + tableName + "\" (" + columnNames + ") VALUES "; let values = []; objs.forEach(row => { let value = []; columns.forEach(col => { let v = row[col]; value.push(v); }); values.push(value); }); values.forEach(v => { sql += "("; v.forEach(i => { if (i === null || i === undefined || i === '') { sql += `${null},`; } else { sql += "\'" + i + "\',"; } }); sql = sql.slice(0, -1); sql += "),"; }); sql = sql.slice(0, -1); if (!this.isTransaction) { this.client = new pg.Client({ user: this.user, // default process.env.PGUSER || process.env.USER password: this.password, //default process.env.PGPASSWORD host: this.host, // default process.env.PGHOST database: this.database, // default process.env.PGDATABASE || process.env.USER port: this.port || 5432, // default process.env.PGPORT }); this.client.connect(); } if(returnValue){ sql += ` RETURNING "${returnValue}"` } console.log("CKN.DATABASE.QUERY> " + sql); this.client.query(sql, (err, res) => { if (err != null) { console.error(err); onError(err); } else { onCompleted(res); } if (!this.isTransaction) this.client.end(); }); }); } update = async (tableName, objs, condition) => { return await new Promise(async (onCompleted, onError) => { if (!Array.isArray(objs)) { objs = [objs]; } objs.forEach(obj => { let sets = ""; Object.keys(obj).forEach(key => { let v = obj[key]; if (typeof v == "string") { v = v.replace(/'/g,'\'\'',); } if (v === null || v === undefined || v === '') { if (!Array.isArray(obj[key])) sets += "\"" + key + "\" = null,"; } else { if (!Array.isArray(obj[key])) sets += "\"" + key + "\" = '" + v + "',"; } }); if (sets.length > 0) sets = sets.slice(0, -1); let sql = "UPDATE \"" + tableName + "\" SET " + sets + " WHERE " + condition; ckn.log.info("QUERY> " + sql); if (!this.isTransaction) { this.client = new pg.Client({ user: this.user, // default process.env.PGUSER || process.env.USER password: this.password, //default process.env.PGPASSWORD host: this.host, // default process.env.PGHOST database: this.database, // default process.env.PGDATABASE || process.env.USER port: this.port || 5432, // default process.env.PGPORT }); this.client.connect(); } console.log("CKN.DATABASE.QUERY> " + sql); this.client.query(sql, (err, res) => { if (err != null) { console.error(err); onError(err); } else { onCompleted(res); } if (!this.isTransaction) this.client.end(); }); }); }); } getFields = async (table) => { let q = "select * from information_schema.columns where table_catalog = '"+this.database+"' AND table_schema = '"+this.schema+"' AND table_name = '"+table+"'"; return this.query(q); } checkTable = async (table) => { let q = "select * from information_schema.columns where table_catalog = '"+this.database+"' AND table_schema = '"+this.schema+"' AND table_name = '"+table+"'"; return (await this.query(q)).length > 0; } } export { PostgreSQLConnector };