@superbright/indexeddb-orm 0.1.1 → 0.1.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/README.md CHANGED
@@ -1,30 +1,365 @@
1
- # indexeddb-orm-starter
1
+ # IndexedDB ORM with Structured Store Actions
2
2
 
3
- Minimal **Vite + TypeScript** starter for an IndexedDB ORM using **Dexie** + **Zod**, with a built-in browser playground.
3
+ A TypeScript-first IndexedDB ORM built with Dexie and Zod, featuring structured Zustand store integration for React applications.
4
4
 
5
- ## Features
6
- - Library build via Vite (ES + CJS) with type declarations (tsc)
7
- - Dexie-powered tables: `users`, `properties`, `units`, and a `kv` store
8
- - Helpers for **favourites** and **preferences**
9
- - Schema version manifest + reset on incompatible version
10
- - Vitest + fake-indexeddb tests
11
- - Browser playground to inspect data
5
+ ## 🚀 Features
6
+
7
+ - **TypeScript-First**: Full type safety with Zod schema validation
8
+ - **Structured Store Actions**: Organized, nested API for better developer experience
9
+ - **IndexedDB Powered**: Built on Dexie for robust browser storage
10
+ - **Zustand Integration**: Seamless state management with React
11
+ - **Schema Validation**: Runtime validation with Zod schemas
12
+ - **Developer Experience**: Hot reloading, type inference, and great autocomplete
13
+ - **Production Ready**: Optimized builds with proper package exports
14
+
15
+ ## 📦 Installation
16
+
17
+ ```bash
18
+ pnpm install @superbright/indexeddb-orm zustand
19
+ ```
20
+
21
+ ## 🏃 Quick Start
22
+
23
+ ### 1. Basic Setup
24
+
25
+ ```typescript
26
+ import { create } from "zustand";
27
+ import { devtools } from "zustand/middleware";
28
+ import {
29
+ createStructuredStore,
30
+ createUseUnitState,
31
+ type StructuredStore,
32
+ type Filters,
33
+ type QueryParams,
34
+ } from "@superbright/indexeddb-orm";
35
+
36
+ // Create your store with structured actions
37
+ export const useStore = create<StructuredStore>()(
38
+ devtools(
39
+ createStructuredStore({
40
+ onFilterUpdate: (apiParams: QueryParams) => {
41
+ console.log("Filters updated:", apiParams);
42
+ },
43
+ }),
44
+ { name: "inresi-store" }
45
+ )
46
+ );
47
+
48
+ export const useUnitState = createUseUnitState()(useStore);
49
+ ```
50
+
51
+ ### 2. Initialize in Your App
52
+
53
+ ```typescript
54
+ // main.tsx or App.tsx
55
+ import { useStore } from './stores/myStore';
56
+
57
+ async function initializeApp() {
58
+ await useStore.getState()._initialize();
59
+ await useStore.getState()._hydrate();
60
+ }
61
+
62
+ initializeApp().then(() => {
63
+ // Render your app
64
+ });
65
+ ```
66
+
67
+ ### 3. Use Structured Actions
68
+
69
+ ```typescript
70
+ function MyComponent() {
71
+ const store = useStore();
72
+
73
+ // Property actions
74
+ const toggleFavorite = async (unitId: string) => {
75
+ await store.property.unit.favorites.toggle(unitId);
76
+ };
77
+
78
+ const markViewed = async (unitId: string, slug: string) => {
79
+ await store.property.unit.viewed.mark(unitId, slug);
80
+ };
81
+
82
+ // Filter actions
83
+ const updateFilters = async () => {
84
+ await store.filters.set({ bedrooms: [1, 2] });
85
+ await store.filters.submit();
86
+ };
87
+
88
+ return (
89
+ <div>
90
+ <button onClick={() => toggleFavorite("unit-123")}>
91
+ Toggle Favorite
92
+ </button>
93
+ <button onClick={() => markViewed("unit-123", "property-slug")}>
94
+ Mark as Viewed
95
+ </button>
96
+ <button onClick={updateFilters}>
97
+ Update Filters
98
+ </button>
99
+ </div>
100
+ );
101
+ }
102
+ ```
103
+
104
+ ## 🏗️ Architecture
105
+
106
+ ### Core Components
107
+
108
+ - **Dexie Database**: IndexedDB abstraction layer
109
+ - **Zod Schemas**: Runtime type validation and TypeScript inference
110
+ - **Unified Store**: Single source of truth combining property and app state
111
+ - **Structured Actions**: Organized API with nested action groups
112
+ - **Zustand Integration**: React state management with devtools support
113
+
114
+ ### Data Flow
115
+
116
+ ```
117
+ React Components
118
+
119
+ Structured Store Actions
120
+
121
+ Unified Store (Zustand)
122
+
123
+ ORM Layer (Validation)
124
+
125
+ IndexedDB (Dexie)
126
+ ```
127
+
128
+ ## 📚 API Reference
129
+
130
+ ### Structured Store Actions
131
+
132
+ #### Property Actions
133
+ ```typescript
134
+ // Unit favorites
135
+ store.property.unit.favorites.toggle(unitId: string)
136
+
137
+ // Unit viewing
138
+ store.property.unit.viewed.mark(unitId: string, slug: string)
139
+
140
+ // Questionnaire
141
+ store.property.questionnaire.setResults(results: unknown)
142
+
143
+ // Tour scheduling
144
+ store.property.tour.setContactedOn()
145
+ store.property.tour.getContactedOn()
146
+ store.property.tour.setContactData(data: TourContactData)
147
+ ```
148
+
149
+ #### Filter Actions
150
+ ```typescript
151
+ // Filter management
152
+ store.filters.set(filters: Partial<Filters>)
153
+ store.filters.setTemp(filters: Partial<Filters>)
154
+ store.filters.setToDefault()
155
+ store.filters.commitTemp(key, defaultValue)
156
+ store.filters.commitAvailability()
157
+ store.filters.submit()
158
+ ```
159
+
160
+ ### Store State
161
+
162
+ ```typescript
163
+ // Property data
164
+ const currentPropertyId = useStore(state => state.currentPropertyId);
165
+ const properties = useStore(state => state.properties);
166
+
167
+ // App data
168
+ const filters = useStore(state => state.filters);
169
+ const units = useStore(state => state.units);
170
+ const resultsMode = useStore(state => state.resultsMode);
171
+
172
+ // Unit state helper
173
+ const unitState = useUnitState("unit-123");
174
+ // Returns: { isFavorite: boolean, viewedDate: string }
175
+ ```
176
+
177
+ ### Direct Store Methods
178
+
179
+ For advanced use cases, all original store methods are available:
180
+
181
+ ```typescript
182
+ // Property operations
183
+ await store.getCurrentProperty()
184
+ await store.setCurrentProperty(propertyId, slug)
185
+ await store.getPropertyData(propertyId)
186
+
187
+ // Unit operations
188
+ await store.getUnitData(unitId)
189
+ await store.setUnitData(unitId, data)
190
+ await store.removeUnitData(unitId)
191
+
192
+ // Filter operations
193
+ await store.loadPersistedFilters()
194
+ await store.handleFilterCommitIndexDB(newFilters)
195
+ ```
196
+
197
+ ## 🛠️ Development
198
+
199
+ ### Local Development Setup
200
+
201
+ 1. **Environment Configuration**
202
+
203
+ Create `.env` in your consuming app:
204
+
205
+ ```bash
206
+ VITE_USE_LOCAL_ORM_LIBRARY=true
207
+ VITE_LOCAL_ORM_LIBRARY_PATH=/path/to/indexeddb-orm-starter-with-playground
208
+ ```
209
+
210
+ 2. **Vite Configuration**
211
+
212
+ ```typescript
213
+ // vite.config.ts
214
+ const alias: Record<string, string> = {};
215
+ if (useLocalORM && ormPath) {
216
+ alias["@superbright/indexeddb-orm"] = resolve(ormPath, "./dist");
217
+ }
218
+ ```
219
+
220
+ ### 🎮 Interactive Playground
221
+
222
+ The ORM includes a comprehensive interactive playground for testing and exploring functionality:
12
223
 
13
- ## Dev / Playground
14
224
  ```bash
15
225
  pnpm i
16
226
  pnpm dev
17
227
  # Visit http://localhost:5173 (or printed URL)
18
- # Use the buttons to write/dump/export, then open DevTools → Application → IndexedDB → inresi-orm
19
228
  ```
20
229
 
21
- ## Build & Test
230
+ #### Playground Features
231
+
232
+ - **🗄️ Database Operations**: Initialize, dump state, export JSON, and reset database
233
+ - **🏠 Property Management**: Create properties, set current property, retrieve property data
234
+ - **⭐ Unit Actions**: Toggle favorites, mark units as viewed, get unit state
235
+ - **🔍 Filter Operations**: Set filters, reset to defaults, submit filter updates
236
+ - **📝 Questionnaire & Tours**: Set questionnaire results, manage tour contact data
237
+ - **📊 Real-time State View**: Live view of database state and action logs
238
+ - **🛠️ Developer Tools Integration**: Inspect IndexedDB directly in DevTools → Application → IndexedDB → `inresi-orm`
239
+
240
+ #### How to Use
241
+
242
+ 1. **Start with Database Operations**: Click "Initialize Database" to set up the ORM
243
+ 2. **Create a Property**: Enter a property ID and slug, then click "Initialize Property"
244
+ 3. **Test Unit Actions**: Enter a unit ID and try toggling favorites or marking as viewed
245
+ 4. **Experiment with Filters**: Set bedroom counts and cost filters, then submit them
246
+ 5. **View State Changes**: Watch the real-time state updates and action logs
247
+ 6. **Inspect Database**: Open browser DevTools to see the actual IndexedDB storage
248
+
249
+ The playground demonstrates all structured store actions and provides immediate feedback, making it perfect for learning the API and testing integrations.
250
+
251
+ ### Build Scripts
252
+
22
253
  ```bash
23
- pnpm build
24
- pnpm test
254
+ # Development
255
+ pnpm run dev # Start playground with hot reload
256
+ pnpm run watch # Watch mode for types and bundle
257
+
258
+ # Building
259
+ pnpm run build # Production build
260
+ pnpm run clean # Clean dist folder
261
+
262
+ # Testing
263
+ pnpm run test # Run tests
264
+ pnpm run test:watch # Watch mode testing
265
+ ```
266
+
267
+ ### Package Structure
268
+
269
+ ```
270
+ dist/
271
+ ├── index.d.ts # Main type definitions
272
+ ├── index.es.js # ES module build
273
+ ├── index.cjs.js # CommonJS build
274
+ ├── adapters/ # Store adapters
275
+ ├── api/ # API modules
276
+ └── stores/ # Store implementations
25
277
  ```
26
278
 
27
- ## Programmatic usage
279
+ ## 🎯 TypeScript Support
280
+
281
+ Full TypeScript support with proper type inference:
282
+
283
+ ```typescript
284
+ import type {
285
+ StructuredStore,
286
+ StructuredStoreActions,
287
+ Filters,
288
+ QueryParams,
289
+ UnitData,
290
+ TourContactData,
291
+ PropertyData,
292
+ ZustandUnifiedStoreState,
293
+ } from "@superbright/indexeddb-orm";
294
+ ```
295
+
296
+ ## 📋 Schema Validation
297
+
298
+ Built-in Zod schemas ensure data integrity:
299
+
300
+ ```typescript
301
+ // Example schema usage
302
+ import { FiltersSchema, UnitDataSchema } from "@superbright/indexeddb-orm";
303
+
304
+ // Validate data at runtime
305
+ const validatedFilters = FiltersSchema.parse(userInput);
306
+ const validatedUnit = UnitDataSchema.parse(apiResponse);
307
+ ```
308
+
309
+ ## 🔧 Configuration
310
+
311
+ ### Store Configuration
312
+
313
+ ```typescript
314
+ createStructuredStore({
315
+ onFilterUpdate?: (apiParams: QueryParams) => void;
316
+ // Add more configuration options as needed
317
+ })
318
+ ```
319
+
320
+ ### Validation Configuration
321
+
322
+ ```typescript
323
+ import { configureValidation } from "@superbright/indexeddb-orm";
324
+
325
+ // Configure validation mode
326
+ configureValidation("strict"); // "strict" | "warn" | "off"
327
+ ```
328
+
329
+ ## 🚀 Production
330
+
331
+ ### Package Exports
332
+
333
+ ```json
334
+ {
335
+ "exports": {
336
+ ".": {
337
+ "types": "./dist/index.d.ts",
338
+ "import": "./dist/index.mjs",
339
+ "require": "./dist/index.cjs"
340
+ }
341
+ }
342
+ }
343
+ ```
344
+
345
+ ### Bundle Optimization
346
+
347
+ - Tree-shakable ES modules
348
+ - Separate type definitions
349
+ - Optimized for Vite and Webpack
350
+ - Source maps included
351
+
352
+ ## 📖 Documentation
353
+
354
+ - [Consuming App Guide](./CONSUMING_APP_GUIDE.md) - Complete integration guide
355
+ - [Migration Summary](./MIGRATION_SUMMARY.md) - Migration from legacy stores
356
+ - [Property Store Guide](./docs/property-store.md) - Property-specific usage
357
+ - [App Store Guide](./docs/app-store-guide.md) - App state management
358
+
359
+ ## 🔧 Legacy API
360
+
361
+ For backward compatibility, the original API is still available:
362
+
28
363
  ```ts
29
364
  import {
30
365
  getDB,
@@ -32,7 +367,7 @@ import {
32
367
  getPreferences, setPreferences,
33
368
  upsertUser, getUser,
34
369
  debugDump, exportJSON
35
- } from "indexeddb-orm-starter";
370
+ } from "@superbright/indexeddb-orm";
36
371
 
37
372
  await getDB();
38
373
  await setFavouritedUnits(["u1","u2"]);
@@ -40,6 +375,22 @@ console.log(await getFavouritedUnits());
40
375
  await exportJSON();
41
376
  ```
42
377
 
43
- ## Notes
44
- - Increment `SCHEMA_VERSION` in `src/schema.ts` to force a reset (basic strategy).
45
- - Add entities/repositories in `src/api/*` following the same pattern.
378
+ ## 🤝 Contributing
379
+
380
+ 1. Fork the repository
381
+ 2. Create a feature branch
382
+ 3. Make your changes
383
+ 4. Add tests if applicable
384
+ 5. Ensure type checking passes: `pnpm run build`
385
+ 6. Submit a pull request
386
+
387
+ ## 📝 License
388
+
389
+ UNLICENSED - Proprietary software for internal use
390
+
391
+ ## 🔗 Related Projects
392
+
393
+ - [Dexie](https://dexie.org/) - IndexedDB wrapper
394
+ - [Zod](https://zod.dev/) - TypeScript schema validation
395
+ - [Zustand](https://zustand-demo.pmnd.rs/) - React state management
396
+ - [Vite](https://vitejs.dev/) - Build tool and dev server
package/dist/index.cjs ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("zod"),at=require("dexie");var U=typeof document<"u"?document.currentScript:null;class B extends Error{constructor(t,e){super(t),this.detail=e,this.name="SchemaMismatchError"}}class Q extends Error{constructor(t,e){super(t),this.detail=e,this.name="OpenDBError"}}class R extends at{constructor(t="inresi-orm"){super(t),this.version(m).stores({users:"uuid",kv:"key"}),this.users=this.table("users"),this.kv=this.table("kv")}}let O="strict",b;function x(a){a.mode&&(O=a.mode),b=a.onIssue}function D(a,t,e){const r=a.safeParse(t);if(r.success)return r.data;const s=r.error.flatten();if(b==null||b(e,s),O==="strict")throw new B(`ORM schema mismatch in ${e}`,s);return O==="warn"&&console.warn(`ORM schema mismatch in ${e}`,s),null}const it={BASE_URL:"/",DEV:!1,MODE:"production",PROD:!0,SSR:!1},I="manifest",nt=n.z.object({schemaVersion:n.z.number().int()});let h=null,f=null;const L=Symbol("validationInstalled");function C(a,t){const e=a;if(e[L])return;if(e[L]=!0,((i,o,c)=>{i.hook("creating",(u,y)=>{const p=D(o,y,`${c}.creating`);if(!p)throw new Error(`Rejected invalid ${c} on create`);return p}),i.hook("updating",(u,y,p)=>{const d={...p,...u};return D(o,d,`${c}.updating`)?u:{}})})(a.users,g,"users"),(t==null?void 0:t.validateReads)??!0){const i=(t==null?void 0:t.dropInvalidOnRead)??!0;((c,u,y)=>{c.hook("reading",p=>{const d=D(u,p,`${y}.reading`);return d||(i?void 0:p)})})(a.users,g,"users")}}function st(){try{if(typeof{url:typeof document>"u"?require("url").pathToFileURL(__filename).href:U&&U.tagName.toUpperCase()==="SCRIPT"&&U.src||new URL("index.cjs",document.baseURI).href}<"u"&&it)return!1}catch{}try{if(typeof process<"u"&&process.env)return process.env.NODE_ENV!=="production"}catch{}return!1}async function w(a={}){if(h)return h;if(f)return f;const t=a.dbName??"inresi-orm";return f=(async()=>{var e,r,s,i;try{const o=st()?"warn":"strict";x({mode:((e=a.validation)==null?void 0:e.mode)??o,onIssue:(r=a.validation)==null?void 0:r.onIssue});const c=new R(t);c.on("versionchange",()=>{var d;try{c.close()}finally{(d=a.onReset)==null||d.call(a,"versionchange")}}),c.on("blocked",()=>{var d;(d=a.onReset)==null||d.call(a,"blocked")}),await c.open();const u=await c.kv.get(I),y=(u==null?void 0:u.value)??null;if(!y)return await c.transaction("rw",c.kv,async()=>{await c.kv.put({key:I,value:{schemaVersion:m}})}),C(c,a.validation),h=c,c;const p=nt.safeParse(y);if(!p.success||p.data.schemaVersion!==m){await c.delete();const d=new R(t);return d.on("versionchange",()=>{var S;try{d.close()}finally{(S=a.onReset)==null||S.call(a,"versionchange")}}),d.on("blocked",()=>{var S;(S=a.onReset)==null||S.call(a,"blocked")}),await d.open(),await d.kv.put({key:I,value:{schemaVersion:m}}),C(d,a.validation),(s=a.onReset)==null||s.call(a,"incompatible"),h=d,d}return C(c,a.validation),h=c,c}catch(o){throw(i=a.onError)==null||i.call(a,o),new Q("Failed to open IndexedDB",o)}finally{f=null}})(),f}async function ot(a){const t=a??"inresi-orm";if(h)try{await h.close()}catch{}await new R(t).delete(),h=null}async function A(a){const e=await(await w()).kv.get(a);return(e==null?void 0:e.value)??null}async function E(a,t){await(await w()).kv.put({key:a,value:t})}async function ct(a){await(await w()).kv.delete(a)}function lt(a){const t=new Map,e=(a==null?void 0:a.prefix)??"",r=s=>`${e}${s}`;return{async getItem(s){try{const o=await(await w()).kv.get(r(s));return o?JSON.stringify(o.value):null}catch(i){if(a!=null&&a.fallbackToMemory)return t.get(r(s))??null;throw i}},async setItem(s,i){try{await(await w()).kv.put({key:r(s),value:JSON.parse(i)})}catch(o){if(a!=null&&a.fallbackToMemory){t.set(r(s),i);return}throw o}},async removeItem(s){try{await(await w()).kv.delete(r(s))}catch(i){if(a!=null&&a.fallbackToMemory){t.delete(r(s));return}throw i}}}}const N=n.z.object({unitId:n.z.string(),viewedDate:n.z.string()}),H=n.z.object({timezone:n.z.string(),favouriteUnits:n.z.array(n.z.string()).optional(),preferences:n.z.record(n.z.unknown())}),q=n.z.object({id:n.z.string(),slug:n.z.string(),favoritedUnits:n.z.array(n.z.string()),tourContactedOn:n.z.string().nullable(),viewedUnits:n.z.array(N),questionnaireResults:n.z.unknown().nullable().optional(),tourContactData:H.nullable().optional()}),ut=n.z.object({data:n.z.record(q),propertySlug:n.z.string().nullable(),propertyId:n.z.string().nullable(),hasPreviouslySearched:n.z.array(n.z.string())}),dt={data:{},propertySlug:null,propertyId:null,hasPreviouslySearched:[]};class J{async getState(){return await A("property")??dt}async setState(t){const e=await this.getState(),r=t(e);await E("property",r)}async setData(t){await this.setState(e=>({...e,data:t}))}async setPropertySlug(t){await this.setState(e=>({...e,propertySlug:t}))}async setPropertyId(t){await this.setState(e=>({...e,propertyId:t}))}async removeData(t){await this.setState(e=>({...e,data:Object.entries(e.data).filter(([r])=>r!==t).reduce((r,[s,i])=>({...r,[s]:i}),{})}))}async clearData(){await this.setState(t=>({...t,data:{}}))}async setHasPreviouslySearched(t){await this.setState(e=>({...e,hasPreviouslySearched:Array.from(new Set([...e.hasPreviouslySearched,t]))}))}async setTourContactedOn(){await this.setState(t=>{const e=t.propertyId;if(!e)return t;const r=t.data[e];return r?{...t,data:{...t.data,[e]:{...r,tourContactedOn:new Date().toISOString()}}}:t})}async getTourContactedOn(){var r;const t=await this.getState(),e=t.propertyId;return e?((r=t.data[e])==null?void 0:r.tourContactedOn)??null:null}async setQuestionnaireResults(t){await this.setState(e=>{const r=e.propertyId;if(!r)return e;const s=e.data[r];return s?{...e,data:{...e.data,[r]:{...s,questionnaireResults:t}}}:e})}async setTourContactData(t){await this.setState(e=>{const r=e.propertyId;if(!r)return e;const s=e.data[r];return s?{...e,data:{...e.data,[r]:{...s,tourContactData:t}}}:e})}async toggleFavorite(t){await this.setState(e=>{const r=e.propertyId;if(!r)return e;const s=e.data[r];if(!s)return e;const o=s.favoritedUnits.includes(t)?s.favoritedUnits.filter(c=>c!==t):[...s.favoritedUnits,t];return{...e,data:{...e.data,[r]:{...s,favoritedUnits:o}}}})}async markUnitAsViewed(t,e){const r=new Date,s=`${String(r.getMonth()+1).padStart(2,"0")}/${String(r.getDate()).padStart(2,"0")}`;await this.setState(i=>{const o=i.propertyId;if(!o)return i;const c=i.data[o];if(!c)return i;const u=[...c.viewedUnits.filter(y=>y.unitId!==t),{unitId:t,viewedDate:s}];return{...i,data:{...i.data,[o]:{...c,viewedUnits:u}}}}),typeof window<"u"&&window.open(`//${e}`,"_blank")}async getUnitState(t){var s;const e=await this.getState(),r=e.propertyId?e.data[e.propertyId]:null;return{isFavorite:(r==null?void 0:r.favoritedUnits.includes(t))??!1,viewedDate:((s=r==null?void 0:r.viewedUnits.find(i=>i.unitId===t))==null?void 0:s.viewedDate)??""}}async getPropertyData(t){const e=await this.getState(),r=t??e.propertyId;return r?e.data[r]??null:null}async getCurrentProperty(){const t=await this.getState();return t.propertyId?t.data[t.propertyId]??null:null}async getFullState(){return this.getState()}async initializeProperty(t,e){await this.setState(r=>r.data[t]?{...r,propertyId:t,propertySlug:e}:{...r,propertyId:t,propertySlug:e,data:{...r.data,[t]:{id:t,slug:e,favoritedUnits:[],tourContactedOn:null,viewedUnits:[],questionnaireResults:null,tourContactData:null}}})}}const yt=new J,G=n.z.object({isFavorite:n.z.boolean().optional(),viewedDate:n.z.string().optional()}),V=n.z.object({availability:n.z.union([n.z.string(),n.z.array(n.z.string())]).nullable().optional(),bedrooms:n.z.array(n.z.number()).nullable().optional(),cost:n.z.number().nullable().optional(),highlights:n.z.array(n.z.string()).optional()}),K=n.z.object({limit:n.z.number().default(10),page:n.z.number().default(1),availability:n.z.array(n.z.string()).optional(),bedrooms:n.z.array(n.z.number()).optional(),cost:n.z.number().nullable().optional(),highlights:n.z.array(n.z.string()).optional()}),pt=n.z.object({data:n.z.record(G),filters:V,tempFilters:V,apiFilters:K,resultsMode:n.z.enum(["all","bestFit","closestMatch","favorites"]),propertySlug:n.z.string().nullable(),resolvedQuestionnaireValues:n.z.record(n.z.array(n.z.string())),sortBy:n.z.enum(["relevance","newest","priceLowToHigh","priceHighToLow"]),filtersLoaded:n.z.boolean()});n.z.object({isFavorite:n.z.boolean().optional(),viewedDate:n.z.string().optional()});const wt=n.z.object({unitId:n.z.string(),viewedDate:n.z.string()}),ht=n.z.object({timezone:n.z.string(),favouriteUnits:n.z.array(n.z.string()).optional(),preferences:n.z.record(n.z.unknown())}),gt=n.z.object({id:n.z.string(),slug:n.z.string(),favoritedUnits:n.z.array(n.z.string()),tourContactedOn:n.z.string().nullable(),viewedUnits:n.z.array(wt),questionnaireResults:n.z.unknown().nullable().optional(),tourContactData:ht.nullable().optional()}),_=n.z.object({availability:n.z.union([n.z.string(),n.z.array(n.z.string())]).nullable().optional(),bedrooms:n.z.array(n.z.number()).nullable().optional(),cost:n.z.number().nullable().optional(),highlights:n.z.array(n.z.string()).optional()}),St=n.z.object({limit:n.z.number().default(10),page:n.z.number().default(1),availability:n.z.array(n.z.string()).optional(),bedrooms:n.z.array(n.z.number()).optional(),cost:n.z.number().nullable().optional(),highlights:n.z.array(n.z.string()).optional()}),ft=n.z.object({properties:n.z.record(gt),currentPropertyId:n.z.string().nullable(),currentPropertySlug:n.z.string().nullable(),hasPreviouslySearched:n.z.array(n.z.string()),filters:_,tempFilters:_,apiFilters:St,resultsMode:n.z.enum(["all","bestFit","closestMatch","favorites"]),resolvedQuestionnaireValues:n.z.record(n.z.array(n.z.string())),sortBy:n.z.enum(["relevance","newest","priceLowToHigh","priceHighToLow"]),filtersLoaded:n.z.boolean()}),M={availability:void 0,bedrooms:void 0,cost:void 0,highlights:void 0},k={properties:{},currentPropertyId:null,currentPropertySlug:null,hasPreviouslySearched:[],filters:M,tempFilters:M,apiFilters:{limit:10,page:1},resultsMode:"all",resolvedQuestionnaireValues:{},sortBy:"relevance",filtersLoaded:!1};class Z{async getState(){const t=await A("app");return t?{...k,...t,properties:t.properties??{}}:k}async setState(t){const e=await this.getState(),r=t(e);await E("app",r)}async initializeProperty(t,e){await this.setState(r=>r.properties&&r.properties[t]?{...r,currentPropertyId:t,currentPropertySlug:e}:{...r,currentPropertyId:t,currentPropertySlug:e,properties:{...r.properties,[t]:{id:t,slug:e,favoritedUnits:[],tourContactedOn:null,viewedUnits:[],questionnaireResults:null,tourContactData:null}}})}async setCurrentProperty(t,e){await this.setState(r=>({...r,currentPropertyId:t,currentPropertySlug:e||r.currentPropertySlug}))}async setCurrentPropertySlug(t){await this.setState(e=>({...e,currentPropertySlug:t}))}async setHasPreviouslySearched(t){await this.setState(e=>({...e,hasPreviouslySearched:Array.from(new Set([...e.hasPreviouslySearched,t]))}))}async toggleFavorite(t){await this.setState(e=>{const r=e.currentPropertyId;if(!r)return e;const s=e.properties[r];if(!s)return e;const o=s.favoritedUnits.includes(t)?s.favoritedUnits.filter(c=>c!==t):[...s.favoritedUnits,t];return{...e,properties:{...e.properties,[r]:{...s,favoritedUnits:o}}}})}async markUnitAsViewed(t,e){const r=new Date,s=`${String(r.getMonth()+1).padStart(2,"0")}/${String(r.getDate()).padStart(2,"0")}`;await this.setState(i=>{const o=i.currentPropertyId;if(!o)return i;const c=i.properties[o];if(!c)return i;const u=[...c.viewedUnits.filter(y=>y.unitId!==t),{unitId:t,viewedDate:s}];return{...i,properties:{...i.properties,[o]:{...c,viewedUnits:u}}}}),typeof window<"u"&&window.open(`//${e}`,"_blank")}async setTourContactedOn(){await this.setState(t=>{const e=t.currentPropertyId;if(!e)return t;const r=t.properties[e];return r?{...t,properties:{...t.properties,[e]:{...r,tourContactedOn:new Date().toISOString()}}}:t})}async getTourContactedOn(){var r;const t=await this.getState(),e=t.currentPropertyId;return e?((r=t.properties[e])==null?void 0:r.tourContactedOn)??null:null}async setQuestionnaireResults(t){await this.setState(e=>{const r=e.currentPropertyId;if(!r)return e;const s=e.properties[r];return s?{...e,properties:{...e.properties,[r]:{...s,questionnaireResults:t}}}:e})}async setTourContactData(t){await this.setState(e=>{const r=e.currentPropertyId;if(!r)return e;const s=e.properties[r];return s?{...e,properties:{...e.properties,[r]:{...s,tourContactData:t}}}:e})}async setFilters(t){await this.setState(e=>({...e,filters:{...e.filters,...t}}))}async setTempFilters(t){await this.setState(e=>({...e,tempFilters:{...e.tempFilters,...t}}))}async setFiltersToDefault(){await this.setState(t=>({...t,filters:M}))}async setApiFilters(t){await this.setState(e=>({...e,apiFilters:{...e.apiFilters,...t}}))}async handleTempFilterChange(t,e){await this.setState(r=>({...r,tempFilters:{...r.tempFilters,[t]:e}}))}async commitTempFilterChange(t,e){const s=(await this.getState()).tempFilters[t]??e;await this.handleTempFilterChange(t,s),await this.submitFilterUpdate()}async handleFilterCommitIndexDB(t){await this.setState(e=>{const r={...e.apiFilters,availability:t.availability||[],bedrooms:t.bedrooms||[],cost:t.cost||null,highlights:t.highlights||[]};return{...e,filters:{...e.filters,...t},apiFilters:r}})}async commitAvailabilityChange(){await this.submitFilterUpdate()}async submitFilterUpdate(){await this.setState(t=>{const e={...t.apiFilters,availability:t.filters.availability||[],bedrooms:t.filters.bedrooms||[],cost:t.filters.cost||null,highlights:t.filters.highlights||[]};return{...t,apiFilters:e}})}async setResultsMode(t){await this.setState(e=>({...e,resultsMode:t}))}async setSortBy(t){await this.setState(e=>({...e,sortBy:t}))}async setResolvedQuestionnaireValues(t,e){await this.setState(r=>({...r,resolvedQuestionnaireValues:{...r.resolvedQuestionnaireValues,[t]:e}}))}async getUnitState(t){var s;const e=await this.getState(),r=e.currentPropertyId?e.properties[e.currentPropertyId]:null;return{isFavorite:(r==null?void 0:r.favoritedUnits.includes(t))??!1,viewedDate:((s=r==null?void 0:r.viewedUnits.find(i=>i.unitId===t))==null?void 0:s.viewedDate)??""}}async getResultsUrl(){const t=await this.getState();return t.currentPropertySlug?`/${t.currentPropertySlug}/results`:null}async getCurrentProperty(){const t=await this.getState();return t.currentPropertyId?t.properties[t.currentPropertyId]??null:null}async getPropertyData(t){const e=await this.getState(),r=t??e.currentPropertyId;return r?e.properties[r]??null:null}async getFullState(){return this.getState()}async initialize(){const t=await this.getState();Object.keys(t.properties).length===0&&!t.filtersLoaded&&await this.setState(e=>({...k,...e,filtersLoaded:!0}))}async loadPersistedFilters(){await this.setState(t=>({...t,filtersLoaded:!0}))}async setData(t){await this.setState(e=>({...e,properties:t}))}async setPropertySlug(t){await this.setCurrentPropertySlug(t)}async setPropertyId(t){await this.setCurrentProperty(t)}async removeData(t){await this.setState(e=>{const{[t]:r,...s}=e.properties;return{...e,properties:s}})}async clearData(){await this.setState(t=>({...t,properties:{}}))}}const l=new Z,m=1,g=n.z.object({uuid:n.z.string().uuid()}),z=n.z.object({propertyId:n.z.string()||n.z.number(),unitIds:n.z.array(n.z.string())}),v="user",Y=()=>{var a;return typeof((a=globalThis.crypto)==null?void 0:a.randomUUID)=="function"?globalThis.crypto.randomUUID():(()=>{var r,s;const t=((s=(r=globalThis.crypto)==null?void 0:r.getRandomValues)==null?void 0:s.call(r,new Uint8Array(16)))??Uint8Array.from({length:16},()=>Math.random()*256|0);t[6]=t[6]&15|64,t[8]=t[8]&63|128;const e=[...t].map(i=>i.toString(16).padStart(2,"0")).join("");return`${e.slice(0,8)}-${e.slice(8,12)}-${e.slice(12,16)}-${e.slice(16,20)}-${e.slice(20)}`})()},T=a=>{var t,e;return((t=a==null?void 0:a.value)==null?void 0:t.useruuid)??((e=a==null?void 0:a.value)==null?void 0:e.uuid)};async function P(a=Y){const t=await w(),e=T(await t.kv.get(v));if(e){const r=await t.users.get(e);if(r)return g.parse(r)}try{return await t.transaction("rw",t.kv,t.users,async()=>{const r=T(await t.kv.get(v));if(r){const o=await t.users.get(r);if(o)return g.parse(o)}const s=a();await t.kv.add({key:v,value:{useruuid:s}});const i=g.parse({uuid:s,createdAt:new Date().toISOString()});return await t.users.add(i),i})}catch(r){if((r==null?void 0:r.name)==="ConstraintError"){const s=T(await t.kv.get(v));if(s){const i=await t.users.get(s);if(i)return g.parse(i)}}throw r}}const mt=async a=>(await P(a)).uuid;async function W(){const a=await w(),t={};for(const e of a.tables)t[e.name]=await e.toArray();return t}async function vt(a="inresi-orm-export.json"){if(typeof window>"u"||typeof document>"u")throw new Error("exportJSON can only run in a browser.");const t=await W(),e=new Blob([JSON.stringify(t,null,2)],{type:"application/json"}),r=document.createElement("a");r.href=URL.createObjectURL(e),r.download=a,document.body.appendChild(r),r.click(),r.remove(),URL.revokeObjectURL(r.href)}const X=(a,t)=>`favorites:${a}:${t}`;async function F(a){const t=await w(),{uuid:e}=await P(),r=X(e,String(a)),s=await t.kv.get(r),i=s?z.safeParse(s.value):null;return i!=null&&i.success?i.data.unitIds:[]}async function j(a,t,e){const r=await w(),{uuid:s}=await P(),i=X(s,String(a));return r.transaction("rw",r.kv,async()=>{const o=await r.kv.get(i),c=o&&z.safeParse(o.value).success?o.value:{unitIds:[],updatedAt:new Date().toISOString()},u=new Set(c.unitIds);e?u.add(t):u.delete(t);const y={unitIds:[...u],updatedAt:new Date().toISOString()},p=z.parse(y);return await r.kv.put({key:i,value:p}),p.unitIds})}async function tt(a,t){const r=!(await F(a)).includes(t);return j(a,t,r)}async function et(a,t){return(await F(a)).includes(t)}function $(a){return(t,e)=>{const r=async()=>{const i=await l.getFullState();t({properties:i.properties,currentPropertyId:i.currentPropertyId,currentPropertySlug:i.currentPropertySlug,hasPreviouslySearched:i.hasPreviouslySearched,filters:i.filters,tempFilters:i.tempFilters,apiFilters:i.apiFilters,resultsMode:i.resultsMode,resolvedQuestionnaireValues:i.resolvedQuestionnaireValues,sortBy:i.sortBy,filtersLoaded:i.filtersLoaded})},s=async()=>{if(a!=null&&a.onFilterUpdate){const i=(await l.getFullState()).apiFilters;a.onFilterUpdate(i)}};return{properties:{},currentPropertyId:null,currentPropertySlug:null,hasPreviouslySearched:[],units:{},filters:{availability:void 0,bedrooms:void 0,cost:void 0,highlights:void 0},tempFilters:{availability:void 0,bedrooms:void 0,cost:void 0,highlights:void 0},apiFilters:{limit:10,page:1},resultsMode:"all",resolvedQuestionnaireValues:{},sortBy:"relevance",filtersLoaded:!1,async initializeProperty(i,o){await l.initializeProperty(i,o),await r()},async setCurrentProperty(i,o){await l.setCurrentProperty(i,o),t({currentPropertyId:i,...o&&{currentPropertySlug:o}})},async setCurrentPropertySlug(i){await l.setCurrentPropertySlug(i),t({currentPropertySlug:i})},async setHasPreviouslySearched(i){await l.setHasPreviouslySearched(i),await r()},async toggleFavorite(i){await l.toggleFavorite(i),await r()},async markUnitAsViewed(i,o){await l.markUnitAsViewed(i,o),await r()},async setTourContactedOn(){await l.setTourContactedOn(),await r()},getTourContactedOn:l.getTourContactedOn.bind(l),async setQuestionnaireResults(i){await l.setQuestionnaireResults(i),await r()},async setTourContactData(i){await l.setTourContactData(i),await r()},async setFilters(i){await l.setFilters(i);const o=e();t({filters:{...o.filters,...i}})},async setTempFilters(i){await l.setTempFilters(i);const o=e();t({tempFilters:{...o.tempFilters,...i}})},async setFiltersToDefault(){await l.setFiltersToDefault(),t({filters:{availability:void 0,bedrooms:void 0,cost:void 0,highlights:void 0}})},async setApiFilters(i){await l.setApiFilters(i);const o=e();t({apiFilters:{...o.apiFilters,...i}})},async handleTempFilterChange(i,o){await l.handleTempFilterChange(i,o);const c=e();t({tempFilters:{...c.tempFilters,[i]:o}})},async commitTempFilterChange(i,o){await l.commitTempFilterChange(i,o),await r(),await s()},async handleFilterCommitIndexDB(i){await l.handleFilterCommitIndexDB(i),await r(),await s()},async commitAvailabilityChange(){await l.commitAvailabilityChange(),await r(),await s()},async submitFilterUpdate(){await l.submitFilterUpdate(),await r(),await s()},async setResultsMode(i){await l.setResultsMode(i),t({resultsMode:i})},async setSortBy(i){await l.setSortBy(i),t({sortBy:i})},async setResolvedQuestionnaireValues(i,o){await l.setResolvedQuestionnaireValues(i,o);const c=e();t({resolvedQuestionnaireValues:{...c.resolvedQuestionnaireValues,[i]:o}})},getUnitState(i){var u;const o=e(),c=o.currentPropertyId?o.properties[o.currentPropertyId]:null;return{isFavorite:(c==null?void 0:c.favoritedUnits.includes(i))??!1,viewedDate:((u=c==null?void 0:c.viewedUnits.find(y=>y.unitId===i))==null?void 0:u.viewedDate)??""}},getResultsUrl:l.getResultsUrl.bind(l),getCurrentProperty:l.getCurrentProperty.bind(l),getPropertyData:l.getPropertyData.bind(l),async setData(i){await l.setData(i),t({properties:i})},async setPropertySlug(i){await l.setPropertySlug(i),t({currentPropertySlug:i})},async setPropertyId(i){await l.setPropertyId(i),t({currentPropertyId:i})},async removeData(i){await l.removeData(i),await r()},async clearData(){await l.clearData(),t({properties:{}})},async loadPersistedFilters(){await l.loadPersistedFilters(),t({filtersLoaded:!0})},async _hydrate(){await r()},async _initialize(){await l.initialize(),await r()}}}}function bt(){return a=>t=>a(e=>e.getUnitState(t))}function rt(a){return{property:{unit:{favorites:{toggle:a.toggleFavorite.bind(a)},viewed:{mark:a.markUnitAsViewed.bind(a)}},questionnaire:{setResults:a.setQuestionnaireResults.bind(a)},tour:{setContactedOn:a.setTourContactedOn.bind(a),getContactedOn:a.getTourContactedOn.bind(a),setContactData:a.setTourContactData.bind(a)}},filters:{set:a.setFilters.bind(a),setTemp:a.setTempFilters.bind(a),setToDefault:a.setFiltersToDefault.bind(a),commitTemp:a.commitTempFilterChange.bind(a),commitAvailability:a.commitAvailabilityChange.bind(a),submit:a.submitFilterUpdate.bind(a)}}}function zt(a){return(t,e)=>{const r=$(a)(t,e),s=rt(r);return{...r,...s}}}const Pt={async isFavorite(a,t){return et(a,t)},async toggle(a,t){return tt(a,t)},async set(a,t,e){return j(a,t,e)},async getAll(a){return F(a)}};exports.AppStoreDataSchema=pt;exports.FavoritesSchema=z;exports.FiltersSchema=V;exports.OpenDBError=Q;exports.PropertyDataSchema=q;exports.PropertyStore=J;exports.PropertyStoreDataSchema=ut;exports.QueryParamsSchema=K;exports.SCHEMA_VERSION=m;exports.SchemaMismatchError=B;exports.TourContactDataSchema=H;exports.UnifiedStore=Z;exports.UnifiedStoreDataSchema=ft;exports.UnitDataSchema=G;exports.UserSchema=g;exports.ViewedUnitSchema=N;exports.configureValidation=x;exports.createORMStringStorage=lt;exports.createStructuredStore=zt;exports.createStructuredStoreActions=rt;exports.createUseUnitState=bt;exports.createZustandPropertyStore=$;exports.createZustandUnifiedStore=$;exports.debugDump=W;exports.defaultIdGenerator=Y;exports.ensureUser=P;exports.exportJSON=vt;exports.favorites=Pt;exports.getDB=w;exports.getFavoritedUnitsForProperty=F;exports.getUserUUID=mt;exports.isUnitFavorited=et;exports.kvGet=A;exports.kvRemove=ct;exports.kvSet=E;exports.propertyStore=yt;exports.resetDB=ot;exports.setFavoriteUnit=j;exports.store=l;exports.toggleFavoriteUnit=tt;
2
+ //# sourceMappingURL=index.cjs.map