Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import oracledb from 'oracledb';
  2.  
  3. export class ConnectionDB {
  4.  
  5.  
  6.  
  7.   constructor() {
  8.   }
  9.  
  10.  
  11.   public getElements(consult :string) : Promise<Object> {
  12.     return new Promise(async function (resolve, reject) {
  13.       let connectionDB;
  14.       await oracledb.getConnection({
  15.         user: "system",
  16.         password: "51264",
  17.         connectString: "localhost:1521/xe"
  18.       }).then(async (connection: oracledb.IConnection) => {
  19.         console.log("Conección abierta.");
  20.         await connection.execute(consult, [],{ outFormat: oracledb.OBJECT })
  21.         .then((result) => { resolve(result.rows) });
  22.         connection.close().then(() => {console.log('Conección cerrada.')});
  23.       }).catch((err: any) => {
  24.         console.error(err.message);
  25.       });
  26.     });
  27.   }
  28.  
  29.   public addElement(element: Object, sql: string, options: Object)
  30.   {
  31.     oracledb.getConnection({
  32.       user: "system",
  33.       password: "51264",
  34.       connectString: "localhost:1521/xe"
  35.     }).then(async (connection: oracledb.IConnection) => {
  36.       console.log("Conección abierta.")
  37.       await connection.execute(sql, element, options).
  38.         then((result)=>{console.log("Rows: \n", result.rowsAffected)})
  39.         .catch((err)=>{console.error("Promise rejected - ",err)});
  40.       connection.close().then( () => { console.log("Conección cerrada.") } )
  41.       .catch((err) => { console.error(err.message) });
  42.     }).catch((err: any) => {
  43.       console.error(err.message);
  44.     });
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement