Skip to content
M ABD AZIZ ALFIAN edited this page Nov 4, 2019 · 21 revisions

Welcome to the fly-json-db wiki!

Table of Contents

Getting Started

For the first time, you have no any data, so we going to insert a data first.

const DB = require('fly-json-db');

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

// Saving above json array into database
var nosql = new DB();
  nosql.loadArray(data1).then(data => {
    nosql.save('path/to/file.nosql').then(data => {
      console.log(data);
    });
  });

Back to top

API

odm

This is an Object Document Mapper for json.
We use fly-json-odm for CRUD, Query, Join and Transform support.
Please see the documentation on their Wiki.

Example:

nosql.loadArray(data1).then(data => {
  // start query
  var result = nosql.odm.select(['user_id','name','age']).where('age','>',10).exec();
  console.log(result);
});

Back to top

loadArray(array, callback)

Load an array into odm.

  • array - this is the json array.
  • callback(error, data) - If you are not using callback, then will return promise. [optional]

Example

nosql.loadArray(data1).then(data => {
  console.log(nosql.odm.exec());
}, error => {
  console.log(error);
});

Back to top

loadFile(filePath, callback)

Load json from file into odm.

  • filePath - this is the file path of file json.
  • callback(error, data) - If you are not using callback, then will return promise. [optional]

Example:

nosql.loadFile('path/to/file.nosql').then(data => {
  console.log(nosql.odm.exec());
});

Back to top

joinArray(array, name, callback)

Load an array data for join into odm.

  • array - this is the json array.
  • name - join identifier name.
  • callback(error, data) - If you are not using callback, then will return promise. [optional]

Example:

nosql.loadFile('path/to/file.nosql').then(result => {
  nosql.joinArray(data1,'profile').then(data => {
    // example join on
    console.log(nosql.odm.on('user_id','id').exec());
  });
});

Note:

  • profile is the identifier join name.
  • user_id is the field name for join on.
  • id is the field name for join on from second table.

Back to top

joinFile(filePath, name, callback)

Load json from file for join into odm.

  • filePath - this is the file path of file json.
  • name - join identifier name.
  • callback(error, data) - If you are not using callback, then will return promise. [optional]

Example:

nosql.loadArray(data1).then(result => {
  nosql.joinFile('path/to/file.nosql','profile').then(data => {
    // example join merge
    console.log(nosql.odm.merge('user_id','id').exec());
  });
});

Back to top

save(filePath, callback)

Save odm result into file.

  • filePath - this is the file path for saving json into file.
  • callback(error, data) - If you are not using callback, then will return promise. [optional]

Example:

nosql.loadArray(data1).then(data => {
  nosql.save('path/to/file.nosql').then(data => {
    console.log(data);
  });
});

Back to top

drop(filePath, callback)

Drop or delete file json.

  • filePath - this is the file path of file json.
  • callback(error, data) - Callback will show error or data status. [optional]

Example:

nosql.drop('path/to/file.nosql',function(err,data) {
  if(err) return console.log(err);
  console.log(data);
});

Back to top

Clone this wiki locally