v0.4.0
Documentation
NPM
Full Changelog
Features
Disable indexing
Closes #28
Fields allow now to disable the automatic index creation (eg. for non indexable types like Blob)
class User extends Model {
@Field({ index: false })
profilePic: Blob
}Migrations
Sometimes, changes to the way existing data is stored are required for an update, to cope with this W-ORM provides an intuitive migration system.
Migrations are defined as list of functions to be executed, depending on the current and target DB versions.
The key is the target version number, and the value is the migration callback.
Eg. { 2: (migration) => { ... } } will execute the migration callback when the current database version is smaller than 2.
A migration callback receives a MigrationContext object as its only argument.
This object contains a transaction to be used for the migration.
It is expected for the callback to create Model classes that represent the table's state in between these two versions.
The fields aren't actually used by W-ORM in this scenario, and only serve to improve the typing within the migration.
The Model methods can be then used to manipulate the data.
It is very important to use the transaction provided by the migration context, otherwise the migration will hang forever.
const migrations: MigrationList = {
2: async (migration) => {
class User extends Model {
id!: number
name!: string
}
const users = await User.all()
for (const user of users) {
user.name = `${user.id} name`
await user.save(migration.tx)
}
// Or with the `forEach method`
await User.forEach(async (instance, tx) => {
instance.name = `${instance.id} name`
await instance.save(tx)
}, migration.tx)
const specificUser = await User.get(69, migration.tx)
await specificUser?.delete(migration.tx)
},
}Model now allows extra fields
The Typescript signature of Model has been changed so that you can provide and get extra, not defined fields from it. This is a feature supported by IndexedDB so it's only natural to support it here as well.
class User extends Model {
id!: string
name!: string
}
const user = await User.create({
id: "joe",
name: "Joe",
lastName: "Mama", // This would have caused an error before, it is allowed now.
})
const otherUser = await User.get("tom")
console.log(user.extraField) // This would have caused an error as well, you'll still need to check that it is definedQuery cloning
Closes #18
Queries can now be cloned, to allow branching and allow code deduplication.
// Regular pagination
const userPage = User.orderBy('id').offset(10).limit(10)
// Filtered pagination
const johnPage = userPage.clone().filter({ name: 'John' })
const janePage = userPage.clone().filter({ name: 'Jane' })Model.keys()
Closes #17
We can now get a list with all the keys in a table (same as Model.all but with keys instead of instances)
const keys = await User.keys()
// equivalent to (even though more performant)
const keys = (await User.all()).map(u => u.keys)
Bug fixes
- After an upgrade, changed indexes will now be recreated and unused indexes will be removed
- Errors during an upgrade will now bubble up properly to the
initcall - Upgrades would fail silently because the automatic table creation would try to create already existing tables
Other
- Errors are now displayed in their own section in the documentation
- Minified
distwould break because theModelclass wasn't calledModelanymore - Documentation improved overall (closes #16)
- Improve test coverage (closes #19)
- UMD
distexposes the whole module underglobalThis.WORM, for the ESM version, you'll have toimport WORM from "https://unpkg.com/@d34d/w-orm@0.4.0/dist/index.esm.js"