# memorystore.js

> MemoryStore.js

Latest version **1.0.0** (published 2018-12-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install memorystore.js
pnpm add memorystore.js
yarn add memorystore.js
bun add memorystore.js
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-12-29 |
| First published | 2018-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | pacifio |
| Maintainers | pacifio |
| Keywords | json, json-db, fs, database |

## Links

- npm: https://www.npmjs.com/package/memorystore.js
- Repository: https://github.com/pacifio/memorystore
- Homepage: https://github.com/pacifio/memorystore#readme
- Issues: https://github.com/pacifio/memorystore/issues
- npm.io page: https://npm.io/package/memorystore.js

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.11

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 1.0.0 (latest) — 2018-12-29

## README

<h2 style="text-align:center;" >MemoryStore.js</h2>
<br>

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](#installation)
- [Usage](#usage)

## Installation
```bash
npm i memorystore.js --save
yarn add memorystore.js
```
## Usage

First init the main constructor
```javascript
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 data](#add)
- [List data](#list)
- [Remove data](#remove)
- [Update data](#update)

## 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 .

```javascript
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 .
```javascript
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 <code>Some demo title</code> as the content for field title . I got that data back with it's ID .
```javascript
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
```javascript
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
```javascript
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
```javascript
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
```javascript
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
```javascript
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](https://github.com/pacifio/)

---
_Source: https://npm.io/package/memorystore.js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
