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

motifs

v1.3.6

Published

Library for implementation of certain patterns like Repository

Downloads

1

Readme

motifs

Library for implementation of certain patterns (motifs) like the following.

Caching Pattern

Ref: https://docs.microsoft.com/en-us/azure/architecture/patterns/cache-aside

export interface ICacher {
  setItem                  (key: string, val: unknown, expiryMs: number): Promise<boolean>;
  getItem<T= unknown>      (key: string)                                : Promise<T>;
  delItem                  (key: string)                                : Promise<boolean>;
  getItemsLike<T = unknown>(keyPrefix: string)                          : Promise<Record<string, T>>;
  getKeys                  (search: string)                             : Promise<string[]>;
}

Observer (Message Listener) Pattern

We are expecting to listen to a channel and messages will be sent in a particular type.

export interface IListener<T = unknown> {
  channelId: string;
  em       : Emittery; // internal event manager

  listen(): Promise<void>;
  onMessage(handler: IListenerMessageHandler<T>): void;
  onError(handler: IListenerErrorHandler): void;
}
export interface IListenerMessageHandler<T = unknown> {
  (msgObj: T): Promise<void>;
}
export interface IListenerErrorHandler {
  (err: unknown): Promise<void>;
}

Repository Pattern

Ref: https://martinfowler.com/eaaCatalog/repository.html

We are expecting to run simple CRUD operations on a "collection" of objects, table of records or document store.

export interface IRepo<T extends IBaseDto = IBaseDto> {
  name: string;
  em  : Emittery; // internal event manager

  findMany (conditions: IFlatObject)    : Promise<T[]>;
  create   (dto: Partial<T>)            : Promise<T>;
  retrieve (id: string)                 : Promise<T>;
  update   (id: string, dto: Partial<T>): Promise<T>;
  delete_  (id: string)                 : Promise<boolean>;
}

Examples

Sample usage for cacher, repo and listener/observer:

import express from 'express';
import { IBaseDto, makeCacher, makeListener, makeRepo, MotifsErrorNotFound } from 'motifs';

main();

async function main() {
  const app = express();

  app.use(express.json());

  const cacheExpiry10Mins = 10 * 60 * 1000;

  const contactCacher = await makeCacher({ kind: 'memory' });

  const contactRepo = await makeRepo<IContact>({ kind: 'memory', name: 'contacts' });

  contactRepo.em.on('contacts.create.after', async ({ dto }) => {
    console.info('contact created', dto);
  });

  const contractListener = await makeListener<IAlienContactCreated>({ channelId: 'mars', kind: 'kafka', kafka: { url: 'localhost:9092' } });

  contractListener.onMessage(async (msg: IAlienContactCreated) => {
    console.info('new contact', msg);
  });
  contractListener.listen();

  app.post('/contacts', (req, res) => {
    const contact: IContact = req.body as IContact; // TODO: avoid pretending, validate
    const data = await contactRepo.create(contact);
    res.json({ data });
  });

  app.get('/contacts/:id', (req, res) => {
    const { id } = req.params;
    const key = `contacts/${id}`;
    try {
      const cached = await contactCacher.getItem<IContact>(key);
      return res.json({ data: cached });
    } catch (err) { // cache miss?
      try {
        const contact = await contactRepo.retrieve(id);
        await contactCacher.setItem(key, contact, cacheExpiry10Mins); // cache aside
        res.json({ data: contact });
      } catch (err) {
        if (err instanceof MotifsErrorNotFound) {
          res.status(404),json({ error: 'not found' });
        } else {
          console.error(err);
          res.status(500).json({ error: 'server error' });
        }
      }
    }
  });

  app.listen(8080);
}

interface IContact extends IBaseDto {
  firstName: string;
  lastName:  string;
  dob:       string;
}

interface IAlienContactCreated {
  id?    : string;
  planet?: string;
}