idb-ts 3.0.0 β 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 +44 -16
- package/package.json +2 -2
- package/index.ts +0 -188
package/README.md
CHANGED
|
@@ -1,15 +1,36 @@
|
|
|
1
|
-
# idb-ts
|
|
1
|
+
# π idb-ts
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
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
|
+
|
|
11
|
+
## π Introduction
|
|
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! π₯
|
|
13
|
+
|
|
14
|
+
## π¦ Installation
|
|
15
|
+
Install via npm and start using IndexedDB like a pro! β‘
|
|
16
|
+
```sh
|
|
5
17
|
npm i idb-ts
|
|
6
18
|
```
|
|
7
19
|
|
|
8
|
-
##
|
|
20
|
+
## β¨ Features
|
|
21
|
+
- β
**Declarative & Type-Safe** - Define your data models with decorators.
|
|
22
|
+
- β‘ **Easy CRUD Operations** - Perform create, read, update, and delete seamlessly.
|
|
23
|
+
- π **Fully Typed API** - Benefit from TypeScriptβs powerful type system.
|
|
24
|
+
- ποΈ **Performance Optimized** - Minimal overhead with IndexedDB's native capabilities.
|
|
9
25
|
|
|
10
|
-
|
|
11
|
-
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## π Example Usage
|
|
12
29
|
|
|
30
|
+
### ποΈ Declaring Entities
|
|
31
|
+
Use decorators to define your data models with automatic schema management.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
13
34
|
@DataClass()
|
|
14
35
|
class User {
|
|
15
36
|
@KeyPath()
|
|
@@ -41,8 +62,10 @@ class Location {
|
|
|
41
62
|
}
|
|
42
63
|
```
|
|
43
64
|
|
|
44
|
-
### CRUD
|
|
45
|
-
|
|
65
|
+
### π CRUD Operations
|
|
66
|
+
Perform database operations in an intuitive way:
|
|
67
|
+
|
|
68
|
+
```typescript
|
|
46
69
|
const db = await Database.build("idb-crud", [User, Location]);
|
|
47
70
|
|
|
48
71
|
const alice = new User("Alice", 25, "123 Main St");
|
|
@@ -52,25 +75,30 @@ await db.create(User, alice);
|
|
|
52
75
|
await db.create(Location, nyc);
|
|
53
76
|
|
|
54
77
|
const readAlice = await db.read(User, "Alice");
|
|
55
|
-
console.log("Read user:", readAlice);
|
|
78
|
+
console.log("π€ Read user:", readAlice);
|
|
56
79
|
|
|
57
80
|
alice.age = 26;
|
|
58
81
|
alice.address = "789 Maple St";
|
|
59
82
|
await db.update(User, alice);
|
|
60
83
|
|
|
61
84
|
const users = await db.list(User);
|
|
62
|
-
console.log("All users:", users);
|
|
85
|
+
console.log("π All users:", users);
|
|
63
86
|
|
|
64
87
|
await db.delete(User, "Alice");
|
|
65
|
-
console.log("User Alice deleted.");
|
|
88
|
+
console.log("β User Alice deleted.");
|
|
66
89
|
|
|
67
90
|
const remainingUsers = await db.list(User);
|
|
68
|
-
console.log("Remaining users:", remainingUsers);
|
|
91
|
+
console.log("π Remaining users:", remainingUsers);
|
|
69
92
|
|
|
70
93
|
const locations = await db.list(Location);
|
|
71
|
-
console.log("All locations:", locations);
|
|
94
|
+
console.log("π All locations:", locations);
|
|
72
95
|
```
|
|
73
96
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## π Useful Links
|
|
100
|
+
- π **GitHub**: [maifeeulasad/idb-ts](https://github.com/maifeeulasad/idb-ts)
|
|
101
|
+
- π¦ **NPM**: [idb-ts](https://www.npmjs.com/package/idb-ts)
|
|
102
|
+
|
|
103
|
+
π **Enjoy seamless IndexedDB integration with TypeScript! Happy coding!** π
|
|
104
|
+
|
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 };
|