DEV Community

Vinicius Basilio
Vinicius Basilio

Posted on

Simple in-memory database in JS

Hello, people!

Lately, I've been exploring a slightly different project. I've always enjoyed creating tools, and this time I decided to develop a simple “in-memory database” in JavaScript. The goal? To improve the handling of object collections and learn more about how databases work under the hood.

I felt it would be interesting to be able to use queries to perform some filtering operations. Many things are common in different projects. The idea is to create an intuitive query structure, inspired by Prisma ORM, making data operations more organized and efficient. Although it's an experiment focused on learning, I want the API to be fluid and easy to use.

I also want to ensure that creations respect a schema, so we'll validate entries using the Zod lib.

You can check out the full project on Github. Here's a small sample:

const client = new Db();
client.CreateModel("users", UserSchema);

client.tables.users.create({
   {
    id: 1,
    name: "Alice",
    age: 25,
    email: "alice@example.com",
    type: "admin",
    country: "USA",
    subscriptionPrice: 29.99,
    createdAt: "2024-02-01T10:00:00Z",
    updatedAt: "2024-02-05T15:30:00Z",
  }
});

const users = client.model.users.findMany({
  where: [
    {
      age: 30,
      type: "common",
    },
  ],
});

Enter fullscreen mode Exit fullscreen mode

The complete project => https://github.com/Vini-ba-dev/InMemoryDb

Leave a comment, I want to hear from you

Top comments (0)