Saturday, June 22, 2019

axios concurrent calls

Axios can make multiple concurrent calls. In app.js calls to two end-points are started at the same time so total waiting time is 4 seconds. Server latency is 4 seconds for demo purposes.

app.js

const axios = require('axios')
 
try {
    console.log("start");
    function getUserName() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    function getId() {
        return axios.get('http://localhost:3000/heroes');
    }
 
    // Performing multiple concurrent requests waiting time 4 seconds
    axios.all([getUserName(), getId()])
        .then(axios.spread(function (acct, perms) {
            const user = acct.data[0].param;
            const id = perms.data[1].param;
            console.log(`${user} ${id}`)
            const str = 'https://jsonplaceholder.typicode.com/' + user + '/' + id
            console.log(str)
            axios.get(str).then(response => console.log(response.data.name));
        }));
} catch (error) {
    console.error(error);
}

Monday, June 17, 2019

axios async await

Sometimes you need to wait for one (axios) call to finish to continue with next one. It's accomplished by using async/await. Last axios call in app.js downloads a file without waiting for getUser() to be finished.

set server delay to 4 seconds for demo purposes

{
  "name": "async-await",
  "scripts": {
    "serve-json": "json-server --delay 4000 --watch db.json",
    "start": "node app.js"
  },
  "dependencies": {
    "axios": "^0.19.0",
    "json-server": "^0.15.0"
  }
}

db.json

{
    "heroes": [
        {
            "param": "users"
        },
        {
            "param": "1"
        }
    ]
}

app.js

const axios = require('axios')
const fs = require('fs')
 
async function getUser() {
    try {
        console.log("start");
        const response = await axios.get('http://localhost:3000/heroes');
        var1 = response.data[0].param; // users
        console.log("4 sec after start ", var1);
        const response2 = await axios.get('http://localhost:3000/heroes');
        var2 = response2.data[1].param; // 1
        console.log("8 sec after start", var2);
        const response3 = await axios.get('https://jsonplaceholder.typicode.com/' + var1 + '/' + var2);
        console.log(response3.data.name);
    } catch (error) {
        console.error(error);
    }
}
 
getUser()
 
// call below doesnot wait for getUser() to be finished 
axios({
    method: 'get',
    url: 'https://jsonplaceholder.typicode.com/users/2',
    responseType: 'stream'
  })
    .then(function (response) {
      response.data.pipe(fs.createWriteStream('full-user.json'))
});






Thursday, February 14, 2019

Oracle 12c Create Table with Auto Increment and Flashback

CREATE TABLE table_name ( 
    id NUMBER(10) GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 NOCACHE ) NOT NULL, 
    ctimestamp TIMESTAMP DEFAULT current_timestamp 
) NOLOGGING; 

CREATE UNIQUE INDEX table_name_pk ON table_name (id ASC ); 

ALTER TABLE table_name ADD CONSTRAINT table_name_pk PRIMARY KEY ( id ); 

SELECT TO_CHAR (SYSDATE, 'YYYY-MM-DD HH24:MI:SS') "NOW" FROM DUAL; 

CREATE TABLESPACE archivetest_tabspace DATAFILE SIZE 10M SEGMENT SPACE MANAGEMENT AUTO; 

CREATE FLASHBACK ARCHIVE archivetest TABLESPACE archivetest_tabspace QUOTA 50M RETENTION 7 DAY; 

ALTER TABLE table_name FLASHBACK ARCHIVE archivetest; 

SELECT * FROM table_name AS OF TIMESTAMP TO_TIMESTAMP ('2018-11-29 14:05:11', 'YYYY-MM-DD HH24:MI:SS');

Sunday, January 6, 2019

Docker and node_modules

Development environment for a NodeJS App


Dockefile

FROM node:16-alpine

WORKDIR /app

COPY package.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

.dockerignore

node_modules
npm-debug.log
.git
Dockerfile
.dockerignore

Open your browser on http://localhost:3000

docker-compose.yaml
version: "3.8"

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - './src:/app/src'
      - './public/assets:/app/public/assets'
      - '/app/node_modules'
    ports:
      - 3000:3000
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true

Friday, December 21, 2018

Mass Update and Insert in Oracle

UPDATE tab1 t1 SET col_1 = (SELECT col_1 FROM tab2 t2 WHERE t1.id=t2.id);

INSERT INTO table1 SELECT * FROM table2 WHERE ...

INSERT INTO table1 (col_1, col_2) SELECT col_1, col_2 FROM table2 WHERE ... 

Oracle triggers

Oracle trigger can be used to modify data before update or insert in the same table.

Oracle update trigger:

CREATE OR REPLACE TRIGGER update_time_col
  BEFORE
    UPDATE OF col_1, col_2
  ON tab
  FOR EACH ROW
BEGIN
  CASE
    WHEN UPDATING('col_1') THEN
      :new.time_col := SYSTIMESTAMP - INTERVAL '1' DAY;
      :new.col_1 := 'updated ' || :old.col_1;
    WHEN UPDATING('col_2') THEN
      :new.time_col := SYSTIMESTAMP + INTERVAL '1' DAY;
      :new.col_2 := 'updated ' || :old.col_2;    
  END CASE;
END;
/

Oracle insert trigger:

CREATE OR REPLACE TRIGGER insert_time_col
  BEFORE
    INSERT
  ON tab
  FOR EACH ROW
BEGIN
  :new.time_col := SYSTIMESTAMP + INTERVAL '10' DAY;
  :new.col_1 := 'insert ' || :new.col_1;  
  :new.col_2 := 'insert ' || :new.col_2;
END;
/




Wednesday, October 3, 2018

Oracle 12c parsing table row by row with for and switch case

Function that will be used:

CREATE OR REPLACE FUNCTION fun2( p_c_id IN CHAR, p_name IN VARCHAR2, p_regio IN NUMBER) 
RETURN VARCHAR2 
AS 
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
  INSERT INTO CON (COUNTRY_ID, COUNTRY_NAME, REGION_ID)
  VALUES ( p_c_id, p_name, p_regio);
COMMIT;
RETURN p_name || ' mod';
END;
/


Parse row by row using CASE SWITCH:

SELECT REGION_ID,
       CASE region_id
           WHEN 2 THEN fun2(country_id, country_name, region_id )
           WHEN 4 THEN 'four'
           ELSE 'not'
       END AS c
FROM countries


Parse row by row using loop

BEGIN
FOR c IN (SELECT field1, field2 FROM mytable) 
  LOOP 
    my_proc(c.field1, c.field2);
  END LOOP;
END;