v0.2.0
Documentation
NPM
Full Changelog
Features
Table inheritance
Now abstract tables can be used to group up reused fields
abstract class BaseModel extends Model {
@Field({ primaryKey: true, default: () => crypto.randomUUID() })
id!: string
}
class User extends BaseModel {
@Field()
username!: string
}The only limitation is that wether a table is created or not depends simply on how many children tables it has.
Transactions
Users can now create and handle transactions themselves, those can be used for any DB interaction.
The main benefits are performance and atomicity.
let result: Test[]
await Transaction('readwrite', async (tx) => {
await Test.create({ id: 1, balance: 100 }, tx)
await Test.create({ id: 2, balance: 50 }, tx)
await Test.create({ id: 3, balance: 300 }, tx)
})
result = Test.all()
A transaction will commit by default if the provided callback succeeds and if an error is thrown inside, it'll be rolled back.
Important notes:
- The transaction object (
tx) should be provided to all the DB operations within the transaction.- The only
awaitcalls that are supported are DB operations, if anything other is called the transaction may auto-commit in between and thus break any following operations. This is an IndexedDB limitation.- The first parameter is the transaction mode, this can either be "readwrite" or "readonly", choose wisely depending on the operations that will happen within the transaction.
Query limit and offset
Queries can now be configured with a limit and an offset.
Those will be taken into account for all the operations (count, all, delete...)
Only exception being limit and first() as the second one is equivalent to having limit(1)
// This will skip the first result, and stop once 3 items have been found
const results = await Test.orderBy('balance').limit(3).offset(1).all()Bug fixes
Modelis now an abstract class
Other
- Target has been changed to ES2017, so that the final code is able to use native async/await