Skip to main content

Posts

Showing posts from October, 2022

How and why I designed a business validatoion framework

Greetings! Business validations are a crucial part of any application. We faced use cases with too many validations. It is hard to maintain and extend the solutions with such a number of validations. This is when I thought to try a solution even though there was no such task in the backlog. Hence I used my own personal time for this. Features Execute a larger number of validations in single use case Reusability by sharing the same validation in different use cases Skip non-mandatory validations by user choice (yes/no dialogs) Expose validations via REST API Extendability by adding/removing validations No performance impact Easy to use in future developments It all started with an Interface My quick solution was to introduce a common interface by utilizing Java generics and Java 8 new features. I did not have all the features in mind when introducing this. However, that is the power of having a proper interface. public interface Validator<T> { void vali...

Design a meeting event scheduler

Greetings! I got a chance to work on a project where we had UIs for meeting schedules and later improved it to sync with external calendars as well. In this article, I am sharing my general ideas on that design. The main business requirement is to access meetings anywhere whether from our system or an external system. (It is a different domain, for the article's purpose I refer to these as meetings) Initial investigation I took a decision to make this a normal meeting request just like Google calendar as making this a meeting event would be more appropriate and user-friendly. My co-worker did the initial investigation on iCalendar format which is standard for calendar clients. We found that sending a meeting request is an email with the header "type=calendar". Initial investigation and the proof-of-concept is done! Let's define the main components. Main components We could complete this by adding simple classes/logic. However, that is not real engineering. Hence, I de...

How and why I designed a meeting (iCalendar) library

Greetings! Have you ever thought about how to send a meeting event programmatically? In one of the projects I worked on, there was a requirement to send the appointment information to the user (in addition to visualizing it in our UIs). This is when I thought to do this as a meeting request. How can we do that? We learned that these calendar applications follow a standard format, that is "icalendar". Now my job is to create these .ics files. We investigated a few existing libraries as well but I was not happy with them as I wanted to send it as a meeting request but it did not work with existing libraries. At the same time, we have a lengthy process when introducing new libraries. Hence, why don't I build my own library 😉? What is iCalendar? The iCalendar is the standard specification for calendaring and scheduling events. It allows users to store and exchange calendaring and scheduling information such as events, to-dos, etc. According to the specification u...

How and why I designed an email library

 Greetings! How do you send emails using Java? You would probably use a free library/framework like Spring or anything available. Or, you might write a simple email-sending class. However, when I needed an emailing functionality for one of the requirements, I decided to do more. Why? In larger projects, we always should think about the bigger picture. Adding a new library should be done with a lot of investigation Module-only features have no value for the product That is why I thought to give this an extra effort and designed an internal email library. General idea This is simple. Any team should be able to use this library without depending on any other external libraries. Also, the email API can have any implementation without impacting consumer modules. Existing libraries Initially, I thought to use Spring email but Spring core doesn't come with it. It looks like Spring should have separate jars for this. But anyway this will not help me now. There are other email libraries but...

Let's solve (N+1) problem in GraphQL

Greetings! This is the fourth article of the GraphQL series and the second part of the Query where I am going to solve (N+1) problem in GraphQL. However, I will add complete codes as well to make it usable on its own. The complete code can be found here . Code with (N+1) problem unresolved is here . (N+1) in brief When we fetch relationships like we did in our movie server where we queried movies, there will be extra N queries to fetch the relationship (director). With the first query to find movies, there are (N+1) queries altogether. Had you run our example by adding a console.log you can see the problem clearly. export function findDirector(id) { console.log(`find director for {id}`); return directors.find(director => director.id === id); } This problem does not depend on the database we used. Whether it is a fake store like we use, MySql, or MongoDB, the problem is the same. Solving N+1 Facebook provides a solution for this using the library data-loa...

Let's build a GraphQL server with Node.js and Express (2)

Greetings! In this third article of the GraphQL series, I would like to discuss the steps needed to create a GraphQL server using Node.js with a somewhat more advanced query than the previous one. The complete code can be found here . What do we build? Movie API where we can query movies and directors' information. Initializing the project npm init -y npm install express graphql express-graphql --save npm install @babel/core @babel/node @babel/preset-env --save-dev npm install nodemon --save-dev Enable ES6 modules in package.json and add a start command for nodemon. { "name": "nodejs-graphql-query", "version": "1.0.0", "description": "", "main": "index.js", "type": "module", "scripts": { "start": "nodemon --exec babel-node src/index", "test": "echo \"Error: no test specified\" && exit 1...

Let's build a GraphQL starter server with Node.js and Express

Greetings! This is the second article of the GraphQL series, I would like to discuss the steps needed to create a GraphQL server using Node.js. Have a look at the first article if haven't read it yet. The complete code can be found here .  What we build As the idea is to create a basic GraphQL server, we will expose the "Hello World" query. Initializing the project npm init -y npm install express graphql express-graphql --save npm install @babel/core @babel/node @babel/preset-env --save-dev npm install nodemon --save-dev Enable ES6 modules in package.json and add a start command for nodemon. { "name": "nodejs-graphql-starter", "version": "1.0.0", "description": "", "main": "index.js", "type": "module", "scripts": { "start": "nodemon --exec babel-node index", "test": "echo \"Error: no...

Introduction to GraphQL

Greetings! GraphQL is a query language for APIs to query data from the server. We are querying data from the server like we do with the database. Cool isn't it? Instead of getting what the server provides, we query what we want and it gives exactly what is asked! nothing more, nothing less. "A query language for your API" Advantages Easy to organize Allows API to evolve without breaking existing queries A single call to fetch data Query exactly what is wanted Better performance due to reduced network calls, and fewer data Common GraphQL terms Schema - GraphQL schema describes all the possible data that clients can query. It is made up of object types that define which kind of object you can request and what fields it has. Resolver - Each query is attached to a function called resolver that is used to produce data. Query - GraphQL query is used to fetch values from the ...

Let's create a Node.js REST CRUD server with MySql

Greetings! In a previous, I briefly discussed the steps to create a simple REST server using Node.js. In this post, I'm discussing the steps needed to create a restful crud API using Node, Express, and MySql with Sequelize. The complete source code can be found here . What we build We are going to create CRUD operations for books. It is very simple and we don't go too in-depth as the idea is to explore how we can build a restful API with Node and MySql. Steps Create the project Create the express server Define end points and layers Implement endpoints layers Package structure Create the project We will use ES6 features hence babel is used. Nodemon is used to auto-start the server with new changes. npm init -y npm install express body-parser mysql2 sequelize cors --save npm install @babel/core @babel/node @babel/preset-env --save-dev npm install nodemon --save-dev { "name": "nodejs-rest-crud", "version": "1.0.0", ...

Let's create a simple RESTful CRUD server with Node.js (and fake db)

Greetings RESTful API development is one of the most used use cases for Node.js. We will explore how we can build such an API in this post. As the focus here is on the web layer, I'll use an in-memory data store (an array). Also, I'll use my starter project to save some typing. The source can be found here . What we will build We are going to create CRUD operations for books. It is very simple and we don't go too in-depth as the idea is to create something to play with. Steps Create the project and install dependencies Create the express server Define end points Fill endpoints Create the project and install dependencies npm init -y npm install express body-parser cors --save npm install @babel/core @babel/node @babel/preset-env --save-dev npm install nodemon --save-dev We need to add module type in package.json in order to use ES6 modules. When we import we need to add .js as well. in package.json "type": "module" "scripts":...

Let's Setup Babel in Node.js

Greetings! Node.js is one of the most popular and most used backend technologies. It is used by many. I just want to create a project to play with node.js. Hence I thought to create a simple starter project with node.js and express (for myself or anyone who is interested). I would like to use ES6+ features hence I'm using babel here. Other than that, node, express, body-parser, and cors will be added. The source code is here . What is Babel? Bable is a JavaScript transcompiler that is mainly used to convert ES2015+ code into backward-compatible JavaScript code. What that means is we can use newer JavaScript features and babel will transcompile it for us to use in older JavaScript engines. Steps Create the project Install dependencies Setup babel Create the express server Create the project We can initialize a node.js project using npm. npm init -y Install dependencies Let's install our required packages. npm install express body-parser cors --sav...

Let's build a RESTful CRUD API using AWS serverless services (part 2)

Greetings! In the previous post , I described how to create a serverless REST API using the serverless framework in which we discussed the project setup and the create operation. In this post, we will complete other operations of our API. You can find the complete source code here . Read Item(s) As we created an item in our previous post, we had to check the value using the AWS console. Now it is time to access items via the REST endpoint. Get all books/items When we define APIs for fetching, we use the "get" HTTP method. As we select all the books, we use the DynamoDB scan command hence we need to allow access to it. Get all handler definition getBooks: handler: src/get-books.handler iamRoleStatements: - Effect: Allow Action: - dynamodb:Scan Resource: !GetAtt crudDynamoDBTable.Arn events: - http: path: /books method: get cors: true Get all handler implementation Create a ne...