@@ -40,6 +40,7 @@ npm i idb-ts
4040- 🚀 ** Fully Typed API** - Benefit from TypeScript’s powerful type system.
4141- 🏎️ ** Performance Optimized** - Minimal overhead with IndexedDB's native capabilities.
4242- 🔄 ** Schema Versioning** - Manage database schema evolution with automatic migration support.
43+ - 🔑 ** Advanced Key Management** - Auto-increment, UUID, timestamp, custom generators, and composite keys.
4344
4445---
4546
@@ -173,6 +174,194 @@ const firstElectronic = await db.Product.findOneByIndex('category', 'Electronics
173174
174175---
175176
177+ ## 🔑 Multi-Field & Composite Key Support
178+
179+ idb-ts provides flexible key management options including auto-increment keys, key generators, and composite keys for complex data relationships.
180+
181+ ### Auto-Increment Keys
182+ Perfect for entities where you want the database to automatically generate sequential IDs:
183+
184+ ``` typescript
185+ @DataClass ()
186+ class Task {
187+ @KeyPath ({ autoIncrement: true })
188+ id! : number ;
189+
190+ title! : string ;
191+ completed! : boolean ;
192+
193+ constructor (title : string , completed = false ) {
194+ this .title = title ;
195+ this .completed = completed ;
196+ }
197+ }
198+
199+ const db = await Database .build (" tasks-db" , [Task ]);
200+
201+ // IDs are automatically generated: 1, 2, 3, etc.
202+ const task1 = await db .Task .create (new Task (" Learn TypeScript" ));
203+ const task2 = await db .Task .create (new Task (" Build amazing apps" ));
204+ console .log (task1 .id ); // 1
205+ console .log (task2 .id ); // 2
206+ ```
207+
208+ ### Key Generators
209+ Generate keys automatically using built-in generators:
210+
211+ #### UUID Keys
212+ ``` typescript
213+ @DataClass ()
214+ class Document {
215+ @KeyPath ({ generator: ' uuid' })
216+ uuid! : string ;
217+
218+ @Index ()
219+ category! : string ;
220+
221+ title! : string ;
222+ content! : string ;
223+
224+ constructor (category : string , title : string , content : string ) {
225+ this .category = category ;
226+ this .title = title ;
227+ this .content = content ;
228+ }
229+ }
230+
231+ const db = await Database .build (" docs-db" , [Document ]);
232+
233+ const doc = await db .Document .create (new Document (" tutorial" , " Getting Started" , " Welcome..." ));
234+ console .log (doc .uuid ); // e.g., "a1b2c3d4-e5f6-7890-abcd-ef1234567890"
235+ ```
236+
237+ #### Timestamp Keys
238+ ``` typescript
239+ @DataClass ()
240+ class Event {
241+ @KeyPath ({ generator: ' timestamp' })
242+ timestamp! : number ;
243+
244+ @Index ()
245+ type! : string ;
246+
247+ data! : any ;
248+
249+ constructor (type : string , data : any ) {
250+ this .type = type ;
251+ this .data = data ;
252+ }
253+ }
254+
255+ const event = await db .Event .create (new Event (" user_login" , { userId: " 123" }));
256+ console .log (event .timestamp ); // e.g., 1696118400000
257+ ```
258+
259+ #### Random Keys
260+ ``` typescript
261+ @DataClass ()
262+ class Session {
263+ @KeyPath ({ generator: ' random' })
264+ sessionId! : string ;
265+
266+ userId! : string ;
267+ expiresAt! : Date ;
268+
269+ constructor (userId : string , expiresAt : Date ) {
270+ this .userId = userId ;
271+ this .expiresAt = expiresAt ;
272+ }
273+ }
274+
275+ const session = await db .Session .create (new Session (" user123" , new Date ()));
276+ console .log (session .sessionId ); // e.g., "xyz789abc123"
277+ ```
278+
279+ ### Custom Key Generators
280+ Create your own key generation logic:
281+
282+ ``` typescript
283+ @DataClass ()
284+ class Invoice {
285+ @KeyPath ({ generator : (entity : any ) => ` INV-${entity .year }-${String (entity .number ).padStart (4 , ' 0' )} ` })
286+ invoiceId! : string ;
287+
288+ year! : number ;
289+ number! : number ;
290+ amount! : number ;
291+
292+ constructor (year : number , number : number , amount : number ) {
293+ this .year = year ;
294+ this .number = number ;
295+ this .amount = amount ;
296+ }
297+ }
298+
299+ const invoice = await db .Invoice .create (new Invoice (2024 , 1 , 1500.00 ));
300+ console .log (invoice .invoiceId ); // "INV-2024-0001"
301+ ```
302+
303+ ### Composite Keys
304+ Handle many-to-many relationships with composite keys using the ` @CompositeKeyPath ` decorator:
305+
306+ ``` typescript
307+ import { CompositeKeyPath } from " idb-ts" ;
308+
309+ @CompositeKeyPath ([' userId' , ' projectId' ])
310+ @DataClass ()
311+ class UserProject {
312+ userId! : string ;
313+ projectId! : string ;
314+
315+ @Index ()
316+ role! : string ;
317+
318+ joinedAt! : Date ;
319+
320+ constructor (userId : string , projectId : string , role : string ) {
321+ this .userId = userId ;
322+ this .projectId = projectId ;
323+ this .role = role ;
324+ this .joinedAt = new Date ();
325+ }
326+ }
327+
328+ const db = await Database .build (" collaboration-db" , [UserProject ]);
329+
330+ // Create relationships
331+ await db .UserProject .create (new UserProject (" user123" , " project456" , " developer" ));
332+ await db .UserProject .create (new UserProject (" user123" , " project789" , " admin" ));
333+ await db .UserProject .create (new UserProject (" user456" , " project456" , " viewer" ));
334+
335+ // Read with composite key
336+ const relationship = await db .UserProject .read ([' user123' , ' project456' ]);
337+ console .log (relationship ?.role ); // "developer"
338+
339+ // Update relationship
340+ if (relationship ) {
341+ relationship .role = " maintainer" ;
342+ await db .UserProject .update (relationship );
343+ }
344+
345+ // Delete with composite key
346+ await db .UserProject .delete ([' user123' , ' project789' ]);
347+
348+ // Query by role index
349+ const developers = await db .UserProject .findByIndex (' role' , ' developer' );
350+ ```
351+
352+ ### Key Generation Utilities
353+ Access key generators directly for your custom logic:
354+
355+ ``` typescript
356+ import { KeyGenerators } from " idb-ts" ;
357+
358+ const uuid = KeyGenerators .uuid (); // Generate UUID
359+ const timestamp = KeyGenerators .timestamp (); // Current timestamp
360+ const random = KeyGenerators .random (); // Random string
361+ ```
362+
363+ ---
364+
176365## 🔄 Schema Versioning
177366
178367idb-ts supports schema versioning to manage database evolution over time. Version your entities and let the library handle automatic migration!
0 commit comments