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 🙏

© 2026 – Pkg Stats / Ryan Hefner

generic-entity-management

v1.0.1

Published

Generic entity manager with pluggable strategies (InMemory, TypeORM, SQL adapter) including filters and pagination.

Readme

ManagementEntity (Generic + Strategy Pattern)

Un gestor genérico de entidades en TypeScript que implementa Strategy Pattern para manejar almacenamiento y operaciones CRUD in-memory u otras estrategias personalizadas.

Si no se proporciona una estrategia, se utiliza InMemoryEntityStrategy por defecto.

Características

  • ✅ Genérico: funciona con cualquier entity (ManagementEntity<T>).
  • Strategy Pattern para intercambiar almacenamiento sin cambiar el Manager.
  • ✅ Estrategia por defecto: en memoria.
  • equalsFn opcional para controlar cómo se comparan entidades (por id, por ejemplo).
  • ✅ API simple y consistente.
  • ✅ Fácil de extender (Map, LocalStorage, IndexedDB, API remota, etc.).

Código base

Puedes copiar estos tres bloques en tus archivos o en uno solo si prefieres.

1) Contrato de estrategia

export interface EntityManagementStrategy<T> {
  add(entity: T): void;
  remove(entity: T, equalsFn?: (a: T, b: T) => boolean): boolean;
  getAll(): T[];
  update(oldEntity: T, newEntity: T, equalsFn?: (a: T, b: T) => boolean): boolean;
  count(): number;
  getOne(predicate: (entity: T) => boolean): T | undefined;
  clear?(): void;
}

2) Estrategia por defecto: InMemory

export class InMemoryEntityStrategy<T> implements EntityManagementStrategy<T> {
  private entities: T[] = [];

  add(entity: T): void {
    this.entities.push(entity);
  }

  remove(entity: T, equalsFn: (a: T, b: T) => boolean = Object.is): boolean {
    const index = this.entities.findIndex(e => equalsFn(e, entity));
    if (index === -1) return false;
    this.entities.splice(index, 1);
    return true;
  }

  getAll(): T[] {
    // Copia defensiva
    return [...this.entities];
  }

  update(
    oldEntity: T,
    newEntity: T,
    equalsFn: (a: T, b: T) => boolean = Object.is
  ): boolean {
    const index = this.entities.findIndex(e => equalsFn(e, oldEntity));
    if (index === -1) return false;
    this.entities[index] = newEntity;
    return true;
  }

  count(): number {
    return this.entities.length;
  }

  getOne(predicate: (entity: T) => boolean): T | undefined {
    return this.entities.find(predicate);
  }

  clear(): void {
    this.entities = [];
  }
}

3) Manager genérico

export class ManagementEntity<T> {
  private readonly strategy: EntityManagementStrategy<T>;
  private readonly equalsFn: (a: T, b: T) => boolean;

  constructor(options?: {
    strategy?: EntityManagementStrategy<T>;
    equalsFn?: (a: T, b: T) => boolean;
  }) {
    this.strategy = options?.strategy ?? new InMemoryEntityStrategy<T>();
    this.equalsFn = options?.equalsFn ?? Object.is;
  }

  addEntity(entity: T): void {
    this.strategy.add(entity);
  }

  removeEntity(entity: T): boolean {
    return this.strategy.remove(entity, this.equalsFn);
  }

  getAllEntities(): T[] {
    return this.strategy.getAll();
  }

  updateEntity(oldEntity: T, newEntity: T): boolean {
    return this.strategy.update(oldEntity, newEntity, this.equalsFn);
  }

  getEntityCount(): number {
    return this.strategy.count();
  }

  getOneEntity(predicate: (entity: T) => boolean): T | undefined {
    return this.strategy.getOne(predicate);
  }

  clearEntities(): void {
    this.strategy.clear?.();
  }
}



Uso

Uso básico (in-memory por defecto)

type User = { id: number; name: string };

const usersManager = new ManagementEntity<User>();

usersManager.addEntity({ id: 1, name: "Ana" });
usersManager.addEntity({ id: 2, name: "Luis" });

console.log(usersManager.getEntityCount()); // 2
console.log(usersManager.getAllEntities());

Comparación por id (recomendado para entidades reales)

type User = { id: number; name: string };

const usersManager = new ManagementEntity<User>({
  equalsFn: (a, b) => a.id === b.id,
});

usersManager.addEntity({ id: 1, name: "Ana" });

usersManager.updateEntity(
  { id: 1, name: "Ana" },
  { id: 1, name: "Ana María" }
);

console.log(usersManager.getOneEntity(u => u.id === 1));



Estrategia personalizada (ejemplo Map)

Si quieres otra forma de almacenamiento sin cambiar el manager:

export class MapEntityStrategy<T> implements EntityManagementStrategy<T> {
  private map = new Map<string, T>();

  constructor(private keyFn: (entity: T) => string) {}

  add(entity: T): void {
    this.map.set(this.keyFn(entity), entity);
  }

  remove(entity: T): boolean {
    return this.map.delete(this.keyFn(entity));
  }

  getAll(): T[] {
    return Array.from(this.map.values());
  }

  update(oldEntity: T, newEntity: T): boolean {
    const key = this.keyFn(oldEntity);
    if (!this.map.has(key)) return false;
    this.map.set(key, newEntity);
    return true;
  }

  count(): number {
    return this.map.size;
  }

  getOne(predicate: (entity: T) => boolean): T | undefined {
    for (const value of this.map.values()) {
      if (predicate(value)) return value;
    }
    return undefined;
  }

  clear(): void {
    this.map.clear();
  }
}

Uso:

type User = { id: number; name: string };

const manager = new ManagementEntity<User>({
  strategy: new MapEntityStrategy<User>(u => String(u.id)),
});



API

Método	Descripción	Retorno
addEntity(entity)	Agrega una entidad	void
removeEntity(entity)	Elimina una entidad usando equalsFn	boolean
getAllEntities()	Obtiene todas las entidades	T[]
updateEntity(old, new)	Reemplaza una entidad	boolean
getEntityCount()	Cuenta las entidades	number
getOneEntity(predicate)	Obtiene una entidad por condición	T | undefined
clearEntities()	Limpia usando estrategia si existe clear()	void


Buenas prácticas
	•	Define equalsFn cuando tus entidades tengan id o clave única.
	•	Mantén las estrategias pequeñas y enfocadas a almacenamiento/