@microphi/json-db
TypeScript icon, indicating that this package has built-in type declarations

2.7.2 • Public • Published

@microphi/json-db npm version

Small, fast and simple file based json-db super powered by minisearch

Install

With your preferred package manager install

@microphi/json-db

How to use

Basic usage:

interface User {
  id: string;
  name: string;
}

// create a new storage for User
const storage = new JsonStorage<User>('users', '.data');

await storage.upsert({id: '1', name:'superman'});
await storage.upsert({id: '2', name:'batman'});

// getting a document by id
const batman = await storage.get('2');
// updating a document
await storage.upsert({id: '2', name: 'super-batman'});

// get all documents
const docs = await storage.getAll();

// remove a document
await storage.remove('1');

// remove all documents
await storage.deleteAll();

A directory with the name provided (in this case .data) will be created and inside there another folder with the entity name. Data is save in json files as in ${id}.json and an index.json will collect all stored documents metadata such as modified date and create date.

Enable minisearch

To enable minisearch create instantiate a storage class such as

const storage = new JsonStorage<User>('users', '.data', {
  fields: ['name'],
  idField: 'id',
});

The third object in the JsonStorage constructor is the Options object of minisearch which documentation can be found here

JsonStorage uses internally rxjs to queue the operations on the index and so to avoid too many read/write to occur simultaneously. By default index.json is written on disk after 3 second of inactivity. This can be changed using the fourth argument of the constructor specifying the time in milliseconds. Also a fifth argument can be passed to change the default rxjs scheduler (asapScheduler).

Minisearch

minisearch functionalities are available through the search field.

storage.search.search('batman', { fields: ['name'] });

Remember documents are added/updated/removed to minisearch automatically. For more info on minisearch please consult their repo.

Readme

Keywords

Package Sidebar

Install

npm i @microphi/json-db

Weekly Downloads

103

Version

2.7.2

License

MIT

Unpacked Size

145 kB

Total Files

17

Last publish

Collaborators

  • davidecavaliere