DEV Community

Cover image for Getting Started with Toge DB, A Javascript Based Database + ORM Layer
M Iqbal Revantama
M Iqbal Revantama

Posted on

Getting Started with Toge DB, A Javascript Based Database + ORM Layer

Intro

Welcome to my article! I’d like to share a mini-project I built with the help of an AI coding assistant: Toge DB.

Honestly, my initial goal was simply to learn how to use AI to boost my productivity, rather than creating something for public release. However, I believe this library is useful for those looking to build desktop or web prototype application quickly using a JavaScript-based tech stack.

This database was built using 100% JavaScript and features an ORM layer that makes it easy to operate. In this article, I’ll show you how to build a simple Electron app using Toge DB.

1. Setup Electron App

If you are new to Electron development, you can refer to this official documentation for a deeper dive.

First, initialize a npm project and install the Electron dependency (please ensure the type value is set to module in your package.json):

mkdir toge-electron-app && cd toge-electron-app
npm init
npm install electron --save-dev
Enter fullscreen mode Exit fullscreen mode

Next, open package.json and add "start": "electron ." to the scripts field:

{
  "name": "toge-electron-app",
  "version": "1.0.0",
  "description": "",
  "license": "ISC",
  "author": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "start": "electron .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "electron": "^40.0.0"
  }
}
Enter fullscreen mode Exit fullscreen mode

Create an index.js file and add the following console.log() script. Then, launch the app using the npm run start command:

console.log('Hello from Electron 👋')
Enter fullscreen mode Exit fullscreen mode

Below is the expected result after running the command 🎉:

2. Setup Toge DB

To install Toge DB, execute the following command in your terminal:

npm install toge-db
Enter fullscreen mode Exit fullscreen mode

Once the installation is complete, create a new .env file in the root of your Electron project and add the following configuration:

TOGE_DB_ADMIN_USER=your-username #fill with your desired username
TOGE_DB_ADMIN_PASSWORD=your-password #fill with your desired password
Enter fullscreen mode Exit fullscreen mode

You can now access the Toge DB CLI using the command below. Log in using the credentials you just created:

npx toge-db
Enter fullscreen mode Exit fullscreen mode

The result:

3. Create New Table and Populate Data

We need to create a table named fruit to store fruit names and their prices. Execute the following query to create the table:

CREATE TABLE fruit (name string PRIMARY KEY, price int)
Enter fullscreen mode Exit fullscreen mode

Next, populate the table with some initial data. Please execute these queries one by one:

INSERT INTO fruit VALUES ('Banana', 5)
INSERT INTO fruit VALUES ('Apple', 10)
INSERT INTO fruit VALUES ('Orange', 8)
Enter fullscreen mode Exit fullscreen mode

Finally, let's retrieve the data using the query below:

SELECT * FROM fruit
Enter fullscreen mode Exit fullscreen mode

The Result:

4. Toge DB + Electron

As mentioned earlier, this database comes with a built-in ORM layer, allowing you to access your data quickly within your JavaScript application. Follow the instructions below to implement the ORM in your new Electron app.

To start using the ORM, import the TogeORM in your index.js file:

import { TogeORM } from 'toge-db';
const orm = new TogeORM('./data');

console.log('Hello from Electron 👋');
Enter fullscreen mode Exit fullscreen mode

Next, we need to define the fruit Model to map the fruits name and their price. Use orm.define(modelName, schema) to create the Model.

const Fruit = orm.define('fruit', {
    name: { type: 'string', primaryKey: true },
    price: { type: 'int' }
});
Enter fullscreen mode Exit fullscreen mode

Now, time to show all the fruits! Use Model.find(condition) to get the data. Add the example code below and then run npm run start.

console.log('Hello from Electron 👋');

// Find all fruits
const allFruits = Fruit.find();
allFruits.forEach(fruit => console.log("name: " + fruit.name + " price: " + fruit.price));
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have successfully integrated Toge DB into your Electron app 🎉:

For a complete CRUD implementation, update your code with the following code:

console.log('Hello from Electron 👋');

const newFruit = new Fruit({
    name: 'Mango',
    price: 100
});
newFruit.save();

Fruit.update(fruit => fruit.name === 'Banana', { price: 90 });
Fruit.delete(fruit => fruit.name === 'Orange');

const allFruits = Fruit.find();
allFruits.forEach(fruit => console.log("name: " + fruit.name + " price: " + fruit.price));
Enter fullscreen mode Exit fullscreen mode

The Result:

Outro

Toge DB is still in active development. In the next version, I plan to add features such as indexing and additional query clauses like JOIN and LIKE. Please stay tuned for the new release! (https://www.npmjs.com/package/toge-db)

Thank you for reading! ^^

Top comments (0)