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

@yannickfricke/use-repository

v3.1.0

Published

![CI workflow status](https://img.shields.io/github/workflow/status/YannickFricke/use-repository/CI) [![codecov](https://codecov.io/gh/YannickFricke/use-repository/branch/develop/graph/badge.svg)](https://codecov.io/gh/YannickFricke/use-repository) ![GitH

Downloads

19

Readme

React repository hook

CI workflow status codecov GitHub watchers

About the project

This package provides repository functions for an array of objects.

Installation

NPM

npm i --save @yannickfricke/use-repository

Yarn

yarn add @yannickfricke/use-repository

TypeScript support

This package includes TypeScript definition files - so with every good IDE you can read those declaration files and get auto complete for the API.

Identifiable objects

The identifiable interface defines the id property. The id must have the type of string or number.

Usage examples

import { useState } from 'react';

// Import the hook
// The "useRepository" provides basic repository functions for an array of entities
import {
    useRepository,
    useLocalStorageRepository,
} from '@yannickfricke/use-repository/dist';

// Define the values for the repository
const [entities, setEntities] = useState([]);

// Initialize the repository
// The first argument is an array of identifiables
// The second argument is an function which will update the data source with the new values
const repository = useRepository(entities, setEntities);

// Returns all entities
const allEntries = repository.getAll();

// Find an entry by its id
const foundEntryById = repository.find('idToSearch');

// Find a single entity with the given props
const foundEntryByProps = repository.findOneBy({
    id: 1,
});

// Find multiple entities by their matching props
const findEntriesByProps = repository.findBy({
    name: 'My name',
});

// Insert a new entry
// NOTE: The "useRepository" makes no assumptions about your data structures,
// so thats why you have to assign ids on yourself
repository.insert({
    name: 'testing useRepository hook',
});

// Update the entity with the given id
repository.update('idToUpdate', {
    name: 'My cool new name!',
});

// Removes the entity with the given ID from data source
repository.remove('idToRemove');

// Remove all entries with the name "Delete me"
repository.removeBy({
    name: 'Delete me',
});

// Remove all entities from the data source
repository.removeAll();

// Initialize the repository with the given table name
// NOTE: The "useLocalStorageRepository" hook returns the same API as the "useRepository" hook.
// The only difference is that the "useLocalStorageRepository"
// will use the local storage of a browser as data source.
const users = useLocalStorageRepository<User>('users');

HINT:

TypeScript users can also use generics to define the type of the entities!

Then it will be correctly type hinted.

License

This package is published under the MIT license.