@@ -48,16 +48,23 @@ npm i idb-ts
4848Use decorators to define your data models with automatic schema management.
4949
5050``` typescript
51+ import { Database , DataClass , KeyPath , Index } from " idb-ts" ;
52+
5153@DataClass ()
5254class User {
5355 @KeyPath ()
5456 name: string ;
57+
58+ @Index ()
59+ email: string ;
60+
5561 age: number ;
5662 cell? : string ;
5763 address: string ;
5864
59- constructor (name : string , age : number , address : string , cell ? : string ) {
65+ constructor (name : string , email : string , age : number , address : string , cell ? : string ) {
6066 this .name = name ;
67+ this .email = email ;
6168 this .age = age ;
6269 this .address = address ;
6370 this .cell = cell ;
@@ -68,7 +75,10 @@ class User {
6875class Location {
6976 @KeyPath ()
7077 id: string ;
78+
79+ @Index ()
7180 city: string ;
81+
7282 country: string ;
7383
7484 constructor (id : string , city : string , country : string ) {
@@ -85,11 +95,15 @@ Perform database operations in an intuitive way:
8595``` typescript
8696const db = await Database .build (" idb-crud" , [User , Location ]);
8797
88- const alice = new User (" Alice" , 25 , " 123 Main St" );
98+ const alice = new User (" Alice" , " alice@example.com" , 25 , " 123 Main St" );
99+ const bob = new User (" Bob" , " bob@example.com" , 30 , " 456 Oak Ave" );
89100const nyc = new Location (" 1" , " New York" , " USA" );
101+ const sf = new Location (" 2" , " San Francisco" , " USA" );
90102
91103await db .create (User , alice );
104+ await db .create (User , bob );
92105await db .create (Location , nyc );
106+ await db .create (Location , sf );
93107
94108const readAlice = await db .read (User , " Alice" );
95109console .log (" 👤 Read user:" , readAlice );
@@ -101,6 +115,12 @@ await db.update(User, alice);
101115const users = await db .list (User );
102116console .log (" 📋 All users:" , users );
103117
118+ const userByEmail = await db .findOneByIndex (User , ' email' , ' bob@example.com' );
119+ console .log (" 🔎 User by email:" , userByEmail );
120+
121+ const locationsInSF = await db .findByIndex (Location , ' city' , ' San Francisco' );
122+ console .log (" 🌆 Locations in San Francisco:" , locationsInSF );
123+
104124await db .delete (User , " Alice" );
105125console .log (" ❌ User Alice deleted." );
106126
@@ -111,6 +131,46 @@ const locations = await db.list(Location);
111131console .log (" 🌍 All locations:" , locations );
112132```
113133
134+ ### 🔍 Indexing Support
135+ Create indexes on fields for fast querying:
136+
137+ ``` typescript
138+ @DataClass ()
139+ class Product {
140+ @KeyPath ()
141+ id: string ;
142+
143+ @Index ()
144+ category: string ;
145+
146+ @Index ()
147+ price: number ;
148+
149+ name: string ;
150+ description: string ;
151+
152+ constructor (id : string , category : string , price : number , name : string , description : string ) {
153+ this .id = id ;
154+ this .category = category ;
155+ this .price = price ;
156+ this .name = name ;
157+ this .description = description ;
158+ }
159+ }
160+
161+ const db = await Database .build (" products-db" , [Product ]);
162+
163+ const electronics = await db .findByIndex (Product , ' category' , ' Electronics' );
164+
165+ const expensiveItems = await db .findByIndex (Product , ' price' , 999.99 );
166+
167+ const firstElectronic = await db .findOneByIndex (Product , ' category' , ' Electronics' );
168+ ```
169+
170+ #### Index Methods:
171+ - ` findByIndex<T>(cls, indexName, value): Promise<T[]> ` - Find all records matching the index value
172+ - ` findOneByIndex<T>(cls, indexName, value): Promise<T | undefined> ` - Find the first record matching the index value
173+
114174---
115175
116176## 🔗 Useful Links
0 commit comments