@noy-db/in-tanstack-query 0.1.0-pre.3

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 vLannaAi
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,33 @@
1
+ # @noy-db/in-tanstack-query
2
+
3
+ [![npm](https://img.shields.io/npm/v/%40noy-db/in-tanstack-query.svg)](https://www.npmjs.com/package/@noy-db/in-tanstack-query)
4
+
5
+ > TanStack Query adapter for noy-db
6
+
7
+ Part of [**`@noy-db/hub`**](https://www.npmjs.com/package/@noy-db/hub) — the zero-knowledge, offline-first, encrypted document store.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pnpm add @noy-db/hub @noy-db/in-tanstack-query
13
+ ```
14
+
15
+ ## What it is
16
+
17
+ TanStack Query adapter for noy-db — queryFn + mutationFn helpers that speak the Collection API, plus automatic invalidation from noy-db change events. Framework-agnostic (React / Vue / Solid / Svelte).
18
+
19
+ ## Status
20
+
21
+ **Pre-release** (`0.1.0-pre.1`). API may change before `1.0`.
22
+
23
+ ## Documentation
24
+
25
+ See the [main repository](https://github.com/vLannaAi/noy-db#readme) for setup, examples, and the full subsystem catalog.
26
+
27
+ - Source — [`packages/in-tanstack-query`](https://github.com/vLannaAi/noy-db/tree/main/packages/in-tanstack-query)
28
+ - Issues — [github.com/vLannaAi/noy-db/issues](https://github.com/vLannaAi/noy-db/issues)
29
+ - Spec — [`SPEC.md`](https://github.com/vLannaAi/noy-db/blob/main/SPEC.md)
30
+
31
+ ## License
32
+
33
+ [MIT](./LICENSE) © vLannaAi
package/dist/index.cjs ADDED
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ bindInvalidation: () => bindInvalidation,
24
+ collectionDeleteOptions: () => collectionDeleteOptions,
25
+ collectionGetOptions: () => collectionGetOptions,
26
+ collectionListOptions: () => collectionListOptions,
27
+ collectionPutOptions: () => collectionPutOptions,
28
+ collectionQueryKey: () => collectionQueryKey,
29
+ collectionQueryOptions: () => collectionQueryOptions
30
+ });
31
+ module.exports = __toCommonJS(index_exports);
32
+ function collectionQueryKey(vault, collection, ...extra) {
33
+ return ["noy-db", vault, collection, ...extra];
34
+ }
35
+ function collectionListOptions(vault, collectionName, getCollection) {
36
+ return {
37
+ queryKey: collectionQueryKey(vault, collectionName, "list"),
38
+ queryFn: async () => {
39
+ const coll = getCollection();
40
+ return coll.list();
41
+ }
42
+ };
43
+ }
44
+ function collectionGetOptions(vault, collectionName, id, getCollection) {
45
+ return {
46
+ queryKey: collectionQueryKey(vault, collectionName, "get", id),
47
+ queryFn: async () => {
48
+ const coll = getCollection();
49
+ return coll.get(id);
50
+ }
51
+ };
52
+ }
53
+ function collectionQueryOptions(vault, collectionName, getCollection, builder, keyTag = "query") {
54
+ return {
55
+ queryKey: collectionQueryKey(vault, collectionName, keyTag),
56
+ queryFn: async () => {
57
+ const coll = getCollection();
58
+ return Promise.resolve(builder(coll.query()));
59
+ }
60
+ };
61
+ }
62
+ function collectionPutOptions(getCollection) {
63
+ return {
64
+ mutationFn: async ({ id, record }) => {
65
+ const coll = getCollection();
66
+ await coll.put(id, record);
67
+ }
68
+ };
69
+ }
70
+ function collectionDeleteOptions(getCollection) {
71
+ return {
72
+ mutationFn: async ({ id }) => {
73
+ const coll = getCollection();
74
+ await coll.delete(id);
75
+ }
76
+ };
77
+ }
78
+ function bindInvalidation(queryClient, vault, collectionName, collection) {
79
+ return collection.subscribe(() => {
80
+ void queryClient.invalidateQueries({
81
+ queryKey: collectionQueryKey(vault, collectionName)
82
+ });
83
+ });
84
+ }
85
+ // Annotate the CommonJS export names for ESM import in node:
86
+ 0 && (module.exports = {
87
+ bindInvalidation,
88
+ collectionDeleteOptions,
89
+ collectionGetOptions,
90
+ collectionListOptions,
91
+ collectionPutOptions,
92
+ collectionQueryKey,
93
+ collectionQueryOptions
94
+ });
95
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * **@noy-db/in-tanstack-query** — TanStack Query adapter for noy-db.\n *\n * Three framework-agnostic pieces:\n *\n * 1. **{@link collectionQueryOptions}** — returns a\n * `{ queryKey, queryFn }` pair ready to hand to `useQuery()` in\n * any TanStack-supported framework (React, Vue, Solid, Svelte).\n *\n * 2. **{@link collectionMutationOptions}** — same shape for\n * `useMutation()`, with `put`/`delete` actions against a\n * `Collection`.\n *\n * 3. **{@link bindInvalidation}** — subscribes to a `Collection`'s\n * change stream and invalidates the matching query keys on every\n * mutation. Drop-in replacement for ad-hoc `invalidateQueries`\n * calls scattered through mutation callbacks.\n *\n * Keeping these as option-factories (not custom hooks) means the\n * same code works across every TanStack framework binding — no\n * framework-specific exports needed.\n *\n * @packageDocumentation\n */\n\nimport type { Collection, Query } from '@noy-db/hub'\nimport type { QueryClient } from '@tanstack/query-core'\n\n/** The canonical TanStack queryKey shape for a collection scope. */\nexport type CollectionQueryKey =\n | readonly ['noy-db', string, string]\n | readonly ['noy-db', string, string, string]\n | readonly ['noy-db', string, string, ...unknown[]]\n\nexport function collectionQueryKey(vault: string, collection: string, ...extra: unknown[]): CollectionQueryKey {\n return ['noy-db', vault, collection, ...extra] as CollectionQueryKey\n}\n\n/**\n * Build `{ queryKey, queryFn }` for a Collection list query. Pass the\n * returned object directly into `useQuery(options)` — works across\n * every TanStack framework binding.\n */\nexport function collectionListOptions<T>(\n vault: string,\n collectionName: string,\n getCollection: () => Collection<T>,\n): { queryKey: CollectionQueryKey; queryFn: () => Promise<T[]> } {\n return {\n queryKey: collectionQueryKey(vault, collectionName, 'list'),\n queryFn: async () => {\n const coll = getCollection()\n return coll.list()\n },\n }\n}\n\n/**\n * Build `{ queryKey, queryFn }` for a single-record query. Returns\n * `null` when the record is absent.\n */\nexport function collectionGetOptions<T>(\n vault: string,\n collectionName: string,\n id: string,\n getCollection: () => Collection<T>,\n): { queryKey: CollectionQueryKey; queryFn: () => Promise<T | null> } {\n return {\n queryKey: collectionQueryKey(vault, collectionName, 'get', id),\n queryFn: async () => {\n const coll = getCollection()\n return coll.get(id)\n },\n }\n}\n\n/**\n * Build `{ queryKey, queryFn }` for an arbitrary query builder. The\n * builder receives the collection's `query()` chain and returns a\n * terminal result (`.toArray()`, `.count()`, `.aggregate({...})`, …).\n */\nexport function collectionQueryOptions<T, R>(\n vault: string,\n collectionName: string,\n getCollection: () => Collection<T>,\n builder: (q: Query<T>) => Promise<R> | R,\n keyTag: string = 'query',\n): { queryKey: CollectionQueryKey; queryFn: () => Promise<R> } {\n return {\n queryKey: collectionQueryKey(vault, collectionName, keyTag),\n queryFn: async () => {\n const coll = getCollection()\n return Promise.resolve(builder(coll.query() as unknown as Query<T>))\n },\n }\n}\n\n/**\n * Build `{ mutationFn }` for a record put. Caller's `onSuccess` is\n * where you'd usually call `invalidateQueries(collectionQueryKey(...))`\n * — or let {@link bindInvalidation} do it automatically.\n */\nexport function collectionPutOptions<T>(\n getCollection: () => Collection<T>,\n): { mutationFn: (args: { id: string; record: T }) => Promise<void> } {\n return {\n mutationFn: async ({ id, record }) => {\n const coll = getCollection()\n await coll.put(id, record)\n },\n }\n}\n\n/** Build `{ mutationFn }` for a record delete. */\nexport function collectionDeleteOptions<T>(\n getCollection: () => Collection<T>,\n): { mutationFn: (args: { id: string }) => Promise<void> } {\n return {\n mutationFn: async ({ id }) => {\n const coll = getCollection()\n await coll.delete(id)\n },\n }\n}\n\n/**\n * Subscribe to a Collection's change stream and invalidate the\n * corresponding query scope on every mutation. Returns an unsubscribe\n * function — call on cleanup (framework-specific).\n *\n * ```ts\n * const stop = bindInvalidation(queryClient, 'acme', 'invoices', invoices)\n * // … later, e.g. in React's useEffect cleanup:\n * stop()\n * ```\n */\nexport function bindInvalidation<T>(\n queryClient: QueryClient,\n vault: string,\n collectionName: string,\n collection: Collection<T>,\n): () => void {\n return collection.subscribe(() => {\n void queryClient.invalidateQueries({\n queryKey: collectionQueryKey(vault, collectionName),\n })\n })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAkCO,SAAS,mBAAmB,OAAe,eAAuB,OAAsC;AAC7G,SAAO,CAAC,UAAU,OAAO,YAAY,GAAG,KAAK;AAC/C;AAOO,SAAS,sBACd,OACA,gBACA,eAC+D;AAC/D,SAAO;AAAA,IACL,UAAU,mBAAmB,OAAO,gBAAgB,MAAM;AAAA,IAC1D,SAAS,YAAY;AACnB,YAAM,OAAO,cAAc;AAC3B,aAAO,KAAK,KAAK;AAAA,IACnB;AAAA,EACF;AACF;AAMO,SAAS,qBACd,OACA,gBACA,IACA,eACoE;AACpE,SAAO;AAAA,IACL,UAAU,mBAAmB,OAAO,gBAAgB,OAAO,EAAE;AAAA,IAC7D,SAAS,YAAY;AACnB,YAAM,OAAO,cAAc;AAC3B,aAAO,KAAK,IAAI,EAAE;AAAA,IACpB;AAAA,EACF;AACF;AAOO,SAAS,uBACd,OACA,gBACA,eACA,SACA,SAAiB,SAC4C;AAC7D,SAAO;AAAA,IACL,UAAU,mBAAmB,OAAO,gBAAgB,MAAM;AAAA,IAC1D,SAAS,YAAY;AACnB,YAAM,OAAO,cAAc;AAC3B,aAAO,QAAQ,QAAQ,QAAQ,KAAK,MAAM,CAAwB,CAAC;AAAA,IACrE;AAAA,EACF;AACF;AAOO,SAAS,qBACd,eACoE;AACpE,SAAO;AAAA,IACL,YAAY,OAAO,EAAE,IAAI,OAAO,MAAM;AACpC,YAAM,OAAO,cAAc;AAC3B,YAAM,KAAK,IAAI,IAAI,MAAM;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,wBACd,eACyD;AACzD,SAAO;AAAA,IACL,YAAY,OAAO,EAAE,GAAG,MAAM;AAC5B,YAAM,OAAO,cAAc;AAC3B,YAAM,KAAK,OAAO,EAAE;AAAA,IACtB;AAAA,EACF;AACF;AAaO,SAAS,iBACd,aACA,OACA,gBACA,YACY;AACZ,SAAO,WAAW,UAAU,MAAM;AAChC,SAAK,YAAY,kBAAkB;AAAA,MACjC,UAAU,mBAAmB,OAAO,cAAc;AAAA,IACpD,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
@@ -0,0 +1,88 @@
1
+ import { Collection, Query } from '@noy-db/hub';
2
+ import { QueryClient } from '@tanstack/query-core';
3
+
4
+ /**
5
+ * **@noy-db/in-tanstack-query** — TanStack Query adapter for noy-db.
6
+ *
7
+ * Three framework-agnostic pieces:
8
+ *
9
+ * 1. **{@link collectionQueryOptions}** — returns a
10
+ * `{ queryKey, queryFn }` pair ready to hand to `useQuery()` in
11
+ * any TanStack-supported framework (React, Vue, Solid, Svelte).
12
+ *
13
+ * 2. **{@link collectionMutationOptions}** — same shape for
14
+ * `useMutation()`, with `put`/`delete` actions against a
15
+ * `Collection`.
16
+ *
17
+ * 3. **{@link bindInvalidation}** — subscribes to a `Collection`'s
18
+ * change stream and invalidates the matching query keys on every
19
+ * mutation. Drop-in replacement for ad-hoc `invalidateQueries`
20
+ * calls scattered through mutation callbacks.
21
+ *
22
+ * Keeping these as option-factories (not custom hooks) means the
23
+ * same code works across every TanStack framework binding — no
24
+ * framework-specific exports needed.
25
+ *
26
+ * @packageDocumentation
27
+ */
28
+
29
+ /** The canonical TanStack queryKey shape for a collection scope. */
30
+ type CollectionQueryKey = readonly ['noy-db', string, string] | readonly ['noy-db', string, string, string] | readonly ['noy-db', string, string, ...unknown[]];
31
+ declare function collectionQueryKey(vault: string, collection: string, ...extra: unknown[]): CollectionQueryKey;
32
+ /**
33
+ * Build `{ queryKey, queryFn }` for a Collection list query. Pass the
34
+ * returned object directly into `useQuery(options)` — works across
35
+ * every TanStack framework binding.
36
+ */
37
+ declare function collectionListOptions<T>(vault: string, collectionName: string, getCollection: () => Collection<T>): {
38
+ queryKey: CollectionQueryKey;
39
+ queryFn: () => Promise<T[]>;
40
+ };
41
+ /**
42
+ * Build `{ queryKey, queryFn }` for a single-record query. Returns
43
+ * `null` when the record is absent.
44
+ */
45
+ declare function collectionGetOptions<T>(vault: string, collectionName: string, id: string, getCollection: () => Collection<T>): {
46
+ queryKey: CollectionQueryKey;
47
+ queryFn: () => Promise<T | null>;
48
+ };
49
+ /**
50
+ * Build `{ queryKey, queryFn }` for an arbitrary query builder. The
51
+ * builder receives the collection's `query()` chain and returns a
52
+ * terminal result (`.toArray()`, `.count()`, `.aggregate({...})`, …).
53
+ */
54
+ declare function collectionQueryOptions<T, R>(vault: string, collectionName: string, getCollection: () => Collection<T>, builder: (q: Query<T>) => Promise<R> | R, keyTag?: string): {
55
+ queryKey: CollectionQueryKey;
56
+ queryFn: () => Promise<R>;
57
+ };
58
+ /**
59
+ * Build `{ mutationFn }` for a record put. Caller's `onSuccess` is
60
+ * where you'd usually call `invalidateQueries(collectionQueryKey(...))`
61
+ * — or let {@link bindInvalidation} do it automatically.
62
+ */
63
+ declare function collectionPutOptions<T>(getCollection: () => Collection<T>): {
64
+ mutationFn: (args: {
65
+ id: string;
66
+ record: T;
67
+ }) => Promise<void>;
68
+ };
69
+ /** Build `{ mutationFn }` for a record delete. */
70
+ declare function collectionDeleteOptions<T>(getCollection: () => Collection<T>): {
71
+ mutationFn: (args: {
72
+ id: string;
73
+ }) => Promise<void>;
74
+ };
75
+ /**
76
+ * Subscribe to a Collection's change stream and invalidate the
77
+ * corresponding query scope on every mutation. Returns an unsubscribe
78
+ * function — call on cleanup (framework-specific).
79
+ *
80
+ * ```ts
81
+ * const stop = bindInvalidation(queryClient, 'acme', 'invoices', invoices)
82
+ * // … later, e.g. in React's useEffect cleanup:
83
+ * stop()
84
+ * ```
85
+ */
86
+ declare function bindInvalidation<T>(queryClient: QueryClient, vault: string, collectionName: string, collection: Collection<T>): () => void;
87
+
88
+ export { type CollectionQueryKey, bindInvalidation, collectionDeleteOptions, collectionGetOptions, collectionListOptions, collectionPutOptions, collectionQueryKey, collectionQueryOptions };
@@ -0,0 +1,88 @@
1
+ import { Collection, Query } from '@noy-db/hub';
2
+ import { QueryClient } from '@tanstack/query-core';
3
+
4
+ /**
5
+ * **@noy-db/in-tanstack-query** — TanStack Query adapter for noy-db.
6
+ *
7
+ * Three framework-agnostic pieces:
8
+ *
9
+ * 1. **{@link collectionQueryOptions}** — returns a
10
+ * `{ queryKey, queryFn }` pair ready to hand to `useQuery()` in
11
+ * any TanStack-supported framework (React, Vue, Solid, Svelte).
12
+ *
13
+ * 2. **{@link collectionMutationOptions}** — same shape for
14
+ * `useMutation()`, with `put`/`delete` actions against a
15
+ * `Collection`.
16
+ *
17
+ * 3. **{@link bindInvalidation}** — subscribes to a `Collection`'s
18
+ * change stream and invalidates the matching query keys on every
19
+ * mutation. Drop-in replacement for ad-hoc `invalidateQueries`
20
+ * calls scattered through mutation callbacks.
21
+ *
22
+ * Keeping these as option-factories (not custom hooks) means the
23
+ * same code works across every TanStack framework binding — no
24
+ * framework-specific exports needed.
25
+ *
26
+ * @packageDocumentation
27
+ */
28
+
29
+ /** The canonical TanStack queryKey shape for a collection scope. */
30
+ type CollectionQueryKey = readonly ['noy-db', string, string] | readonly ['noy-db', string, string, string] | readonly ['noy-db', string, string, ...unknown[]];
31
+ declare function collectionQueryKey(vault: string, collection: string, ...extra: unknown[]): CollectionQueryKey;
32
+ /**
33
+ * Build `{ queryKey, queryFn }` for a Collection list query. Pass the
34
+ * returned object directly into `useQuery(options)` — works across
35
+ * every TanStack framework binding.
36
+ */
37
+ declare function collectionListOptions<T>(vault: string, collectionName: string, getCollection: () => Collection<T>): {
38
+ queryKey: CollectionQueryKey;
39
+ queryFn: () => Promise<T[]>;
40
+ };
41
+ /**
42
+ * Build `{ queryKey, queryFn }` for a single-record query. Returns
43
+ * `null` when the record is absent.
44
+ */
45
+ declare function collectionGetOptions<T>(vault: string, collectionName: string, id: string, getCollection: () => Collection<T>): {
46
+ queryKey: CollectionQueryKey;
47
+ queryFn: () => Promise<T | null>;
48
+ };
49
+ /**
50
+ * Build `{ queryKey, queryFn }` for an arbitrary query builder. The
51
+ * builder receives the collection's `query()` chain and returns a
52
+ * terminal result (`.toArray()`, `.count()`, `.aggregate({...})`, …).
53
+ */
54
+ declare function collectionQueryOptions<T, R>(vault: string, collectionName: string, getCollection: () => Collection<T>, builder: (q: Query<T>) => Promise<R> | R, keyTag?: string): {
55
+ queryKey: CollectionQueryKey;
56
+ queryFn: () => Promise<R>;
57
+ };
58
+ /**
59
+ * Build `{ mutationFn }` for a record put. Caller's `onSuccess` is
60
+ * where you'd usually call `invalidateQueries(collectionQueryKey(...))`
61
+ * — or let {@link bindInvalidation} do it automatically.
62
+ */
63
+ declare function collectionPutOptions<T>(getCollection: () => Collection<T>): {
64
+ mutationFn: (args: {
65
+ id: string;
66
+ record: T;
67
+ }) => Promise<void>;
68
+ };
69
+ /** Build `{ mutationFn }` for a record delete. */
70
+ declare function collectionDeleteOptions<T>(getCollection: () => Collection<T>): {
71
+ mutationFn: (args: {
72
+ id: string;
73
+ }) => Promise<void>;
74
+ };
75
+ /**
76
+ * Subscribe to a Collection's change stream and invalidate the
77
+ * corresponding query scope on every mutation. Returns an unsubscribe
78
+ * function — call on cleanup (framework-specific).
79
+ *
80
+ * ```ts
81
+ * const stop = bindInvalidation(queryClient, 'acme', 'invoices', invoices)
82
+ * // … later, e.g. in React's useEffect cleanup:
83
+ * stop()
84
+ * ```
85
+ */
86
+ declare function bindInvalidation<T>(queryClient: QueryClient, vault: string, collectionName: string, collection: Collection<T>): () => void;
87
+
88
+ export { type CollectionQueryKey, bindInvalidation, collectionDeleteOptions, collectionGetOptions, collectionListOptions, collectionPutOptions, collectionQueryKey, collectionQueryOptions };
package/dist/index.js ADDED
@@ -0,0 +1,64 @@
1
+ // src/index.ts
2
+ function collectionQueryKey(vault, collection, ...extra) {
3
+ return ["noy-db", vault, collection, ...extra];
4
+ }
5
+ function collectionListOptions(vault, collectionName, getCollection) {
6
+ return {
7
+ queryKey: collectionQueryKey(vault, collectionName, "list"),
8
+ queryFn: async () => {
9
+ const coll = getCollection();
10
+ return coll.list();
11
+ }
12
+ };
13
+ }
14
+ function collectionGetOptions(vault, collectionName, id, getCollection) {
15
+ return {
16
+ queryKey: collectionQueryKey(vault, collectionName, "get", id),
17
+ queryFn: async () => {
18
+ const coll = getCollection();
19
+ return coll.get(id);
20
+ }
21
+ };
22
+ }
23
+ function collectionQueryOptions(vault, collectionName, getCollection, builder, keyTag = "query") {
24
+ return {
25
+ queryKey: collectionQueryKey(vault, collectionName, keyTag),
26
+ queryFn: async () => {
27
+ const coll = getCollection();
28
+ return Promise.resolve(builder(coll.query()));
29
+ }
30
+ };
31
+ }
32
+ function collectionPutOptions(getCollection) {
33
+ return {
34
+ mutationFn: async ({ id, record }) => {
35
+ const coll = getCollection();
36
+ await coll.put(id, record);
37
+ }
38
+ };
39
+ }
40
+ function collectionDeleteOptions(getCollection) {
41
+ return {
42
+ mutationFn: async ({ id }) => {
43
+ const coll = getCollection();
44
+ await coll.delete(id);
45
+ }
46
+ };
47
+ }
48
+ function bindInvalidation(queryClient, vault, collectionName, collection) {
49
+ return collection.subscribe(() => {
50
+ void queryClient.invalidateQueries({
51
+ queryKey: collectionQueryKey(vault, collectionName)
52
+ });
53
+ });
54
+ }
55
+ export {
56
+ bindInvalidation,
57
+ collectionDeleteOptions,
58
+ collectionGetOptions,
59
+ collectionListOptions,
60
+ collectionPutOptions,
61
+ collectionQueryKey,
62
+ collectionQueryOptions
63
+ };
64
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["/**\n * **@noy-db/in-tanstack-query** — TanStack Query adapter for noy-db.\n *\n * Three framework-agnostic pieces:\n *\n * 1. **{@link collectionQueryOptions}** — returns a\n * `{ queryKey, queryFn }` pair ready to hand to `useQuery()` in\n * any TanStack-supported framework (React, Vue, Solid, Svelte).\n *\n * 2. **{@link collectionMutationOptions}** — same shape for\n * `useMutation()`, with `put`/`delete` actions against a\n * `Collection`.\n *\n * 3. **{@link bindInvalidation}** — subscribes to a `Collection`'s\n * change stream and invalidates the matching query keys on every\n * mutation. Drop-in replacement for ad-hoc `invalidateQueries`\n * calls scattered through mutation callbacks.\n *\n * Keeping these as option-factories (not custom hooks) means the\n * same code works across every TanStack framework binding — no\n * framework-specific exports needed.\n *\n * @packageDocumentation\n */\n\nimport type { Collection, Query } from '@noy-db/hub'\nimport type { QueryClient } from '@tanstack/query-core'\n\n/** The canonical TanStack queryKey shape for a collection scope. */\nexport type CollectionQueryKey =\n | readonly ['noy-db', string, string]\n | readonly ['noy-db', string, string, string]\n | readonly ['noy-db', string, string, ...unknown[]]\n\nexport function collectionQueryKey(vault: string, collection: string, ...extra: unknown[]): CollectionQueryKey {\n return ['noy-db', vault, collection, ...extra] as CollectionQueryKey\n}\n\n/**\n * Build `{ queryKey, queryFn }` for a Collection list query. Pass the\n * returned object directly into `useQuery(options)` — works across\n * every TanStack framework binding.\n */\nexport function collectionListOptions<T>(\n vault: string,\n collectionName: string,\n getCollection: () => Collection<T>,\n): { queryKey: CollectionQueryKey; queryFn: () => Promise<T[]> } {\n return {\n queryKey: collectionQueryKey(vault, collectionName, 'list'),\n queryFn: async () => {\n const coll = getCollection()\n return coll.list()\n },\n }\n}\n\n/**\n * Build `{ queryKey, queryFn }` for a single-record query. Returns\n * `null` when the record is absent.\n */\nexport function collectionGetOptions<T>(\n vault: string,\n collectionName: string,\n id: string,\n getCollection: () => Collection<T>,\n): { queryKey: CollectionQueryKey; queryFn: () => Promise<T | null> } {\n return {\n queryKey: collectionQueryKey(vault, collectionName, 'get', id),\n queryFn: async () => {\n const coll = getCollection()\n return coll.get(id)\n },\n }\n}\n\n/**\n * Build `{ queryKey, queryFn }` for an arbitrary query builder. The\n * builder receives the collection's `query()` chain and returns a\n * terminal result (`.toArray()`, `.count()`, `.aggregate({...})`, …).\n */\nexport function collectionQueryOptions<T, R>(\n vault: string,\n collectionName: string,\n getCollection: () => Collection<T>,\n builder: (q: Query<T>) => Promise<R> | R,\n keyTag: string = 'query',\n): { queryKey: CollectionQueryKey; queryFn: () => Promise<R> } {\n return {\n queryKey: collectionQueryKey(vault, collectionName, keyTag),\n queryFn: async () => {\n const coll = getCollection()\n return Promise.resolve(builder(coll.query() as unknown as Query<T>))\n },\n }\n}\n\n/**\n * Build `{ mutationFn }` for a record put. Caller's `onSuccess` is\n * where you'd usually call `invalidateQueries(collectionQueryKey(...))`\n * — or let {@link bindInvalidation} do it automatically.\n */\nexport function collectionPutOptions<T>(\n getCollection: () => Collection<T>,\n): { mutationFn: (args: { id: string; record: T }) => Promise<void> } {\n return {\n mutationFn: async ({ id, record }) => {\n const coll = getCollection()\n await coll.put(id, record)\n },\n }\n}\n\n/** Build `{ mutationFn }` for a record delete. */\nexport function collectionDeleteOptions<T>(\n getCollection: () => Collection<T>,\n): { mutationFn: (args: { id: string }) => Promise<void> } {\n return {\n mutationFn: async ({ id }) => {\n const coll = getCollection()\n await coll.delete(id)\n },\n }\n}\n\n/**\n * Subscribe to a Collection's change stream and invalidate the\n * corresponding query scope on every mutation. Returns an unsubscribe\n * function — call on cleanup (framework-specific).\n *\n * ```ts\n * const stop = bindInvalidation(queryClient, 'acme', 'invoices', invoices)\n * // … later, e.g. in React's useEffect cleanup:\n * stop()\n * ```\n */\nexport function bindInvalidation<T>(\n queryClient: QueryClient,\n vault: string,\n collectionName: string,\n collection: Collection<T>,\n): () => void {\n return collection.subscribe(() => {\n void queryClient.invalidateQueries({\n queryKey: collectionQueryKey(vault, collectionName),\n })\n })\n}\n"],"mappings":";AAkCO,SAAS,mBAAmB,OAAe,eAAuB,OAAsC;AAC7G,SAAO,CAAC,UAAU,OAAO,YAAY,GAAG,KAAK;AAC/C;AAOO,SAAS,sBACd,OACA,gBACA,eAC+D;AAC/D,SAAO;AAAA,IACL,UAAU,mBAAmB,OAAO,gBAAgB,MAAM;AAAA,IAC1D,SAAS,YAAY;AACnB,YAAM,OAAO,cAAc;AAC3B,aAAO,KAAK,KAAK;AAAA,IACnB;AAAA,EACF;AACF;AAMO,SAAS,qBACd,OACA,gBACA,IACA,eACoE;AACpE,SAAO;AAAA,IACL,UAAU,mBAAmB,OAAO,gBAAgB,OAAO,EAAE;AAAA,IAC7D,SAAS,YAAY;AACnB,YAAM,OAAO,cAAc;AAC3B,aAAO,KAAK,IAAI,EAAE;AAAA,IACpB;AAAA,EACF;AACF;AAOO,SAAS,uBACd,OACA,gBACA,eACA,SACA,SAAiB,SAC4C;AAC7D,SAAO;AAAA,IACL,UAAU,mBAAmB,OAAO,gBAAgB,MAAM;AAAA,IAC1D,SAAS,YAAY;AACnB,YAAM,OAAO,cAAc;AAC3B,aAAO,QAAQ,QAAQ,QAAQ,KAAK,MAAM,CAAwB,CAAC;AAAA,IACrE;AAAA,EACF;AACF;AAOO,SAAS,qBACd,eACoE;AACpE,SAAO;AAAA,IACL,YAAY,OAAO,EAAE,IAAI,OAAO,MAAM;AACpC,YAAM,OAAO,cAAc;AAC3B,YAAM,KAAK,IAAI,IAAI,MAAM;AAAA,IAC3B;AAAA,EACF;AACF;AAGO,SAAS,wBACd,eACyD;AACzD,SAAO;AAAA,IACL,YAAY,OAAO,EAAE,GAAG,MAAM;AAC5B,YAAM,OAAO,cAAc;AAC3B,YAAM,KAAK,OAAO,EAAE;AAAA,IACtB;AAAA,EACF;AACF;AAaO,SAAS,iBACd,aACA,OACA,gBACA,YACY;AACZ,SAAO,WAAW,UAAU,MAAM;AAChC,SAAK,YAAY,kBAAkB;AAAA,MACjC,UAAU,mBAAmB,OAAO,cAAc;AAAA,IACpD,CAAC;AAAA,EACH,CAAC;AACH;","names":[]}
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@noy-db/in-tanstack-query",
3
+ "version": "0.1.0-pre.3",
4
+ "description": "TanStack Query adapter for noy-db — queryFn + mutationFn helpers that speak the Collection API, plus automatic invalidation from noy-db change events. Framework-agnostic (React / Vue / Solid / Svelte).",
5
+ "license": "MIT",
6
+ "author": "vLannaAi <vicio@lanna.ai>",
7
+ "homepage": "https://github.com/vLannaAi/noy-db/tree/main/packages/in-tanstack-query#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/vLannaAi/noy-db.git",
11
+ "directory": "packages/in-tanstack-query"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/vLannaAi/noy-db/issues"
15
+ },
16
+ "type": "module",
17
+ "sideEffects": false,
18
+ "exports": {
19
+ ".": {
20
+ "import": {
21
+ "types": "./dist/index.d.ts",
22
+ "default": "./dist/index.js"
23
+ },
24
+ "require": {
25
+ "types": "./dist/index.d.cts",
26
+ "default": "./dist/index.cjs"
27
+ }
28
+ }
29
+ },
30
+ "main": "./dist/index.cjs",
31
+ "module": "./dist/index.js",
32
+ "types": "./dist/index.d.ts",
33
+ "files": [
34
+ "dist",
35
+ "README.md",
36
+ "LICENSE"
37
+ ],
38
+ "engines": {
39
+ "node": ">=18.0.0"
40
+ },
41
+ "peerDependencies": {
42
+ "@tanstack/query-core": "^5.0.0",
43
+ "@noy-db/hub": "0.1.0-pre.3"
44
+ },
45
+ "devDependencies": {
46
+ "@tanstack/query-core": "^5.59.0",
47
+ "@noy-db/hub": "0.1.0-pre.3"
48
+ },
49
+ "keywords": [
50
+ "noy-db",
51
+ "in-tanstack-query",
52
+ "tanstack",
53
+ "query",
54
+ "cache",
55
+ "data-fetching"
56
+ ],
57
+ "publishConfig": {
58
+ "access": "public",
59
+ "tag": "latest"
60
+ },
61
+ "scripts": {
62
+ "build": "tsup",
63
+ "test": "vitest run",
64
+ "lint": "eslint src/",
65
+ "typecheck": "tsc --noEmit"
66
+ }
67
+ }