@noy-db/in-pinia 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.
@@ -0,0 +1,411 @@
1
+ import * as pinia from 'pinia';
2
+ import { StateTree, PiniaPlugin } from 'pinia';
3
+ import { ShallowRef, Ref, ComputedRef } from 'vue';
4
+ import { Query, Noydb, StandardSchemaV1, NoydbStore as NoydbStore$1, NoydbOptions, Role, Vault } from '@noy-db/hub';
5
+
6
+ /**
7
+ * Reactive handle returned by `store.liveQuery(fn)`. Mirrors a hub
8
+ * `LiveQuery<R>` into Vue refs; `items` updates on every left- or
9
+ * joined-right-side mutation, `error` carries re-run errors as state
10
+ * (a `DanglingReferenceError` in strict join mode is the common case),
11
+ * `stop()` tears down upstream subscriptions. Auto-disposed on scope
12
+ * teardown when called inside a Vue setup / Pinia store body.
13
+ */
14
+ interface NoydbLiveQuery<R> {
15
+ items: ShallowRef<readonly R[]>;
16
+ error: Ref<Error | null>;
17
+ stop(): void;
18
+ }
19
+ /**
20
+ * Options accepted by `defineNoydbStore`.
21
+ *
22
+ * Generic `T` is the record shape — defaults to `unknown` if the caller
23
+ * doesn't supply a type. Use `defineNoydbStore<Invoice>('invoices', {...})`
24
+ * for full type safety.
25
+ */
26
+ interface NoydbStoreOptions<T> {
27
+ /** Vault (tenant) name. */
28
+ vault: string;
29
+ /** Collection name within the vault. Defaults to the store id. */
30
+ collection?: string;
31
+ /**
32
+ * Optional explicit Noydb instance. If omitted, the store resolves the
33
+ * globally bound instance via `getActiveNoydb()`.
34
+ */
35
+ noydb?: Noydb | null;
36
+ /**
37
+ * If true (default), hydration kicks off immediately when the store is
38
+ * first instantiated. If false, hydration is deferred until the first
39
+ * call to `refresh()` or any read accessor.
40
+ */
41
+ prefetch?: boolean;
42
+ /**
43
+ * Optional schema validator.
44
+ *
45
+ * Accepts any [Standard Schema v1](https://standardschema.dev) validator
46
+ * — Zod, Valibot, ArkType, Effect Schema, etc. The same validator is
47
+ * installed on the underlying `Collection`, so every `put()` is
48
+ * validated **before encryption** and every read is validated **after
49
+ * decryption**. The store's `add`/`update` methods inherit this
50
+ * validation automatically; no duplicate `.parse()` call is needed.
51
+ *
52
+ * Schema-less stores behave exactly as before (no validation, no
53
+ * perf cost, backwards compatible with usage).
54
+ */
55
+ schema?: StandardSchemaV1<unknown, T>;
56
+ }
57
+ /**
58
+ * The runtime shape of the store returned by `defineNoydbStore`.
59
+ * Exposed as a public type so consumers can write `useStore: ReturnType<typeof useInvoices>`.
60
+ */
61
+ interface NoydbStore<T> {
62
+ items: Ref<T[]>;
63
+ count: ComputedRef<number>;
64
+ $ready: Promise<void>;
65
+ byId(id: string): T | undefined;
66
+ add(id: string, record: T): Promise<void>;
67
+ update(id: string, record: T): Promise<void>;
68
+ remove(id: string): Promise<void>;
69
+ refresh(): Promise<void>;
70
+ query(): Query<T>;
71
+ liveQuery<R = T>(build: (q: Query<T>) => Query<R>): NoydbLiveQuery<R>;
72
+ }
73
+ /**
74
+ * Define a Pinia store that's wired to a NOYDB collection.
75
+ *
76
+ * Generic T defaults to `unknown` — pass `<MyType>` for full type inference.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * import { defineNoydbStore } from '@noy-db/in-pinia';
81
+ *
82
+ * export const useInvoices = defineNoydbStore<Invoice>('invoices', {
83
+ * vault: 'C101',
84
+ * schema: InvoiceSchema, // optional
85
+ * });
86
+ * ```
87
+ */
88
+ declare function defineNoydbStore<T>(id: string, options: NoydbStoreOptions<T>): pinia.StoreDefinition<string, Pick<{
89
+ items: Ref<T[], T[]>;
90
+ count: ComputedRef<number>;
91
+ $ready: Promise<void>;
92
+ byId: (id: string) => T | undefined;
93
+ add: (id: string, record: T) => Promise<void>;
94
+ update: (id: string, record: T) => Promise<void>;
95
+ remove: (id: string) => Promise<void>;
96
+ refresh: () => Promise<void>;
97
+ query: () => Query<T>;
98
+ liveQuery: <R = T>(build: (q: Query<T>) => Query<R>) => NoydbLiveQuery<R>;
99
+ }, "items" | "$ready">, Pick<{
100
+ items: Ref<T[], T[]>;
101
+ count: ComputedRef<number>;
102
+ $ready: Promise<void>;
103
+ byId: (id: string) => T | undefined;
104
+ add: (id: string, record: T) => Promise<void>;
105
+ update: (id: string, record: T) => Promise<void>;
106
+ remove: (id: string) => Promise<void>;
107
+ refresh: () => Promise<void>;
108
+ query: () => Query<T>;
109
+ liveQuery: <R = T>(build: (q: Query<T>) => Query<R>) => NoydbLiveQuery<R>;
110
+ }, "count">, Pick<{
111
+ items: Ref<T[], T[]>;
112
+ count: ComputedRef<number>;
113
+ $ready: Promise<void>;
114
+ byId: (id: string) => T | undefined;
115
+ add: (id: string, record: T) => Promise<void>;
116
+ update: (id: string, record: T) => Promise<void>;
117
+ remove: (id: string) => Promise<void>;
118
+ refresh: () => Promise<void>;
119
+ query: () => Query<T>;
120
+ liveQuery: <R = T>(build: (q: Query<T>) => Query<R>) => NoydbLiveQuery<R>;
121
+ }, "byId" | "add" | "update" | "remove" | "refresh" | "query" | "liveQuery">>;
122
+
123
+ /**
124
+ * Active NOYDB instance binding.
125
+ *
126
+ * `defineNoydbStore` resolves the `Noydb` instance from one of three places,
127
+ * in priority order:
128
+ *
129
+ * 1. The store options' explicit `noydb:` field (highest precedence — useful
130
+ * for tests and multi-database apps).
131
+ * 2. A globally bound instance set via `setActiveNoydb()` — this is what the
132
+ * Nuxt module's runtime plugin and playground apps use.
133
+ * 3. Throws a clear error if neither is set.
134
+ *
135
+ * Keeping the binding pluggable means tests can pass an instance directly
136
+ * without polluting global state.
137
+ */
138
+
139
+ /** Bind a Noydb instance globally. Called by the Nuxt module / app plugin. */
140
+ declare function setActiveNoydb(instance: Noydb | null): void;
141
+ /** Returns the globally bound Noydb instance, or null if none. */
142
+ declare function getActiveNoydb(): Noydb | null;
143
+ /**
144
+ * Resolve the Noydb instance to use for a store. Throws if no instance is
145
+ * bound — the error message points the developer at the three options.
146
+ */
147
+ declare function resolveNoydb(explicit?: Noydb | null): Noydb;
148
+
149
+ /**
150
+ * `createNoydbPiniaPlugin` — augmentation path for existing Pinia stores.
151
+ *
152
+ * Lets a developer take any existing `defineStore()` call and opt into NOYDB
153
+ * persistence by adding a single `noydb:` option, without touching component
154
+ * code. The plugin watches the chosen state key(s), encrypts on change, syncs
155
+ * to a NOYDB collection, and rehydrates on store init.
156
+ *
157
+ * @example
158
+ * ```ts
159
+ * import { createPinia } from 'pinia';
160
+ * import { createNoydbPiniaPlugin } from '@noy-db/in-pinia';
161
+ * import { jsonFile } from '@noy-db/to-file';
162
+ *
163
+ * const pinia = createPinia();
164
+ * pinia.use(createNoydbPiniaPlugin({
165
+ * adapter: jsonFile({ dir: './data' }),
166
+ * user: 'owner-01',
167
+ * secret: () => promptPassphrase(),
168
+ * }));
169
+ *
170
+ * // existing store — add one option, no component changes:
171
+ * export const useClients = defineStore('clients', {
172
+ * state: () => ({ list: [] as Client[] }),
173
+ * noydb: { vault: 'C101', collection: 'clients', persist: 'list' },
174
+ * });
175
+ * ```
176
+ *
177
+ * Design notes
178
+ * ------------
179
+ * - Each augmented store persists a SINGLE document at id `__state__`
180
+ * containing the picked keys. We don't try to map state arrays onto
181
+ * per-element records — that's `defineNoydbStore`'s territory.
182
+ * - The Noydb instance is constructed lazily on first store-with-noydb
183
+ * instantiation, then memoized for the lifetime of the Pinia app.
184
+ * This means apps that don't actually use any noydb-augmented stores
185
+ * pay zero crypto cost.
186
+ * - `secret` is a function so the passphrase can come from a prompt,
187
+ * biometric unlock, or session token — never stored in config.
188
+ * - The plugin sets `store.$noydbReady` (a `Promise<void>`) and
189
+ * `store.$noydbError` (an `Error | null`) on every augmented store
190
+ * so components can await hydration and surface failures.
191
+ */
192
+
193
+ /**
194
+ * Per-store NOYDB configuration. Attached to a Pinia store via the `noydb`
195
+ * option inside `defineStore({ ..., noydb: {...} })`.
196
+ *
197
+ * `persist` selects which top-level state keys to mirror into NOYDB.
198
+ * Pass a single key, an array of keys, or `'*'` to mirror the entire state.
199
+ */
200
+ interface StoreNoydbOptions<S extends StateTree = StateTree> {
201
+ /** Vault (tenant) name. */
202
+ vault: string;
203
+ /** Collection name within the vault. */
204
+ collection: string;
205
+ /**
206
+ * Which state keys to persist. Defaults to `'*'` (the entire state object).
207
+ * Pass a string or string[] to scope to specific keys.
208
+ */
209
+ persist?: keyof S | (keyof S)[] | '*';
210
+ /**
211
+ * Optional schema validator applied at the document level (the persisted
212
+ * subset of state, not individual records). Throws if validation fails on
213
+ * hydration — the store stays at its initial state and `$noydbError` is set.
214
+ */
215
+ schema?: {
216
+ parse: (input: unknown) => unknown;
217
+ };
218
+ }
219
+ /**
220
+ * Configuration for `createNoydbPiniaPlugin`. Mirrors `NoydbOptions` but
221
+ * makes `secret` a function so the passphrase can come from a prompt
222
+ * rather than being stored in config.
223
+ */
224
+ interface NoydbPiniaPluginOptions {
225
+ /** The NOYDB store to use for persistence. */
226
+ adapter: NoydbStore$1;
227
+ /** User identifier (matches the keyring file). */
228
+ user: string;
229
+ /**
230
+ * Passphrase provider. Called once on first noydb-augmented store
231
+ * instantiation. Return a string or a Promise that resolves to one.
232
+ */
233
+ secret: () => string | Promise<string>;
234
+ /** Optional Noydb open-options forwarded to `createNoydb`. */
235
+ noydbOptions?: Partial<Omit<NoydbOptions, 'store' | 'user' | 'secret'>>;
236
+ }
237
+ /**
238
+ * Create a Pinia plugin that wires NOYDB persistence into any store
239
+ * declaring a `noydb:` option.
240
+ *
241
+ * Returns a `PiniaPlugin` directly usable with `pinia.use(...)`.
242
+ */
243
+ declare function createNoydbPiniaPlugin(opts: NoydbPiniaPluginOptions): PiniaPlugin;
244
+ declare module 'pinia' {
245
+ interface DefineStoreOptionsBase<S extends StateTree, Store> {
246
+ /**
247
+ * Opt this store into NOYDB persistence via the
248
+ * `createNoydbPiniaPlugin` augmentation plugin.
249
+ *
250
+ * The chosen state keys are encrypted and persisted to the configured
251
+ * vault + collection on every mutation, and rehydrated on first
252
+ * store access.
253
+ */
254
+ noydb?: StoreNoydbOptions<S>;
255
+ }
256
+ interface PiniaCustomProperties {
257
+ /**
258
+ * Resolves once this store has finished its initial hydration from
259
+ * NOYDB. `undefined` for stores that don't declare a `noydb:` option.
260
+ */
261
+ $noydbReady?: Promise<void>;
262
+ /**
263
+ * Set when hydration or persistence fails. `null` while healthy.
264
+ * Plugins (and devtools) can poll this to surface storage errors.
265
+ */
266
+ $noydbError?: Error | null;
267
+ /**
268
+ * `true` if this store opted into NOYDB persistence via the `noydb:`
269
+ * option, `false` otherwise. Useful for debugging and devtools.
270
+ */
271
+ $noydbAugmented?: boolean;
272
+ /**
273
+ * Wait for all in-flight encrypted persistence puts to complete.
274
+ * Useful in tests for deterministic flushing, and in app code before
275
+ * unmounting components that just mutated the store.
276
+ */
277
+ $noydbFlush?: () => Promise<void>;
278
+ }
279
+ }
280
+
281
+ /**
282
+ * `useCapabilityGrant` — Vue/Pinia composable for time-boxed
283
+ * capability approval flows.
284
+ *
285
+ * The composable orchestrates a request → approve → expire / release
286
+ * lifecycle for a session-scoped capability. It manages UI state,
287
+ * persists the request to a reserved `_capability_requests` collection
288
+ * so a separate approver session can see it, and tracks TTL with
289
+ * auto-revert.
290
+ *
291
+ * **It does NOT itself flip capability bits.** Capability mechanisms
292
+ * vary across adopters: tier-based deployments wire `onGrant` to
293
+ * `vault.elevate(tier, opts)`; keyring-based deployments wire it to
294
+ * `db.grant(...)`; custom deployments do their own thing. Keeping the
295
+ * actual flip behind a callback avoids introducing a parallel
296
+ * "capability elevation" primitive in hub when the existing
297
+ * `vault.elevate()` already covers the time-boxed-grant pattern.
298
+ *
299
+ * ## State machine
300
+ *
301
+ * idle ─── .request() ───────► requested
302
+ * requested ─── .approve() ──► granted
303
+ * granted ─── TTL expires ─► idle
304
+ * granted ─── .release() ──► idle
305
+ *
306
+ * @module
307
+ */
308
+
309
+ /** Reserved internal collection that holds capability-grant lifecycle records. */
310
+ declare const CAPABILITY_REQUESTS_COLLECTION = "_capability_requests";
311
+ type CapabilityGrantState = 'idle' | 'requested' | 'granted' | 'expired';
312
+ /**
313
+ * On-disk shape of a capability-grant lifecycle record. Persisted in
314
+ * the reserved {@link CAPABILITY_REQUESTS_COLLECTION}. Encrypted with
315
+ * that collection's DEK at the storage layer; the in-memory shape
316
+ * here is plaintext.
317
+ *
318
+ * The audit trail invariant: this record carries metadata only —
319
+ * capability name, roles, ttl, reason. Never plaintext payload.
320
+ */
321
+ interface CapabilityGrantRecord {
322
+ readonly id: string;
323
+ readonly capability: string;
324
+ readonly requestedBy: string;
325
+ readonly approverRole: Role;
326
+ readonly reason: string;
327
+ readonly ttlMs: number;
328
+ readonly status: 'requested' | 'granted' | 'released' | 'expired';
329
+ readonly requestedAt: string;
330
+ readonly approvedBy?: string;
331
+ readonly approvedAt?: string;
332
+ readonly expiresAt?: string;
333
+ }
334
+ interface UseCapabilityGrantOptions {
335
+ /** TTL in milliseconds for the granted window. */
336
+ readonly ttlMs: number;
337
+ /** Role required to call `.approve()`. Mismatch throws on `.approve()`. */
338
+ readonly approver: Role;
339
+ /** Audit-ledger string. Stamped on the request record; no plaintext payload. */
340
+ readonly reason: string;
341
+ /**
342
+ * Optional explicit vault. Either a `Vault` instance or its name.
343
+ * When omitted, resolves the active Noydb instance via
344
+ * `setActiveNoydb()` and opens the first vault the caller has
345
+ * already loaded.
346
+ */
347
+ readonly vault: Vault | string;
348
+ /**
349
+ * Called on the approver's session when `.approve()` succeeds. Wire
350
+ * this to whatever capability flip your codebase uses —
351
+ * `vault.elevate(tier, opts)` for tier-based deployments,
352
+ * `db.grant(...)` for keyring-based, custom for custom.
353
+ *
354
+ * The composable does NOT enforce that the capability was actually
355
+ * granted — it just tracks the lifecycle. The post-expiry "gated
356
+ * call throws" contract comes from the underlying mechanism the
357
+ * callback wires up (e.g., `ElevationExpiredError` from
358
+ * `vault.elevate`'s lazy TTL check).
359
+ */
360
+ readonly onGrant?: (ctx: {
361
+ record: CapabilityGrantRecord;
362
+ vault: Vault;
363
+ }) => void | Promise<void>;
364
+ /**
365
+ * Called when the grant ends (TTL expiry OR voluntary release).
366
+ * Mirror of `onGrant`. Idempotent — may be called twice if release
367
+ * and expiry race; callers should no-op on the second invocation.
368
+ */
369
+ readonly onRelease?: (ctx: {
370
+ record: CapabilityGrantRecord;
371
+ vault: Vault;
372
+ cause: 'released' | 'expired';
373
+ }) => void | Promise<void>;
374
+ }
375
+ interface UseCapabilityGrantReturn {
376
+ readonly state: Ref<CapabilityGrantState>;
377
+ /** Milliseconds remaining on the granted window; 0 outside `granted`. */
378
+ readonly timeRemaining: ComputedRef<number>;
379
+ /** Most recent error from request/approve/release (resets on next op). */
380
+ readonly error: Ref<Error | null>;
381
+ /** Issue a request. State must be `idle`. */
382
+ request(): Promise<void>;
383
+ /** Approve a pending request. State must be `requested`. */
384
+ approve(): Promise<void>;
385
+ /** Voluntarily revoke an active grant. State must be `granted`. */
386
+ release(): Promise<void>;
387
+ }
388
+ /**
389
+ * Build a reactive capability-grant lifecycle handle.
390
+ *
391
+ * @example Tier-based capability flip
392
+ * ```ts
393
+ * let elevated: ElevatedHandle | null = null
394
+ * const grant = useCapabilityGrant('canExportPlaintext', {
395
+ * vault: 'V1',
396
+ * ttlMs: 15 * 60_000,
397
+ * approver: 'admin',
398
+ * reason: 'bulk export',
399
+ * onGrant: async ({ vault, record }) => {
400
+ * elevated = await vault.elevate(2, {
401
+ * ttlMs: record.ttlMs,
402
+ * reason: record.reason,
403
+ * })
404
+ * },
405
+ * onRelease: async () => { await elevated?.release(); elevated = null },
406
+ * })
407
+ * ```
408
+ */
409
+ declare function useCapabilityGrant(capability: string, options: UseCapabilityGrantOptions): UseCapabilityGrantReturn;
410
+
411
+ export { CAPABILITY_REQUESTS_COLLECTION, type CapabilityGrantRecord, type CapabilityGrantState, type NoydbLiveQuery, type NoydbPiniaPluginOptions, type NoydbStore, type NoydbStoreOptions, type StoreNoydbOptions, type UseCapabilityGrantOptions, type UseCapabilityGrantReturn, createNoydbPiniaPlugin, defineNoydbStore, getActiveNoydb, resolveNoydb, setActiveNoydb, useCapabilityGrant };