1.0.1 • Published 6 years ago

flexidbjs v1.0.1

Weekly downloads
4
License
-
Repository
-
Last release
6 years ago

Flexi DB

A flexible database server written in NodeJS. Flexible, fast and efficient. You can run your queries, manipulate data and return results all in JavaScript. No SQL language, just pure JavaScript. This server runs via NodeJS and you can connect to the server over HTTP.

Need a GUI Client?

Head over to the FlexiDB Client repository to download the latest release. https://github.com/mrbenosborne/FlexiDB-Client

Installation / How to

  1. Download NodeJS and install it on your server if you have not already got it.

DEV

  1. Clone this repository into a directory of your choice with the command below, this is for the continuous development releases:

    git clone https://github.com/mrbenosborne/Flexi-DB.git .

Stable

If you want the stable release head over to our Releases part of this repository:

https://github.com/mrbenosborne/Flexi-DB/releases

  1. Navigate to the directory where you cloned this repository and run the following command:

    npm install

  2. Start the server with this command:

    node main.js

That's it!

Your FlexiDB Server should now be up and running.

JavaScript Query Syntax, built-in functions and commands

When writing your queries in JavaScript you are free to do whatever you want, that is why FlexiDB is so powerful, it's flexible and fast allowing you to create any data set you desire.

  1. Query Syntax
  2. Accessing the data pool
  3. Create a collection
  4. Remove a collection
  5. Insert an object into a collection
  6. Insert an array of objects into a collection (Bulk insert)
  7. Update an object in a collection
  8. Remove/Delete an object or objects from a collection

Query Syntax

When returning a data set it is as simple as returning an array of JavaScript objects. The below example will return an array of JavaScript objects where my_collection is the a collection (a JavaScript array of Objects).

return $.data_pool.my_collection;

The above would return (JSON String):

[{"name": "object 1"},{"name": "object 2"}]

Built-In Functions

Accessing the data pool

The data pool is where all of your collections live and are accessible in any query you execute, the below example shows how to return all of the objects in a single collection.

Syntax

return $.data_pool.{collection_name};

Example

return $.data_pool.myfirstcollection;

What about if we wanted to use multiple collections in one query? Well with FlexiDB you have the power of JavaScript in your hands, below we use 2 collections.

Example

// Grab our airlines collection
let airlines = $.data_pool.airlines;

// Grab our flights collection
let flights = $.data_pool.flights;

// If we wanted to we can filter down flights based on an airline for example, let's say British Airways.
let data_set = [];

for(let i = 0; i < flights.length; i++){

    if(flights[i].airline == 'British Airways'){
        
        // Add this flight to our data_set
        data_set.push(flights[i]);
        
    }

}

// Return our data set
return data_set;

Create a collection

The command to create a collection is below, the function returns 0 for False or 1 if True. The parameters required is the collection name.

Syntax

$.collection( {collection_name} ).create();

Example

let result = $.collection('mycollection').create();

Remove a collection

The command to remove a collection is below, the function returns 0 for False or 1 if True. The parameters required is the collection name.

Syntax

$.collection( {collection_name} ).remove();

Example

let result = $.collection('mycollection').remove();

Insert an object into a collection

This command inserts a JavaScript Object into a collection. To do bulk inserts take a look at the insert_bulk command.

Syntax

$.collection( {collection_name} ).insert( { object } );

Example

let new_row = {
    name: "Object 1",
    time_added: new Date()
};

$.collection('mycollection').insert( new_row );

Insert an array of objects into a collection (Bulk insert)

This command will boost performace by far if you are inserting many objects into a collection. An example of how fast this command would be compared to the normal insert command, 10,000 objects were inserted using the insert command, this took 82 seconds whilst the insert_bulk took only 32ms.

Syntax

$.collection( {collection_name} ).insert_bulk( { array of objects } );

Example

let new_rows = [];

for(let i = 0; i < 10000;i++){
    new_rows.push({
        name: "Object " + i
    });
}

$.collection('mycollection').insert_bulk( new_rows );

Update an object in a collection

This command updates an object with the data sent in the first parameter, you do not need to send all of the fields but only the fields you would like to update in the given object.

Syntax

$.collection( {collection_name} ).update( {update_data: object}, {filter: object} );

Example

// First, let's create a collection
$.collection('countries').create();

// Insert 2 objects
$.collection('countries').insert_bulk([
    {
        name: "United States",
        code: "US"
    },
    {
        name: "United Kingdom",
        code: "US"
    }
]);

// As you can see from the above, we have set the code to be "US" for both countries.
// Let's run the update command, the first parameter is the data we are updating, the second parameter is the filter.
$.collection('countries').update(
    {
        code: "GB"
    },
    {
        name: "United Kingdom" // Where object.name is equal to "United Kingdom"
    }
);

// Return the countries
return $.data_pool.countries;

Remove/Delete an object or objects from a collection

This command removes an object or objects from a collection.

Syntax

$.collection( {collection_name} ).remove_object( {filter: object} );

Example

/*

    Let's say for example we have 2 items in our collection, each object has an id field and we want to delete the object where id
    is equal to 2.

*/

let success = $.collection('countries').remove_object( { id: 2 } );