idb-ts 3.0.1 → 3.0.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 +8 -0
- package/package.json +2 -2
- package/index.ts +0 -188
package/README.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# 🚀 idb-ts
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/idb-ts)
|
|
4
|
+
[](https://badgen.net/bundlephobia/min/idb-ts)
|
|
5
|
+
[](https://badgen.net/bundlephobia/minzip/idb-ts)
|
|
6
|
+
|
|
7
|
+
[](https://github.com/maifeeulasad/idb-ts/stargazers)
|
|
8
|
+
[](https://github.com/maifeeulasad/idb-ts/watchers)
|
|
9
|
+
[](https://img.shields.io/github/commits-since/maifeeulasad/idb-ts/latest/main?include_prereleases)
|
|
10
|
+
|
|
3
11
|
## 📌 Introduction
|
|
4
12
|
**idb-ts** is a lightweight, declarative, and type-safe way to work with IndexedDB using TypeScript. Effortlessly perform CRUD operations on your database with clean, structured code! 🔥
|
|
5
13
|
|
package/package.json
CHANGED
package/index.ts
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import 'reflect-metadata';
|
|
2
|
-
|
|
3
|
-
function KeyPath(): PropertyDecorator {
|
|
4
|
-
return (target: Object, propertyKey: string | symbol) => {
|
|
5
|
-
const constructor = target.constructor as Function;
|
|
6
|
-
Reflect.defineMetadata("keypath", [propertyKey as string], constructor);
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
function DataClass(): ClassDecorator {
|
|
11
|
-
return (target: Function) => {
|
|
12
|
-
if (!Reflect.getMetadata("keypath", target)) {
|
|
13
|
-
throw new Error(`No keypath field defined for the class ${target.name}.`);
|
|
14
|
-
}
|
|
15
|
-
const keyPathFields = Reflect.getMetadata("keypath", target) || [];
|
|
16
|
-
if (keyPathFields.length > 1) {
|
|
17
|
-
throw new Error(`Only one keypath field can be defined for the class ${target.name}.`);
|
|
18
|
-
}
|
|
19
|
-
Reflect.defineMetadata("dataclass", true, target);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
class Database {
|
|
24
|
-
private dbName: string;
|
|
25
|
-
private classes: Function[];
|
|
26
|
-
private db: IDBDatabase | null = null;
|
|
27
|
-
|
|
28
|
-
private constructor(dbName: string, classes: Function[]) {
|
|
29
|
-
this.dbName = dbName;
|
|
30
|
-
if (!classes.every(cls => Reflect.getMetadata("dataclass", cls))) {
|
|
31
|
-
throw new Error("All classes should be decorated with @DataClass.");
|
|
32
|
-
}
|
|
33
|
-
this.classes = classes;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
public static async build(dbName: string, classes: Function[]): Promise<Database> {
|
|
37
|
-
const instance = new Database(dbName, classes);
|
|
38
|
-
await instance.initDB();
|
|
39
|
-
return instance;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
private async initDB(): Promise<void> {
|
|
43
|
-
return new Promise((resolve, reject) => {
|
|
44
|
-
const request = indexedDB.open(this.dbName, 1);
|
|
45
|
-
|
|
46
|
-
request.onupgradeneeded = () => {
|
|
47
|
-
const db = request.result;
|
|
48
|
-
|
|
49
|
-
this.classes.forEach((cls) => {
|
|
50
|
-
const keyPathFields = Reflect.getMetadata("keypath", cls) || [];
|
|
51
|
-
|
|
52
|
-
const storeName = cls.name.toLowerCase() + "s";
|
|
53
|
-
|
|
54
|
-
if (!db.objectStoreNames.contains(storeName)) {
|
|
55
|
-
db.createObjectStore(storeName, { keyPath: keyPathFields[0] });
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
request.onsuccess = () => {
|
|
61
|
-
this.db = request.result;
|
|
62
|
-
console.log(`Database initialized with object stores for: ${this.classes.map(cls => cls.name).join(", ")}`);
|
|
63
|
-
resolve();
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
request.onerror = () => {
|
|
67
|
-
console.error("Error initializing database:", request.error);
|
|
68
|
-
reject(request.error);
|
|
69
|
-
};
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private getObjectStore(className: string, mode: IDBTransactionMode): IDBObjectStore {
|
|
74
|
-
if (!this.db) {
|
|
75
|
-
throw new Error("Database not initialized.");
|
|
76
|
-
}
|
|
77
|
-
const storeName = className.toLowerCase() + "s";
|
|
78
|
-
const transaction = this.db.transaction(storeName, mode);
|
|
79
|
-
return transaction.objectStore(storeName);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
async create<T>(cls: { new(...args: any[]): T }, item: T): Promise<void> {
|
|
83
|
-
return new Promise((resolve, reject) => {
|
|
84
|
-
try {
|
|
85
|
-
const store = this.getObjectStore(cls.name, "readwrite");
|
|
86
|
-
const request = store.add(item);
|
|
87
|
-
|
|
88
|
-
request.onsuccess = () => {
|
|
89
|
-
console.log(`Item added to ${cls.name}:`, item);
|
|
90
|
-
resolve();
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
request.onerror = () => {
|
|
94
|
-
console.error(`Error adding item to ${cls.name}:`, request.error);
|
|
95
|
-
reject(request.error);
|
|
96
|
-
};
|
|
97
|
-
} catch (error) {
|
|
98
|
-
reject(error);
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
async read<T>(cls: { new(...args: any[]): T }, key: string): Promise<T | undefined> {
|
|
104
|
-
return new Promise((resolve, reject) => {
|
|
105
|
-
try {
|
|
106
|
-
const store = this.getObjectStore(cls.name, "readonly");
|
|
107
|
-
const request = store.get(key);
|
|
108
|
-
|
|
109
|
-
request.onsuccess = () => {
|
|
110
|
-
console.log(`Item read from ${cls.name}:`, request.result);
|
|
111
|
-
resolve(request.result as T | undefined);
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
request.onerror = () => {
|
|
115
|
-
console.error(`Error reading item from ${cls.name}:`, request.error);
|
|
116
|
-
reject(request.error);
|
|
117
|
-
};
|
|
118
|
-
} catch (error) {
|
|
119
|
-
reject(error);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async update<T>(cls: { new(...args: any[]): T }, item: T): Promise<void> {
|
|
125
|
-
return new Promise((resolve, reject) => {
|
|
126
|
-
try {
|
|
127
|
-
const store = this.getObjectStore(cls.name, "readwrite");
|
|
128
|
-
const request = store.put(item);
|
|
129
|
-
|
|
130
|
-
request.onsuccess = () => {
|
|
131
|
-
console.log(`Item updated in ${cls.name}:`, item);
|
|
132
|
-
resolve();
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
request.onerror = () => {
|
|
136
|
-
console.error(`Error updating item in ${cls.name}:`, request.error);
|
|
137
|
-
reject(request.error);
|
|
138
|
-
};
|
|
139
|
-
} catch (error) {
|
|
140
|
-
reject(error);
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async delete<T>(cls: { new(...args: any[]): T }, key: string): Promise<void> {
|
|
146
|
-
return new Promise((resolve, reject) => {
|
|
147
|
-
try {
|
|
148
|
-
const store = this.getObjectStore(cls.name, "readwrite");
|
|
149
|
-
const request = store.delete(key);
|
|
150
|
-
|
|
151
|
-
request.onsuccess = () => {
|
|
152
|
-
console.log(`Item deleted from ${cls.name}:`, key);
|
|
153
|
-
resolve();
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
request.onerror = () => {
|
|
157
|
-
console.error(`Error deleting item from ${cls.name}:`, request.error);
|
|
158
|
-
reject(request.error);
|
|
159
|
-
};
|
|
160
|
-
} catch (error) {
|
|
161
|
-
reject(error);
|
|
162
|
-
}
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
async list<T>(cls: { new(...args: any[]): T }): Promise<T[]> {
|
|
167
|
-
return new Promise((resolve, reject) => {
|
|
168
|
-
try {
|
|
169
|
-
const store = this.getObjectStore(cls.name, "readonly");
|
|
170
|
-
const request = store.getAll();
|
|
171
|
-
|
|
172
|
-
request.onsuccess = () => {
|
|
173
|
-
console.log(`All items from ${cls.name}:`, request.result);
|
|
174
|
-
resolve(request.result as T[]);
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
request.onerror = () => {
|
|
178
|
-
console.error(`Error listing items from ${cls.name}:`, request.error);
|
|
179
|
-
reject(request.error);
|
|
180
|
-
};
|
|
181
|
-
} catch (error) {
|
|
182
|
-
reject(error);
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
export { Database, KeyPath, DataClass };
|