Skip to content

Latest commit

 

History

History
261 lines (181 loc) · 6.2 KB

documentation.md

File metadata and controls

261 lines (181 loc) · 6.2 KB

Table of Contents

isValidJson

Parameters

  • string string The string to check

Returns boolean Return wether the string is valid JSON

isValidDBON

Parameters

  • obj Object The Object to check

Returns boolean Return wether the string is valid DBON

newDatabase

Creates a valid mode 1 database for Databasetify

Parameters

  • name string Name you want to give to the database
  • path string Path of the JSON file for the database

Databasetify

Main databasetify class. Opens a file as a database, performs operations on it adding or deleting elements and finding for data

Parameters

  • path string Path of the json file to databasetify.
  • mode number 0 or 1. 0 opens the file in simple mode, 1 opens the file in strict mode.

addTable

Adds a table to the currently open database.

Parameters

  • name string Name of the table.
  • columns Array<column> Array of columns. Check the right structure in the documentation.

insert

Adds a value to an existent table.

Parameters

  • tableName string Name of the table where you want to insert values
  • key string Name of the key.
  • value Object<string, any> Object of values, with columns' names as keys

set

Set a different value given the table, the key and the column names.

Parameters

  • tableName string Name of the table where you want to insert values
  • key string Name of the key to modify
  • column string Name of the column
  • value any Value to set at given table, key and column

removeKey

Removes the specified key in the specified table

Parameters

  • tableName string Name of the table where you want to remove key
  • key string Key to remove

get

Get a value from the database, given the table name, the key and the column

Parameters

  • tableName string Table of the value you want to get
  • key string Key of the value you want to get
  • column string Column of the value you want to get

Returns any? Value at specified Table, Key and Column, if it exists. If not, returns null

find

Returns the first element that makes the finder function true

Parameters

  • tableName string Name of the table where you want to search
  • finder finder The function to filter all the values

Returns foundValue Object with the value, the key, the column and the counter relatives to the value

findAll

Returns all the elements that makes the finder function true

Parameters

  • tableName string Name of the table where you want to search
  • finder finder The function to filter all the values

Returns Array<foundValue> Array of Objects with the value, the key, the column and the counter relatives to the value

Database Modes

Mode 0

A simple, empty JSON file is valid for mode 0. Every table is a key in the JSON root and its value is an object. For each key of the table, there is a corresponding object, with as keys the columns of the tables and as values the corresponding values. It does not check for eventual errors, it just works.

{
    "table1": {
        "key1": {
            "col1": "value1",
            "col2": "value2",
            "col3": "value3"
        },
        "key2": {
            "col2": "value2",
            "col4": "value3"
        }
    }
}

Mode 1

For mode 1, you need a Databasetify structured object. This is the structure:

{
    "name": "databaseName",
    "tableCount": 1,
    "tables": [
        {
            "name": "tableName",
            "cols": ["col1", "col2"]
            "numOfCols": 2,
            "keys": ["key1", "key2"],
            "numOfKeys": 2,
            "values" [
                ["value1", "value2"],
                ["value1", "value2"]
            ]
            "isRelational": false,
            "relations": [
                null,
                null
            ]
        }
    ]
}

It may seem very complex, but you can create an empty mode 1 database writing:

{
    "name": "databaseName",
    "tableCount: 0,
    "tables": []
}

Or simply using the function newDatabase (for this, you can use the Node console)

const databasetify = require("databasetify");
databasetify.newDatabase("name", "path/to/db.json");

This mode checks for eventual structure errors.