Skip to content
M ABD AZIZ ALFIAN edited this page Aug 16, 2023 · 114 revisions

Welcome to the fly-json-odm wiki!

Table of Contents


Why should I use this?

TLDR;
Here is the benchmark to do queries with Fly Json ODM.
Most common queries with 1 million object arrays, tested in Real Browser.
Queries with Fly Json ODM

fly-json-odm is the ODM library to handle JSON on the fly like NOSQL does. You are able to make manipulation of JSON like ORM. For example is to do Insert, Read, Update, Modify, Delete, Join, Query and Transform.

Here is the reason why you should use fly-json-odm :

  • Simply handle data table with json object in memory.
  • Query json array with SQL Query logic.
  • Restructuring or make operations in json.
  • Manage memory storage cache.
  • Manipulation dom table with json.
  • Fuzzy Search Support.
  • Flexible and Extendable with Mixin feature.
  • NodeJS, Browser and Typescript support.
  • Very fast to handle big data, see benchmark.
  • Lightweight to use in browser (minified and gzipped size only 4.5 kB).
  • Browser version use ES5 Standard (support for old browser).

Back to top

Getting Started

Install using NPM

$ npm install fly-json-odm

Or simply use in Browser with CDN

<!-- Always get the latest version -->
<!-- Not recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/fly-json-odm/dist/flyjson.min.js"></script>

<!-- Get minor updates and patch fixes within a major version -->
<script src="https://cdn.jsdelivr.net/npm/fly-json-odm@1/dist/flyjson.min.js"></script>

<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/npm/fly-json-odm@1.21/dist/flyjson.min.js"></script>

<!-- Get a specific version -->
<!-- Recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/fly-json-odm@1.21.0/dist/flyjson.min.js"></script>

Usage

const FlyJson = require('fly-json-odm'); // in browser doesn't need this line

var nosql = new FlyJson();

// example data json
var data = [
    {user_id:1,name:'budi',age:10},
    {user_id:5,name:'wawan',age:20},
    {user_id:3,name:'tono',age:30}
];

// Synchronous
var result = nosql.set(data)
  .where('age', '>', 10)
  .orderBy('age', true)
  .exec();
console.log(result);

// Asynchronous
nosql.promisify((builder) => {return builder}).then((table) => {
  var result = table.set(data)
    .where('name', '==', 'wawan')
    .exec();
  console.log(result);
});

Note:

  • Structure Data Table JSON must be an Array which is contains Object like example above.
  • fly-json-odm is synchronous as default.

Back to top


Main Method

  • constructor(mixins={}) - Create instance or with inject your custom function via Mixin. [+ v1.19.0]
  • .setMode(name) - Set mode shallow or deep. Default is deep. [+ v1.7.0]
  • .set(data) - Set data table.
  • .insert(obj) - Insert new single object into data table.
  • .insertMany(obj) - Insert new multiple object into data table. [+ v1.5.0]
  • .update(id,value,obj) - Update single data object in data table.
  • .updateMany(id,value,obj) - Update multiple data object in data table. [+ v1.5.0]
  • .modify(id,value,obj) - Modify or Add new key for single data object in data table.
  • .modifyMany(id,value,obj) - Modify or Add new key for multiple data object in data table. [+ v1.5.0]
  • .delete(id,value) - Delete single data object in data table.
  • .deleteMany(id,value) - Delete multiple data object in data table. [+ v1.5.0]
  • .select(key) - Filter data by select name key.
  • .where(...args) - Filter data by where. Please see comparison-operators.
  • .begin() - Beginning to build query with condition OR.
  • .or() - Add new OR condition.
  • .end() - Ending of build query with condition OR.
  • .distinct(fieldName='') - Remove duplicate data. [+ v1.10.0]
  • .clean() - Cleanup data table and all temporary object.
  • .join(name,data) - Join two data table.
  • .merge(a,b) - Merge two data table.
  • .on(a,b,nested=true,caseSensitive=true) - Set indentifier to joining two data table.
  • .orderBy(name,desc=false,primer) - Sort data ascending or descending by key name (support primer function).
  • .groupBy(name,sumField=[]) - Grouping data or with sum field (sumfield is column name). [+ v1.1.0]
  • .groupDetail(name,groupName='') - Grouping data with detail nested. [+ v1.1.0]
  • .skip(size) - Skip data by size.
  • .take(size) - Take data by size.
  • .paginate(page,page_size) - Paginate data by page and page_size.
  • .fuzzySearch(query,keys,caseSensitive=false,sort=false) - An advanced search like search engine does. Please see Fuzzy Search. [+ v1.19.0]
  • .exec() - Return output of data table.
  • .promisify(fn) - Make asynchronous process with Promise.

Helper Method

  • .isString(value) - Determine value is string.
  • .isInteger(value) - Determine value is integer.
  • .isBoolean(value) - Determine value is boolean.
  • .isArray(value) - Determine value is array.
  • .isObject(value) - Determine value is object.
  • .isEmpty(value) - Determine value is empty.
  • .isEmptyArray(value) - Determine value is empty and array.
  • .isEmptyObject(value) - Determine object value is empty.
  • .fastCheckArrayObject(value) - Determine value is an array object. [+ v1.21.0]
  • .blockingTest(ms=1000) - Blocking execution of code for asynchronous test.
  • .foreach() - Foreach for an array or object. [+ v1.3.0]
  • .safeStringify(value,[space]) - Safe JSON.stringify to avoid type error converting circular structure to json.
  • .shallowClone(array) - Clone an array (shallow).
  • .deepClone(array) - Very safe deep clone an array.
  • .jsonTransform(data, map) - For restructuring and performing operations on JSON. Please see how to use jsonTransform. [+ v1.3.0]
  • .fuzzy(haystack,query='',keys=[],caseSensitive=false,sort=false) - An advanced search like search engine does. Please see Fuzzy Search. [+ v1.20.0]

Back to top


Comparison Operators

Here is the comparison operator to use with .where(...args).
Example:

  • .where('address','jakarta') - Default is ===; Case Sensitive.
  • .where('address','!=','jakarta') - Case Sensitive
  • .where('address','like','Jakarta',false) - Case Insensitive.
  • .where('address','regex',/[a-j]/) - Support Regex.
  • Support custom Function for advanced use.
    .where('name', 'FUNCTION', function (value) {
      // example if we want to searching name for john doe
      return (value.toLowerCase().indexOf('john doe') !== -1);
    })
Name Details Version
= or == Equality
=== Strict Equality
!= Not Equals
!== Strict Not Equals
> Greater than
>= Greater than or equal
< Less than
<= Less than or equal
IN Checks if the value is within an array or object [+ v1.4.0]
IN LIKE Search the value is within an array or object [+ v1.17.0]
NOT IN Checks if the value is not within an array or object [+ v1.11.0]
NOT IN LIKE Search the value is not within an array or object [+ v1.17.0]
NOT Not Equals (same as !=)
LIKE Search the value
NOT LIKE Search the value (opposite)
REGEX Regex search [+ v1.2.0]
FUNC Use function for advanced search (short version) [+ v1.17.0]
FUNCTION Use function for advanced search [+ v1.9.0]

Back to top

Limitation

  • This is not like a client database library, so there is no adapter for connecting to popular database engine like MySQL, SQLite, MongoDB, etc.
  • In this library there is no feature how to read JSON from file, stream or something like that. If you need this, you can see this project fly-json-db.
  • fly-json-odm is using query function style. You can see this project fly-json-ql, if you are interest to do query with JsonQL.

Back to top

Minimum Requirement

Back to top