use-entity 0.0.1-alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ name: release-please
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - "**"
7
+
8
+ jobs:
9
+ release-please:
10
+ runs-on: ubuntu-latest
11
+ timeout-minutes: 5
12
+ env:
13
+ NODE_ENV: production
14
+ CI: true
15
+
16
+ steps:
17
+ - uses: googleapis/release-please-action@v4
18
+ id: release
19
+ with:
20
+ token: ${{ secrets.RELEASE_PLEASE_GH_TOKEN }}
21
+ release-type: node
22
+
23
+ - uses: actions/checkout@v6
24
+
25
+ - name: Set up Bun
26
+ uses: oven-sh/setup-bun@v2
27
+ with:
28
+ bun-version: "latest"
29
+
30
+ - name: Install dependencies
31
+ run: bun install
32
+
33
+ - name: Run typecheck
34
+ run: bun run typecheck
35
+
36
+ - name: Run lint
37
+ run: bun run biome:check
38
+
39
+ - name: Run test
40
+ run: bun test
41
+
42
+ - name: Run build
43
+ run: bun run build
44
+
45
+ - run: bunx pkg-pr-new publish
46
+ continue-on-error: true
47
+
48
+ - name: Publish to npm
49
+ run: bun publish --access public
50
+ env:
51
+ NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }}
52
+ if: ${{ steps.release.outputs.release_created }}
package/CLAUDE.md ADDED
@@ -0,0 +1,111 @@
1
+ ---
2
+ description: Use Bun instead of Node.js, npm, pnpm, or vite.
3
+ globs: "*.ts, *.tsx, *.html, *.css, *.js, *.jsx, package.json"
4
+ alwaysApply: false
5
+ ---
6
+
7
+ Default to using Bun instead of Node.js.
8
+
9
+ - Use `bun <file>` instead of `node <file>` or `ts-node <file>`
10
+ - Use `bun test` instead of `jest` or `vitest`
11
+ - Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
12
+ - Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
13
+ - Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
14
+ - Use `bunx <package> <command>` instead of `npx <package> <command>`
15
+ - Bun automatically loads .env, so don't use dotenv.
16
+
17
+ ## APIs
18
+
19
+ - `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
20
+ - `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
21
+ - `Bun.redis` for Redis. Don't use `ioredis`.
22
+ - `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
23
+ - `WebSocket` is built-in. Don't use `ws`.
24
+ - Prefer `Bun.file` over `node:fs`'s readFile/writeFile
25
+ - Bun.$`ls` instead of execa.
26
+
27
+ ## Testing
28
+
29
+ Use `bun test` to run tests.
30
+
31
+ ```ts#index.test.ts
32
+ import { test, expect } from "bun:test";
33
+
34
+ test("hello world", () => {
35
+ expect(1).toBe(1);
36
+ });
37
+ ```
38
+
39
+ ## Frontend
40
+
41
+ Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
42
+
43
+ Server:
44
+
45
+ ```ts#index.ts
46
+ import index from "./index.html"
47
+
48
+ Bun.serve({
49
+ routes: {
50
+ "/": index,
51
+ "/api/users/:id": {
52
+ GET: (req) => {
53
+ return new Response(JSON.stringify({ id: req.params.id }));
54
+ },
55
+ },
56
+ },
57
+ // optional websocket support
58
+ websocket: {
59
+ open: (ws) => {
60
+ ws.send("Hello, world!");
61
+ },
62
+ message: (ws, message) => {
63
+ ws.send(message);
64
+ },
65
+ close: (ws) => {
66
+ // handle close
67
+ }
68
+ },
69
+ development: {
70
+ hmr: true,
71
+ console: true,
72
+ }
73
+ })
74
+ ```
75
+
76
+ HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
77
+
78
+ ```html#index.html
79
+ <html>
80
+ <body>
81
+ <h1>Hello, world!</h1>
82
+ <script type="module" src="./frontend.tsx"></script>
83
+ </body>
84
+ </html>
85
+ ```
86
+
87
+ With the following `frontend.tsx`:
88
+
89
+ ```tsx#frontend.tsx
90
+ import React from "react";
91
+ import { createRoot } from "react-dom/client";
92
+
93
+ // import .css files directly and it works
94
+ import './index.css';
95
+
96
+ const root = createRoot(document.body);
97
+
98
+ export default function Frontend() {
99
+ return <h1>Hello, world!</h1>;
100
+ }
101
+
102
+ root.render(<Frontend />);
103
+ ```
104
+
105
+ Then, run index.ts
106
+
107
+ ```sh
108
+ bun --hot ./index.ts
109
+ ```
110
+
111
+ For more information, read the Bun API docs in `node_modules/bun-types/docs/**.mdx`.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lukas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # use-entity
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.3.6. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
package/biome.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "$schema": "https://biomejs.dev/schemas/2.3.11/schema.json",
3
+ "vcs": {
4
+ "enabled": true,
5
+ "clientKind": "git",
6
+ "useIgnoreFile": true
7
+ },
8
+ "files": {
9
+ "ignoreUnknown": false
10
+ },
11
+ "formatter": {
12
+ "enabled": true,
13
+ "indentStyle": "tab"
14
+ },
15
+ "linter": {
16
+ "enabled": true,
17
+ "rules": {
18
+ "recommended": true
19
+ }
20
+ },
21
+ "javascript": {
22
+ "formatter": {
23
+ "quoteStyle": "double"
24
+ }
25
+ },
26
+ "assist": {
27
+ "enabled": true,
28
+ "actions": {
29
+ "source": {
30
+ "organizeImports": "on"
31
+ }
32
+ }
33
+ }
34
+ }
package/bun.lock ADDED
@@ -0,0 +1,180 @@
1
+ {
2
+ "lockfileVersion": 1,
3
+ "configVersion": 1,
4
+ "workspaces": {
5
+ "": {
6
+ "name": "use-entity",
7
+ "devDependencies": {
8
+ "@biomejs/biome": "2.3.11",
9
+ "@happy-dom/global-registrator": "20.1.0",
10
+ "@reduxjs/toolkit": "^2.0.0",
11
+ "@tanstack/react-store": "0.8.0",
12
+ "@testing-library/dom": "10.4.1",
13
+ "@testing-library/jest-dom": "6.9.1",
14
+ "@testing-library/react": "16.3.1",
15
+ "@types/bun": "latest",
16
+ "@types/react": "19.2.8",
17
+ "@types/react-dom": "19.2.3",
18
+ "lefthook": "2.0.15",
19
+ "react": "^19.2.3",
20
+ },
21
+ "peerDependencies": {
22
+ "@reduxjs/toolkit": "^2.0.0",
23
+ "@tanstack/react-store": "0.8.0",
24
+ "react": ">18.0.0",
25
+ "typescript": "^5",
26
+ },
27
+ },
28
+ },
29
+ "packages": {
30
+ "@adobe/css-tools": ["@adobe/css-tools@4.4.4", "", {}, "sha512-Elp+iwUx5rN5+Y8xLt5/GRoG20WGoDCQ/1Fb+1LiGtvwbDavuSk0jhD/eZdckHAuzcDzccnkv+rEjyWfRx18gg=="],
31
+
32
+ "@babel/code-frame": ["@babel/code-frame@7.28.6", "", { "dependencies": { "@babel/helper-validator-identifier": "^7.28.5", "js-tokens": "^4.0.0", "picocolors": "^1.1.1" } }, "sha512-JYgintcMjRiCvS8mMECzaEn+m3PfoQiyqukOMCCVQtoJGYJw8j/8LBJEiqkHLkfwCcs74E3pbAUFNg7d9VNJ+Q=="],
33
+
34
+ "@babel/helper-validator-identifier": ["@babel/helper-validator-identifier@7.28.5", "", {}, "sha512-qSs4ifwzKJSV39ucNjsvc6WVHs6b7S03sOh2OcHF9UHfVPqWWALUsNUVzhSBiItjRZoLHx7nIarVjqKVusUZ1Q=="],
35
+
36
+ "@babel/runtime": ["@babel/runtime@7.28.6", "", {}, "sha512-05WQkdpL9COIMz4LjTxGpPNCdlpyimKppYNoJ5Di5EUObifl8t4tuLuUBBZEpoLYOmfvIWrsp9fCl0HoPRVTdA=="],
37
+
38
+ "@biomejs/biome": ["@biomejs/biome@2.3.11", "", { "optionalDependencies": { "@biomejs/cli-darwin-arm64": "2.3.11", "@biomejs/cli-darwin-x64": "2.3.11", "@biomejs/cli-linux-arm64": "2.3.11", "@biomejs/cli-linux-arm64-musl": "2.3.11", "@biomejs/cli-linux-x64": "2.3.11", "@biomejs/cli-linux-x64-musl": "2.3.11", "@biomejs/cli-win32-arm64": "2.3.11", "@biomejs/cli-win32-x64": "2.3.11" }, "bin": { "biome": "bin/biome" } }, "sha512-/zt+6qazBWguPG6+eWmiELqO+9jRsMZ/DBU3lfuU2ngtIQYzymocHhKiZRyrbra4aCOoyTg/BmY+6WH5mv9xmQ=="],
39
+
40
+ "@biomejs/cli-darwin-arm64": ["@biomejs/cli-darwin-arm64@2.3.11", "", { "os": "darwin", "cpu": "arm64" }, "sha512-/uXXkBcPKVQY7rc9Ys2CrlirBJYbpESEDme7RKiBD6MmqR2w3j0+ZZXRIL2xiaNPsIMMNhP1YnA+jRRxoOAFrA=="],
41
+
42
+ "@biomejs/cli-darwin-x64": ["@biomejs/cli-darwin-x64@2.3.11", "", { "os": "darwin", "cpu": "x64" }, "sha512-fh7nnvbweDPm2xEmFjfmq7zSUiox88plgdHF9OIW4i99WnXrAC3o2P3ag9judoUMv8FCSUnlwJCM1B64nO5Fbg=="],
43
+
44
+ "@biomejs/cli-linux-arm64": ["@biomejs/cli-linux-arm64@2.3.11", "", { "os": "linux", "cpu": "arm64" }, "sha512-l4xkGa9E7Uc0/05qU2lMYfN1H+fzzkHgaJoy98wO+b/7Gl78srbCRRgwYSW+BTLixTBrM6Ede5NSBwt7rd/i6g=="],
45
+
46
+ "@biomejs/cli-linux-arm64-musl": ["@biomejs/cli-linux-arm64-musl@2.3.11", "", { "os": "linux", "cpu": "arm64" }, "sha512-XPSQ+XIPZMLaZ6zveQdwNjbX+QdROEd1zPgMwD47zvHV+tCGB88VH+aynyGxAHdzL+Tm/+DtKST5SECs4iwCLg=="],
47
+
48
+ "@biomejs/cli-linux-x64": ["@biomejs/cli-linux-x64@2.3.11", "", { "os": "linux", "cpu": "x64" }, "sha512-/1s9V/H3cSe0r0Mv/Z8JryF5x9ywRxywomqZVLHAoa/uN0eY7F8gEngWKNS5vbbN/BsfpCG5yeBT5ENh50Frxg=="],
49
+
50
+ "@biomejs/cli-linux-x64-musl": ["@biomejs/cli-linux-x64-musl@2.3.11", "", { "os": "linux", "cpu": "x64" }, "sha512-vU7a8wLs5C9yJ4CB8a44r12aXYb8yYgBn+WeyzbMjaCMklzCv1oXr8x+VEyWodgJt9bDmhiaW/I0RHbn7rsNmw=="],
51
+
52
+ "@biomejs/cli-win32-arm64": ["@biomejs/cli-win32-arm64@2.3.11", "", { "os": "win32", "cpu": "arm64" }, "sha512-PZQ6ElCOnkYapSsysiTy0+fYX+agXPlWugh6+eQ6uPKI3vKAqNp6TnMhoM3oY2NltSB89hz59o8xIfOdyhi9Iw=="],
53
+
54
+ "@biomejs/cli-win32-x64": ["@biomejs/cli-win32-x64@2.3.11", "", { "os": "win32", "cpu": "x64" }, "sha512-43VrG813EW+b5+YbDbz31uUsheX+qFKCpXeY9kfdAx+ww3naKxeVkTD9zLIWxUPfJquANMHrmW3wbe/037G0Qg=="],
55
+
56
+ "@happy-dom/global-registrator": ["@happy-dom/global-registrator@20.1.0", "", { "dependencies": { "@types/node": "^20.0.0", "happy-dom": "^20.1.0" } }, "sha512-8c2dSLe7/Rdiq5mAZ8G6hZhx+mp3QzUp1sQD6dmTilLf4bz5X0AAHvBW3AcpcUYGBiTamB4EFuXXtWi5ZVqtRg=="],
57
+
58
+ "@reduxjs/toolkit": ["@reduxjs/toolkit@2.11.2", "", { "dependencies": { "@standard-schema/spec": "^1.0.0", "@standard-schema/utils": "^0.3.0", "immer": "^11.0.0", "redux": "^5.0.1", "redux-thunk": "^3.1.0", "reselect": "^5.1.0" }, "peerDependencies": { "react": "^16.9.0 || ^17.0.0 || ^18 || ^19", "react-redux": "^7.2.1 || ^8.1.3 || ^9.0.0" }, "optionalPeers": ["react", "react-redux"] }, "sha512-Kd6kAHTA6/nUpp8mySPqj3en3dm0tdMIgbttnQ1xFMVpufoj+ADi8pXLBsd4xzTRHQa7t/Jv8W5UnCuW4kuWMQ=="],
59
+
60
+ "@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],
61
+
62
+ "@standard-schema/utils": ["@standard-schema/utils@0.3.0", "", {}, "sha512-e7Mew686owMaPJVNNLs55PUvgz371nKgwsc4vxE49zsODpJEnxgxRo2y/OKrqueavXgZNMDVj3DdHFlaSAeU8g=="],
63
+
64
+ "@tanstack/react-store": ["@tanstack/react-store@0.8.0", "", { "dependencies": { "@tanstack/store": "0.8.0", "use-sync-external-store": "^1.6.0" }, "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-1vG9beLIuB7q69skxK9r5xiLN3ztzIPfSQSs0GfeqWGO2tGIyInZx0x1COhpx97RKaONSoAb8C3dxacWksm1ow=="],
65
+
66
+ "@tanstack/store": ["@tanstack/store@0.8.0", "", {}, "sha512-Om+BO0YfMZe//X2z0uLF2j+75nQga6TpTJgLJQBiq85aOyZNIhkCgleNcud2KQg4k4v9Y9l+Uhru3qWMPGTOzQ=="],
67
+
68
+ "@testing-library/dom": ["@testing-library/dom@10.4.1", "", { "dependencies": { "@babel/code-frame": "^7.10.4", "@babel/runtime": "^7.12.5", "@types/aria-query": "^5.0.1", "aria-query": "5.3.0", "dom-accessibility-api": "^0.5.9", "lz-string": "^1.5.0", "picocolors": "1.1.1", "pretty-format": "^27.0.2" } }, "sha512-o4PXJQidqJl82ckFaXUeoAW+XysPLauYI43Abki5hABd853iMhitooc6znOnczgbTYmEP6U6/y1ZyKAIsvMKGg=="],
69
+
70
+ "@testing-library/jest-dom": ["@testing-library/jest-dom@6.9.1", "", { "dependencies": { "@adobe/css-tools": "^4.4.0", "aria-query": "^5.0.0", "css.escape": "^1.5.1", "dom-accessibility-api": "^0.6.3", "picocolors": "^1.1.1", "redent": "^3.0.0" } }, "sha512-zIcONa+hVtVSSep9UT3jZ5rizo2BsxgyDYU7WFD5eICBE7no3881HGeb/QkGfsJs6JTkY1aQhT7rIPC7e+0nnA=="],
71
+
72
+ "@testing-library/react": ["@testing-library/react@16.3.1", "", { "dependencies": { "@babel/runtime": "^7.12.5" }, "peerDependencies": { "@testing-library/dom": "^10.0.0", "@types/react": "^18.0.0 || ^19.0.0", "@types/react-dom": "^18.0.0 || ^19.0.0", "react": "^18.0.0 || ^19.0.0", "react-dom": "^18.0.0 || ^19.0.0" }, "optionalPeers": ["@types/react", "@types/react-dom"] }, "sha512-gr4KtAWqIOQoucWYD/f6ki+j5chXfcPc74Col/6poTyqTmn7zRmodWahWRCp8tYd+GMqBonw6hstNzqjbs6gjw=="],
73
+
74
+ "@types/aria-query": ["@types/aria-query@5.0.4", "", {}, "sha512-rfT93uj5s0PRL7EzccGMs3brplhcrghnDoV26NqKhCAS1hVo+WdNsPvE/yb6ilfr5hi2MEk6d5EWJTKdxg8jVw=="],
75
+
76
+ "@types/bun": ["@types/bun@1.3.6", "", { "dependencies": { "bun-types": "1.3.6" } }, "sha512-uWCv6FO/8LcpREhenN1d1b6fcspAB+cefwD7uti8C8VffIv0Um08TKMn98FynpTiU38+y2dUO55T11NgDt8VAA=="],
77
+
78
+ "@types/node": ["@types/node@20.19.29", "", { "dependencies": { "undici-types": "~6.21.0" } }, "sha512-YrT9ArrGaHForBaCNwFjoqJWmn8G1Pr7+BH/vwyLHciA9qT/wSiuOhxGCT50JA5xLvFBd6PIiGkE3afxcPE1nw=="],
79
+
80
+ "@types/react": ["@types/react@19.2.8", "", { "dependencies": { "csstype": "^3.2.2" } }, "sha512-3MbSL37jEchWZz2p2mjntRZtPt837ij10ApxKfgmXCTuHWagYg7iA5bqPw6C8BMPfwidlvfPI/fxOc42HLhcyg=="],
81
+
82
+ "@types/react-dom": ["@types/react-dom@19.2.3", "", { "peerDependencies": { "@types/react": "^19.2.0" } }, "sha512-jp2L/eY6fn+KgVVQAOqYItbF0VY/YApe5Mz2F0aykSO8gx31bYCZyvSeYxCHKvzHG5eZjc+zyaS5BrBWya2+kQ=="],
83
+
84
+ "@types/whatwg-mimetype": ["@types/whatwg-mimetype@3.0.2", "", {}, "sha512-c2AKvDT8ToxLIOUlN51gTiHXflsfIFisS4pO7pDPoKouJCESkhZnEy623gwP9laCy5lnLDAw1vAzu2vM2YLOrA=="],
85
+
86
+ "@types/ws": ["@types/ws@8.18.1", "", { "dependencies": { "@types/node": "*" } }, "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg=="],
87
+
88
+ "ansi-regex": ["ansi-regex@5.0.1", "", {}, "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ=="],
89
+
90
+ "ansi-styles": ["ansi-styles@5.2.0", "", {}, "sha512-Cxwpt2SfTzTtXcfOlzGEee8O+c+MmUgGrNiBcXnuWxuFJHe6a5Hz7qwhwe5OgaSYI0IJvkLqWX1ASG+cJOkEiA=="],
91
+
92
+ "aria-query": ["aria-query@5.3.0", "", { "dependencies": { "dequal": "^2.0.3" } }, "sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A=="],
93
+
94
+ "bun-types": ["bun-types@1.3.6", "", { "dependencies": { "@types/node": "*" } }, "sha512-OlFwHcnNV99r//9v5IIOgQ9Uk37gZqrNMCcqEaExdkVq3Avwqok1bJFmvGMCkCE0FqzdY8VMOZpfpR3lwI+CsQ=="],
95
+
96
+ "css.escape": ["css.escape@1.5.1", "", {}, "sha512-YUifsXXuknHlUsmlgyY0PKzgPOr7/FjCePfHNt0jxm83wHZi44VDMQ7/fGNkjY3/jV1MC+1CmZbaHzugyeRtpg=="],
97
+
98
+ "csstype": ["csstype@3.2.3", "", {}, "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ=="],
99
+
100
+ "dequal": ["dequal@2.0.3", "", {}, "sha512-0je+qPKHEMohvfRTCEo3CrPG6cAzAYgmzKyxRiYSSDkS6eGJdyVJm7WaYA5ECaAD9wLB2T4EEeymA5aFVcYXCA=="],
101
+
102
+ "dom-accessibility-api": ["dom-accessibility-api@0.5.16", "", {}, "sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg=="],
103
+
104
+ "happy-dom": ["happy-dom@20.1.0", "", { "dependencies": { "@types/node": "^20.0.0", "@types/whatwg-mimetype": "^3.0.2", "@types/ws": "^8.18.1", "whatwg-mimetype": "^3.0.0", "ws": "^8.18.3" } }, "sha512-ebvqjBqzenBk2LjzNEAzoj7yhw7rW/R2/wVevMu6Mrq3MXtcI/RUz4+ozpcOcqVLEWPqLfg2v9EAU7fFXZUUJw=="],
105
+
106
+ "immer": ["immer@11.1.3", "", {}, "sha512-6jQTc5z0KJFtr1UgFpIL3N9XSC3saRaI9PwWtzM2pSqkNGtiNkYY2OSwkOGDK2XcTRcLb1pi/aNkKZz0nxVH4Q=="],
107
+
108
+ "indent-string": ["indent-string@4.0.0", "", {}, "sha512-EdDDZu4A2OyIK7Lr/2zG+w5jmbuk1DVBnEwREQvBzspBJkCEbRa8GxU1lghYcaGJCnRWibjDXlq779X1/y5xwg=="],
109
+
110
+ "js-tokens": ["js-tokens@4.0.0", "", {}, "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ=="],
111
+
112
+ "lefthook": ["lefthook@2.0.15", "", { "optionalDependencies": { "lefthook-darwin-arm64": "2.0.15", "lefthook-darwin-x64": "2.0.15", "lefthook-freebsd-arm64": "2.0.15", "lefthook-freebsd-x64": "2.0.15", "lefthook-linux-arm64": "2.0.15", "lefthook-linux-x64": "2.0.15", "lefthook-openbsd-arm64": "2.0.15", "lefthook-openbsd-x64": "2.0.15", "lefthook-windows-arm64": "2.0.15", "lefthook-windows-x64": "2.0.15" }, "bin": { "lefthook": "bin/index.js" } }, "sha512-sl5rePO6UUOLKp6Ci+MMKOc86zicBaPUCvSw2Cq4gCAgTmxpxhIjhz7LOu2ObYerVRPpTq3gvzPTjI71UotjnA=="],
113
+
114
+ "lefthook-darwin-arm64": ["lefthook-darwin-arm64@2.0.15", "", { "os": "darwin", "cpu": "arm64" }, "sha512-ygAqG/NzOgY9bEiqeQtiOmCRTtp9AmOd3eyrpEaSrRB9V9f3RHRgWDrWbde9BiHSsCzcbeY9/X2NuKZ69eUsNA=="],
115
+
116
+ "lefthook-darwin-x64": ["lefthook-darwin-x64@2.0.15", "", { "os": "darwin", "cpu": "x64" }, "sha512-3wA30CzdSL5MFKD6dk7v8BMq7ScWQivpLbmIn3Pv67AaBavN57N/hcdGqOFnDDFI5WazVwDY7UqDfMIk5HZjEA=="],
117
+
118
+ "lefthook-freebsd-arm64": ["lefthook-freebsd-arm64@2.0.15", "", { "os": "freebsd", "cpu": "arm64" }, "sha512-FbYBBLVbX8BjdO+icN1t/pC3TOW3FAvTKv/zggBKNihv6jHNn/3s/0j2xIS0k0Pw9oOE7MVmEni3qp2j5vqHrQ=="],
119
+
120
+ "lefthook-freebsd-x64": ["lefthook-freebsd-x64@2.0.15", "", { "os": "freebsd", "cpu": "x64" }, "sha512-udHMjh1E8TfC0Z7Y249XZMATJOyj1Jxlj9JoEinkoBvAsePFKDEQg5teuXuTGhjsHYpqVekfSvLNNfHKUUbbjw=="],
121
+
122
+ "lefthook-linux-arm64": ["lefthook-linux-arm64@2.0.15", "", { "os": "linux", "cpu": "arm64" }, "sha512-1HAPmdYhfcOlubv63sTnWtW2rFuC+kT1MvC3JvdrS5V6zrOImbBSnYZMJX/Dd3w4pm0x2ZJb9T+uef8a0jUQkg=="],
123
+
124
+ "lefthook-linux-x64": ["lefthook-linux-x64@2.0.15", "", { "os": "linux", "cpu": "x64" }, "sha512-Pho87mlNFH47zc4fPKzQSp8q9sWfIFW/KMMZfx/HZNmX25aUUTOqMyRwaXxtdAo/hNJ9FX4JeuZWq9Y3iyM5VA=="],
125
+
126
+ "lefthook-openbsd-arm64": ["lefthook-openbsd-arm64@2.0.15", "", { "os": "openbsd", "cpu": "arm64" }, "sha512-pet03Edlj1QeFUgxcIK1xu8CeZA+ejYplvPgdfe//69+vQFGSDaEx3H2mVx8RqzWfmMbijM2/WfkZXR2EVw3bw=="],
127
+
128
+ "lefthook-openbsd-x64": ["lefthook-openbsd-x64@2.0.15", "", { "os": "openbsd", "cpu": "x64" }, "sha512-i+a364CcSAeIO5wQzLMHsthHt/v6n3XwhKmRq/VBzPOUv9KutNeF55yCE/6lvuvzwxpdEfBjh6cXPERC0yp98w=="],
129
+
130
+ "lefthook-windows-arm64": ["lefthook-windows-arm64@2.0.15", "", { "os": "win32", "cpu": "arm64" }, "sha512-69u5GdVOT4QIxc2TK5ce0cTXLzwB55Pk9ZnnJNFf1XsyZTGcg9bUWYYTyD12CIIXbVTa0RVXIIrbU9UgP8O1AQ=="],
131
+
132
+ "lefthook-windows-x64": ["lefthook-windows-x64@2.0.15", "", { "os": "win32", "cpu": "x64" }, "sha512-/zYEndCUgj8XK+4wvLYLRk3AcfKU6zWf2GHx+tcZ4K2bLaQdej4m+OqmQsVpUlF8N2tN9hfwlj1D50uz75LUuQ=="],
133
+
134
+ "lz-string": ["lz-string@1.5.0", "", { "bin": { "lz-string": "bin/bin.js" } }, "sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ=="],
135
+
136
+ "min-indent": ["min-indent@1.0.1", "", {}, "sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg=="],
137
+
138
+ "picocolors": ["picocolors@1.1.1", "", {}, "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA=="],
139
+
140
+ "pretty-format": ["pretty-format@27.5.1", "", { "dependencies": { "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" } }, "sha512-Qb1gy5OrP5+zDf2Bvnzdl3jsTf1qXVMazbvCoKhtKqVs4/YK4ozX4gKQJJVyNe+cajNPn0KoC0MC3FUmaHWEmQ=="],
141
+
142
+ "react": ["react@19.2.3", "", {}, "sha512-Ku/hhYbVjOQnXDZFv2+RibmLFGwFdeeKHFcOTlrt7xplBnya5OGn/hIRDsqDiSUcfORsDC7MPxwork8jBwsIWA=="],
143
+
144
+ "react-dom": ["react-dom@19.2.3", "", { "dependencies": { "scheduler": "^0.27.0" }, "peerDependencies": { "react": "^19.2.3" } }, "sha512-yELu4WmLPw5Mr/lmeEpox5rw3RETacE++JgHqQzd2dg+YbJuat3jH4ingc+WPZhxaoFzdv9y33G+F7Nl5O0GBg=="],
145
+
146
+ "react-is": ["react-is@17.0.2", "", {}, "sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w=="],
147
+
148
+ "redent": ["redent@3.0.0", "", { "dependencies": { "indent-string": "^4.0.0", "strip-indent": "^3.0.0" } }, "sha512-6tDA8g98We0zd0GvVeMT9arEOnTw9qM03L9cJXaCjrip1OO764RDBLBfrB4cwzNGDj5OA5ioymC9GkizgWJDUg=="],
149
+
150
+ "redux": ["redux@5.0.1", "", {}, "sha512-M9/ELqF6fy8FwmkpnF0S3YKOqMyoWJ4+CS5Efg2ct3oY9daQvd/Pc71FpGZsVsbl3Cpb+IIcjBDUnnyBdQbq4w=="],
151
+
152
+ "redux-thunk": ["redux-thunk@3.1.0", "", { "peerDependencies": { "redux": "^5.0.0" } }, "sha512-NW2r5T6ksUKXCabzhL9z+h206HQw/NJkcLm1GPImRQ8IzfXwRGqjVhKJGauHirT0DAuyy6hjdnMZaRoAcy0Klw=="],
153
+
154
+ "reselect": ["reselect@5.1.1", "", {}, "sha512-K/BG6eIky/SBpzfHZv/dd+9JBFiS4SWV7FIujVyJRux6e45+73RaUHXLmIR1f7WOMaQ0U1km6qwklRQxpJJY0w=="],
155
+
156
+ "scheduler": ["scheduler@0.27.0", "", {}, "sha512-eNv+WrVbKu1f3vbYJT/xtiF5syA5HPIMtf9IgY/nKg0sWqzAUEvqY/xm7OcZc/qafLx/iO9FgOmeSAp4v5ti/Q=="],
157
+
158
+ "strip-indent": ["strip-indent@3.0.0", "", { "dependencies": { "min-indent": "^1.0.0" } }, "sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ=="],
159
+
160
+ "typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
161
+
162
+ "undici-types": ["undici-types@6.21.0", "", {}, "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="],
163
+
164
+ "use-sync-external-store": ["use-sync-external-store@1.6.0", "", { "peerDependencies": { "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-Pp6GSwGP/NrPIrxVFAIkOQeyw8lFenOHijQWkUTrDvrF4ALqylP2C/KCkeS9dpUM3KvYRQhna5vt7IL95+ZQ9w=="],
165
+
166
+ "whatwg-mimetype": ["whatwg-mimetype@3.0.0", "", {}, "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q=="],
167
+
168
+ "ws": ["ws@8.19.0", "", { "peerDependencies": { "bufferutil": "^4.0.1", "utf-8-validate": ">=5.0.2" }, "optionalPeers": ["bufferutil", "utf-8-validate"] }, "sha512-blAT2mjOEIi0ZzruJfIhb3nps74PRWTCz1IjglWEEpQl5XS/UNama6u2/rjFkDDouqr4L67ry+1aGIALViWjDg=="],
169
+
170
+ "@testing-library/jest-dom/dom-accessibility-api": ["dom-accessibility-api@0.6.3", "", {}, "sha512-7ZgogeTnjuHbo+ct10G9Ffp0mif17idi0IyWNVA/wcwcm7NPOD/WEHVP3n7n3MhXqxoIYm8d6MuZohYWIZ4T3w=="],
171
+
172
+ "@types/ws/@types/node": ["@types/node@25.0.8", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-powIePYMmC3ibL0UJ2i2s0WIbq6cg6UyVFQxSCpaPxxzAaziRfimGivjdF943sSGV6RADVbk0Nvlm5P/FB44Zg=="],
173
+
174
+ "bun-types/@types/node": ["@types/node@25.0.8", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-powIePYMmC3ibL0UJ2i2s0WIbq6cg6UyVFQxSCpaPxxzAaziRfimGivjdF943sSGV6RADVbk0Nvlm5P/FB44Zg=="],
175
+
176
+ "@types/ws/@types/node/undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
177
+
178
+ "bun-types/@types/node/undici-types": ["undici-types@7.16.0", "", {}, "sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw=="],
179
+ }
180
+ }
package/bunfig.toml ADDED
@@ -0,0 +1,2 @@
1
+ [test]
2
+ preload = ["./happydom.ts", "./testing-library.ts"]
package/happydom.ts ADDED
@@ -0,0 +1,3 @@
1
+ import { GlobalRegistrator } from "@happy-dom/global-registrator";
2
+
3
+ GlobalRegistrator.register();
package/index.ts ADDED
@@ -0,0 +1 @@
1
+ export { createEntityStore } from "./src/useEntity.ts";
package/lefthook.yml ADDED
@@ -0,0 +1,5 @@
1
+ pre-commit:
2
+ commands:
3
+ check:
4
+ glob: "*.{js,ts,cjs,mjs,d.cts,d.mts,jsx,tsx,json,jsonc}"
5
+ run: npx @biomejs/biome check --no-errors-on-unmatched --files-ignore-unknown=true --colors=off {staged_files}
package/matchers.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { TestingLibraryMatchers } from "@testing-library/jest-dom/matchers";
2
+
3
+ declare module "bun:test" {
4
+ interface Matchers<T>
5
+ extends TestingLibraryMatchers<typeof expect.stringContaining, T> {}
6
+ interface AsymmetricMatchers extends TestingLibraryMatchers {}
7
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "use-entity",
3
+ "module": "index.ts",
4
+ "type": "module",
5
+ "version": "0.0.1-alpha",
6
+ "scripts": {
7
+ "biome:check": "biome check",
8
+ "biome:fix": "biome check --write --unsafe",
9
+ "biome:format": "biome format --write",
10
+ "test": "bun test",
11
+ "typecheck": "tsc",
12
+ "test:coverage": "jest"
13
+ },
14
+ "devDependencies": {
15
+ "@biomejs/biome": "2.3.11",
16
+ "@happy-dom/global-registrator": "20.1.0",
17
+ "@reduxjs/toolkit": "^2.0.0",
18
+ "@tanstack/react-store": "0.8.0",
19
+ "@testing-library/dom": "10.4.1",
20
+ "@testing-library/jest-dom": "6.9.1",
21
+ "@testing-library/react": "16.3.1",
22
+ "@types/bun": "latest",
23
+ "@types/react": "19.2.8",
24
+ "@types/react-dom": "19.2.3",
25
+ "lefthook": "2.0.15",
26
+ "react": "^19.2.3"
27
+ },
28
+ "peerDependencies": {
29
+ "typescript": "^5",
30
+ "react": ">18.0.0",
31
+ "@reduxjs/toolkit": "^2.0.0",
32
+ "@tanstack/react-store": "0.8.0"
33
+ }
34
+ }
@@ -0,0 +1,271 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { act, renderHook } from "@testing-library/react";
3
+ import { createEntityStore } from "../useEntity.ts";
4
+
5
+ type TestEntity = { id: string; name: string };
6
+
7
+ describe("useEntity", () => {
8
+ test("exposes selectors for the initial state", () => {
9
+ const initial: TestEntity[] = [
10
+ { id: "1", name: "Alpha" },
11
+ { id: "2", name: "Beta" },
12
+ ];
13
+ const { useEntity } = createEntityStore<TestEntity>(initial);
14
+
15
+ const { result } = renderHook(() => useEntity());
16
+
17
+ expect(result.current[0].all).toEqual(initial);
18
+ expect(result.current[0].ids).toEqual(["1", "2"]);
19
+ expect(result.current[0].byId("2")).toEqual({
20
+ id: "2",
21
+ name: "Beta",
22
+ });
23
+ });
24
+
25
+ test("supports add and remove operations", () => {
26
+ const { useEntity } = createEntityStore<TestEntity>();
27
+ const { result } = renderHook(() => useEntity());
28
+ const [, actions] = result.current;
29
+
30
+ act(() => {
31
+ actions.addOne({ id: "1", name: "Alpha" });
32
+ actions.addMany([
33
+ { id: "2", name: "Beta" },
34
+ { id: "3", name: "Gamma" },
35
+ ]);
36
+ });
37
+
38
+ expect(result.current[0].all).toHaveLength(3);
39
+ expect([...result.current[0].ids].sort()).toEqual(["1", "2", "3"]);
40
+
41
+ act(() => {
42
+ actions.removeOne("2");
43
+ actions.removeMany(["1", "3"]);
44
+ });
45
+
46
+ expect(result.current[0].all).toEqual([]);
47
+
48
+ act(() => {
49
+ actions.addOne({ id: "4", name: "Delta" });
50
+ actions.removeAll();
51
+ });
52
+
53
+ expect(result.current[0].all).toEqual([]);
54
+ });
55
+
56
+ test("supports update and upsert operations", () => {
57
+ const { useEntity } = createEntityStore<TestEntity>([
58
+ { id: "1", name: "Alpha" },
59
+ ]);
60
+ const { result } = renderHook(() => useEntity());
61
+ const [, actions] = result.current;
62
+
63
+ act(() => {
64
+ actions.updateOne({ id: "1", changes: { name: "Alpha+" } });
65
+ });
66
+
67
+ expect(result.current[0].byId("1")).toEqual({
68
+ id: "1",
69
+ name: "Alpha+",
70
+ });
71
+
72
+ act(() => {
73
+ actions.upsertOne({ id: "2", name: "Beta" });
74
+ actions.updateMany([
75
+ { id: "1", changes: { name: "Alpha++" } },
76
+ { id: "2", changes: { name: "Beta+" } },
77
+ ]);
78
+ actions.upsertMany({
79
+ "2": { id: "2", name: "Beta++" },
80
+ "3": { id: "3", name: "Gamma" },
81
+ });
82
+ });
83
+
84
+ expect(result.current[0].byId("1")).toEqual({
85
+ id: "1",
86
+ name: "Alpha++",
87
+ });
88
+ expect(result.current[0].byId("2")).toEqual({
89
+ id: "2",
90
+ name: "Beta++",
91
+ });
92
+ expect(result.current[0].byId("3")).toEqual({
93
+ id: "3",
94
+ name: "Gamma",
95
+ });
96
+ });
97
+
98
+ test("supports set operations and total/entities selectors", () => {
99
+ const { useEntity } = createEntityStore<TestEntity>();
100
+ const { result } = renderHook(() => useEntity());
101
+ const [, actions] = result.current;
102
+
103
+ act(() => {
104
+ actions.setOne({ id: "1", name: "Alpha" });
105
+ actions.setMany([
106
+ { id: "2", name: "Beta" },
107
+ { id: "3", name: "Gamma" },
108
+ ]);
109
+ });
110
+
111
+ expect(result.current[0].total).toBe(3);
112
+ expect(result.current[0].entities).toEqual({
113
+ "1": { id: "1", name: "Alpha" },
114
+ "2": { id: "2", name: "Beta" },
115
+ "3": { id: "3", name: "Gamma" },
116
+ });
117
+
118
+ act(() => {
119
+ actions.setAll([{ id: "4", name: "Delta" }]);
120
+ });
121
+
122
+ expect(result.current[0].all).toEqual([{ id: "4", name: "Delta" }]);
123
+ expect(result.current[0].total).toBe(1);
124
+ });
125
+
126
+ test("returns undefined for missing entities", () => {
127
+ const { useEntity } = createEntityStore<TestEntity>();
128
+ const { result } = renderHook(() => useEntity());
129
+
130
+ expect(result.current[0].byId("missing")).toBeUndefined();
131
+ });
132
+
133
+ test("ignores remove/update of missing entities", () => {
134
+ const { useEntity } = createEntityStore<TestEntity>([
135
+ { id: "1", name: "Alpha" },
136
+ ]);
137
+ const { result } = renderHook(() => useEntity());
138
+ const [, actions] = result.current;
139
+
140
+ act(() => {
141
+ actions.removeOne("missing");
142
+ actions.removeMany(["missing-a", "missing-b"]);
143
+ actions.updateOne({ id: "missing", changes: { name: "Nope" } });
144
+ actions.updateMany([{ id: "missing-2", changes: { name: "Nope 2" } }]);
145
+ });
146
+
147
+ expect(result.current[0].all).toEqual([{ id: "1", name: "Alpha" }]);
148
+ });
149
+
150
+ test("keeps selectors in sync after multiple operations", () => {
151
+ const { useEntity } = createEntityStore<TestEntity>();
152
+ const { result } = renderHook(() => useEntity());
153
+ const [, actions] = result.current;
154
+
155
+ act(() => {
156
+ actions.addMany([
157
+ { id: "1", name: "Alpha" },
158
+ { id: "2", name: "Beta" },
159
+ ]);
160
+ actions.removeOne("1");
161
+ actions.upsertOne({ id: "2", name: "Beta+" });
162
+ actions.upsertMany({
163
+ "3": { id: "3", name: "Gamma" },
164
+ "4": { id: "4", name: "Delta" },
165
+ });
166
+ });
167
+
168
+ expect([...result.current[0].ids].sort()).toEqual(["2", "3", "4"]);
169
+ expect(result.current[0].byId("2")).toEqual({
170
+ id: "2",
171
+ name: "Beta+",
172
+ });
173
+ });
174
+
175
+ test("selector full returns the full selector object", () => {
176
+ const { useEntity } = createEntityStore<TestEntity>([
177
+ { id: "1", name: "Alpha" },
178
+ { id: "2", name: "Beta" },
179
+ ]);
180
+ const { result } = renderHook(() => useEntity("full"));
181
+
182
+ expect(result.current[0].all).toEqual([
183
+ { id: "1", name: "Alpha" },
184
+ { id: "2", name: "Beta" },
185
+ ]);
186
+ expect(result.current[0].byId("2")).toEqual({ id: "2", name: "Beta" });
187
+ });
188
+
189
+ test("selector all returns array of entities", () => {
190
+ const { useEntity } = createEntityStore<TestEntity>([
191
+ { id: "1", name: "Alpha" },
192
+ ]);
193
+ const { result } = renderHook(() => useEntity("all"));
194
+ const [, actions] = result.current;
195
+
196
+ expect(result.current[0]).toEqual([{ id: "1", name: "Alpha" }]);
197
+
198
+ act(() => {
199
+ actions.addOne({ id: "2", name: "Beta" });
200
+ });
201
+
202
+ expect(result.current[0]).toEqual([
203
+ { id: "1", name: "Alpha" },
204
+ { id: "2", name: "Beta" },
205
+ ]);
206
+ });
207
+
208
+ test("selector ids returns array of entity ids", () => {
209
+ const { useEntity } = createEntityStore<TestEntity>([
210
+ { id: "1", name: "Alpha" },
211
+ { id: "2", name: "Beta" },
212
+ ]);
213
+ const { result } = renderHook(() => useEntity("ids"));
214
+ const [, actions] = result.current;
215
+
216
+ expect([...result.current[0]].sort()).toEqual(["1", "2"]);
217
+
218
+ act(() => {
219
+ actions.addOne({ id: "3", name: "Gamma" });
220
+ });
221
+
222
+ expect([...result.current[0]].sort()).toEqual(["1", "2", "3"]);
223
+ });
224
+
225
+ test("selector entities returns entity map", () => {
226
+ const { useEntity } = createEntityStore<TestEntity>([
227
+ { id: "1", name: "Alpha" },
228
+ ]);
229
+ const { result } = renderHook(() => useEntity("entities"));
230
+ const [, actions] = result.current;
231
+
232
+ expect(result.current[0]).toEqual({
233
+ "1": { id: "1", name: "Alpha" },
234
+ });
235
+
236
+ act(() => {
237
+ actions.addOne({ id: "2", name: "Beta" });
238
+ });
239
+
240
+ expect(result.current[0]).toEqual({
241
+ "1": { id: "1", name: "Alpha" },
242
+ "2": { id: "2", name: "Beta" },
243
+ });
244
+ });
245
+
246
+ test("selector total returns total entity count", () => {
247
+ const { useEntity } = createEntityStore<TestEntity>([
248
+ { id: "1", name: "Alpha" },
249
+ { id: "2", name: "Beta" },
250
+ ]);
251
+ const { result } = renderHook(() => useEntity("total"));
252
+ const [, actions] = result.current;
253
+
254
+ expect(result.current[0]).toBe(2);
255
+
256
+ act(() => {
257
+ actions.removeOne("1");
258
+ });
259
+
260
+ expect(result.current[0]).toBe(1);
261
+ });
262
+
263
+ test("defaults to full selector when no selector is provided", () => {
264
+ const { useEntity } = createEntityStore<TestEntity>([
265
+ { id: "1", name: "Alpha" },
266
+ ]);
267
+ const { result } = renderHook(() => useEntity());
268
+
269
+ expect(result.current[0].all).toEqual([{ id: "1", name: "Alpha" }]);
270
+ });
271
+ });
package/src/types.ts ADDED
@@ -0,0 +1,48 @@
1
+ import type { EntityId, Update } from "@reduxjs/toolkit";
2
+ import type { UncheckedIndexedAccess } from "./uncheckedindexed.ts";
3
+
4
+ export interface EntityStateAdapter<T, Id extends EntityId> {
5
+ addOne(entity: T): void;
6
+
7
+ addMany(entities: readonly T[] | Record<Id, T>): void;
8
+
9
+ setOne(entity: T): void;
10
+
11
+ setMany(entities: readonly T[] | Record<Id, T>): void;
12
+
13
+ setAll(entities: readonly T[] | Record<Id, T>): void;
14
+
15
+ removeOne(key: Id): void;
16
+
17
+ removeMany(keys: readonly Id[]): void;
18
+
19
+ removeAll(): void;
20
+
21
+ updateOne(update: Update<T, Id>): void;
22
+
23
+ updateMany(updates: ReadonlyArray<Update<T, Id>>): void;
24
+
25
+ upsertOne(entity: T): void;
26
+
27
+ upsertMany(entities: readonly T[] | Record<Id, T>): void;
28
+ }
29
+
30
+ type Id<T> = {
31
+ [K in keyof T]: T[K];
32
+ } & {};
33
+
34
+ export interface EntitySelectors<T, IdType extends EntityId> {
35
+ selectIds: () => IdType[];
36
+ selectEntities: () => Record<IdType, T>;
37
+ selectAll: () => T[];
38
+ selectTotal: () => number;
39
+ selectById: (id: IdType) => Id<UncheckedIndexedAccess<T>>;
40
+ }
41
+
42
+ export interface EntitySelectorsData<T, IdType extends EntityId> {
43
+ ids: IdType[];
44
+ entities: Record<IdType, T>;
45
+ all: T[];
46
+ total: number;
47
+ byId: (id: IdType) => Id<UncheckedIndexedAccess<T>>;
48
+ }
@@ -0,0 +1,16 @@
1
+ // inlined from https://github.com/EskiMojo14/uncheckedindexed
2
+ // relies on remaining as a TS file, not .d.ts
3
+ type IfMaybeUndefined<T, True, False> = [undefined] extends [T] ? True : False;
4
+
5
+ const testAccess = ({} as Record<string, 0>).a;
6
+
7
+ export type IfUncheckedIndexedAccess<True, False> = IfMaybeUndefined<
8
+ typeof testAccess,
9
+ True,
10
+ False
11
+ >;
12
+
13
+ export type UncheckedIndexedAccess<T> = IfUncheckedIndexedAccess<
14
+ T | undefined,
15
+ T
16
+ >;
@@ -0,0 +1,128 @@
1
+ import {
2
+ createEntityAdapter,
3
+ type EntityAdapter,
4
+ type EntitySelectors,
5
+ type EntityState,
6
+ } from "@reduxjs/toolkit";
7
+ import { Store, useStore } from "@tanstack/react-store";
8
+ import { useMemo } from "react";
9
+ import type { EntitySelectorsData, EntityStateAdapter } from "./types.ts";
10
+
11
+ const getEntityActionsTanstack = <
12
+ T extends {
13
+ id: string;
14
+ },
15
+ >(
16
+ store: Store<EntityState<T, T["id"]>>,
17
+ adapter: EntityAdapter<T, T["id"]>,
18
+ ): EntityStateAdapter<T, T["id"]> => ({
19
+ setOne: (entity) => store.setState((prev) => adapter.setOne(prev, entity)),
20
+ setMany: (entities) =>
21
+ store.setState((prev) => adapter.setMany(prev, entities)),
22
+ setAll: (entities) =>
23
+ store.setState((prev) => adapter.setAll(prev, entities)),
24
+
25
+ addOne: (entity) => store.setState((prev) => adapter.addOne(prev, entity)),
26
+ addMany: (entities) =>
27
+ store.setState((prev) => adapter.addMany(prev, entities)),
28
+
29
+ removeOne: (entityId) =>
30
+ store.setState((prev) => adapter.removeOne(prev, entityId)),
31
+ removeMany: (entityIds) =>
32
+ store.setState((prev) => adapter.removeMany(prev, entityIds)),
33
+ removeAll: () => store.setState((prev) => adapter.removeAll(prev)),
34
+
35
+ updateOne: (update) =>
36
+ store.setState((prev) => adapter.updateOne(prev, update)),
37
+ updateMany: (updates) =>
38
+ store.setState((prev) => adapter.updateMany(prev, updates)),
39
+
40
+ upsertOne: (entity) =>
41
+ store.setState((prev) => adapter.upsertOne(prev, entity)),
42
+ upsertMany: (entities) =>
43
+ store.setState((prev) => adapter.upsertMany(prev, entities)),
44
+ });
45
+
46
+ const getEntityStateTanstack = <
47
+ T extends {
48
+ id: string;
49
+ },
50
+ >(
51
+ entityState: EntityState<T, T["id"]>,
52
+ selectors: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>,
53
+ ): EntitySelectorsData<T, T["id"]> => ({
54
+ all: selectors.selectAll(entityState),
55
+ byId: (id) => selectors.selectById(entityState, id),
56
+ ids: selectors.selectIds(entityState) as T["id"][],
57
+ entities: selectors.selectEntities(entityState),
58
+ total: selectors.selectTotal(entityState),
59
+ });
60
+ const getSelectors = <
61
+ T extends {
62
+ id: string;
63
+ },
64
+ >(
65
+ baseSelectors: EntitySelectors<T, EntityState<T, T["id"]>, T["id"]>,
66
+ ) =>
67
+ ({
68
+ all: baseSelectors.selectAll,
69
+ entities: baseSelectors.selectEntities,
70
+ ids: baseSelectors.selectIds,
71
+ total: baseSelectors.selectTotal,
72
+ full: (state) => getEntityStateTanstack(state, baseSelectors),
73
+ }) satisfies Record<
74
+ keyof Omit<EntitySelectorsData<T, T["id"]>, "byId"> | "full",
75
+ (state: EntityState<T, T["id"]>) => unknown
76
+ >;
77
+
78
+ export const createEntityStore = <
79
+ T extends {
80
+ id: string;
81
+ },
82
+ >(
83
+ initialState?: T[],
84
+ ) => {
85
+ const adapter = createEntityAdapter<T>();
86
+ const store = new Store(
87
+ initialState
88
+ ? adapter.setAll(adapter.getInitialState(), initialState)
89
+ : adapter.getInitialState(),
90
+ );
91
+ const baseSelectors = adapter.getSelectors<EntityState<T, T["id"]>>(
92
+ (input) => input,
93
+ );
94
+ const MySelectors = getSelectors(baseSelectors);
95
+
96
+ type SelectorKey = keyof typeof MySelectors;
97
+ type SelectorReturn<K extends SelectorKey> = ReturnType<
98
+ (typeof MySelectors)[K]
99
+ >;
100
+
101
+ function useEntity(): [
102
+ SelectorReturn<"full">,
103
+ EntityStateAdapter<T, T["id"]>,
104
+ ];
105
+ function useEntity(
106
+ selector: "full",
107
+ ): [SelectorReturn<"full">, EntityStateAdapter<T, T["id"]>];
108
+ function useEntity<K extends Exclude<SelectorKey, "full">>(
109
+ selector: K,
110
+ ): [SelectorReturn<K>, EntityStateAdapter<T, T["id"]>];
111
+
112
+ function useEntity<K extends SelectorKey>(selector: K = "full" as K) {
113
+ const entityState = useStore(
114
+ store,
115
+ MySelectors[selector] as () => SelectorReturn<K>,
116
+ );
117
+ const actions = useMemo<EntityStateAdapter<T, T["id"]>>(
118
+ () => getEntityActionsTanstack(store, adapter),
119
+ [],
120
+ );
121
+ return [entityState, actions] satisfies [
122
+ SelectorReturn<K>,
123
+ EntityStateAdapter<T, T["id"]>,
124
+ ];
125
+ }
126
+
127
+ return { useEntity, store, adapter };
128
+ };
@@ -0,0 +1,10 @@
1
+ import { afterEach, expect } from "bun:test";
2
+ import * as matchers from "@testing-library/jest-dom/matchers";
3
+ import { cleanup } from "@testing-library/react";
4
+
5
+ expect.extend(matchers);
6
+
7
+ // Optional: cleans up `render` after each test
8
+ afterEach(() => {
9
+ cleanup();
10
+ });
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext"],
5
+ "target": "ESNext",
6
+ "module": "Preserve",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+
11
+ // Bundler mode
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "verbatimModuleSyntax": true,
15
+ "noEmit": true,
16
+
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedIndexedAccess": true,
22
+ "noImplicitOverride": true,
23
+
24
+ // Some stricter flags (disabled by default)
25
+ "noUnusedLocals": false,
26
+ "noUnusedParameters": false,
27
+ "noPropertyAccessFromIndexSignature": false
28
+ }
29
+ }