Synchronous programming using pg-prom

I am new to node.js and the pg promise coming from the background of synchronizing programming, which requires new thinking.

I would like to start with a script to start database initialization to the main program logic.

Following this example: https://github.com/vitaly-t/pg-promise/wiki/Learn-by-Example#simple-select

db.any("select * from users where active=$1", [true])
  .then(function (data) {
    // success;
  })
  .catch(function (error) {
    // error;
  });

Will my code read:

db.query("DELETE FROM records")
  .then(function (data) {
    // success;
    console.log("This is where all my main logic goes");
  })
  .catch(function (error) {
    // error;
  });

Or it will read like:

db.query("DELETE FROM records")
  .then(function (data) {
    // success;
  })
  .catch(function (error) {
    // error;
  });
console.log("This is where all my main logic goes");

I understand that with the last message will be displayed before deleting entries.

Update: After reading numerous articles on promises and callbacks, I understand that pg-promes uses a chain that is .then and .catch for success and error scenarios. I also now believe that I understand that the pg-prom command should be placed in the shell of a function, for example:

function initialiseDB() {
  db.query("DELETE FROM records")
    .then(function (data) {
      // success;
    })
    .catch(function (error) {
      // error;
    });
  }

Then from my code I would call the function using a simple command that would execute the function asynchronously:

initialiseDB();
Nevertheless, I am still not sure how this concept of asynchronous coding and promises fits into the general structure of the program, since almost everything that the program requires will require first initialization. Accordingly, the entire program should not be placed in the ".then" section of the function? those. the only code at the top level will be the intialiseDB () function?
// Start of code here 
var pgp = require('pg-promise')();

//Configure the database connection
var config = {
  user:               'username', //env var: PGUSER 
  database:           'database', //env var: PGDATABASE 
  password:           'password', //env var: PGPASSWORD 
};

var db = pgp(config);

function initialiseDB() {
  db.query("DELETE FROM records")
    .then(function (data) {
      // This is where the main program logic goes after this line;
    })
    .catch(function (error) {
      // error;
    });
}

// Commence the program here
initialiseDB();
+4
1

PG- generators async/await nodejs. generators promises, , generator function

generators:

db.task(t => {
        // this.ctx = task config + state context;
        return t.one('SELECT * FROM users WHERE id = $1', 123)
            .then(user => {
                return t.any('SELECT * FROM events WHERE login = $1',user.name);
            });
    })
    .then(events => {
        // success;
    })
    .catch(error => {
        // error;
    });

generators:

db.task(function * (t) {
        // this.ctx = task config + state context;
        let user = yield t.one('SELECT * FROM users WHERE id = $1', 123);
        return yield t.any('SELECT * FROM events WHERE login = $1', user.name);
    })
    .then(events => {
        // success;
    })
    .catch(error => {
        // error
    });

async/await, function * (t) async function (t) yield await, .

:

try catch , , generators async/await

db.task(function * (t) {
    yield Promise.reject(new Error('This will cause your app to error')
}

pg-prom! , , SQL , !

0

Source: https://habr.com/ru/post/1649748/


All Articles