data
data copied to clipboard
Add a "snapshot" utility
Expected behavior
The snapshot
utility would memorize the state of the database (its entities) at a call time, and allow to restore to that memorized state at any further point of time (i.e. before each test).
Usage
import { factory, primaryKey, snapshot } from '@mswjs/data'
const db = factory({
user: {
id: primaryKey(String),
firstName: String
}
})
db.user.create({ firstName: 'John' })
db.user.create({ firstName: 'Kate' })
// Takes a snapshot of the database
// in this point of time.
const restore = snapshot(db)
// Perform some other actions.
db.user.create({ firstName: 'Joe' })
// Restores the database to the taken snapshot.
// Will result in a database with 2 users:
// "John" and "Kate".
restore()
GitHub
- Originated from #51
- Related to #85
Restoring a database is effectively reset it to a given initial state. This may mean that #49 is a prerequisite for this task, as persistency also uses a restoration logic that hydrates a database instance from the sessionStorage
.