🟥 ScarletDB - Replit's first real database (Python)
- 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.
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?
- Scarlet works on replit.
- Scarlet is more lightweight.
- Scarlet is faster (because it works in memory).
- 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.
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".