Skip to content
← Back to Community
Repl.it DB
Profile icon
hacker
has Hacker Plan
Coder100

Example test run for repl.it db
Working Form

Turn on explorer mode to use it!

Repl.it DB

Hello everyone! Repl.it just announced a new way to host your databases. They have created their own database type. It's unlike any other SQL/noSQL database: It is JSON based!!!

Links

Test run
Client code
NPM package
Github Code
Working "What do you think of node.js?" REPL

Node.js TUTORIAL

So, with help from

@masfrost
, I have created an npm package for it! It contains the native curl functions, and includes more (like emptying database, setting automatic types for values and more!) Also along the way, I learned testing with jest...
Also, I got the hacker plan!
image

Without further ado, here are the docs!

1) Click on the floppy disk:

image

This is the homepage for your database. You can view storage usage and amount of keys.

2) Click on the box icon:

image

Install replitdb-client.

3) Copy and paste this code into the editor (if all you want is a minimalistic template).

const Client = require("replitdb-client"); const client = new Client(); (async () => { // Add async/await methods here. })();

4) Now, you are ready to set some commands! Inside the async function, you can do some methods. For our tutorial, we will make a what do you think of the node.js engine survey.

Some quick docs

Setting data

await client.set("<key>", "<value>")

getting data

await client.get("<key>", { raw: true|false })

you can leave out the raw part, but if you set raw to false, you will either get an object, or an error (the data wasn't valid json)
listing keys

await client.list("<optional beginsWith>")

getting all data as an object

await client.getAll()

delete data

await client.delete("<key>")

delete all data

await client.empty()

Easy, right?

5) Prompt

Add this to your code:

const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("What do you think of node.js? ", async ans => { rl.question("What is your name? ", async name => { // TODO: Save to database }) })

This just gets our form ready. Remember the async because we are using await calls here.

6) Save to database

Quite easy. We just refer to our set method in the docs.

const Client = require("replitdb-client"); const client = new Client(); const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("What do you think of node.js? ", async ans => { rl.question("What is your name? ", async name => { // Save await client.set(name, ans); }) })

7) Get all data

Now, we can use client.getAll to accomplish this!

const Client = require("replitdb-client"); const client = new Client(); const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("What do you think of node.js? ", async ans => { rl.question("What is your name? ", async name => { // Save await client.set(name, ans); console.log("Thank you for your valuable input!"); console.log("Previous users have said:"); // Get all data let data = await client.getAll(); }) })

8) Parse the data

Right now our data is just an object, let's make it human-readable!

const Client = require("replitdb-client"); const client = new Client(); const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); rl.question("What do you think of node.js? ", async ans => { rl.question("What is your name? ", async name => { // Save await client.set(name, ans); console.log("Thank you for your valuable input!"); console.log("Previous users have said:"); // Get all data let data = await client.getAll(); for (const user in data) { console.log(user, "has said", data[user]); } }) })
  1. And that's it! We have gone through almost all of what you will be using for a database (with delete as an exception). I can't wait to see what you will make with the client!

Also, here's the working code for you to hack ;)

Homework

  • Make it so if the user entered a name that is already in the database, don't save to the database. (hint: db.get)
  • Add colors!!
  • Get a specific user's entry

Have a great day!

Ask in the comments below if you have any questions ;)

Voters
Profile icon
WILFREDBallesta
Profile icon
KarioTheCoder
Profile icon
MingYuChen1
Profile icon
Abidzar16
Profile icon
kkonrad
Profile icon
MushyAvocado
Profile icon
AlphaCentauri4
Profile icon
AngusInfinity
Profile icon
OldWizard209
Profile icon
NoobieZr
Comments
hotnewtop
Profile icon
ironblockhd

Omg, thats so cool! Now i finally can use a database that doesnt reset!

Profile icon
ironblockhd

I made a wrapper for your wrapper haha

Profile icon
ironblockhd

implemented in ironMedia now, ill give you credits of corse

Profile icon
Coder100
Profile icon
DrExperiment

Is there a way to use repl.it db in HTML? Or to save any data from my website to a server? E.g. if I have a sign up page, how can I save accounts that have been created by users?

Profile icon
Coder100

no it would not be secure nor feasible

you'd probably want to use repl auth and a mix of backend with that

@DrExperiment

Profile icon
cloutjs

so there is a way of using a repl db in another repl with the db link from the .env file. but it seems like this link is updating like daily. do you maybe know a way to stop that? or do you know if it's maybe not allowed to use a repl db in more than one repl?

hope you can help me, pretty new to this and you seem to have a bit more knowledge on that haha

Profile icon
Coder100

nope you can't and you should not be

@cloutjs

Profile icon
cloutjs

okay, thank you!

@Coder100

Profile icon
JimmyDean3

I don't even understand some of the details in this article, but there is some amazing information for me. I am aiming to build an engaging attractive website named wordle unlimited wordle and I got some ideas after reading this article. Thank you.

Profile icon
Polargaming

this is pretty cool

Profile icon
Coder100

thank you!

@Polargaming

Profile icon
Qwertyboi12

Whenever someone else runs the code, it throws errors. Is there a way to prevent that?

btw I use python

Profile icon
Coder100

no, because the error probably says something about the token not being defined, and the reason it isn't is because of security reasons. You probably want to make some sort of webserver instead

@Qwertyboi12

Profile icon
MayankSK44689

I know this thing is pretty old but still wanted to ask. When I put the variable in which I use "new database" in "console.log" to see what it returns, I get a link. I examined the link for a while and I wanna if I can I use the link to access the DB from an HTML File.

Profile icon
Coder100

hi
if you can access the link others can too -- and that is a security risk!
Also, for security too the link changes every so often

@MayankSK44689

Profile icon
VulcanWM

can i use this in a js file in a html repl?

Profile icon
Coder100
Profile icon
helloyanis

I have a few questions, and i am kinda new so sorry if that's dumb questions but...


  1. What type of data can you store in your database? I know you can do it with strings, but can you do it with integers, floats or booleans?

  1. The line to import the database in your example is :
const Client = require("replitdb-client");

However the line to import the database that repl gives me is :

const Database = require("@replit/database")

Does that mean that some stuff from the code is outdated? Or does your example still work?


  1. Can you create multiple databases in a single repl? I am trying to code a game in a discord bot that would remember both the gold the user has and the experience points won. I tried something like
const Database = require("@replit/database") //As in the insertable code const gold = new Database() const exp = new Database()

Would it be possible to manipulate both databases like that?


  1. [SOLVED] I don't know why but a lot of times the operations with the database are done out of order. For example, i have this program that's supposed to delete the database then add 2 values/keys and once it does so it logs them in the console :
const Database = require("@replit/database") //Import the database const db = new Database() //Create the database db.empty() //Clear the database db.set("key", "value").then(() => {});// Put a value and a key in the database. db.list().then(keys => {console.log(keys)}); //Logs all keys. I would like it to only log the key I added on the previous line db.set("otherkey", "othervalue").then(() => {}); //Adds another key and another value db.list().then(keys => {console.log(keys)}); //Logs the keys again. I would like it to log both keys I added on the previous lines

However, when I run it, the logs changes on every run, and I get sometimes this log :

[] [ 'key', 'otherkey' ]

Sometimes I get...

[ 'key', 'otherkey' ] [ 'key', 'otherkey' ]

And it changes exery time. What is the cause of that and how can I fix it? (Here is the repl's link : https://repl.it/@helloyanis/testdb#index.js )
Nevermind I solved it, jut had to add

.then(() => {});

after the line of code that clears the database. The link has the fixed code if someone wants it

Profile icon
Coder100
  1. Any JSON serializable object.
  2. It only differs by name, this is an older tutorial, after replit adapted by library they must have changed how you defined things to be slightly more precise. There haven't been any breaking changes yet so it should be correct.
  3. psst don't do that one db client is good, but you can have multiple. It might cause some data races

    @helloyanis
Profile icon
helloyanis

@Coder100
Thank you for your reply!!

Profile icon
Coder100

np!
did it help?

@helloyanis

Profile icon
helloyanis

@Coder100
Yeah thank you!

Profile icon
triplemilkshake

@helloyanis
dude you seriously read my mind. im making a discord bot too, and i was using json files before, now i want to change to replit's database.

Profile icon
Coder100
Profile icon
programmeruser

Technically Repl.it DB is NoSQL.

Profile icon
Coder100

no, it's a key value database through and through.
After all, there is no 'collection' nor 'document', but you could align your db that way

@programmeruser

Profile icon
[deleted]

Why am I still asking questions about this?
This has stumped me for a long, long time
How does the code know that the user is the same as name? And for the data[user], how does it know that that is ans??? (In this chunk of code):

for (const user in data) { console.log(user, "has said", data[user]); }
Profile icon
Coder100
Profile icon
Coder100

error undefined variable name and ans

@CarlosRosiles

Profile icon
Kookiez

I can't see the floppy disk even tho I'm in explorer mode...

image

Profile icon
matthewproskils

Hey,

@coder100
, i have found a problem with the delete function. If you delete an item, traces will still be left : result for getall is

{'': null}
Profile icon
Coder100

ah yes, I'll ask fluffin about that

@matthewproskils

Profile icon
matthewproskils

@Coder100
k.thanks

Profile icon
matthewproskils

@coder100
i realized that if u search for an item now (using GET), if it is not there, then it will return NULL instead of UNDEFINED

Profile icon
Coder100

yes, of course. Is there a problem?

@matthewproskils

Profile icon
matthewproskils

@Coder100
Oh, sorry maybe i should've bolded instead of uppercased

Profile icon
Coder100

yeah, but what's the problem
null is explicit undefined
undefined is well, undefined.

@matthewproskils

Profile icon
rediar

I cant figure out what client.get returns

Profile icon
ironblockhd

@Coder100
just a warning, use encode/decodeURIComponent for your next project with your database, i just found out why mine broke like once a week
IMG_2750

Profile icon
Coder100

hi hmmm idk maybe I should just make it happen by default lol

@ironblockhd

Profile icon
cherryblossom00

Would you mind changing the implementation of getAll and setAll so it is more efficient? Currently, it waits for the fetching/setting the value for each key before continuning with the next key:

// getAll for (const key of data) { let value = await this.get(key); output[key] = value; } // setAll for (const key in obj) { let val = obj[key]; await this.set(key, val); }

This could be improved by using something like this:

// getAll const entries = await Promise.all(data.map(async key => [key, await this.get(key)])) const output = {} entries.forEach(([key, value]) => output[key] = value) return output // or with ES2019's Object.fromEntries: return Object.fromEntries(entries) // setAll await Promise.all(Object.entries(obj).map(([key, val]) => this.set(key, val)))
Profile icon
Coder100

Hello. Thanks for your suggestion! Currently, I'm doing it this way is because of concerns about what the API can handle and not handle. Promise.all executes them all at the same time, while the await method will do it one-by-one. A bit slower, but until sid gets back,
this will be the implementation for now.

@cherryblossom00

Profile icon
HahaYes

wat no sql....
First actual...

HAHAYES

Profile icon
Coder100
Profile icon
AmazingMech2418

Nice, though, how do you get the floppy disk button?

Profile icon
Coder100

Turn on explorer mode

@AmazingMech2418

Profile icon
HahaYes

Oh no, coder100 is trying to topple my domain. Oh no must be better

Profile icon
Coder100
Profile icon
HahaYes

@Coder100
Hehe I rule the memes therefore I rule REPL

Profile icon
Bookie0

wait you keep repeating that... what memes do you rule?

@HahaYes

Profile icon
Coder100

oh he rules the tutorial section apparently

@Bookie0

Profile icon
HahaYes

@Bookie0
I RULE THE MEMES

Profile icon
Bookie0

um ok lol

@Coder100

Profile icon
Bookie0

um ok lol

@HahaYes

Profile icon
firefish

@coder100
Ignore the bottom; I got bored

image

Profile icon
Coder100
Profile icon
firefish

Ah... couldn't find any documentation when I discovered it 24 hours ao yesterday morning

Profile icon
Coder100
Profile icon
firefish

@Coder100
That's one day old. A more up-to-date version would be 56 hours ago.

Profile icon
Coder100
Profile icon
firefish

@Coder100
57 hours.

Profile icon
DynamicSquid

what's the data base

Profile icon
Coder100

what do you mean

@DynamicSquid

Profile icon
DynamicSquid

@Coder100

Oh i meant to say what is a data base. Like:

Hello everyone! Repl.it just announced a new way to host your databases. They have created their own database type. It's unlike any other SQL/noSQL database: It is JSON based!!!

like what are they referring to when they say database

Profile icon
firefish

@DynamicSquid
Read the rest man

Profile icon
TheForArkLD

noice!

Profile icon
Coder100
Profile icon
TheForArkLD

@Coder100
btw
repl.it db is for collect other programmers info?
btw
can we do Stekovaya?

Profile icon
Coder100

hello, repl.it db is to have persistent key/value data

@TheForArkLD

Profile icon
TheForArkLD

@Coder100
can not invited user set data?

Profile icon
TheForArkLD

@Coder100
( from program )

Profile icon
TheForArkLD

@Coder100
btwwwwwww can we do stekovaya?

Profile icon
firefish

@TheForArkLD
If he doesn't he doesn't. And?

Profile icon
firefish

@TheForArkLD
Yes, but it has to be stored in JSON. You key/value pairs only.

Profile icon
TheForArkLD
Profile icon
firefish

@TheForArkLD
Erm... what did you say OK to?

Profile icon
firefish
Profile icon
TheForArkLD
Profile icon
HahaYes

noice

Profile icon
Coder100

thanks!

@HahaYes

Profile icon
RohilPatel

why the mrlapiz GitHub lol?

Profile icon
Coder100

um because that was the connected acc on desktop

@RohilPatel

Profile icon
randomlylelo

Nice!

Profile icon
Coder100
Profile icon
Highwayman

👌

Profile icon
Coder100
Profile icon
Highwayman
Profile icon
Coder100
Profile icon
Highwayman
Profile icon
JDOG787

nice :P

Profile icon
Coder100

thanks! lol

@JDOG787

Profile icon
JDOG787