universe-code 0.0.66 → 0.0.68

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 @@
1
+ export { IdbService } from './indexdb/services/idbService';
@@ -0,0 +1,9 @@
1
+ import { InjectionToken } from '@angular/core';
2
+
3
+ export interface IdbConfig {
4
+ dbName: string;
5
+ version: number;
6
+ storeName: string;
7
+ }
8
+
9
+ export const IDB_CONFIG = new InjectionToken<IdbConfig>('IDB_CONFIG');
@@ -0,0 +1,72 @@
1
+ import { Injectable, PLATFORM_ID, inject } from "@angular/core";
2
+ import { isPlatformBrowser } from "@angular/common";
3
+ import { DBManager, DB } from "universe-code";
4
+ import { IDB_CONFIG } from "./idb.config";
5
+
6
+ @Injectable({ providedIn: "root" })
7
+ export class IdbService {
8
+ private db: IDBDatabase | null = null;
9
+ private manager: any = null;
10
+
11
+ private platformId = inject(PLATFORM_ID);
12
+ private options = inject(IDB_CONFIG);
13
+
14
+ private isBrowser = isPlatformBrowser(this.platformId);
15
+
16
+ private async connect() {
17
+ if (!this.isBrowser) return null;
18
+
19
+ if (!this.manager) {
20
+ this.manager = new DBManager(
21
+ this.options.dbName,
22
+ this.options.version
23
+ );
24
+ }
25
+
26
+ if (!this.db) {
27
+ this.db = await this.manager.connect([
28
+ { name: this.options.storeName },
29
+ ]);
30
+ }
31
+
32
+ return this.db;
33
+ }
34
+
35
+ get(key: string) {
36
+ return this.connect().then(db =>
37
+ db ? DB.get(db, this.options.storeName, key) : null
38
+ );
39
+ }
40
+
41
+ put(data: any) {
42
+ return this.connect().then(db =>
43
+ db ? DB.put(db, this.options.storeName, data) : null
44
+ );
45
+ }
46
+
47
+ remove(key: string) {
48
+ return this.connect().then(db =>
49
+ db ? DB.remove(db, this.options.storeName, key) : null
50
+ );
51
+ }
52
+
53
+ clear() {
54
+ return this.connect().then(db =>
55
+ db ? DB.clear(db, this.options.storeName) : null
56
+ );
57
+ }
58
+
59
+ getWithExpiry(key: string, ttl: number, api: () => Promise<any>) {
60
+ return this.connect().then(db =>
61
+ db
62
+ ? DB.getWithExpiry(
63
+ db,
64
+ this.options.storeName,
65
+ key,
66
+ ttl,
67
+ api
68
+ )
69
+ : null
70
+ );
71
+ }
72
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "universe-code",
3
- "version": "0.0.66",
3
+ "version": "0.0.68",
4
4
  "description": "Universal utility functions for all JS frameworks",
5
5
  "license": "ISC",
6
6
  "type": "module",
@@ -48,6 +48,7 @@
48
48
  "test": "echo \"No tests yet\""
49
49
  },
50
50
  "dependencies": {
51
+ "@angular/common": "^21.0.6",
51
52
  "@angular/core": "^21.0.6",
52
53
  "react": "^19.2.3"
53
54
  },
@@ -56,4 +57,4 @@
56
57
  "angular-indexdb",
57
58
  "react-indexdb"
58
59
  ]
59
- }
60
+ }
@@ -1 +0,0 @@
1
- export { IdbService } from './services/idbService.ts';
@@ -1,66 +0,0 @@
1
- import { Injectable } from '@angular/core';
2
- import { DBManager, DB } from 'universe-code/indexdb';
3
-
4
- @Injectable({ providedIn: 'root' })
5
- export class IdbService {
6
-
7
- private manager = new DBManager('exchange', 1);
8
- private db: IDBDatabase | null = null;
9
- private store: any = null;
10
- private readonly storeName = 'exchangeStore';
11
-
12
- private async getStore() {
13
- if (!this.store) {
14
- if (!this.db) {
15
- this.db = await this.manager.connect([{ name: this.storeName }]);
16
- }
17
-
18
- this.store = {
19
- get: (key: string) =>
20
- DB.get(this.db!, this.storeName, key),
21
-
22
- getWithExpiry: (
23
- key: string,
24
- ttl: number,
25
- api: () => Promise<any>
26
- ) =>
27
- DB.getWithExpiry(this.db!, this.storeName, key, ttl, api),
28
-
29
- put: (data: any) =>
30
- DB.put(this.db!, this.storeName, data),
31
-
32
- remove: (key: string) =>
33
- DB.remove(this.db!, this.storeName, key),
34
-
35
- clear: () =>
36
- DB.clear(this.db!, this.storeName)
37
- };
38
- }
39
-
40
- return this.store;
41
- }
42
-
43
- async fetchWithCache(
44
- key: string,
45
- ttl: number,
46
- apiFn: () => Promise<any>
47
- ) {
48
- return (await this.getStore()).getWithExpiry(key, ttl, apiFn);
49
- }
50
-
51
- async get(key: string) {
52
- return (await this.getStore()).get(key);
53
- }
54
-
55
- async set(data: any) {
56
- return (await this.getStore()).put(data);
57
- }
58
-
59
- async remove(key: string) {
60
- return (await this.getStore()).remove(key);
61
- }
62
-
63
- async clear() {
64
- return (await this.getStore()).clear();
65
- }
66
- }
File without changes