memorystore.js v1.0.0
Simple way to store your data inside a raw .json file . Built on top of the node.js , the filesystem and lodash library . You can use different json files as models . Again this needs more work and has some experimental methods available such as realtime methods and events ...
it only works on backend / pure node apps such as express / electron / API apps etc etc ...
Table of contents
Installation
npm i memorystore.js --save
yarn add memorystore.js
Usage
First init the main constructor
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json', { // Options is not required
truncate: false // The truncate option is by default false
})
Add
Callback returns two parameters . First one is error and the second one is data itself . The data is schemaless and have an ID field .
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.add({ title: 'Some demo content' }, function (err, data) { //ID IS AUTO GENERATED
if(err) { console.error(err) } else {
console.log(data); // data return { title: 'Some demo content', id: '3de422e44ed0bdde' }
}
})
List
You can list all data or find a specific data . Here is a list of som useful methods .
List all
The listAll() function takes only a function as it's parameter and returns all the data as an array . Here is an example below .
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.listAll(function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an array [{ title: 'Some demo content', id: '3de422e44ed0bdde' }, ...]
}
})
Find
EXPERIMENTAL The find() function takes two parameters and then returns that object / multiple objects in an array responding / matching that specific object . An example is shown below . Here I searched for a data which has Some demo title as the content for field title . I got that data back with it's ID .
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.find({title: 'Some demo content'} ,function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an object { title: 'Some demo content', id: '3de422e44ed0bdde' } or {} for nothing
}
})
Find by ID
This function returns only one object responding to the matching ID
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.find('3de422e44ed0bdde' ,function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an object { title: 'Some demo content', id: '3de422e44ed0bdde' } or {} for nothing
}
})
Delete
You can delete some specific data using some useful methods exposed by this package
Delete by finding
EXPERIMENTAL This function deletes an object / an array of objects responding the where object please try to use the deleteById method as it's not fully functional yet / can be buggy
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.delete({title: 'Some demo content'} ,function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an object { title: 'Some demo content', id: '3de422e44ed0bdde' } or {} for nothing
}
});
Delete by ID
This function deletes an object responding the ID
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.deleteById('3de422e44ed0bdde' ,function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an object { title: 'Some demo content', id: '3de422e44ed0bdde' } or {} for nothing
}
})
Update
You can update some specific data using some useful methods exposed by this package
Update by finding
EXPERIMENTAL This method updates the object in the main array responding the where object
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.update({title: 'Some demo content'} ,{ title: 'Demo content updated' },function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an object { title: 'Demo content updated', id: '3de422e44ed0bdde' }
}
});
Update by ID
This function updates an object responding the ID
const MemoryStore = require('memorystore.js');
const memory = new MemoryStore('./data.json') // PATH TO JSON FILE
memory.updateById('3de422e44ed0bdde' ,{ title: 'Demo content updated' }, function (err, data) {
if(err) { console.error(err) } else {
console.log(data); // data return an object { title: 'Demo content updated', id: '3de422e44ed0bdde' } or {} for nothing
}
})
MIT License . Created by Adib Mohsin
5 years ago