@murumets-ee/settings 0.1.5 → 0.1.6

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.
@@ -1,68 +0,0 @@
1
- import { Logger } from '@murumets-ee/core';
2
- import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
3
-
4
- /**
5
- * ViewStateClient — schemaless user-scoped state persistence.
6
- *
7
- * Used by admin UI for table filters, column order, panel state, etc.
8
- * Optional TTL for auto-cleanup of stale state.
9
- *
10
- * @example
11
- * ```typescript
12
- * import { createViewStateClient } from '@murumets-ee/settings/view-state'
13
- *
14
- * const viewState = createViewStateClient({ db, userId: currentUser.id })
15
- *
16
- * await viewState.save('articles-table', {
17
- * filters: { status: 'published' },
18
- * columnOrder: ['title', 'status', 'date'],
19
- * sortBy: 'date',
20
- * })
21
- *
22
- * const state = await viewState.load('articles-table')
23
- * ```
24
- */
25
-
26
- interface ViewStateClientConfig {
27
- /** Database client (read-write) */
28
- db: PostgresJsDatabase;
29
- /** User ID for scoping */
30
- userId: string;
31
- /** Logger instance */
32
- logger?: Logger;
33
- /** Default TTL in seconds for view state entries. Defaults to 30 days. */
34
- defaultTtl?: number;
35
- }
36
- declare class ViewStateClient {
37
- private db;
38
- private userId;
39
- private logger?;
40
- private defaultTtl;
41
- constructor(config: ViewStateClientConfig);
42
- /**
43
- * Save view state (upsert).
44
- */
45
- save<T extends Record<string, unknown>>(viewKey: string, state: T, options?: {
46
- ttl?: number;
47
- }): Promise<void>;
48
- /**
49
- * Load view state. Returns null if not found or expired.
50
- */
51
- load<T extends Record<string, unknown> = Record<string, unknown>>(viewKey: string): Promise<T | null>;
52
- /**
53
- * Clear a specific view state entry.
54
- */
55
- clear(viewKey: string): Promise<void>;
56
- /**
57
- * Clear all expired view state entries (maintenance task).
58
- * Call periodically (e.g., from a cron job or queue task).
59
- * Returns the number of entries removed.
60
- */
61
- clearExpired(): Promise<number>;
62
- }
63
- /**
64
- * Factory function following toolkit conventions.
65
- */
66
- declare function createViewStateClient(config: ViewStateClientConfig): ViewStateClient;
67
-
68
- export { ViewStateClient, type ViewStateClientConfig, createViewStateClient };
@@ -1 +0,0 @@
1
- import{b as t}from"./chunk-E3K4GXDC.js";import{and as d,eq as i,lt as u}from"drizzle-orm";var g=720*60*60,n=class{db;userId;logger;defaultTtl;constructor(e){if(typeof window<"u")throw new Error("ViewStateClient cannot be used in browser code.");this.db=e.db,this.userId=e.userId,this.logger=e.logger,this.defaultTtl=e.defaultTtl??g}async save(e,r,s){this.logger?.debug({userId:this.userId,viewKey:e},"Saving view state");let l=s?.ttl??this.defaultTtl,a=new Date(Date.now()+l*1e3);await this.db.insert(t).values({userId:this.userId,viewKey:e,state:r,expiresAt:a,updatedAt:new Date}).onConflictDoUpdate({target:[t.userId,t.viewKey],set:{state:r,expiresAt:a,updatedAt:new Date}})}async load(e){this.logger?.debug({userId:this.userId,viewKey:e},"Loading view state");let r=await this.db.select({state:t.state,expiresAt:t.expiresAt}).from(t).where(d(i(t.userId,this.userId),i(t.viewKey,e))).limit(1);if(r.length===0)return null;let s=r[0];return s.expiresAt&&s.expiresAt<new Date?(await this.clear(e),null):s.state}async clear(e){this.logger?.debug({userId:this.userId,viewKey:e},"Clearing view state"),await this.db.delete(t).where(d(i(t.userId,this.userId),i(t.viewKey,e)))}async clearExpired(){this.logger?.info("Clearing expired view state entries");let r=(await this.db.delete(t).where(u(t.expiresAt,new Date)).returning({id:t.id})).length;return this.logger?.info({count:r},"Expired view state entries cleared"),r}};function h(o){return new n(o)}export{n as ViewStateClient,h as createViewStateClient};