universe-code 0.0.73 โ†’ 0.0.74

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,19 +1,17 @@
1
1
  # ๐ŸŒŒ universe-code (Angular)
2
2
 
3
- A lightweight and powerful **IndexedDB utility for Angular** that helps you cache API responses, manage offline storage, and reduce unnecessary network calls โ€” with a **clean, service-based API**.
4
-
5
- Designed for **modern Angular (standalone + DI)** and production-ready applications.
3
+ A lightweight and powerful **IndexedDB utility for Angular** that helps you cache API data, manage offline storage, and reduce unnecessary network calls โ€” with a clean and simple API that works in any Angular application.
6
4
 
7
5
  ---
8
6
 
9
7
  ## โœจ Features
10
8
 
11
- * โšก Easy IndexedDB configuration
12
- * ๐Ÿง  Built-in API caching with expiry
13
- * ๐Ÿ“ฆ Simple CRUD helpers
14
- * ๐Ÿ” Auto refresh when cache expires
15
- * ๐Ÿงฉ Angular Dependency Injection support
16
- * ๐Ÿ›  Clean & professional service-based API
9
+ - โšก Easy IndexedDB setup in Angular
10
+ - ๐Ÿง  Built-in API caching with expiry (TTL)
11
+ - ๐Ÿ“ฆ Simple CRUD operations
12
+ - ๐Ÿ” Auto re-fetch when cache expires
13
+ - ๐Ÿงฑ Framework-agnostic core with Angular-friendly usage
14
+ - ๐Ÿ›  Clean and developer-friendly API
17
15
 
18
16
  ---
19
17
 
@@ -29,116 +27,165 @@ npm install universe-code@latest
29
27
 
30
28
  ## โš™๏ธ IndexedDB Setup (One Time Only)
31
29
 
32
- IndexedDB should be initialized **once at application bootstrap level**.
30
+ Configure IndexedDB once in your Angular app and reuse it everywhere via a shared Angular service.
33
31
 
34
32
  ---
35
33
 
36
- ## 1๏ธโƒฃ Import Angular Compiler (Required)
34
+ ## 1๏ธโƒฃ Create an Angular IndexedDB Service
37
35
 
38
- `universe-code` internally uses Angular decorators and DI.
39
- For JIT environments, Angular Compiler **must be available**.
36
+ Create the following file:
40
37
 
41
- ๐Ÿ“ `main.ts`
38
+ **๐Ÿ“ `src/app/services/app-idb.service.ts`**
42
39
 
43
40
  ```ts
44
- import '@angular/compiler';
45
- import { bootstrapApplication } from '@angular/platform-browser';
46
- import { appConfig } from './app/app.config';
47
- import { App } from './app/app';
48
-
49
- bootstrapApplication(App, appConfig)
50
- .catch(console.error);
51
- ```
41
+ import { Injectable } from '@angular/core';
42
+ import { IdbService as CoreIdbService } from 'universe-code/indexdb';
43
+ // If your bundler does not support subpath exports, use this instead:
44
+ // import { IdbService as CoreIdbService } from 'universe-code/dist/indexdb/services/idbService.js';
45
+
46
+ interface IdbConfig {
47
+ dbName: string;
48
+ version: number;
49
+ storeName: string;
50
+ }
52
51
 
53
- > โš ๏ธ **Note:**
54
- > This avoids `JIT compilation failed for injectable class` errors.
52
+ // Centralized IndexedDB configuration for this Angular app
53
+ const IDB_CONFIG: IdbConfig = {
54
+ dbName: 'db',
55
+ version: 1,
56
+ storeName: 'store',
57
+ };
58
+
59
+ @Injectable({ providedIn: 'root' })
60
+ export class AppIdbService {
61
+ private readonly client = new CoreIdbService(IDB_CONFIG);
62
+
63
+ fetchWithCache<T>(
64
+ key: string,
65
+ ttlMs: number,
66
+ apiFn: () => Promise<T>
67
+ ): Promise<T | null> {
68
+ return this.client.getWithExpiry(key, ttlMs, apiFn);
69
+ }
55
70
 
56
- ---
71
+ get<T = any>(key: string): Promise<T | null> {
72
+ return this.client.get(key);
73
+ }
57
74
 
58
- ## 2๏ธโƒฃ Import the IndexedDB Service
75
+ set(data: any): Promise<any> {
76
+ return this.client.put(data);
77
+ }
59
78
 
60
- `IdbService` is provided via Angular DI and requires **no manual provider setup**.
79
+ remove(key: string): Promise<any> {
80
+ return this.client.remove(key);
81
+ }
61
82
 
62
- ```ts
63
- import { IdbService } from 'universe-code/angular';
83
+ clear(): Promise<any> {
84
+ return this.client.clear();
85
+ }
86
+ }
64
87
  ```
65
88
 
66
- ---
89
+ ### โœ… What this service does
90
+
91
+ - Creates a single `IdbService` instance for the entire app
92
+ - Centralizes database name, version, and store name
93
+ - Exposes clean methods usable in any component or service
67
94
 
68
- ## ๐Ÿš€ Using IndexedDB in Angular Components
95
+ ---
69
96
 
70
- Inject the service inside any component or service.
97
+ ## 2๏ธโƒฃ Use the Service in Your Angular Component
71
98
 
72
- ๐Ÿ“ `app.ts`
99
+ Example `AppComponent`:
73
100
 
74
101
  ```ts
75
102
  import { Component, OnInit } from '@angular/core';
76
- import { IdbService } from 'universe-code/angular';
103
+ import { AppIdbService } from './services/app-idb.service';
104
+ import { ApiService } from './services/api.service';
105
+ import { minutesToMs } from 'universe-code/core';
77
106
 
78
107
  @Component({
79
108
  selector: 'app-root',
80
- template: `<h1>Angular App</h1>`,
109
+ templateUrl: './app.component.html',
110
+ styleUrls: ['./app.component.css'],
81
111
  })
82
- export class App implements OnInit {
83
-
84
- constructor(private idb: IdbService) {}
85
-
86
- async ngOnInit() {
87
- const data = await this.idb.fetchWithCache(
88
- 'example-key',
89
- 60_000, // cache expiry in ms
90
- async () => {
91
- const res = await fetch('https://example.com/api/data');
92
- return res.json();
93
- }
94
- );
95
-
96
- console.log(data);
112
+ export class AppComponent implements OnInit {
113
+ constructor(
114
+ private readonly api: ApiService,
115
+ private readonly idb: AppIdbService
116
+ ) {}
117
+
118
+ async ngOnInit(): Promise<void> {
119
+ try {
120
+ const data = await this.idb.fetchWithCache(
121
+ 'allEventList',
122
+ minutesToMs(1),
123
+ () => this.api.getAllEvents()
124
+ );
125
+
126
+ console.log('All Events:', data);
127
+ } catch (error) {
128
+ console.error('Failed to load events:', error);
129
+ }
97
130
  }
98
131
  }
99
132
  ```
100
133
 
134
+ ### โœ… IndexedDB is now ready
135
+
136
+ Once `AppIdbService` is created, no additional setup is required.
137
+ Simply inject it wherever IndexedDB access is needed.
138
+
101
139
  ---
102
140
 
103
- ## ๐Ÿ” Fetch API Data with Cache + Expiry
141
+ ## ๐Ÿ” API Fetch + Cache with Expiry
104
142
 
105
143
  ```ts
106
- const result = await idb.fetchWithCache(
107
- 'users',
108
- 5 * 60 * 1000,
109
- async () => {
110
- const res = await fetch('/api/users');
111
- return res.json();
112
- }
113
- );
144
+ import { minutesToMs } from 'universe-code/core';
145
+
146
+ async function loadUserProfile(idb: AppIdbService) {
147
+ const user = await idb.fetchWithCache(
148
+ 'user-profile',
149
+ minutesToMs(5),
150
+ async () => {
151
+ const res = await fetch('https://example.com/api/user');
152
+ const json = await res.json();
153
+ return json.data;
154
+ }
155
+ );
156
+
157
+ console.log('User profile:', user);
158
+ }
114
159
  ```
115
160
 
116
- ### ๐Ÿง  How it Works
161
+ ### ๐Ÿง  How it works
117
162
 
118
- * Checks IndexedDB for cached data
119
- * Validates expiry timestamp
120
- * Calls API only if cache is missing or expired
121
- * Stores fresh data automatically
122
- * Returns cached data instantly on next call
163
+ - Calls API only if data is missing or expired
164
+ - Stores response in IndexedDB with timestamp
165
+ - Returns cached data instantly within TTL
166
+ - Falls back to cached data if API fails
123
167
 
124
168
  ---
125
169
 
126
- ## ๐Ÿ“ฅ Get Cached Data Only
170
+ ## ๐Ÿ“ฅ Get Data from IndexedDB
127
171
 
128
172
  ```ts
129
- const data = await idb.get('users');
173
+ const data = await this.idb.get('users');
174
+ console.log(data);
130
175
  ```
131
176
 
132
177
  ---
133
178
 
134
- ## โœ๏ธ Insert / Update Data Manually
179
+ ## โœ๏ธ Insert / Update Data
180
+
181
+ > Default store `keyPath` is `id`
135
182
 
136
183
  ```ts
137
- await idb.put({
138
- id: 'profile',
184
+ await this.idb.set({
185
+ id: 'users',
139
186
  data: {
140
- name: 'John',
141
- role: 'Admin',
187
+ name: 'Mark',
188
+ country: 'USA',
142
189
  },
143
190
  timestamp: Date.now(),
144
191
  });
@@ -146,10 +193,10 @@ await idb.put({
146
193
 
147
194
  ---
148
195
 
149
- ## ๐Ÿ—‘๏ธ Remove a Specific Record
196
+ ## ๐Ÿ—‘๏ธ Delete a Specific Record
150
197
 
151
198
  ```ts
152
- await idb.remove('profile');
199
+ await this.idb.remove('users');
153
200
  ```
154
201
 
155
202
  ---
@@ -157,34 +204,50 @@ await idb.remove('profile');
157
204
  ## ๐Ÿงน Clear Entire IndexedDB Store
158
205
 
159
206
  ```ts
160
- await idb.clear();
207
+ await this.idb.clear();
161
208
  ```
162
209
 
163
210
  ---
164
211
 
165
- ## ๐Ÿ“š API Reference
212
+ ## ๐Ÿ“š API Reference (Angular Wrapper)
213
+
214
+ ### `AppIdbService`
166
215
 
167
- | Method | Description |
168
- | -------------------------------------- | ---------------------------- |
169
- | `get(id)` | Retrieve data from IndexedDB |
170
- | `put(payload)` | Insert or update data |
171
- | `remove(id)` | Delete a specific record |
172
- | `clear()` | Clear entire IndexedDB |
173
- | `fetchWithCache(id, expiry, callback)` | Fetch + cache with expiry |
216
+ | Method | Signature | Description |
217
+ |------|----------|-------------|
218
+ | fetchWithCache | `(key, ttlMs, apiFn)` | Fetch & cache data with expiry |
219
+ | get | `(key)` | Get record by key |
220
+ | set | `(data)` | Insert or update record |
221
+ | remove | `(key)` | Delete record |
222
+ | clear | `()` | Clear store |
223
+
224
+ ---
225
+
226
+ ## โš™๏ธ Core: `IdbService`
227
+
228
+ Used internally from `universe-code/indexdb`:
229
+
230
+ - `connect()`
231
+ - `get(key)`
232
+ - `put(data)`
233
+ - `remove(key)`
234
+ - `clear()`
235
+ - `getWithExpiry(key, ttl, apiFn)`
236
+
237
+ Can also be used outside Angular (JS / TS / other frameworks).
174
238
 
175
239
  ---
176
240
 
177
241
  ## โœ… Ideal Use Cases
178
242
 
179
- * API response caching
180
- * Offline-first Angular apps
181
- * Performance optimization
182
- * Reducing repeated network calls
183
- * Storing session-level data
243
+ - API response caching
244
+ - Offline-first Angular apps
245
+ - Performance optimization
246
+ - Local session & config storage
247
+ - Shared data across routes/components
184
248
 
185
249
  ---
186
250
 
187
251
  ## ๐Ÿ›ก License
188
252
 
189
- MIT License ยฉ universe-code
190
-
253
+ MIT License ยฉ **universe-code**
@@ -1,3 +1,4 @@
1
+ import { connectDB } from "./idbStore.js";
1
2
  let config = null;
2
3
 
3
4
  export const configureIdb = (options) => {
@@ -5,6 +6,7 @@ export const configureIdb = (options) => {
5
6
  return;
6
7
  }
7
8
  config = options;
9
+ connectDB();
8
10
  };
9
11
 
10
12
  export const getConfig = () => {
@@ -6,7 +6,7 @@ let store = null;
6
6
  let manager = null;
7
7
 
8
8
  // โœ… index db connection
9
- const connectDB = async () => {
9
+ export const connectDB = async () => {
10
10
  const { dbName, dbVersion, storeName } = getConfig(); // โœ… FIXED
11
11
 
12
12
  if (!manager) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universe-code",
3
- "version": "0.0.73",
3
+ "version": "0.0.74",
4
4
  "description": "Universal utility functions for all JS frameworks",
5
5
  "license": "ISC",
6
6
  "type": "module",