{"id":2686,"date":"2018-10-31T18:18:50","date_gmt":"2018-10-31T16:18:50","guid":{"rendered":"http:\/\/hasselba.ch\/blog\/?p=2686"},"modified":"2018-11-04T17:33:20","modified_gmt":"2018-11-04T15:33:20","slug":"node-js-domino-db-docker-3-adding-domino-db","status":"publish","type":"post","link":"https:\/\/hasselba.ch\/blog\/?p=2686","title":{"rendered":"node.js, domino-db &#038; Docker (3): Adding domino-db"},"content":{"rendered":"<p>The express application is still the boilerplate created by express generator. Now let&#8217;s look into the existing code and use the domino-db package. First we have to understand express a little bit better. I won&#8217;t go deeply into details, because there are many tutorials available, and the <a href=\"https:\/\/expressjs.com\/\" target=\"_blank\" rel=\"noopener\">documentation<\/a> is really awesome.<\/p>\n<h1>Overview<\/h1>\n<h2>Directory structure<\/h2>\n<pre><code>\/domino-express\r\n   |-app\r\n      |-app.js\r\n      |-bin\r\n         |-...\r\n      |-public\r\n         |-...\r\n      |-routes\r\n         |-...\r\n      |-views\r\n         |-...<\/code><\/pre>\n<h2>app.js<\/h2>\n<p>This is the main application. It contains the configuration of the application and global modules (like used the middleware, global route handlers, etc.). At the moment we don&#8217;t need to change anything here.<\/p>\n<h2>\/bin<\/h2>\n<p>This folder contains the starting script for the application.<\/p>\n<h2>\/public<\/h2>\n<p>Contains publicly accessible static resources like images, stylesheets, etc.<\/p>\n<h2>\/routes<\/h2>\n<p>The handling for existing routes, which means how the application should process incoming requests.<\/p>\n<h2>\/views<\/h2>\n<p>Contains the templates for the generated output. We are using <a href=\"http:\/\/jade-lang.com\/\" target=\"_blank\" rel=\"noopener\">Jade<\/a> as our template engine.<\/p>\n<h1>The boilerplate<\/h1>\n<p>The first thing to look at is the <em>\/routes\/index.js<\/em> file. Just open it in Atom, and see what the express generator created for us:<a href=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/10\/express-routes-index.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2687\" src=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/10\/express-routes-index.png\" alt=\"\" width=\"870\" height=\"446\" srcset=\"https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/10\/express-routes-index.png 870w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/10\/express-routes-index-300x154.png 300w, https:\/\/hasselba.ch\/blog\/wp-content\/uploads\/2018\/10\/express-routes-index-768x394.png 768w\" sizes=\"auto, (max-width: 870px) 100vw, 870px\" \/><\/a><\/p>\n<p>The first line loads the express package and gives access to it. The next line gives us access to the router, which then is used to define a handle for all incoming requests on the application root (<em>http:\/\/localhost:3000\/<\/em>).<\/p>\n<p>When this route is called, the defined function renders the response using the view template &#8218;index&#8216; (which can be found in the <em>\/views<\/em> folder). The variables used in the template are contained in the object handed over as second parameter.<\/p>\n<p>The last line exports the router instance and gives access to it outside of our module. Just for a better understanding: Everything in this file (aka module) is private, and here it is defined what is public to the rest of our express application. For more details, have a look here: <a href=\"https:\/\/www.sitepoint.com\/understanding-module-exports-exports-node-js\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.sitepoint.com\/understanding-module-exports-exports-node-js\/<\/a><\/p>\n<h2>Change to ES6<\/h2>\n<p>After changing everything as proposed by the editor, the code should now look like this:<\/p>\n<pre><code>const express = require('express');\r\n\r\nconst router = express.Router();\r\n\r\n\/* GET home page. *\/\r\nrouter.get('\/', (req, res) =&gt; {\r\n  res.render('index', { title: 'Express' });\r\n});\r\n\r\nmodule.exports = router;<\/code><\/pre>\n<p>Now we add the requirement for the <em>domino-db<\/em> package:<\/p>\n<pre><code>const { useServer } = require('@domino\/domino-db');<\/code><\/pre>\n<p>The curly brackets notation is used to &#8222;import&#8220; only the <em>useServer<\/em> element from the <em>domino-db<\/em> package.<\/p>\n<p>Then we add the configuration for our Domino backend (shamelessly copied from the example in the dev pack):<\/p>\n<pre><code>const serverConfig = {\r\n  hostName: 'your.server.com', \/\/ Host name of your server\r\n  connection: {\r\n    port: '3002', \/\/ Proton port on your server\r\n  },\r\n};\r\n\r\nconst databaseConfig = {\r\n  filePath: 'node-demo.nsf', \/\/ The database file name\r\n};\r\n\r\nconst createOptions = {\r\n  documents: [\r\n    {\r\n      Form: 'Contact',\r\n      FirstName: 'Aaron',\r\n      LastName: 'Aardman',\r\n      City: 'Arlington',\r\n      State: 'MA',\r\n    },\r\n    {\r\n      Form: 'Contact',\r\n      FirstName: 'Brian',\r\n      LastName: 'Zelnick',\r\n      City: 'Chelmsford',\r\n      State: 'MA',\r\n    },\r\n  ],\r\n};<\/code><\/pre>\n<p>And now we add the connection of the database query when our base path is accessed:<\/p>\n<pre><code>router.get('\/', (req, res) =&gt; {\r\n  useServer(serverConfig).then(\r\n      async server =&gt; {\r\n        const database = await server.useDatabase(databaseConfig);\r\n        const response = await database.bulkCreateDocuments(createOptions);\r\n\r\n        \/\/ Display the new document UNIDs\r\n        const unids = response.documents.map(doc =&gt; doc['@unid']);\r\n        res.render('index', { title: 'Express', result: `Documents created: ${unids}` });\r\n  });\r\n});<\/code><\/pre>\n<p>Before we get into the details of this code, let&#8217;s start our application with <em>npm start<\/em>.After accessing the URL of the application <a href=\"http:\/\/localhost:3000\" target=\"_blank\" rel=\"noopener\">http:\/\/localhost:3000<\/a>, nothing happens.<\/p>\n<p>Just some console output tells us that we need some error handling when our Domino server is not reachble.<\/p>\n<pre><code>GET \/ - - ms - -\r\n(node:13268) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 6): DominoDbError: gRPC client error<\/code><\/pre>\n<p>This topic and more details about the <em>domino-db<\/em> module will be covered in the next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The express application is still the boilerplate created by express generator. Now let&#8217;s look into the existing code and use the domino-db package. First we have to understand express a little bit better. I won&#8217;t go deeply into details, because &hellip; <a href=\"https:\/\/hasselba.ch\/blog\/?p=2686\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[139,9,131],"tags":[138,140,133,137,132],"class_list":["post-2686","post","type-post","status-publish","format-standard","hentry","category-es6","category-javascript","category-node-js","tag-domino-db","tag-es6","tag-express","tag-jade","tag-node-js"],"_links":{"self":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2686","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2686"}],"version-history":[{"count":9,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions"}],"predecessor-version":[{"id":2705,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2686\/revisions\/2705"}],"wp:attachment":[{"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasselba.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}