Skip to content

Commit 082cf92

Browse files
committed
[chores]: added schema versioning example;
1 parent 21c4292 commit 082cf92

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ npm i idb-ts
3939
-**Easy CRUD Operations** - Perform create, read, update, and delete seamlessly.
4040
- 🚀 **Fully Typed API** - Benefit from TypeScript’s powerful type system.
4141
- 🏎️ **Performance Optimized** - Minimal overhead with IndexedDB's native capabilities.
42+
- 🔄 **Schema Versioning** - Manage database schema evolution with automatic migration support.
4243

4344
---
4445

@@ -172,6 +173,68 @@ const firstElectronic = await db.Product.findOneByIndex('category', 'Electronics
172173

173174
---
174175

176+
## 🔄 Schema Versioning
177+
178+
idb-ts supports schema versioning to manage database evolution over time. Version your entities and let the library handle automatic migration!
179+
180+
### Basic Usage
181+
182+
```typescript
183+
@DataClass({ version: 1 })
184+
class User {
185+
@KeyPath() id!: string;
186+
@Index() email!: string;
187+
name!: string;
188+
}
189+
190+
@DataClass({ version: 2 })
191+
class Post {
192+
@KeyPath() id!: string;
193+
@Index() authorId!: string;
194+
title!: string;
195+
content!: string;
196+
}
197+
198+
@DataClass({ version: 3 })
199+
class Comment {
200+
@KeyPath() id!: string;
201+
@Index() postId!: string;
202+
@Index() authorId!: string;
203+
text!: string;
204+
}
205+
206+
// Database version will be 3 (highest entity version)
207+
const db = await Database.build("blog", [User, Post, Comment]);
208+
209+
console.log(db.getDatabaseVersion()); // 3
210+
console.log(db.getEntityVersions()); // Map with entity versions
211+
```
212+
213+
### Key Features
214+
215+
- **Automatic Version Calculation**: Database version = highest entity version
216+
- **Seamless Migration**: Only new/updated entities are processed during upgrades
217+
- **Backward Compatibility**: Entities without version default to version 1
218+
- **Index Evolution**: New indexes are automatically created during migration
219+
220+
### Version Management
221+
222+
```typescript
223+
// Check versions
224+
const dbVersion = db.getDatabaseVersion();
225+
const entityVersions = db.getEntityVersions();
226+
const userVersion = db.getEntityVersion('User');
227+
228+
// Version upgrade flow:
229+
// v1.0: User(v1) → Database v1
230+
// v1.1: User(v1), Post(v2) → Database v2
231+
// v1.2: User(v1), Post(v2), Comment(v3) → Database v3
232+
```
233+
234+
📖 **[Complete Schema Versioning Guide](./SCHEMA_VERSIONING.md)** - Detailed documentation with examples and best practices.
235+
236+
---
237+
175238
## 🔗 Useful Links
176239
- 📂 **GitHub**: [maifeeulasad/idb-ts](https://github.com/maifeeulasad/idb-ts)
177240
- 📦 **NPM**: [idb-ts](https://www.npmjs.com/package/idb-ts)

0 commit comments

Comments
 (0)