{"id":13494951,"url":"https://github.com/YannickFricke/use-repository","last_synced_at":"2025-03-28T15:32:14.561Z","repository":{"id":54311381,"uuid":"295055237","full_name":"YannickFricke/use-repository","owner":"YannickFricke","description":"Use basic repository functions in react based on arrays","archived":false,"fork":false,"pushed_at":"2021-02-24T22:31:49.000Z","size":609,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"develop","last_synced_at":"2024-10-31T09:37:19.265Z","etag":null,"topics":["datastructures","react","reacthooks","repository-pattern","typescipt"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/YannickFricke.png","metadata":{"files":{"readme":"Readme.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-13T01:19:42.000Z","updated_at":"2023-01-14T17:53:08.000Z","dependencies_parsed_at":"2022-08-13T11:40:47.211Z","dependency_job_id":null,"html_url":"https://github.com/YannickFricke/use-repository","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YannickFricke%2Fuse-repository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YannickFricke%2Fuse-repository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YannickFricke%2Fuse-repository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/YannickFricke%2Fuse-repository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/YannickFricke","download_url":"https://codeload.github.com/YannickFricke/use-repository/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246053876,"owners_count":20716278,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["datastructures","react","reacthooks","repository-pattern","typescipt"],"created_at":"2024-07-31T19:01:29.838Z","updated_at":"2025-03-28T15:32:14.232Z","avatar_url":"https://github.com/YannickFricke.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# React repository hook \u003c!-- omit in toc --\u003e\n\n![CI workflow status](https://img.shields.io/github/workflow/status/YannickFricke/use-repository/CI)\n[![codecov](https://codecov.io/gh/YannickFricke/use-repository/branch/develop/graph/badge.svg)](https://codecov.io/gh/YannickFricke/use-repository)\n![GitHub watchers](https://img.shields.io/github/watchers/YannickFricke/use-repository?style=social)\n\n- [About the project](#about-the-project)\n- [Installation](#installation)\n  - [NPM](#npm)\n  - [Yarn](#yarn)\n- [TypeScript support](#typescript-support)\n  - [Identifiable objects](#identifiable-objects)\n- [Usage examples](#usage-examples)\n- [License](#license)\n\n## About the project\n\nThis package provides repository functions for an array of objects.\n\n## Installation\n\n### NPM\n\n```\nnpm i --save @yannickfricke/use-repository\n```\n\n### Yarn\n\n```\nyarn add @yannickfricke/use-repository\n```\n\n## TypeScript support\n\nThis package includes TypeScript definition files - so with every good IDE you can read those declaration files\nand get auto complete for the API.\n\n### Identifiable objects\n\nThe identifiable interface defines the `id` property.\nThe `id` must have the type of string or number.\n\n## Usage examples\n\n```ts\nimport { useState } from 'react';\n\n// Import the hook\n// The \"useRepository\" provides basic repository functions for an array of entities\nimport {\n    useRepository,\n    useLocalStorageRepository,\n} from '@yannickfricke/use-repository/dist';\n\n// Define the values for the repository\nconst [entities, setEntities] = useState([]);\n\n// Initialize the repository\n// The first argument is an array of identifiables\n// The second argument is an function which will update the data source with the new values\nconst repository = useRepository(entities, setEntities);\n\n// Returns all entities\nconst allEntries = repository.getAll();\n\n// Find an entry by its id\nconst foundEntryById = repository.find('idToSearch');\n\n// Find a single entity with the given props\nconst foundEntryByProps = repository.findOneBy({\n    id: 1,\n});\n\n// Find multiple entities by their matching props\nconst findEntriesByProps = repository.findBy({\n    name: 'My name',\n});\n\n// Insert a new entry\n// NOTE: The \"useRepository\" makes no assumptions about your data structures,\n// so thats why you have to assign ids on yourself\nrepository.insert({\n    name: 'testing useRepository hook',\n});\n\n// Update the entity with the given id\nrepository.update('idToUpdate', {\n    name: 'My cool new name!',\n});\n\n// Removes the entity with the given ID from data source\nrepository.remove('idToRemove');\n\n// Remove all entries with the name \"Delete me\"\nrepository.removeBy({\n    name: 'Delete me',\n});\n\n// Remove all entities from the data source\nrepository.removeAll();\n\n// Initialize the repository with the given table name\n// NOTE: The \"useLocalStorageRepository\" hook returns the same API as the \"useRepository\" hook.\n// The only difference is that the \"useLocalStorageRepository\"\n// will use the local storage of a browser as data source.\nconst users = useLocalStorageRepository\u003cUser\u003e('users');\n```\n\nHINT:\n\nTypeScript users can also use generics to define the type of the entities!\n\nThen it will be correctly type hinted.\n\n## License\n\nThis package is published under the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FYannickFricke%2Fuse-repository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FYannickFricke%2Fuse-repository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FYannickFricke%2Fuse-repository/lists"}