Skip to content
Sign upLog in
← Back to Community

πŸŸ₯ ScarletDB - Replit's first real database (Python)

Profile icon
xfinnbar

Prerequisites

  • Should know what a database is.
  • Should know basic-intermediate Python.

What is a document database?

To put it simply, a document database is a type of database that works a lot like a list of JSON objects (a collection of key value pairs), but with some extra cool beans added on top. You can have multiple of these collections, which can serve different purposes. For example, here is a basic structure for storing users.

[ // the list { // an object "name": "joe", "bio": "mama" }, { // another object "name": "sue", "bio": "kon-deez" } ]

We can easily represent this in a document database.

Why should I use a document database over a JSON file or the built-in Replit database?

Document databases provide a lot more functionality than a JSON file. For example, you can get things by their values without wasting a ton of processing power.

Why ScarletDB? Isn't MongoDB better?

Pros:

  • Scarlet works on replit.
  • Scarlet is more lightweight.
  • Scarlet is faster (because it works in memory).

Cons:

  • Scarlet only works with Python (so far).
  • Scarlet is memory-hungry (no feasible solution, unfortunately).
  • Scarlet can't store a lot of data (but definitely enough for an average to large replit project.

Getting started

First, install Scarlet using the packages button on the sidebar. The package is called scarletdb.
image


Take a look at this example:

from scarletdb import ScarletDB db = ScarletDB([]) # create the database # load the data from replit db db.replit("data") # data is replit db the key that the database is stored in db.clear() # empty the db for testing purposes db.insert_many([ { "name": "AJ", "pet": "Rabbit" }, { "name": "Jack", "pet": "Dog" } ]) # insert some documents db.update({"name": "Jack"}, {"name": "Tom"}) # update some documents, while querying them by a value (does not loop over them, uses a data structure to save them in a way where they can be queried) print(db.get({ "pet": "Dog" })) # get a document by another key than before

As the comments describe, we first need to create the database so we can access it. Next, we load the data from replit database (if you aren't using replit, pass data to the constructor and use db.list to store it). After that, we clear the database to give us an empty canvas. You should remove this if you want your data to persist over restarts.

Then, we insert some documents into the database. We could use db.insert many times here, passing one dict instead of a list of them, but insert_many is more concise. Now we'll update anything with the name "Jack" and change the name to "Tom".

Documentation.

Voters
Profile icon
kirbybros45
Profile icon
ch1ck3n
Profile icon
CosmicBear
Profile icon
xfinnbar
Comments
hotnewtop
Profile icon
ch1ck3n

how does scarletdb store the data in replit

Profile icon
xfinnbar

@ch1ck3n
It works through normal Python dicts and lists, so it can be made into a JSON string and stored in a file, replit DB and many other places.

Profile icon
RoBlockHead

@xfinnbar
so it doesn’t actually store the data? It just stringifies it?

Profile icon
xfinnbar

@RoBlockHead
By default it does not store anything but you can either call db.replit() to set it up for storage in replit DB or provide your own commit (save) function and data in the constructor. For example, here is how you would set it up with a JSON file.

from scarletdb import ScarletDB import json def commit(): with open("file.json", "w") as file: file.write(json.dumps(db.list)) def get_data(): with open("file.json", "r") as file: return json.loads(file.read()) db = ScarletDB(get_data(), commit)```
Profile icon
ch1ck3n

umm i think its private

Profile icon
RayhanADev

Replit's first real database (Python)

Arguable, but regardless pretty kewl.