universe-code 0.0.67 → 0.0.69
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/dist/angular/index.js +1 -0
- package/dist/angular/indexdb/services/idb.config.mjs +10 -0
- package/dist/angular/indexdb/services/idbService.mjs +50 -0
- package/package.json +1 -1
- package/dist/angular/index.ts +0 -1
- package/dist/angular/indexdb/services/idb.config.ts +0 -9
- package/dist/angular/indexdb/services/idbService.ts +0 -72
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { IdbService } from './indexdb/services/idbService.mjs';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// idb.config.mjs
|
|
2
|
+
import { InjectionToken } from '@angular/core';
|
|
3
|
+
|
|
4
|
+
export const IDB_CONFIG = new InjectionToken('IDB_CONFIG');
|
|
5
|
+
|
|
6
|
+
/** @typedef {Object} IdbConfig
|
|
7
|
+
* @property {string} dbName
|
|
8
|
+
* @property {number} version
|
|
9
|
+
* @property {string} storeName
|
|
10
|
+
*/
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DBManager, DB } from "universe-code";
|
|
2
|
+
import { IDB_CONFIG } from "./idb.config.mjs";
|
|
3
|
+
|
|
4
|
+
export class IdbService {
|
|
5
|
+
db = null;
|
|
6
|
+
manager = null;
|
|
7
|
+
isBrowser = typeof window !== "undefined"; // Check if running in browser
|
|
8
|
+
options = IDB_CONFIG;
|
|
9
|
+
|
|
10
|
+
constructor() { }
|
|
11
|
+
|
|
12
|
+
async connect() {
|
|
13
|
+
if (!this.isBrowser) return null;
|
|
14
|
+
|
|
15
|
+
if (!this.manager) {
|
|
16
|
+
this.manager = new DBManager(this.options.dbName, this.options.version);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (!this.db) {
|
|
20
|
+
this.db = await this.manager.connect([{ name: this.options.storeName }]);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return this.db;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async get(key) {
|
|
27
|
+
const db = await this.connect();
|
|
28
|
+
return db ? DB.get(db, this.options.storeName, key) : null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async put(data) {
|
|
32
|
+
const db = await this.connect();
|
|
33
|
+
return db ? DB.put(db, this.options.storeName, data) : null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async remove(key) {
|
|
37
|
+
const db = await this.connect();
|
|
38
|
+
return db ? DB.remove(db, this.options.storeName, key) : null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async clear() {
|
|
42
|
+
const db = await this.connect();
|
|
43
|
+
return db ? DB.clear(db, this.options.storeName) : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async getWithExpiry(key, ttl, api) {
|
|
47
|
+
const db = await this.connect();
|
|
48
|
+
return db ? DB.getWithExpiry(db, this.options.storeName, key, ttl, api) : null;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/package.json
CHANGED
package/dist/angular/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { IdbService } from './indexdb/services/idbService.ts';
|
|
@@ -1,72 +0,0 @@
|
|
|
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
|
-
}
|