You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// nullable field, any other field will throw an error if not defined
99
+
@Field({ nullable: true })
100
+
email?:boolean
101
+
}
102
+
```
103
+
104
+
A `Field` has the following parameters:
105
+
-`primaryKey`: Whether the field is the primary key of the model, if multiple fields are marked as primary key, their combination will be the key.
106
+
> **Important note**: A limitation in IndexedDB makes it so the primary key can't be changed once the table is created and W-ORM will throw an error, a way to circumvent this is explained in [migrations](#migration-system).
107
+
-`unique`: Whether the field has an unique constraint. This will be enforced by the database.
108
+
-`nullable`: Whether the field can be `null`/`undefined`, primary keys cannot be nullable.
109
+
-`default`: The default value of the field, it can be a value or a function that returns the value.
110
+
-`index`: Whether the field should be indexed, it is recommended to keep it unless the type isn't indexable (eg. a Blob).
111
+
112
+
More info in the [API documentation](https://w-orm.d34d.one/dev/?page=W-ORM.Function.Field).
More info in the [API documentation](https://w-orm.d34d.one/dev/?page=W-ORM.Class.Model).
143
+
144
+
## Transactions
145
+
146
+
Sometimes DB operations are meant to be executed as a "bundle", so that they either all pass or fail together.
147
+
148
+
Transactions allow us to implement this, with automatic rollbacks on error. And even if you don't need this, there are performance benefits to using transactions.
// Any error thrown in the callback will abort the transaction, this will rollback any changes made
156
+
thrownewError('rollback')
157
+
// If no error is thrown, the transaction will be committed
158
+
})
159
+
```
160
+
161
+
> **Important note**: Because of a limitation in the IndexedDB API, the transaction will be automatically committed if we wait for any non-transactional operation. (e.g. fetching some data from the network).
162
+
163
+
More info in the [API documentation](https://w-orm.d34d.one/dev/?page=W-ORM.Function.Transaction).
164
+
165
+
## Migration system
166
+
167
+
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.
168
+
169
+
Migrations are defined as list of functions to be executed, depending on the current and target DB versions.
170
+
The key is the target version number, and the value is the migration callback.
171
+
172
+
Eg. `{ 2: (migration) => { ... } }` will execute the migration callback when the current database version is smaller than 2.
173
+
174
+
A migration callback receives a
175
+
`MigrationContext` object as its only argument.
176
+
This object contains a transaction to be used for the migration.
177
+
178
+
It is expected for the callback to create Model classes that represent the table's state in between these two versions.
179
+
The fields aren't actually used by W-ORM in this scenario, and only serve to improve the typing within the migration.
180
+
181
+
The `Model` methods can be then used to manipulate the data.
182
+
It is very important to use the transaction provided by the migration context, otherwise the migration will hang forever.
0 commit comments