Continuing from the last post, I am going to write about populating subdocument in GraphQL.

Context

So, what I have done last time was building two schemas then I nested one of them in an array of another. To give you little more about context about this software, this is a POS software that there are a bunch of products and each payment has an array of products a customer paid.

// models/Transaction.js
import mongoose from 'mongoose';

const Schema = mongoose.Schema;

const itemSchema = new Schema({
name: { type: String },
amount: { type: Number },
});

const transactionSchema = new Schema(
{
price: { type: Number },
method: { type: String, default: 'VISA' },
cardNumber: { type: String },
paidTime: { type: Date, default: new Date() },
items: [itemSchema], // we will change this part
}
);

export const Transaction = mongoose.model('Transaction', …

How to traverse a binary tree in Python

In Binary Search Tree, there are three ways to traverse a binary tree, pre-order, in-order and post-order. I would only show an example of pre-order since the logic is the same.

when n is visited, the node will be printed or appended to a stack. You will see the example below.

n = visited stack

l = traversing to left child

r = traversing to right child

Preorder (NLR)

Binary Tree Traversal

Image for post
Image for post
Preorder step 1 — visiting left node

Once we visited n, remove n from the stack and same for l and r, execute l stack, which is visiting left node recursively until there is no left node. …


Getting Started

GraphQL, Apollo server and MongoDB all connected on your app.

Dependencies to install

devDependencies are optional, only for the sake of your convenience.

// package.json
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon --exec babel-node src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"apollo-server-express": "^2.19.0",
"express": "^4.17.1",
"graphql": "^15.4.0",
"mongoose": "^5.10.11"
},
"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/node": "^7.12.1",
"@babel/preset-env": "^7.12.1",
"nodemon": "^2.0.6"
}
}

How it works

There are three things to define to use graphQL and the logic might not be specifically applied to MongoDB + graphQL. The logic is simple.

  1. Let MongoDB how your schemas look…

This is tutorial of making an application that simulates transaction software. In this post, I would cover how to integrate PostgreSQL and node.js. Here the tools and skills we are going to use:

  • express
  • PostgreSQL

Middlewares

  • cors: connects server-side with client-side
  • pg: connects server with database

Prerequisite for this tutorial

  • basic SQL
  • basic HTTP REQUEST

Setup

Installation

mkdir server
cd server
npm i express pg cors
touch index.js
  1. Create a server directory inside of project folder
  2. Install express, pg, cors
  3. create index.js file where there is server-side code.

Basic backend setup

We are going to use express as a backend framework, this is pretty much standard for node.js. …


Continuing from the last post, Heap Part 01, I would explain heap sort which happens when inserting and deleting items from a heap. Let me recap from what I covered last time. We were doing a min-heap example and its basic methods such as parent(), left_child(), right_child().

Violation

Violation in a heap is when a child node is greater than a parent node in max-heap or child node is less than a parent node in min-heap.

Insertion

We are going to have a look a trivial example of how items inserted, to understand the heap sort. …


It has been a while that I posted data structure since I covered stack. In this post, I would like to talk about one of the easiest trees, Heap. Let’s crack into it!

Priority Queue

Before understanding Heap, you need to know what Priority Queue is. Priority Queue is an abstract data type(ADT) in which data has priority and the one that has higher priority will be deleted first. It has three primary operations. Hold on, what is an abstract data type? It is data structure like queue and stack.

  • insert(Q, x) — insert an item with a key x into priority queue…

This is one of the series I am going to write as I am learning this topic, node.js, arguably very popular backend environment where you can use JavaScript for it.

Global object

The global object is something like a window object on the browser console of JavaScript. If you print global, you can see a bunch of methods in it. There are a few methods you might use them, clearInterval, clearTimeout, setInterval, setTimeout.

__dirname and __filename

__dirname gets you an absolute path of your current value and __filename gets you an absolute path of the folder with the file name added on.

Difference between node.js and JavaScript

In node.js, DOM selectors such as querySelector wouldn’t work as in JavaScript. …


How to write a Merge Sort algorithm in Python

Previously, we had a look of Insertion Sort and implemented it in Python.

However, its worst-case scenario was O(n²), which is really slow isn’t it? and I promised you to more sophisticated and faster algorithms to sort an array that is Merge Sort.

Divide and Conquer

So how can we save time for sorting when it comes to a list of elements? The answer is to divide and conquer. I assume you must know binary search at this juncture, where you already started learning algorithms or you can go back to learn binary search. Let me give you a big picture of the whole plan. Firstly, we will have an array of size n, we will call it A. Secondly, We will then divide in a half (until it is not dividable), which will be two arrays Left and Right. Next, the two arrays will be sorted. …


How to write an Insertion Sort algorithm in Python

Sorting

Sorting is one of the concepts of the basic algorithm as important as searching algorithms. There are tons of algorithms to sort items. Why do we need to sort elements? Because problems become easier once elements are sorted. For example, you can find a median in constant time when an array is sorted, meaning that you can use a binary search.

Insertion Sort

If you think of the way to sort most easily in your head, it might be similar to insertion sort. How it works is, basically, it swaps pairs of elements from left to right until they are sorted. …


Hi guys, we have previously been learning basic data structures a bit, such as arrays, linked list and stack. we are going to learn algorithms this time, the very basic but very important topic, binary search.

I can code without knowing algorithms, why would I be bothered to learn them?

Of course, you can build software without using algorithms technically and it is not mandatory (I suppose so). However, when you solved your software problem, have you ever questioned yourself “Can I do better?”. This is where algorithms come into play, and ultimately, this thought leads you to become a better programmer.

Let me explain why it is important.

Services like Facebook have more than 2.7 billion users, meaning that Facebook engineers presumably have to handle the data of a huge number of users. There are program A and program B to handle those data. Program A takes 1 second to handle each user’s data, and 2 seconds for the counterpart. What would be the speed difference between the two programs? Well, you don’t even need to calculate them, B takes twice as long as A. …

About

Jenny Yang

Self-taught software engineer | Enthusiasm for programming and computer science

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store