npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nut-persister

v1.0.5

Published

a very simple repository manager

Downloads

2

Readme

nut-persister

a very simple repository manager. The aim of this package is wrapping all repositories to prioritize one repository to others. This library provides two functions named get and set.

get function requires function name which retrieves data in your repository like getEvent, getCustomer, etc, and parameters of it. If it finds in first repository, than it return data directly, otherwise when it finds data in second or other repositories, than it requires to call set function for previous repositories one by one.

set function requires function name which write data in your repository. It calls all repositories to write data one by one.

Sample Scenario

Assume that you have Redis, AWS DynamoDB and MySQL repositories. Your master data is always in MySQL database but you don't want to reach always due to performance issue. So you can save in Redis but it is costly in long-term so you should set TTL(Time To Live) to delete some data when it is not used. In this case, you can still think to cache it to have better performance and lower cost so AWS DynamoDB is good option to use. AWS Dynamodb is slower than but faster than database and also cheaper than Redis.

Install

Install the package.

$ npm i -S nut-persister

Usage

Create your repositories like the following code. According to the following code;

index.ts

import { NutPersister } from 'nut-persister';
import { v4 as uuid } from 'uuid';

export interface IEvent {
    id?: string;
    eventType: string;
    data: any;
}

export interface IEventRepository {
    getEvent(id: string): IEvent | undefined;
    setEvent(event: IEvent): IEvent;
}

const createEvent = (event: IEvent): IEvent => ({ id: uuid(), ...event });

export class EventRedisRepository implements IEventRepository {

    constructor(private events: Array<IEvent> = []) { }

    getEvent(id: string): IEvent | undefined {
        return this.events.find(item => item.id === id);
    }
    setEvent(event: IEvent): IEvent {

        const newEvent: IEvent = createEvent(event);

        this.events.push(newEvent);

        return newEvent;
    }
}

export class EventCosmosDbRepository implements IEventRepository {

    constructor(private events: Array<IEvent> = []) { }

    getEvent(id: string): IEvent | undefined {
        return this.events.find(item => item.id === id);
    }
    setEvent(event: IEvent): IEvent {

        const newEvent: IEvent = createEvent(event);

        this.events.push(newEvent);

        return newEvent;
    }
}

Create instances of repositories.

const eventRedisRepository = new EventRedisRepository();

const eventCosmosdbRepository = new EventCosmosDbRepository();

Create instance of Persister.

const eventPersister = new NutPersister<IEventRepository>([eventRedisRepository, eventCosmosdbRepository]);
const eventId = '4e0c71bb-eedf-43dd-b9a1-911a7ed6d74f';
const newEvent: IEvent = { id: eventId, eventType: 'Submission', data: {} };

// it will call setEvent function of eventRedisRepository and eventCosmosdbRepository respectively.
eventPersister.set('setEvent', op => op(newEvent)); 

// this usage is same as above usage, just second parameters requires tuple instead of arrow function.
// eventPersister.setV2('setEvent', [newEvent]);

// it will call getEvent function in eventRedisRepository firstly and find it because set action is already called above line. So, setEvent function will not be called for any repositories.
const result1 = eventPersister.get('getEvent', op => op(eventId), (persister, result) => persister.setEvent(result!), newEvent);

console.log(result1);

// Same behaviour will continue in this line as well.
const result2 = eventPersister.getV2('getEvent', [eventId], (persister, result) => persister.setEvent(result!), newEvent);

console.log(result2);