Lo JsonDB
Save your data in .json files and query it with create, delete, update and find. Thanks to lodash :)
Why?
Just for fun.
Thinking about performance, it's not very useful for large amounts of data.
But, jsondb don't need to install anything, and it's easy to start to use.
You can use to prototyping a project or to save a small data.
Getting started
Install:
npm install lo-jsondb --save
Collection:
var jsondb = ;var pokemons = ;
It will use a pokemons.json
file to store data.
If file does not exist, it will be created.
You can create with deep path too, example:
jsondb('my/list/of/pokemons')
Also, you can pass .json directly, if you want for something reason:
jsondb('my/pokemons.json')
{pretty: true}
means that file will be saved in readable json (with tabs)
Pretty shorthand:
var pokemons = jsondb;
Create:
pokemons; pokemons; pokemons;
Don't care about IDS!
JsonDB uses auto incremention to generate ids, like mysql (1, 2, 3...);
Find:
var grassPokemons = pokemons;console;// [ {name: 'Bulbasaur', types: ['grass', 'poison'], id: 3},// {name: 'Exeggcute', types: ['grass', 'psychic'] id: 4} ] var normalPokemons = pokemonsconsole;// [ {name: 'Rattata', ...} ] var normalPokemon = pokemonsconsole;// {name: 'Rattata', ...} var byId = pokemons;console;// {name: 'Pikachu', ...} var byIds = pokemons;console;// [ {name: 'Rattata', ...},// {name: 'Pikachu', ...} ] var byFunc = pokemons;console;// [ {name: 'Rattata', ...},// {name: 'Pikachu', ...} ] var last = pokemons;console;// Exeggcute var lastGrass = pokemons;console;// Exeggcute var first = pokemons;console;// Rattata var firstGrass = pokemons;console;// Bulbasaur
.find()
always return an array of documents. Even if it's an id.
.findOne()
always return the document. If query match more than one, it will return the first only.
.findAll()
is shorthand for collection.find({})
.findLast(query)
is shorthand for _.last(collection.find(query))
.findFirst(query)
is shorthand for _.first(collection.find(query))
You can find with Int (document id), Object (query) or a Function that return an id or query; Also, you can pass an Array with Objects (two or more queries), or array with Ids
Update:
pokemons;// -> {id: 2, name: 'Pika Pika', types: ['electric'], foo: 'bar'} pokemons;// -> {id: 2, foo: 'bar'} pokemons;// -> {id: 3, name: 'Bulbasaur', types: ['grass', 'poison'], foo: 'bar'}// -> {id: 4, name: 'Exeggcute', types: ['grass', 'psychic'], foo: 'bar'} pokemons;// -> {id: 1, name: 'Rattata', types: ['normal'], foo: 'bar'}
You can also query with functions and arrays:
pokemons;// updates Rattata and Pikachu pokemons;// updates Rattata and Pikachu pokemons;// updates Rattata and Pikachu
Delete:
pokemons; // Rattatapokemons; // Rattata and Pikachupokemons; // Bulbasaur and Exeggcutepokemons; // Bulbasaur, Exeggcute and Rattatapokemons; // Pikachu
Save:
pokemons; // updatepokemons; // update identicalpokemons; // create
.save()
is a shorthand of create and update.
Will update if has id
property in query, or create if not.
Accept second boolean argument for identical, in updates.
Others:
var people = ;people; // getLastInsertIdvar lastid = people;console; // 2
File Data:
The file is an object with a settings and data properties:
Empty collection:
With documents:
Single Documents
With single documents, you can create/read json files without settings/data structure:
Example:
var jsondb = ;var settings = jsondb;
settings.json:
*.js
settings;
settings.json:
*.js
var siteName = settings;console; // -> Pokemon
Contributions:
Feel free to send pull requests.
If you add more features, please don't forget to add tests in test/test.js