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!
Without further ado, here are the docs!
1) Click on the floppy disk:
This is the homepage for your database. You can view storage usage and amount of keys.
2) Click on the box icon:
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]);
}
})
})
9) 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 ;)
Technically Repl.it DB is NoSQL.
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
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]);
}
error undefined variable name
and ans
@CarlosRosiles
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}
ah yes, I'll ask fluffin about that @matthewproskils
@Coder100 k.thanks
@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
yes, of course. Is there a problem? @matthewproskils
@Coder100 Oh, sorry maybe i should've bolded instead of uppercased
yeah, but what's the problemnull
is explicit undefinedundefined
is well, undefined. @matthewproskils
I cant figure out what client.get returns
@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
hi hmmm idk maybe I should just make it happen by default lol @ironblockhd
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)))
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
wat no sql....
First actual...
HAHAYES
Nice, though, how do you get the floppy disk button?
Turn on explorer mode @AmazingMech2418
Oh no, coder100 is trying to topple my domain. Oh no must be better
@Coder100 Hehe I rule the memes therefore I rule REPL
wait you keep repeating that... what memes do you rule? @HahaYes
oh he rules the tutorial section apparently @Bookie0
@Bookie0 I RULE THE MEMES
um ok lol @Coder100
um ok lol @HahaYes
@coder100 Ignore the bottom; I got bored
Ah... couldn't find any documentation when I discovered it 24 hours ao yesterday morning
@Coder100 That's one day old. A more up-to-date version would be 56 hours ago.
@Coder100 57 hours.
what's the data base
what do you mean @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
@DynamicSquid Read the rest man
noice!
thanks! @TheForArkLD
hello, repl.it db is to have persistent key/value
data @TheForArkLD
@Coder100 can not invited user set data?
@Coder100 ( from program )
@Coder100 btwwwwwww can we do stekovaya?
@TheForArkLD If he doesn't he doesn't. And?
@TheForArkLD Yes, but it has to be stored in JSON. You key/value pairs only.
@TheForArkLD Erm... what did you say OK to?
@TheForArkLD Hallo?
noice
thanks! @HahaYes
why the mrlapiz GitHub lol?
um because that was the connected acc on desktop @RohilPatel
Nice!
Thanks! @randomlylelo
nice :P
thanks! lol @JDOG787
Omg, thats so cool! Now i finally can use a database that doesnt reset!
I made a wrapper for your wrapper haha
implemented in ironMedia now, ill give you credits of corse
nice!!! @ironblockhd