EZReplDB
EZReplDB is a quick and easy way to interact with Replit's database with next to no set up.
Using EZReplDB
const db = require("ez-repldb");
Yep, that's all you need to do, you can start using the database right away!
Methods
These are methods of the exported database object.
Note: EZReplDB supports storage of all valid JSON datatypes.
db.set(String key, Any value);
Sets a key
in the Replit database to value
.
Returns a promise which resolves to true
or false
, indicating the success of the operation.
db.set("key", 123).then(console.log);
console.log(await db.set("msg", "Hello world!"));
db.set(Any[] keysAndValues);
Reads the input (one dimensionsal array) as key
-value
pairs and sets all the key
s to their corresponding value
s in the Replit database.
Returns a promise which resolves to true
or false
, indicating the success of the operation.
db.set("key", 123, "msg", "Hello world!").then(console.log);
console.log(await db.set("key", 123, "msg", "Hello world!"));
db.get(String key);
Gets the value stored using key
from the Replit database.
Returns a promise which resolves to the value stored using the input key
or null
(if an error occurs or the key
didn't exist).
db.get("key").then(console.log);
console.log(await db.get("msg"));
db.get(String[] keys);
Gets an array of values stored using the key
s in keys
from the Replit database.
Returns a promise which resolves to an array of values stored using the input key
s.
db.get("key", "msg").then(console.log);
console.log(await db.get("key", "msg"));
db.delete(String key);
Deletes the value stored using key
from the Replit database.
Returns a promise which resolves to true
or false
, indicating the success of the operation.
db.delete("key").then(console.log);
console.log(await db.delete("msg"));
db.delete(String[] keys);
Deletes the values stored using the key
s in keys
from the Replit database.
Returns a promise which resolves to true
or false
, indicating the success of the operation.
db.delete("key", "msg").then(console.log);
console.log(await db.delete("key", "msg"));
db.list();
Gets the keys currently stored in the Replit database.
Returns a promise which resolves to an array containing the keys stored in the Replit database (the array will be empty if the database has had no keys set or if an error occurs).
db.list().then(console.log);
console.log(await db.list());
db.list(String prefix);
Gets the keys currently stored in the Replit database that start with the input prefix
.
Returns a promise which resolves to an array containing the keys stored in the Replit database that begin with the input prefix
(the array will be empty if the database has had no keys set or if an error occurs).
db.list("k").then(console.log);
console.log(await db.list("m"));
db.clear();
Removes all key-value pairs from the Replit database.
Returns a promise which resolves to an array indicating the success of deleting each key-value pair from the Replit database.
db.clear().then(console.log);
console.log(await db.clear());
db.getObject(Object object);
Retrieves the keys stored as properties within the input object
from the Replit database and applies them to object
.
Returns a promise that resolves with the resulting object (note that Object
s are passed by reference so this will actually alter the object passed in to the function).
const obj = {
"key": null,
"msg": null
};
db.getObject(obj).then(function() {
console.log(obj);
});
await db.getObject(obj);
console.log(obj);
db.applyObject(Object object);
Sets the keys stored as properties within the input object
to their respcetive values and applies them to the Replit database.
Returns a promise that resolves to true
or false
depending on the success of the operation.
const obj = {
"key": 123,
"msg": "Hello world!"
};
db.applyObject(obj).then(console.log);
console.log(await db.applyObject(obj));
db.import(String url);
Imports the key-value pairs from another Replit database using the input URL (to get this URL, in the shell of the Repl you would like to import the database from enter echo $REPLIT_DB_URL
).
Returns a promise that resolves the true
or false
depending on the success of the operation.
Also logs to the console all imported keys and potentially unsuccessfully imported keys (or upon failure, during which operation the failure occurred).
db.import("https://kv.replit.com/some-arbitrary-string").then(console.log);
Also output:
Imported the following keys: "key", "msg"