data icon indicating copy to clipboard operation
data copied to clipboard

Add a "snapshot" utility

Open kettanaito opened this issue 3 years ago • 1 comments

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

kettanaito avatar Apr 28 '21 08:04 kettanaito

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.

kettanaito avatar Apr 28 '21 08:04 kettanaito