idb-ts 1.0.0 → 2.0.0

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 CHANGED
@@ -1,4 +1,9 @@
1
- # ts-indexeddb
1
+ # idb-ts
2
+
3
+ ## Installation
4
+ ```
5
+ npm i idb-ts
6
+ ```
2
7
 
3
8
  ## Example
4
9
 
@@ -64,4 +69,8 @@ console.log("Remaining users:", remainingUsers);
64
69
 
65
70
  const locations = await db.list(Location);
66
71
  console.log("All locations:", locations);
67
- ```
72
+ ```
73
+
74
+ ## Links
75
+ - GitHub: https://github.com/maifeeulasad/idb-ts
76
+ - NPM: https://www.npmjs.com/package/idb-ts
package/index.ts CHANGED
@@ -25,40 +25,49 @@ class Database {
25
25
  private classes: Function[];
26
26
  private db: IDBDatabase | null = null;
27
27
 
28
- constructor(dbName: string, classes: Function[]) {
28
+ private constructor(dbName: string, classes: Function[]) {
29
29
  this.dbName = dbName;
30
30
  if (!classes.every(cls => Reflect.getMetadata("dataclass", cls))) {
31
31
  throw new Error("All classes should be decorated with @DataClass.");
32
32
  }
33
33
  this.classes = classes;
34
- this.initDB();
35
34
  }
36
35
 
37
- private initDB(): void {
38
- const request = indexedDB.open(this.dbName, 1);
36
+ 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);
39
45
 
40
- request.onupgradeneeded = () => {
41
- const db = request.result;
46
+ request.onupgradeneeded = () => {
47
+ const db = request.result;
42
48
 
43
- this.classes.forEach((cls) => {
44
- const keyPathFields = Reflect.getMetadata("keypath", cls) || [];
49
+ this.classes.forEach((cls) => {
50
+ const keyPathFields = Reflect.getMetadata("keypath", cls) || [];
45
51
 
46
- const storeName = cls.name.toLowerCase() + "s";
52
+ const storeName = cls.name.toLowerCase() + "s";
47
53
 
48
- if (!db.objectStoreNames.contains(storeName)) {
49
- db.createObjectStore(storeName, { keyPath: keyPathFields[0] });
50
- }
51
- });
52
- };
54
+ if (!db.objectStoreNames.contains(storeName)) {
55
+ db.createObjectStore(storeName, { keyPath: keyPathFields[0] });
56
+ }
57
+ });
58
+ };
53
59
 
54
- request.onsuccess = () => {
55
- this.db = request.result;
56
- console.log(`Database initialized with object stores for: ${this.classes.map(cls => cls.name).join(", ")}`);
57
- };
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
+ };
58
65
 
59
- request.onerror = () => {
60
- console.error("Error initializing database:", request.error);
61
- };
66
+ request.onerror = () => {
67
+ console.error("Error initializing database:", request.error);
68
+ reject(request.error);
69
+ };
70
+ });
62
71
  }
63
72
 
64
73
  private getObjectStore(className: string, mode: IDBTransactionMode): IDBObjectStore {
package/lib/index.d.ts CHANGED
@@ -5,7 +5,8 @@ declare class Database {
5
5
  private dbName;
6
6
  private classes;
7
7
  private db;
8
- constructor(dbName: string, classes: Function[]);
8
+ private constructor();
9
+ static build(dbName: string, classes: Function[]): Promise<Database>;
9
10
  private initDB;
10
11
  private getObjectStore;
11
12
  create<T>(cls: {
package/lib/index.js CHANGED
@@ -39,27 +39,39 @@ class Database {
39
39
  throw new Error("All classes should be decorated with @DataClass.");
40
40
  }
41
41
  this.classes = classes;
42
- this.initDB();
42
+ }
43
+ static build(dbName, classes) {
44
+ return __awaiter(this, void 0, void 0, function* () {
45
+ const instance = new Database(dbName, classes);
46
+ yield instance.initDB();
47
+ return instance;
48
+ });
43
49
  }
44
50
  initDB() {
45
- const request = indexedDB.open(this.dbName, 1);
46
- request.onupgradeneeded = () => {
47
- const db = request.result;
48
- this.classes.forEach((cls) => {
49
- const keyPathFields = Reflect.getMetadata("keypath", cls) || [];
50
- const storeName = cls.name.toLowerCase() + "s";
51
- if (!db.objectStoreNames.contains(storeName)) {
52
- db.createObjectStore(storeName, { keyPath: keyPathFields[0] });
53
- }
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ return new Promise((resolve, reject) => {
53
+ const request = indexedDB.open(this.dbName, 1);
54
+ request.onupgradeneeded = () => {
55
+ const db = request.result;
56
+ this.classes.forEach((cls) => {
57
+ const keyPathFields = Reflect.getMetadata("keypath", cls) || [];
58
+ const storeName = cls.name.toLowerCase() + "s";
59
+ if (!db.objectStoreNames.contains(storeName)) {
60
+ db.createObjectStore(storeName, { keyPath: keyPathFields[0] });
61
+ }
62
+ });
63
+ };
64
+ request.onsuccess = () => {
65
+ this.db = request.result;
66
+ console.log(`Database initialized with object stores for: ${this.classes.map(cls => cls.name).join(", ")}`);
67
+ resolve();
68
+ };
69
+ request.onerror = () => {
70
+ console.error("Error initializing database:", request.error);
71
+ reject(request.error);
72
+ };
54
73
  });
55
- };
56
- request.onsuccess = () => {
57
- this.db = request.result;
58
- console.log(`Database initialized with object stores for: ${this.classes.map(cls => cls.name).join(", ")}`);
59
- };
60
- request.onerror = () => {
61
- console.error("Error initializing database:", request.error);
62
- };
74
+ });
63
75
  }
64
76
  getObjectStore(className, mode) {
65
77
  if (!this.db) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "idb-ts",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Easy CRUD for indexed-db, written in TypeScript",
5
5
  "main": "index.ts",
6
6
  "scripts": {