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

pojo-repository

v2.0.0

Published

Repository interfaces for CRUD operations on plain-old-javascript/json-objects (pojos), and HttpRestRepositry provider for use with standard rest apis.

Downloads

5

Readme

pojo-repository

Repository interfaces for CRUD operations on plain-old-javascript/json-objects (pojos), and HttpRestRepositry provider for use with standard rest apis.

All methods are asynchonous using the Promise API.

Installation

Install with npm:

npm install pojo-repository

Or with yarn:

yarn add pojo-repository

Basic Usage

Import

import { HttpRestRepository, HttpRestReadRepository } from 'pojo-repository';

Reading a model from its source


let repository = new HttpRestRepository<MyModel>('https://...');

let model = repository.find('my_id');

Saving changes to a model


let repository = new HttpRestRepository<MyModel>('https://...');

let model = repository.find('my_id');
model.name = 'frog';

repository.save(model.id, model);

Creating and saving new models


let repository = new HttpRestRepository<MyModel>('https://...');

let model = await repository.create();
model.name = 'frog';

await repository.save(model.id, model, true /* Will perform POST instead of PUT for new records. */);

Removing models


let repository = new HttpRestRepository<MyModel>('https://...');

await repository.remove('my_id');

Readonly Repositories

A read-only APi for sources that cannot be saved to is available as the ReadRepository interface and HttpRestReadRepository class.


let repository = new HttpRestReadRepository<MyModel>('https://...');

let model = repository.find('my_id');

// repository is readonly, save(), create(), remove() are not available.

Authorization or Custom Headers

HttpRestRespository and HttpRestReadRepository use ApiFetch from apifetch-json internally. You can pass an optional pre-configured ApiFetch object to the repository constructor to handle authorization or other special HTTP options.


let apiFetch = new ApiFetch({
        headers: {
            'Authorization': `Bearer ${userState.token}`
        }
    });

let repository = new HttpRestReadRepository<MyModel>('https://...', apiFetch);

let model = repository.find('my_id');

For more examples of using ApiFetch check out the apifetch-json project.

Custom Repositories

You an also provide your own Repository and ReadRespository implementations by implementing the Repository or RepositoryRead interfaces.

import { HttpRestRepository, HttpRestReadRepository } from 'pojo-repository';

export class CustomRepository<T = any, Key = any> implements Repository {
    /**
     * Constructor.
     */
    constructor() {
    }

    /**
     * Find an model in the store by its key.  If no match is found null is returned.
     * @param id
     */
    async find(id: string): Promise<T> {
        // Custom code here.
    }

    /**
     * Get all models in the store.
     */
    async findAll(): Promise<Array<T>> {
        // Custom code here.
    }

    /**
     * Create a new model.
     *
     * NOTE this does not add the model to the store.  That is only done if save() is called on the returned model.
     */
    async create(): Promise<T> {
        // Custom code here.
    }

    /**
     * Save a model back to it's store.
     * @param id
     * @param model
     * @param isCreate
     */
    async save(id: string, model: T, isCreate?: boolean): Promise<void> {
        // Custom code here.
    }

    /**
     * Remove a model from the store.
     * @param id
     */
    async remove(id: string): Promise<void> {
        // Custom code here.
    }
}

Javascript Usage

pojo-repository works just as well with Javascript as Typescript. All you need to do is remove the type information from the above examples.

Here are the basic usage examples in plain Javascript:

Import

import { HttpRestRepository, HttpRestReadRepository } from 'pojo-repository';

Reading a model from its source


var repository = new HttpRestRepository('https://...');

var model = repository.find('my_id');

Saving changes to a model


var repository = new HttpRestRepository('https://...');

var model = repository.find('my_id');
model.name = 'frog';

repository.save(model.id, model);

Creating and saving new models


var repository = new HttpRestRepository('https://...');

var model = await repository.create();
model.name = 'frog';

await repository.save(model.id, model, true /* Will perform POST instead of PUT for new records. */);

Removing models


var repository = new HttpRestRepository('https://...');

await repository.remove('my_id');

Readonly Repositories

A read-only APi for sources that cannot be saved to is available as the ReadRepository interface and HttpRestReadRepository class.


var repository = new HttpRestReadRepository('https://...');

var model = repository.find('my_id');

// repository is readonly, save(), create(), remove() are not available.

Typescript

This project is written in typescript and comes with its own bindings.

License

Licensed under the MIT license.