jdb_net

0.0.1 • Public • Published

jDB Net

Hello, welcome to the documentation of jDB Net. This module is focused on making an efficient database with real time connection.

Area Quick Description
Introduction Have a basic Idea of the module.
Structure Have an idea of how the database looks.
Getting Started Get started using the module.
Server Set-up How to set-up the Server.
Client Set-up How to set-up the Client.
Usage How to use the Module.
Examples Examples using this Module.

Introduction

This module have two sides. The server side that will hold the database, listen and validate requests. and the client side that will send requests to get, add, or delete data. There are permissions based on keys the server makes (or you). This permissions include read (Read the data), write (Add items, change their value), and manage (Delete whole rows and add news). And you can use the same computer as a server and client.

Structure

"collection"{
    "item": "value"
}

Structure the JSON code below is a valid in this database. There are collections. Inside those collections there are items that cointains values.

collection => item => your info
// Example can Be
user => yaxeldragon => subscription => premium

// It supports JSON so you can add as much as you want.

Is fine if you don't get this now. In the practice you will understand it more.

Getting Started

To start using this database type the following in your console

npm install jdb_net

Wait until installed. Then import the module in your project by typing the following in your project

const jDB_Net = require('jdb_net');

Server Set-up

To set up the server is very easy. First construct the client like this

// Imports
const jDB_Net = require('jdb_net');
// Set-up the Server
const server = new jDB_Net.server({
    verbose: true, // Shows ALL Activity. (optional) (default: false)
});
  • Note : The JSON inside the constructor is totally optional. doing new jDB_Net.server(); would work, and have default values.

Then make the server listen for requests like this

server.listen(8080); // 8080: Is the PORT

Once you run this code it will give you a LOGIN KEY with default read: true, write: true, manage: false. You can make more going to etc/keys.json file and following the template.

  • Note : Make sure you have READ, and WRITE, and the IP of the machine you are running the server on.

Client Set-up

To set up the client it is slightly more complicated, and you need the IP, of server, and PORT, and LOGIN KEY. In this example the IP will be 127.0.0.1, the port will be 8080, and the key will be jdb (Not Recommended)

First construct the client like this

// Imports
const jDB_Net = require('jdb_net');
// Set-up the Client
const client = new jDB_Net.client('127.0.0.1:8080', 'jdb', {
    verbose: false // Shows ALL Activity. (optional) (default: false)
});
  • Note : The JSON inside the constructor is totally optional. doing new jDB_Net.client('127.0.0.1:8080', 'jdb'); would work, and have default values.

Then connect by doing

client.connect().then(() => {
    // All your code here.
    // (jDB is made so it waits for the server to connect to execute this.)
    [ ... ]
});

Usage

Command Action
client.createCollection('collection'); Creates a Collection.
client.collectionExists('collection'); Checks if a collection Exists.
client.createItem('collection', 'item/s'); Creates an Item inside a Collection.
client.fetch('collection'); Returns items inside a Collection.
client.setItem('collection', 'item', 'value/s') Sets a value to an existing Item.

Create Collection

client.connect().then(() => {                // Connect
    client.createCollection('collection');   // Create Collection
});

Simply creates a new Collection.

  • Needed Permissions :
    • Manage

Collection Exists

client.connect().then(() => {                // Connect
    client.collectionExists('collection').then(exists => {
        if ( exists == true ){
            console.log('Collection Exists') // Logs "Collection Exists"
        } else {
            console.log('Collection Doesn\'t Exists') // Logs "Collection Doesn't Exists" 
        }
    });
});

Checks if a collection Exists

  • Needed Permissions :
    • Read

Create Item

client.connect().then(() => {                           // Connect
    client.createItem('collection', 'item');            // Add a single Item
                  // OR
    client.createItem('collection', ['item', 'itemB]'); // Add multiple Items
});

Adds an item inside a collection.

  • Needed Permissions :
    • Write

Fetch

client.connect().then(() => {                           // Connect
    client.fetch('collection').then(data => {
        console.log(data);
        // If there are ITEMS you can do :
        console.log(data.item);
    });
});

Looks for all items inside a collection and return it as JSON

  • Needed Permissions :
    • Read

Set Item

client.connect().then(() => {        // Connect
 
    client.setItem('collection', 'item', 'value');
 
        // OR to expand more the Possibilities
 
    client.setItem('collection', 'item', {
        subitem: 'another value, Wow!'
    });
    
});

Set a value to an existing item. (It creates It as well)

  • Needed Permissions :
    • Write

Examples

Creating and Displaying Data

client.connect().then(() => {
    client.collectionExists('users').then(exists => {      // Checks if Collection Exists
        if (exists == true) {                              // ---------------------------
            client.createItem('users', '001');             // Create Items '001' Item in 'users' collection.
            client.setItem('users', '001', {               // Set values to '001' item in 'users' collection.
                plan: 'premium',                           // |  This value
                expires: '12/12/2012'                      // |_ And this value
            });                                            // ---------------------------
            client.fetch('users').then((users) => {        // Fetch all items in 'users' Collection.
                console.log(users['001'].plan);            // Logs "premium"
            });
        }
    });
});

Package Sidebar

Install

npm i jdb_net

Weekly Downloads

1

Version

0.0.1

License

ISC

Unpacked Size

53.5 kB

Total Files

3

Last publish

Collaborators

  • yaxeldragon