Coder Social home page Coder Social logo

database's Introduction

My Own Database

  • currently it uses caching only
  • it can be used without auth key yet
  • it's still in development

โš ๏ธ This project is made for experiments and testing; and it's not meant to be used on personal projects as i'm still learning about http module. For now, just use it for fun!

What's New?

  • You can now list keys that have a prefix!

Features I'm working on

  • an authentication url
  • a library based on it
  • rate limit system

How to use it?

First Project (or Repl)

Yes, you can run this code on repl.it too! Infact this project was also made on replit. Follow these steps to create your own webserver for database:

  • Import this code and run it; a webserver will be created.
  • Copy the webserver URL as we're going to use it later.
  • Make a new project with any language you like (keep the webserver on)

Second Project (or Repl)

Now it's time to work on the second repl. Keep it in any language you want; but I'm gonna use Node.js for example. As an http client, I'm using phin.

Initial Code
  • Import the phin library
const phin = require('phin');

I'm going to use webserver_url for the reference of the webserver URL in the upcoming code snippets. Replace that with that of yours.

  • Make an async function
async function Database () {}

We are going to write some code inside this function. In the code snippets below I've repeated same variable name; but you can edit it if you are going to put all code at once.

  • Set a key to a value
const response = await phin({
  method: 'POST',
  url: 'webserver_url/set',
  parse: 'json',
  data: {
    key: 'guild_83726472',
    value: 6000
  }
})

The above code assigns a value to a key. The key can be in the form of string. If we talk about value, you can put strings, booleans as well as objects ! We have set our key successfully. Let's fetch its value now! Wondering what response contains? log it and find out! (Main respose is in response.body)

  • Get a key's value
const response = await phin({
  method: 'GET',
  url: 'webserver_url/get',
  parse: 'json',
  data: {
    key: 'guild_83726472'
  }
}); console.log(response.body.value) // -> 6000

The above code gets value of a key. If you provide an invalid key (means if you didn't assign a value to the key), the response body will return the value as null !

  • Delete a key
const response = await phin({
  method: 'DELETE',
  parse: 'json',
  url: 'https://httpServer.grvcdz.repl.co/delete',
  data: {
    key: 'guild_83726472'
  }
}); console.log(response.body.deleted) // -> true

You can check whether a key is deleted or not by the response body's deleted property. If it is true, then the task is done !

  • List all keys with a prefix (NEW)
phin({
  method: 'GET',
  parse: 'json',
  url: 'https://httpServer.grvcdz.repl.co/list',
    data: {
      prefix: `guild_`,
      showKeys: true
    }
  }).then(res => {
    const arrayOfKeys = res.body.result;
})

The above code is an example of how you can list keys that have a prefix in common. The showKeys option isn't required to write. It is already true by default. If you set showKeys to false, you will get the array of values only.

  • Code So Far
const phin = require('phin');

async function Database () {

  /* Set a key to a value */
  const setkey = await phin({
    method: 'POST',
    url: 'webserver_url/set',
    parse: 'json',
    data: {
      key: 'guild_83726472',
      value: 6000
    }
  });

  /* Get a key's value */
  const getkey = await phin({
    method: 'GET',
    url: 'webserver_url/get',
    parse: 'json',
    data: {
      key: 'guild_83726472'
    }
  }); 
  const value = getkey.body.value;
  console.log(value); // -> 6000

  /* Delete a key */
  const delkey = await phin({
    method: 'DELETE',
    parse: 'json',
    url: 'https://httpServer.grvcdz.repl.co/delete',
    data: {
      key: 'guild_83726472'
    }
  }); 
  const deleted = delkey.body.deleted;
  if (deleted) console.log('deleted!');

  phin({
  method: 'GET',
  parse: 'json',
  url: 'https://httpServer.grvcdz.repl.co/list',
    data: {
      prefix: `guild_`,
      showKeys: true
    }
  }).then(res => {
    const arrayOfKeys = res.body.result;
  });
}

// Call the function 
Database();

Contribution

If you think a feature is broken; or if you want me to add some features, you can open an issue! Make sure to star this repo if you liked my work. Well, I use discord too. If you wanna contact me, you can dm me aka Sxlitude#8885 !

database's People

Contributors

sxlitude avatar

Stargazers

MessyDaMice avatar Devarshi Shimpi 2 avatar

Watchers

 avatar

Forkers

kristianrose

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.