Skip to content

Cup-Of-JavaScript/javascript-express-api-postgres-2

Repository files navigation

JavaScript Express API Postgress 2

Assignments are located here.

Getting Started

  • Clone this repo
  • Install dependencies: npm install (execute in the same directory as package.json file)
  • Create file: postgres-pool.js from file: postgres-pool.js.template
    • Update postgres-pool.js with your database password and check database name

Notes

  • api.template.js is a file that can be used to start building your Express API
  • Run create-banking-db.sql in your student database (if you haven't already)

Videos

Transactions

Client side transactions in Postgres are accomplished in the following manner:

module.exports.getAccountData = async (accountId) => {
    let retval = null;
    try {
        await pool.query("BEGIN")

        //
        // TODO... your queries here
        //

        await pool.query("COMMIT")
    } catch (err) {
        await pool.query("ROLLBACK")
        console.error(err);
    }
    return retval;
}

API Endpoint Template

//
// GET /persons/:id
//

app.get('/ex1/persons/:id', cors(corsOptions), async (req, res) => { 
    // Parsing...
    // const id = req.params['id'];                 // Parse the path params from URL (e.g. /persons/1)
    // const queryParam1 = req.query['personType']  // Parse the query string from URL (e.g. ?personType=manager)
    // const body = req.body;                       // Parse the the body from the request
    
    // Data access & business logic...
    // const result = await dataAccess.<YOUR FUNCTION HERE>
    
    // Response...
    // res.status(404); // 201, 400, 403, etc.
    // res.send(<YOUR OBJECT HERE>);
});

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 6