@superbright/indexeddb-orm 0.1.2 → 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 +370 -19
- package/package.json +19 -19
package/README.md
CHANGED
|
@@ -1,30 +1,365 @@
|
|
|
1
|
-
#
|
|
1
|
+
# IndexedDB ORM with Structured Store Actions
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A TypeScript-first IndexedDB ORM built with Dexie and Zod, featuring structured Zustand store integration for React applications.
|
|
4
4
|
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
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
|
-
|
|
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
|
-
|
|
24
|
-
pnpm
|
|
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
|
-
##
|
|
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
|
|
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
|
-
##
|
|
44
|
-
|
|
45
|
-
|
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superbright/indexeddb-orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Vite + TypeScript starter for an IndexedDB ORM (Dexie + Zod) with playground.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
|
@@ -15,9 +15,23 @@
|
|
|
15
15
|
}
|
|
16
16
|
},
|
|
17
17
|
"sideEffects": false,
|
|
18
|
-
"files": [
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
"files": ["dist"],
|
|
19
|
+
|
|
20
|
+
"scripts": {
|
|
21
|
+
"clean": "rimraf dist",
|
|
22
|
+
"build": "pnpm clean && vite build && tsc -p tsconfig.build.json",
|
|
23
|
+
"prepare": "pnpm build",
|
|
24
|
+
"start": "vite --clearScreen false",
|
|
25
|
+
"dev": "concurrently -k -n PLAY,BUILD \"pnpm:start\" \"pnpm:watch\"",
|
|
26
|
+
"watch": "concurrently -k -n TYPES,BUNDLE \"pnpm:watch:types\" \"pnpm:watch:bundle\"",
|
|
27
|
+
"watch:types": "tsc -w -p tsconfig.build.json",
|
|
28
|
+
"watch:bundle": "vite build --watch",
|
|
29
|
+
"test": "vitest run",
|
|
30
|
+
"test:watch": "vitest",
|
|
31
|
+
"format": "pnpm format:write",
|
|
32
|
+
"format:write": "prettier --write .",
|
|
33
|
+
"format:check": "prettier --check ."
|
|
34
|
+
},
|
|
21
35
|
"dependencies": {
|
|
22
36
|
"dexie": "^4.0.8",
|
|
23
37
|
"zod": "^3.23.8"
|
|
@@ -32,19 +46,5 @@
|
|
|
32
46
|
"typescript": "^5.4.5",
|
|
33
47
|
"vite": "^5.4.0",
|
|
34
48
|
"vitest": "^2.0.5"
|
|
35
|
-
},
|
|
36
|
-
"scripts": {
|
|
37
|
-
"clean": "rimraf dist",
|
|
38
|
-
"build": "pnpm clean && vite build && tsc -p tsconfig.build.json",
|
|
39
|
-
"start": "vite --clearScreen false",
|
|
40
|
-
"dev": "concurrently -k -n PLAY,BUILD \"pnpm:start\" \"pnpm:watch\"",
|
|
41
|
-
"watch": "concurrently -k -n TYPES,BUNDLE \"pnpm:watch:types\" \"pnpm:watch:bundle\"",
|
|
42
|
-
"watch:types": "tsc -w -p tsconfig.build.json",
|
|
43
|
-
"watch:bundle": "vite build --watch",
|
|
44
|
-
"test": "vitest run",
|
|
45
|
-
"test:watch": "vitest",
|
|
46
|
-
"format": "pnpm format:write",
|
|
47
|
-
"format:write": "prettier --write .",
|
|
48
|
-
"format:check": "prettier --check ."
|
|
49
49
|
}
|
|
50
|
-
}
|
|
50
|
+
}
|