File size: 3,141 Bytes
748250d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | const fetch = require("node-fetch");
class Client {
/**
* Initiates Class.
* @param {String} key Custom database URL
*/
constructor(key) {
if (key) this.key = key;
else this.key = process.env.REPLIT_DB_URL;
}
// Native Functions
/**
* Gets a key
* @param {String} key Key
* @param {boolean} [options.raw=false] Makes it so that we return the raw string value. Default is false.
*/
async get(key, options) {
return await fetch(this.key + "/" + key)
.then((e) => e.text())
.then((strValue) => {
if (options && options.raw) {
return strValue;
}
if (!strValue) {
return null;
}
let value = strValue;
try {
// Try to parse as JSON, if it fails, we throw
value = JSON.parse(strValue);
} catch (_err) {
throw new SyntaxError(
`Failed to parse value of ${key}, try passing a raw option to get the raw value`
);
}
if (value === null || value === undefined) {
return null;
}
return value;
});
}
/**
* Sets a key
* @param {String} key Key
* @param {any} value Value
*/
async set(key, value) {
const strValue = JSON.stringify(value);
await fetch(this.key, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: encodeURIComponent(key) + "=" + encodeURIComponent(strValue),
});
return this;
}
/**
* Deletes a key
* @param {String} key Key
*/
async delete(key) {
await fetch(this.key + "/" + key, { method: "DELETE" });
return this;
}
/**
* List key starting with a prefix or list all.
* @param {String} prefix Filter keys starting with prefix.
*/
async list(prefix = "") {
return await fetch(
this.key + `?encode=true&prefix=${encodeURIComponent(prefix)}`
)
.then((r) => r.text())
.then((t) => {
if (t.length === 0) {
return [];
}
return t.split("\n").map(decodeURIComponent);
});
}
// Dynamic Functions
/**
* Clears the database.
*/
async empty() {
const promises = [];
for (const key of await this.list()) {
promises.push(this.delete(key));
}
await Promise.all(promises);
return this;
}
/**
* Get all key/value pairs and return as an object
*/
async getAll() {
let output = {};
for (const key of await this.list()) {
let value = await this.get(key);
output[key] = value;
}
return output;
}
/**
* Sets the entire database through an object.
* @param {Object} obj The object.
*/
async setAll(obj) {
for (const key in obj) {
let val = obj[key];
await this.set(key, val);
}
return this;
}
/**
* Delete multiple entries by keys
* @param {Array<string>} args Keys
*/
async deleteMultiple(...args) {
const promises = [];
for (const arg of args) {
promises.push(this.delete(arg));
}
await Promise.all(promises);
return this;
}
}
module.exports = Client;
|