Data and Services

In the last blog post, we covered some of the motivation behind the website, the initial setup for the node.js LWC app, and discussed some of the building blocks of the front end user interface. Today, we will switch gears and learn how the backend of the application is built to serve the content of this site. The interactions with the backend are very simple, but fitting all the pieces together can be a challenge, so let’s dive in.

Create the Database

We need to use a database to store the content of each of the blog posts for this site. I decided to go with PostrgreSQL as the persistent data storage to serve this website. To interact with the database, we will use the command line interface.

createdb myapp
psql -a myapp

If completed correctly, the terminal will prompt you for input for the myapp database. We can interact with the database from the terminal by entering sql statements directly to the prompt.

psql (13.4)
Type "help" for help.

myapp=# SELECT table_name FROM information_schema.tables WHERE table_schema='public';

(0 rows)


To exit the psql terminal, press Ctrl+d.

Define the Entities

Now that we have a provisioned database, let’s go over the design. The database for this site is remarkably simple; it only has one table.

id serial Automatically generated Id for each blog post
post_date date Date when the post was first created
title character varying Title of the blog post
post_name character varying The URL addressable name of the blog post
body character varying The raw HTML body of the blog post

To define this table, we need to execute a SQL script. Instead of writing our SQL directly from the terminal, we will write our commands to a .sql file, then pipe that file into our command. First, create a file called createDatabase.sql with the following contents:

    id        serial            NOT NULL,
    post_date date              NOT NULL,
    title     character varying NOT NULL,
    post_name character varying NOT NULL,
    body      character varying NOT NULL

Interacting with the database can be accomplished easily by using the command line interface. To execute a predefined SQL script, use the -f flag and pass in the path to the .sql file.

psql -a myapp -f createDatabase.sql

For this database, the rows contain raw HTML for the markup of each blog post. To make interacting with the database much easier, I have added some utility scripts which prevent me from needing to write raw SQL commands every time. For each blog post, I create a local newpost.html then I define the contents of that file as a variable to be referenced in a SQL script for inserts and updates by using the \set notation.

<h1>Hello World!</h1>
<p>Thank you for visiting my blog.</p>
\set content `cat newpost.html`
INSERT INTO blog_posts (
    'Hello World',
\set content `cat newpost.html`
UPDATE blog_posts
SET body = :'content'
WHERE post_name = 'hello_world';

Now we can define, insert, and update blog posts as rows in the database, but we need to allow the web application to transact with the database.

Create a REST-ful API

The web application needs to perform two different interactions with the database. First, when a user navigates to, the system must query for all records in the blog_posts table ordered by their post_date.

blog query

Second, when a user navigates to a specific blog post, the system must query for all of the details for the blog post. Each blog post record has a URL addressable post_name that will be used to identify the post and create the SQL query.

blog post query


To facilitate these interactions, we must create two new files within the server: database.js to establish the initial connection pool and define the queries which can be executed on the Postgres database, and router.js to route traffic from the site to API endpoints which can handle the interactions.

const { Pool } = require('pg');

const pool = new Pool({
        process.env.DATABASE_URL || 'postgres://localhost/myapp',
    ssl: process.env.DATABASE_URL ? { rejectUnauthorized: false } : false

const getPosts = async () =>
        'SELECT id, post_date, title, post_name, body FROM blog_posts ORDER BY post_date DESC'

const getPost = async (postName) =>
        `SELECT id, post_date, title, post_name,  body FROM blog_posts WHERE post_name = $1`,

module.exports = {
// router.js
const { Router } = require("express");
const router = new Router();
const db = require("./database");
const bodyParser = require("body-parser");

router.use(bodyParser.urlencoded({ extended: true }));

router.get("/posts", (request, response) => {
  doQuery(response, db.getPosts);

router.get("/posts/:id", (request, response) => {
  doQuery(response, db.getPost,;

async function doQuery(response, queryFunction, ...params) {
  try {
    const results = await queryFunction(...params);
    if (results) {
    } else {
  } catch (error) {
    response.status(500).json({ status: "error", message: `Error:${error}` });

module.exports = router;


Now that these functions are defined, we must import these functions from our server to the web client so that our lighting web components will be able to call the functions. We accomplish this by first creating a new file in src/client/modules/data called dataService.js.

const apiUrl = process.env.API_ENDPOINT || "http://localhost:5000"; // eslint-disable-line
const POSTS_URL = `${apiUrl}/api/v1/posts`;

export const getPosts = () =>
    .then((response) => {
      if (!response.ok) {
        throw new Error("No response from server");
      return response.json();
    .then((result) => {
      return result;
    .catch((error) => {
      console.log(`Error retrieving posts: ${error}`);

export const getPostById = (postsId) =>
    .then((response) => {
      if (!response.ok) {
        throw new Error("No response from server");
      return response.json();
    .then((result) => {
      return result;
    .catch((error) => {
      console.log(`Error retrieving post: ${error}`);

The .js controller file in the lightning web component bundle can then import the dataService. We also can use routeParams from @lwce/router to grab URL parameters and use them as input to our database queries. Below is the source code for post.js which will grab the id parameter from the URL, pass that into the getPostById function, and parse the results from the database. Finally, the post details can be passed to the lightning web component’s HTML, and rendered to the user.

import { LightningElement, wire } from "lwc";
import { getPostById } from "../../data/dataService";
import { routeParams } from "@lwce/router"; // eslint-disable-line
export default class Post extends LightningElement {
  @wire(routeParams) params;

  thePost = {};

  connectedCallback() {
      .then((result) => {
        if (result.length > 0) {
          let post = result[0];
          let postWithDetail = {
            body: post.body,
            post_name: post.post_name,
            formatted_date: this.formatDate(new Date(post.post_date)),
            title: post.title,
            link: "/blog/" + post.post_name,
          this.thePost = postWithDetail;
      .catch((error) => {
        console.log(`Error retrieving posts: ${error}`);

  formatDate(theDate) {
    let options = {
      weekday: "long",
      year: "numeric",
      month: "long",
      day: "numeric",
    return theDate.toLocaleDateString("en-US", options);
  <div class="slds-p-around_large">
    <div class="slds-box">
      <div class="slds-text-heading_medium">
        <lwce-link href="{}">{thePost.title}</lwce-link>
      <p class="slds-p-vertical_medium slds-border_bottom">
        <b> {thePost.formatted_date} </b>
      <br />

We’re Almost Complete…

Awesome! We have accomplished a lot of things at this point; we have:

  • Created a Postgres database
  • Defined the entities (one table in this case)
  • Inserted and modified some rows
  • Created a REST interface for interacting with the database
  • Written a component which uses the defined REST interface to fetch rows from the database

Things are looking pretty good, but we still need to take some final steps to make our web app production ready and available at the domain name of our choice. Stay tuned for the final post in this blog series where we will discuss how to get your web app up and running using Heroku.