An exercise in building a very minimal (and very stupid) in-memory SQL-like database for educational purposes.

Overview

Stupid Database

This is an exercise in building a very minimal (and very stupid) in-memory SQL-like database for educational purposes.

None of this code is to be considered usable in a real project. There are no safeguards, security, checks, or anything that resembles production-ready code. And it's written this way to be easily understandable for a very beginner programmer.

The goal is to demystify the SQL syntax like:

SELECT name FROM users WHERE age > 30

To an equivalent javascript call like:

select('name', from('users', { where: 'users.age > 30' }))

And the idea is to have a "mental model" of what SQL means in a more general programming language

You can run the example with:

node test

REPL

You can open an interactive REPL to run SQL command directly by running:

./repl.js

It's a node REPL and you can send SQL commands like this:

stupid sql > select name from users where users.city = 'Gotham'

Client/Server

There is also a demonstration of the most minimal bare-bones "SQL-like" database. You can start this server at port 4000:

./stupid-server.mjs

And connect to it with this client:

./stupid-client.mjs

It will open an interactive prompt so you can send the SQL commands

AVL Tree based Index

Just to show how an index is built and used (in a normal SQL server, it's obviously a B+Tree, but as this is a stupid in-memory hash, a simple AVL Tree should suffice).

We can compare the times for the equivalente of a 'table scan' filtering every single element in the table or using the index.

./bench_index.mjs

Spoilers: on my i9-9900K, with NVME, running inside WSL2, this is the result:

` ❯ ./bench_index.mjs from('users', { where: 'users.city === "Wakanda"'}).length took 145 ms normal select: 524288

getFromIds(users,getFromIndex('users', 'city', 'Wakanda')).length took 20 ms indexed select: 524288 `

The AVL indexed version can go from anywhere around 5 to 10 times faster, obviously.

ANTLR4

Just out of fun, I did a very small and very naive implementation of a SQL parser using Antlr4.

You can run very basic SQL against the stupid database. Just add the commands in the test.sql file and run doing:

./stupid.mjs

The parser is a stripped down version copied from the SQLite3 parser.

Again, this is for educational purposes only.


Copyright (C) Fabio Akita, 2022 This project should not be used out of context, without the permission of the author

You might also like...

ClientDB is an open source in-memory database for enabling real-time web apps.

ClientDB ClientDB is an open source in-memory database for enabling real-time web apps. Build fast, scalable apps that feel silky smooth for users. In

Dec 24, 2022

ClientDB is an open source in-memory database for enabling real-time web apps.

ClientDB ClientDB is an open-source in-memory database for enabling real-time web apps. Build fast, scalable apps that feel silky smooth for users. In

Aug 27, 2022

🗃️ An in-memory JS database optimized for large scale storage on the frontend.

BlinkDB is a in-memory JS database optimized for large scale storage on the frontend. import { many } from 'blinkdb'; const items = await many(userTa

Dec 23, 2022

women who code - object oriented programming exercise

Table of contents General info Technologies Setup General info Authorizer APP Technologies Project is created with: Typescript: 4.2 Setup Requirements

Oct 1, 2022

This is a full-stack exercise tracker web application built using the MERN (MongoDB, ExpressJS, ReactJS, NodeJS) stack. You can easily track your exercises with this Full-Stack Web Application.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Dec 25, 2021

This repository serves as a starter kit for doing simple TDD exercise

This repository serves as a starter kit for doing simple TDD exercise

Feb 19, 2022

Hands-on real world projects that will help you exercise your knowledge of TypeScript.

Learning TypeScript Projects Hands-on real world projects that will help you exercise your knowledge of TypeScript. Welcome to the repository housing

Dec 26, 2022

Simple budget-tracker web app developed using Vanilla JavaScript. Exercise from Warren Tech Academy.

Simple budget-tracker web app developed using Vanilla JavaScript. Exercise from Warren Tech Academy.

Willow Personal Finance - Exercise from Warren Tech Academy About Project screenshots Installation Extra notes About Tools: HTML CSS (SASS) JavaScript

Dec 14, 2022
Owner
Fabio Akita
OmniTrade co-founder (crypto exchange). Codeminer 42 co-founder (offshore outsourcing). THECONF organizer (since 2017). Ex-Rubyconf Brasil Organizer (2008-2016)
Fabio Akita
Michael Schneider 4 Oct 26, 2022
A stupid simple way to test

@asdgf A stupid simple way to test. Packages @asdgf/core The core of the test framework, and nothing else @asdgf/cli CLI tool to run tests in node, us

Pascal Schilp 16 Nov 17, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
In-memory abstract-level database for Node.js and browsers.

memory-level In-memory abstract-level database for Node.js and browsers, backed by a fully persistent red-black tree. The successor to memdown and lev

Level 14 Dec 27, 2022
Boost is a Microsoft Excel Add-in to help developers import large excel workbooks into their database using SQL queries

Microsoft Excel Add-in for Developers About Us Boost is a Microsoft Excel Add-in to help developers import large excel workbooks into their database u

OSLabs Beta 30 Sep 30, 2022
SQL-based database adapter for AdminJS

adminjs-sql This is an inofficial AdminJS adapter which integrates SQL-based database into AdminJS. Installation: yarn add adminjs-sql Usage The plugi

null 2 Sep 9, 2022
startupDB is an Express middleware function implementing a high-performance in-memory database

startupDB startupDB is a database designed to create REST APIs. It is implemented as an Express middleware function and allows for easy implementation

Jeroen de Vries 8 Jul 26, 2022
javascript embeddable / in-memory database

LokiJS The super fast in-memory javascript document oriented database. Enable offline-syncing to your SQL/NoSQL database servers with SyncProxy !! Cod

Joe Minichino 6.4k Jan 9, 2023