4.0.2 • Published 2 years ago

json-atlas v4.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

Json Atlas

simple & quick way to store & manage json data localy.

init

const JsonDataBase = require("json-atlas");
const JsonDb = new JsonDataBase({
  dir: "./myDatabases/", // specify the databases path
});

// quicker way
const JsonDb = new JsonDataBase(); // this method store databases directly into the /node_modules/ dir ( not recommended )

connect to db

// quicker way
const db = JsonDb.connectTo("mydbname");

//or
const db = JsonDb.connectTo({
  dbname: "mydbname", // define database name
  charset: "utf-8", // define the charset
  logs: true, //save all tables opperations, true by default
  logsPath: "./[optional file name]" // set the logs path, the default path is in the database dir
});

module.exports = db;

insert single items

db.items.settings = {
  theme: "dark"
};

get single items

console.log(db.items.settings);

remove single items

db.items.remove("settings");

insert items

// insert one client in the table "clients"
db.table("clients").insert({
  firstname: "John",
  lastname: "Doe"
});

// insert multiple clients at once in the table "clients"
db.table("clients").insert([
  {
    firstname: "John",
    lastname: "Doe"
  },
  {
    firstname: "David",
    lastname: "Doe"
  },
]);

select items

// select items from table "clients"

// select all items
db.table("clients").select();

// select items with a condition
db.table("clients").select(item => item.firstname === "John");

//select attributes
db.table("clients").select(["firstName"]);

//as well
db.table("clients").select(["lastName"], item => item.firstname === "John");

update items

// update items from table "clients"

// select all items
db.table("clients").update(true, {
  firstname: "Johny"
});

// select items with a condition
db.table("clients").update(item => item.firstname === "John", {
  firstname: "Johny"
});

map items

// map all items
JsonDb.table("clients").map(item => {
  return {...item, lastname: item.firstname};
});

// map specifics
JsonDb.table("clients").map(item => item.firstname === "John", item => {
  return {...item, lastname: item.firstname};
});

delete items

db.table("clients").remove(item => item.firstname === "John");

select instances

const clients = db.tables("clients").selectInstances(/*select args*/);
clients.forEach(client => {
  console.log(client); // log class instance
  console.log(client.content()); // log item attributes values
  client.remove(); // remove
  client.update(/*update arg*/);
  client.content.firstname = "David"; // update single attribute
});

callback

//insert
db.table("clients").insert({
  firstname: "John",
  lastname: "Doe"
}, newItem => {
  console.log(newItem); //retuns single item
});

db.table("clients").insert([
  {
    firstname: "John",
    lastname: "Doe"
  },
  {
    firstname: "David",
    lastname: "Doe"
  },
], newItems => {
  console.log(newItems); //retuns array of items
});

//or

const newItem = db.table("clients").insert({
  firstname: "John",
  lastname: "Doe"
}); //retuns single item

const newItems = db.table("clients").insert([
  {
    firstname: "John",
    lastname: "Doe"
  },
  {
    firstname: "David",
    lastname: "Doe"
  },
]); //retuns array of items

//remove
db.table("clients").remove(item => item.firstname === "John", deletedItems => {
  console.log(deletedItems); //retuns array of items
});

//or

const deletedItems = db.table("clients").remove(item => item.firstname === "John"); //retuns array of items

add events to tables

// add one event
db.table("clients").on("insert", (event, data) => console.log(data)); //log the new item
// add multiple events
db.table("clients").on("insert update", (event, data) => console.log(data)); //log the new item
// or
db.table("clients").on("*", (event, data) => console.log(data)); //log the new item

list of events

insert remove update

group result by attribute value

db.table("clients").groupBy("company").select();

join multiple tables

db.table("employees").insert([
  {
    name: "John Doe",
    company: 1
  },
  {
    name: "Charles Roberts",
    company: 2
  }
]);

db.table("companies").insert([
  { name: "Front-End & Co" },
  { name: "Back-End & Co" }
]);

const tableName = "companies";
const alias = "company"; // not mandatory if already defined in the table schema

const employees = db.table("employees").join(tableName, alias, (current, accumulator) => current.company === accumulator._id).select(["name", ["company.name", "company"]]);
console.log(employees);
//
// returns:
//  [
//      { name: 'John Doe', company: 'Front-End & Co' },
//      { name: 'Charles Roberts', company: 'Back-End & Co' }
//  ]
//

clear database

db.dumpAll();

db methods

.use() //use extern plugin
.unuse() //remove extern plugin by passing its name
.dumpAll() //dump each database tables
.delete() //delete database
.dispatcher //get the database dispatcher

table methods

//operations
.insert() //insert elements into the table
.remove() //remove elements from the table
.update() //update elements from the table
.select() //select elements from the table
.map() //map elements from the tables
.selectInstances() //select instances from the table

//misc
.dump() //dump table
.delete() //delete table
.json //get file content in JSON format
.count // get number of items

optional schema (recommended)

// set table schema
db.table("clients").schema = {
  alias: "client",
  unique: false,
  items: {
    firstname: {
      type: "string",
      required: true,
      unique: true
    },
    lastname: {
      type: "string",
      required: true
    },
    company: {
      type: "number",
      default: 1
    }
  }
};

// delete table schema
db.table("clients").schema.delete();
// get schema
db.table("clients").schema();

schema types

null undefined boolean number string list array int float
3.0.1

2 years ago

4.0.1

2 years ago

4.0.0

2 years ago

4.0.2

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

1.0.1

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

3.0.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.0

2 years ago