1.0.0 • Published 5 years ago

camelbase v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

CamelBase

A simple, json-based, local database for Node.js projects.

Usage

Install with npm

npm i camelbase

Start a local database

const CamelBase = require("camelbase");

const db = new CamelBase("./db.json");

db.default({
  pokemons: {
    "1": {
      name: "Bulbasaur",
      types: ["Grass"],
    },
    "880": {
      name: "Dracozolt",
      types: ["Eletric", "Dragon"],
    },
  },
});

db.save();

This will create a default database

{
  "pokemons": {
    "1": {
      "name": "Bulbasaur",
      "types": ["Grass"]
    },
    "880": {
      "name": "Dracozolt",
      "types": ["Eletric", "Dragon"]
    }
  }
}

Operations

  • db.get()
  • db.value()
  • db.add()
  • db.set()
  • db.delete()

db.get()

this will create a reference for the next method

db.get("pokemons").get("880"); // will make a reference

db.value()

will return the value inside the last db.get()

const d = db.get("pokemons").get("880").value(); //

console.log(d); // {name: Dracozolt, types: ["Eletric", "Dragon"]}

const types = db.get("pokemons").get("1").get("types").value();

console.log(types); // ["Grass"]

In camelBase database, every collection is a key-value pair of id and data.

db.add() receives a object containing a id and a data, to add to the database.

if no id property is given, the database will generate a random id.

db.get("pokemons").add({
  id: "637",
  data: { name: "Volcarona", types: ["Bug", "Fire"] },
});

db.save();

Now the database is

{
  "pokemons": {
    "1": {
      "name": "Bulbasaur",
      "types": ["Grass"]
    },
    "637": {
      "name": "Volcarona",
      "types": ["Bug", "Fire"]
    },
    "880": {
      "name": "Dracozolt",
      "types": ["Eletric", "Dragon"]
    }
  }
}

user db.set() to change a database object

db.get("pokemons").get("1").set({ evolution: "Ivysaur" });

Now the database is

{
  "pokemons": {
    "1": {
      "name": "Bulbasaur",
      "types": ["Grass"],
      "evolution": "Ivysaur"
    },
    "637": {
      "name": "Volcarona",
      "types": ["Bug", "Fire"]
    },
    "880": {
      "name": "Dracozolt",
      "types": ["Eletric", "Dragon"]
    }
  }
}

If you need to delete something, use db.delete

db.get("pokemons").get("880"); // deletes Dracozolt
db.get("pokemons").delete(); // deletes all pokemons

Example: Updating every pokemon

const CamelBase = require("./src/camelBase");

const db = new CamelBase("./db.json");

const pokemons = db.get("pokemons").value();

Object.keys(pokemons).forEach((pokemon) => {
  pokemons[pokemon].level = 1;
});

db.get("pokemons").set(pokemons);

db.save();
1.0.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago