lowdb-crud

1.0.5 • Public • Published

lowdb-crud

Installation

npm install lowdb-crud

or

yarn add lowdb-crud

import or require

import { LowdbCrud } from 'lowdb-crud'

or

const { LowdbCrud } = require('lowdb-crud')

Documentation

Table of Contents

LowdbCrud

LowdbCrud provides create, read, update, delete (CRUD) operations on a json data structure that resembles a database table. LowdbCrud has 7 methods: create(), read(), update(), upsert(), delete(), list_table(), and delete_table(). LowdbCrud is a wrapper of lowdb that stores all data in one json file that can represent multiple tables. LowdbCrud uses a data object that stores all data (tables). Each key of the object represents a table name. The data of each table is stored in an array of objects. The table data structure example below shows the data structure of 2 tables (person and phone). Additionally, the database object, data object, and the table object (see syntax example bellow) can be manipulated directly by using lowdb and lodash.

Parameters

  • param_obj Object {'db_file': 'full_path'}

Examples

Sample data:
    const person_list = [{"name":"Alexa", "age": 35},
                         {"name":"Drew", "age": 21}]

    const phone_list =  [{"home":"234-567-8901", "cell":"234-567-8900"},
                         {"home":"234-789-8901", "cell":"234-789-8900"}]
Instantiate a LowdbCrud object:
      import { LowdbCrud } from 'lowdb-crud'
      const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
Adding data:

  let param_obj = {'table_name': 'person', 'row_obj_list': person_list}
  const person_uuid_list = my_obj.create(param_obj)

  param_obj = {'table_name': 'phone', 'row_obj_list': phone_list}
  const phone_uuid_list = my_obj.create(param_obj)
my_obj.attr.db_obj.data has the following data:
    {
      "person":[{"uuid": "sUQT2zDUEGPEWMKxkbuKff", "name":"Alexa", "age": 35},
                {"uuid": "cTAA6KsiP4kjRvwDKZbMTL", "name":"Drew", "age": 21}],
      "phone":[{"uuid": "o3wFgSyu8XALCMDQCSu1x8", "home":"234-567-8901", "cell":"234-567-8900"},
               {"uuid": "hrFiDi8wdSqCfEHrcbmTJf","home":"234-789-8901", "cell":"234-789-8900"}]
     }
my_obj.attr.db_obj.data['person'] has the following data:
     [{"uuid": "sUQT2zDUEGPEWMKxkbuKff", "name":"Alexa", "age": 35},
{"uuid": "cTAA6KsiP4kjRvwDKZbMTL", "name":"Drew", "age": 21}]

   my_obj.attr.db_obj.data['phone'] has the following data:
     [{"uuid": "o3wFgSyu8XALCMDQCSu1x8", "home":"234-567-8901", "cell":"234-567-8900"},
      {"uuid": "hrFiDi8wdSqCfEHrcbmTJf", "home":"234-789-8901", "cell":"234-789-8900"}]
The database object and data object can be manipulated using lowdb and lodash:
      my_obj.attr.db_obj
      my_obj.attr.db_obj.data

create

The create() method inserts one or more rows of object into a table. This method returns an array of uuid automatically created for each inserted row.

Parameters
  • param_obj Object {'table_name': 'string', 'row_obj_list': array}
Examples
const person_obj_list = [{"name":"Alexa", "age": 35},
                       {"name":"Drew", "age": 21}]

  const param_obj = {'table_name': 'person',
                     'row_obj_list': person_obj_list}

  const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const uuid_list = my_obj.create(param_obj)
  // Note: uuid_list looks like ["vXJUGH55tNo2iP91eUYLpk", "siysrj3q2PWn8EFX7rP5SL"}]

Returns array An uuid array of created rows.

read

The read() method retrieves one or more rows of object from a table. This method returns an array of row objects based on value_filter_obj and col_select_list parameters. if value_filter_obj is not given, all rows of the table are retrieved. if col_select_list is not given, all columns of each row is retrieved.

Parameters
  • param_obj object {'table_name': 'string', 'value_filter_obj': object} 'col_select_list': array}
Examples
const param_obj = {'table_name': 'person',
                     'value_filter_obj': { 'age': 21 },
                     'col_select_list': ['name']}

  const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const row_obj_list = my_obj.read(param_obj)
  // Note: row_obj_list is [{"name":"Drew"}]

Returns array An row object array that looks like [{"name":"Drew", "age": 21}].

update

The update() method changes the value(s) of selected row(s), base on value_filter_obj, to the values of the update_obj parameter. This method returns an array of uuid of the updated row(s).

Parameters
  • param_obj Object {'table_name': 'string', 'row_obj_list': array}
Examples
const param_obj = {'table_name': 'person',
                     'value_filter_obj': { 'name': 'Drew' },
                     'update_obj': { 'name': 'Andrew' }}

  const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const uuid_list = my_obj.update(param_obj)
  // Note: uuid_list looks like ["vXJUGH55tNo2iP91eUYLpk"}]

Returns array An uuid array of updated rows.

upsert

The upsert() method calls update() method. If the target row is not found, it calls create() method inserting the values of update_obj parameter as a new row. This method returns an array of uuid of the updated or inserted row(s).

Parameters
  • param_obj Object {'table_name': 'string', 'row_obj_list': array}
Examples
const param_obj = {'table_name': 'person',
                     'value_filter_obj': { 'name': 'Drew' },
                     'update_obj': { 'name': 'Andrew' }}

  const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const uuid_list = my_obj.upsert(param_obj)
  // Note: uuid_list looks like ["vXJUGH55tNo2iP91eUYLpk"}]

Returns array An uuid array of updated or inserted rows.

delete

The delete() method removes selected row(s) from a table base on value_filter_obj parameter. This method returns an array of uuid of the deleted row(s). If the value_filter_obj parameter is not given or empty, all rows of the table will be deleted.

Parameters
  • param_obj Object {'table_name': 'string', 'value_filter_obj': array}
Examples
const param_obj = {'table_name': 'person',
                   'value_filter_obj': { 'name': 'Drew' }}

  const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const uuid_list = my_obj.delete(param_obj)
  // Note: uuid_list looks like ["vXJUGH55tNo2iP91eUYLpk"}]

Returns array An uuid array of deleted rows.

list_table

The list_table() returns an array of exiting table names.

Parameters
  • param_obj
Examples
const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const uuid_list = my_obj.list_table()
  // Note: table names looks like ["person", "address"]

Returns array An array table names.

delete_table

The delete_table() method removes selected table from the data object base on table_name parameter. This method returns the deleted table_name or null if the table name does not exist.

Parameters
  • param_obj Object {'table_name': 'string'}
Examples
const param_obj = {'table_name': 'person'}
  const my_obj = new LowdbCrud({'db_file':'/home/my-app/data.json'})
  const table_name = my_obj.delete(param_obj)
  // Note: table_name is 'person'

Returns string The deleted table name.

Package Sidebar

Install

npm i lowdb-crud

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

388 kB

Total Files

8

Last publish

Collaborators

  • saun4app