node.js and Domino – Part 2

Part 1 – node.js and Domino

In Part 1 we got a very small taste of node.js and a simple example of what it is. It’s basically a JavaScript engine that runs on the desktop. While Part 1 wasn’t very in depth or that useful, I think it’s a perfectly simple example of node.js in action.

So that leaves to question, what can we do with it? Well, we can do a whole lot with it actually. Below is a very limited list of the things I do with node.js:

  • Custom Build Systems
  • Custom tooling
  • One off scripts
  • Communicating with databases (other than .NSF but that’s hopefully coming fairly soon)
  • Alexa Skills
  • Automated starter-kits
  • Code conversion tools
  • File Manipulation tools
  • HTTP and/or HTTP 2.0 Application Server
  • This list is endless

In this post we’ll initialize a node.js project and develop a simple import into Firebase Cloud Firestore. This will allow us to use their REST service for a web app if you so choose. I’m choosing Firebase because it’s free, powerful and is a document database. This same process (probably not the code tho) can be used once we have our Domino node.js module(s).

So, first things first. Let’s setup our Firebase project. Visit the Firebase website. If you have a Google account you can log in with that. Once logged in, click the “Go To Console” button in the top right corner. Once at the console click the “Add Project” tile.

Once you click the “Add Project” tile you’ll be prompted with a dialog to name your project. I’m using “hello-node-js”.

Once that’s done you will end up on your project’s Overview page. When you created this project, a Google project was created along with a Service Account. This is the account we will use for authentication purposes from node.js. However, we need to download the key file that will accompany our project. So, follow the below screen shots to get to the appropriate place to get this file.

Back in the Firebase console, as long as you’re on the same page that we left off on. You’ll want to copy the “Admin SDK configuration snippet” as we’ll need that to use the Firebase SDK.

OK, now that we’ve got that out of the way, we’re ready to define the database engine we want to use. Click the “Database” option in the left sidebar and you’ll be presented with a choice: “Cloud Firestore Beta” or “Realtime Database”. Click the “Get Started” button in the “Cloud Firestore Beta” tile. You’ll be presented with a dialog. Just accept the defaults.

Once that’s done, you will be able to see your database and any records (it’ll be empty). Leave this page open for later verification and we’re ready to rock-n-roll.

In Part 1 we created a simple script, we’ll delete our hello-world.js file and get going on something useful. First thing, open your editor and use the embedded terminal. Type npm init and press enter. This will ask a few questions pertinent to our project and create a package.json file. This file will be used to keep track of the dependencies for our project and can also provide scripts that we can run.

As you can see in the above screen shot we now have a package.json file. The content is as follows:

{
  "name": "hello-world",
  "version": "1.0.0",
  "description": "Import data to Firebase Cloud Firestore",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Keith Strickland <keith@redpillnow.com>",
  "license": "MIT"
}

Since we’re working with Firebase, we’ll need to install the firebase-admin module via NPM (Node Package Manager, was installed with node.js). So, back in our terminal we’ll type npm install firebase-admin --save-dev. This command will record the dependency in our package.json file under “devDependencies”. If we were wanting this to be a production dependency instead of --save-dev we would use --save. Once this is done installing, you’ll notice we now have a node_modules directory, along with a new file called package-lock.json. Also, our package.json now contains this:

{
  "name": "hello-world",
  "version": "1.0.0",
  "description": "Import data to Firebase Cloud Firestore",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Keith Strickland <keith@redpillnow.com>",
  "license": "MIT",
  "devDependencies": {
    "firebase-admin": "^5.12.0"
  }
}

We can now create our index.js file. This is where all of our code will reside.  Now paste the snippet we copied earlier for the authentication piece of Firebase into index.js. You should end up with the following (note: I changed the var to const):

const admin = require("firebase-admin");
const serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://hello-node-js-13d14.firebaseio.com"
});

We want to change that “path/to/serviceAccountKey.json” to match the file we downloaded. So go ahead and change that to “./hello-node-js-13d14-99ebb929df60.json”. Now our code should read:

const admin = require("firebase-admin");
const serviceAccount = require("./hello-node-js-13d14-99ebb929df60.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://hello-node-js-13d14.firebaseio.com"
});

While writing this post, I actually saved the output of ?readViewEntries&outputFormat=json on a simple book review .NSF so that we would have something to import that had at least a little bit of interest. I saved this file to books.json in our project. So we need to define that in our code so we can loop through it and import those entries. The loop will just read each field of each entry, create a JSON object and import that, then move to the next one until we reach the end. So now our index.js looks like:

const admin = require("firebase-admin");
const serviceAccount = require("./hello-node-js-13d14-99ebb929df60.json");
const entries = require('./books.json');

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://hello-node-js-13d14.firebaseio.com"
});

const db = admin.firestore();

let count = 0;
for (let i = 0; i < entries.length; i++) {
  let ent = entries[i];
  let docRef = db.collection('docs').doc(ent['@unid']);
  let doc = {};
  for (let j = 0; j < ent.entrydata.length; j++) {
    let fld = ent.entrydata[j];
    let val = fld.textlist ? fld.textlist : fld.text ? fld.text['0'] : null;
    doc[fld['@name']] = val;
  }
  docRef.set(doc);
  count++;
}
console.log('Imported ' + count + ' documents');

Let’s go ahead and run this and see what happens. So in the embedded terminal, we’ll just type node index.js and press enter. Hopefully if everything went ok you should get a `console.log` output of “Imported 66 documents”.

If we go back to our Firebase page and refresh the page we should see that we now have a collection named “docs” and there should be 66 of them:

Now I realize this short series wasn’t really a tutorial on how to work with Domino via node.js and more of a tutorial on how to setup Firebase. However, instead of a JSON file we imported from ?readViewEntries&outputFormat=json we could just have easily used Ajax to request those entries and do something interesting with them. I hope this series piqued your curiosity to go and start experimenting with node.js as I believe it will be relevant in the not so distant future.

If you’re interested you can find the repository for this post on Github.

Until next time, Happy Coding!

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.