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

data-provider-core

v1.1.16

Published

This is an abstractions for data-provider in the app

Downloads

49

Readme

@positional-advantage-libs/data-provider-core

A framework-agnostic TypeScript library for creating a backend-agnostic data access layer. It provides a set of core contracts that allow you to completely decouple your application's business logic from its data source.

The primary goal of this library is to remove direct dependency of your project on data provider of you choice.

Core Concepts

The architecture is built on three primary abstractions:

  1. Entity: A base interface that describes any data model in your system (e.g., a Todo or a User).
  2. EntityConverter: A "translator" that knows how to convert data between a plain object (from the backend) and an instance of your entity class, and vice-versa.
  3. DataProvider: The main contract that defines the operations that can be performed on your data (getEntity, listenToCollectionChanges, etc.). You implement this contract to connect to a specific data source.

Dependencies

  • rxjs
  • typescript

Installation

npm install data-provider-core rxjs

API Reference

This library exports three primary abstractions that form the core of the pattern.

Entity

Entity<ID> - A fundamental interface that all of your domain models must implement.
    
export interface Entity<ID> {
  id: ID;
  typeKey: string;
}

- id: The unique identifier for the entity.
- typeKey: A static string discriminator (e.g., 'TODO') used by the system to dynamically find the correct EntityConverter for a given object.
    
## Example:
class Todo implements Entity<string> {
    public static typeKey = 'TODO';
    public typeKey: string;
    public id!: string;
    public name!: string;

    constructor({id, name}: Omit<Todo, 'typeKey'>) {
        this.typeKey = Todo.typeKey;
        this.id = id
        this.name = name;
    }
}

EntityConverter

EntityConverter<ID, T> - An abstract class responsible for the serialization and deserialization of your models.
    You must provide a concrete implementation for each entity.
    
abstract class EntityConverter<ID = string, T extends Entity<ID> = Entity<ID>> {
    abstract toPlainObject(entity: T): object;
    abstract fromPlainObject(value: any): T | null;
    abstract validateCreation(value: any): boolean;
}

interface EntityConverterConfig {
    typeKey: string;
    converter: EntityConverter<any, Entity<any>>;
}

- toPlainObject(entity) - Converts a class instance into a plain object suitable for storage.
- fromPlainObject(value) - Converts a raw object from the data source into a new class instance.
- validateCreation(value) - A gatekeeper method that validates a raw object before conversion.
    
## Example:
    class TodoConverter implements EntityConverter<string, Todo> {
        public toPlainObject(todo: Todo): Todo {
            return {id: todo.id, typeKey: Todo.typeKey, name: todo.name};
        }

        public fromPlainObject(value: Todo): Todo | null {
            if (!this.validateCreation(value)) {
                console.error(`Error during creation validation for TODO, value:`, value);
                return null;
            }

            return new Todo(value);
        }

        public validateCreation(value: Todo): boolean {
            return !!(value.id && value.typeKey === Todo.typeKey);
        }
    }

    export const TodoConverterConfig: EntityConverterConfig = {
        typeKey: Todo.typeKey,
        converter: new TodoConverter()
    }

DataProvider

DataProvider - The central abstract class that serves as the contract for your data access layer. This is the class you will implement to connect to a specific data source.
    abstract class DataProvider {
        public abstract getEntity<T extends Entity<string>>(path: string): Observable<T | undefined>;
        public abstract listenToCollectionChanges<T extends Entity<string>>(path: string): Observable<T[]>;
        public abstract convertIntoEntity<T extends Entity<string>>(rawObject: any): T | undefined;
        protected abstract converterMap: Map<string, EntityConverter<any, any>>
    }
    
    - getEntity(path) - Get entity once, returns an Observable with the item
    - listenToCollectionChanges(path) - Listens to collection of entities
    - convertIntoEntity(rawObject) - Converts simple objects in domain level Entites
    - converterMap - map of converters, for example { 'TODO': TodoConverter }

    Example:
      @Injectable({
        providedIn: 'root'
      })
      class FirestoreDataProviderService implements DataProvider {
        constructor(
                private firestore: Firestore,
                @Inject(ENTITY_CONVERTER_MAP_TOKEN) private converterMap: Map<string, EntityConverter<any, any>>
        ) {}
  
        public getEntity<T extends Entity<string>>(path: string): Observable<T | undefined> {
          const docRef = doc(this.firestore, path);
          return docData(docRef, { idField: 'id' }).pipe(
                  map((plainObject: any) => this.convertIntoEntity(plainObject))
          );
        }
  
        public listenToCollectionChanges<T extends Entity<string>>(path: string): Observable<T[]> {
          const collectionRef: CollectionReference = collection(this.firestore, path);
          const q: Query = query(collectionRef);
  
          return collectionData(q, { idField: 'id' }).pipe(
                  map((plainObjects: DocumentData[]) => {
                    return plainObjects
                            .map(obj => this.convertIntoEntity<T>(obj))
                            .filter(Boolean) as T[];
                  })
          );
        }
  
        public convertIntoEntity<T extends Entity<string>>(rawObject: any): T | undefined {
          if (!rawObject || !rawObject.typeKey) {
            return undefined;
          }
  
          const converter = this.converterMap.get(rawObject.typeKey);
  
          if (converter) {
            return converter.fromPlainObject(rawObject) as T;
          }
  
          console.warn(`No converter registered for typeKey: "${rawObject.typeKey}"`);
          return undefined;
        }