remote-sqlite-database

1.0.8 • Public • Published

Built with Love Made with Javascript

Discord server NPM version NPM downloads Get Started Now Patreon

npm installnfo

remote-sqlite-database

  • A remote nodejs Database using sqlite
  • It has auto-restore features
  • It is fast
  • It uses enmap aka sqlite
  • The clients are cached
  • Supports SHARDED Applications (like Discord Bots, which used enmap before)

Installation

npm install https://github.com/Tomato6966/remote-sqlite-database
npm install remote-sqlite-database

Usage

  1. Create the DB - Create a File-Folder-Project, at your CACHE-SERVER (it's a cache and db at the same time) and put in this Code:
Click to see the Code
const { remoteCacheServer } = require("remote-sqlite-database");

const Server = new remoteCacheServer({
    username: "TheUserNameForTheCacheServer",
    password: "ThePasswordForTheCacheServer",
    name: "databaseName",
    dataDir: "./path/to/database/",
    port: 4040, // Any port
    tls: true,
    debug: false // if enabled u see all the actions ;)
});
// Following Events are optional
Server
    .on("serverReady", () => {
        console.log("DatabaseCacheServer ready and waiting for connections");
    })
    .on("serverError", (error) => {
        console.error("DatabaseCacheServer error, ERROR:\n", error, "\n---\n");
    })
    .on("serverClose", (reason) => {
        console.log("DatabaseCacheServer closed");
    })
    .on("serverConnect", (connection, payload) => {
        console.log("DatabaseCacheServer a Client Connected");
    })
    .on("serverDisconnect", (connection, reason) => {
        console.log("DatabaseCacheServer a Client Disconnected");
    })
    .on("serverMessage", (message) => {
        // console.log("DatabaseCacheServer, received a Message", message);
    })
    .on("serverRequest", async (request, response, client) => {
        // console.log("DatabaseCacheRequest, received a Request", request);
    });
  1. To connect the cache do this:
Click to see the Code
const { remoteCacheClient } = require("remote-sqlite-database");
const client = new remoteCacheClient({
    username: "db_cache",
    password: "db_cache",
    host: "localhost",
    port: 5000,
    tls: true,
    keyPathing: true, // true|false ... enables if the key contains a "." that it's splitted to a path, e.g.: "hello.world.hi" --> key = "hello", path = "world.hi", db.get("hello") --> {world: {hi: "value"}}
}); 
//keyPathing should be set to false, if you want to do "key.hello" as a key

// following events are optional
client
    .on("cacheReady", () => {
        console.log("DATABASECACHECLIENT ready and connected");
    })
    .on("cacheError", (error) => {
        console.error("DATABASECACHECLIENT error, ERROR:\n", error, "\n---\n");
    })
    .on("cacheClose", (reason) => {
        console.log("DATABASECACHECLIENT closed, REASON?:\n", reason, "\n---\n");
    })
    .on("cacheMessage", (message) => {
        console.log("message", message);
    })
    .on("cacheRequest", async (request, response, client) => {
        console.log("REQUEST", request);
    });

// example usage
async function yourProgram(){
    await client.set("hi", "bye").then(console.log).catch(console.error);
    await client.get("hi").then(console.log).catch(console.error);
    await client.set("array", []).then(console.log).catch(console.error);
    await client.push("array", "element").then(console.log).catch(console.error);
    await client.push("array", "element2").then(console.log).catch(console.error);
    await client.size().then(console.log).catch(console.error);
    await client.get("array").then(console.log).catch(console.error);
    await client.all().then(console.log).catch(console.error);
}

yourProgram();

Methods (Functions) for the CACHE-CLIENT(s)

  • all "path"'s are optional
  • get(key, path) get any value from a key
  • set(key, path) set any value in a key
  • add(key, amount, path) add a number to a key/path
  • substract(key, amount, path) removes a number from a key/path
  • math(key, operator, amount, path) make a mathematical operation like adding, substracting multiplying etc.
  • push(key, element, path) add an element in an array
  • remove(key, element, path) remove an element out of an array
  • has(key, path) check if the key / key-path exists
  • delete(key, path) delete something
  • clear() clears the whole cache Map
  • all() / values() array of all values
  • entries() array of [key, value]
  • keys() array of all keys
  • ping() shows the ping
  • size() shows the cache-map-size
  • ensure(key, data, path) ensures data in the db

Events for the CACHE-SERVER

  • serverReady shows when the server is ready, you don't need to listen to it!
  • serverError shows when the server errors, you don't need to listen to it!
  • serverClose shows when the server closes, you don't need to listen to it!
  • serverConnect shows when a client connects, you don't need to listen to it!
  • serverDisconnect shows when a client disconnects, you don't need to listen to it!
  • serverMessage receives messages from a cache-client, you don't need to listen to it!
  • serverRequest received requets from a cache-client, used for sending the cache datas, you don't need to listen to it!

Events for the CACHE-Client(s)

  • cacheReady shows when the server is ready, you don't need to listen to it!
  • cacheError shows when the server errors, you don't need to listen to it!
  • cacheClose shows when the server closes, you don't need to listen to it!
  • cacheMessage receives messages from the cache-server, you don't need to listen to it!
  • cacheRequest receives requets from the cache-server, you don't need to listen to it!

Package Sidebar

Install

npm i remote-sqlite-database

Weekly Downloads

1

Version

1.0.8

License

MIT

Unpacked Size

26.9 kB

Total Files

5

Last publish

Collaborators

  • tomato6966