notes for 20200512

my sense is that last time we managed to

  1. get an airtable API key
  2. install dotenv and create a .env file containing our API key
  3. use require('dotenv').config() to load up our environment variables.

I think maybe the next steps are these:

  1. list the contents of an airtable table at the /airtable route
  2. get and display a specified record at /airtable/:record_id
  3. start to understand JS data types:
    • strings
    • numbers
    • arrays
    • objects
    • booleans
    • dates (which is just an object not its own type, but a special object we'll use a lot)
  4. maybe work on styling this stuff and handling loops in the ejs
  5. maybe use a cli.js file and npm link to create commands?
  6. maybe work on shell scripting at some point? or using child process to open up stuff in the terminal?
  7. anything else?

When we code our new routes, we'll end up with something like this for the /airtable route:

router.get('/airtable', function(req, res, next) {
  // go get some data from Airtable and render it
  // look for stuff using record_id or other search term
  var base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_WATCH_BASE);
  var theRecords = [];
  base('Media').select({
      // Selecting most recent 1000 emoji
      maxRecords: 100,
      view: "Main View"
  }).eachPage(function page(records, fetchNextPage) {
    // go through each page and accumulate reactionType in theRecords array
    theRecords.push(...records);
    fetchNextPage()
  }).then(()=>{
    // now that you've accumulated all the records, render them
    res.render('json', {
      title: req.params.record_id,
      timestamp: moment().format('YYYYMMDD-HHmmss.SSS'),
      json: JSON.stringify(theRecords, null, 4)
    })
  }, function done(err) {
      if (err) { console.error(err); return; }
  });
});

and then something like this for the specific records:


router.get('/airtable/:record_id', function(req, res, next) {
  var base = new Airtable({apiKey: process.env.AIRTABLE_API_KEY}).base(process.env.AIRTABLE_WATCH_BASE);
  base('Media').find(req.params.record_id, function(err, record) {
      if (err) {
        console.error(err); return;
      }
      console.log('Retrieved', record.id);
      res.render('json', {
        title: req.params.record_id,
        timestamp: moment().format('YYYYMMDD-HHmmss.SSS'),
        json: JSON.stringify(record, null, 4)
      });
  });
});

the 'json' view file (i.e. /views/json.ejs) I'm using just looks like this:

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
  </head>
  <body>
    <h1><%= title %></h1>
    <h3><%= timestamp %></h3>
    <p>Welcome to <%= title %></p>
    <pre>
<%= json %>
    </pre>
  </body>
</html>